• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 
33 #if ENABLE(WORKERS)
34 
35 #include "DedicatedWorkerContext.h"
36 
37 #include "DedicatedWorkerThread.h"
38 #include "DOMWindow.h"
39 #include "MessageEvent.h"
40 #include "WorkerObjectProxy.h"
41 
42 namespace WebCore {
43 
DedicatedWorkerContext(const KURL & url,const String & userAgent,DedicatedWorkerThread * thread)44 DedicatedWorkerContext::DedicatedWorkerContext(const KURL& url, const String& userAgent, DedicatedWorkerThread* thread)
45     : WorkerContext(url, userAgent, thread)
46 {
47 }
48 
~DedicatedWorkerContext()49 DedicatedWorkerContext::~DedicatedWorkerContext()
50 {
51     ASSERT(currentThread() == thread()->threadID());
52     // Notify parent worker we are going away. This can free the WorkerThread object, so do not access it after this.
53     thread()->workerObjectProxy().workerContextDestroyed();
54 }
55 
forwardException(const String & errorMessage,int lineNumber,const String & sourceURL)56 void DedicatedWorkerContext::forwardException(const String& errorMessage, int lineNumber, const String& sourceURL)
57 {
58     thread()->workerObjectProxy().postExceptionToWorkerObject(errorMessage, lineNumber, sourceURL);
59 }
60 
addMessage(MessageDestination destination,MessageSource source,MessageType type,MessageLevel level,const String & message,unsigned lineNumber,const String & sourceURL)61 void DedicatedWorkerContext::addMessage(MessageDestination destination, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL)
62 {
63     thread()->workerObjectProxy().postConsoleMessageToWorkerObject(destination, source, type, level, message, lineNumber, sourceURL);
64 }
65 
postMessage(const String & message,ExceptionCode & ec)66 void DedicatedWorkerContext::postMessage(const String& message, ExceptionCode& ec)
67 {
68     postMessage(message, 0, ec);
69 }
70 
postMessage(const String & message,MessagePort * port,ExceptionCode & ec)71 void DedicatedWorkerContext::postMessage(const String& message, MessagePort* port, ExceptionCode& ec)
72 {
73     if (isClosing())
74         return;
75     // Disentangle the port in preparation for sending it to the remote context.
76     OwnPtr<MessagePortChannel> channel = port ? port->disentangle(ec) : 0;
77     if (ec)
78         return;
79     thread()->workerObjectProxy().postMessageToWorkerObject(message, channel.release());
80 }
81 
dispatchMessage(const String & message,PassRefPtr<MessagePort> port)82 void DedicatedWorkerContext::dispatchMessage(const String& message, PassRefPtr<MessagePort> port)
83 {
84     // Since close() stops the thread event loop, this should not ever get called while closing.
85     ASSERT(!isClosing());
86     RefPtr<Event> evt = MessageEvent::create(message, "", "", 0, port);
87 
88     if (m_onmessageListener.get()) {
89         evt->setTarget(this);
90         evt->setCurrentTarget(this);
91         m_onmessageListener->handleEvent(evt.get(), false);
92     }
93 
94     ExceptionCode ec = 0;
95     dispatchEvent(evt.release(), ec);
96     ASSERT(!ec);
97 }
98 
importScripts(const Vector<String> & urls,const String & callerURL,int callerLine,ExceptionCode & ec)99 void DedicatedWorkerContext::importScripts(const Vector<String>& urls, const String& callerURL, int callerLine, ExceptionCode& ec)
100 {
101     Base::importScripts(urls, callerURL, callerLine, ec);
102     thread()->workerObjectProxy().reportPendingActivity(hasPendingActivity());
103 }
104 
thread()105 DedicatedWorkerThread* DedicatedWorkerContext::thread()
106 {
107     return static_cast<DedicatedWorkerThread*>(Base::thread());
108 }
109 
110 } // namespace WebCore
111 
112 #endif // ENABLE(WORKERS)
113