initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WebWorkerImpl.cpp
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 #include "WebWorkerImpl.h"
33
34 #include "CrossThreadTask.h"
35 #include "DedicatedWorkerContext.h"
36 #include "DedicatedWorkerThread.h"
37 #include "KURL.h"
38 #include "MessageEvent.h"
39 #include "MessagePort.h"
40 #include "MessagePortChannel.h"
41 #include "ScriptExecutionContext.h"
42 #include "SecurityOrigin.h"
43 #include "SerializedScriptValue.h"
44 #include "SubstituteData.h"
45 #include "WorkerInspectorController.h"
46 #include "WorkerScriptDebugServer.h"
47 #include <wtf/OwnPtr.h>
48 #include <wtf/Threading.h>
49
50 #include "PlatformMessagePortChannel.h"
51 #include "WebMessagePortChannel.h"
52 #include "WebString.h"
53 #include "WebURL.h"
54 #include "WebWorkerClient.h"
55
56 using namespace WebCore;
57
58 namespace WebKit {
59
60 #if ENABLE(WORKERS)
61
62 WebWorker* WebWorker::create(WebWorkerClient* client)
63 {
64     return new WebWorkerImpl(client);
65 }
66
67
68 WebWorkerImpl::WebWorkerImpl(WebWorkerClient* client)
69     : m_client(client)
70 {
71 }
72
73 WebWorkerImpl::~WebWorkerImpl()
74 {
75 }
76
77 NewWebCommonWorkerClient* WebWorkerImpl::newCommonClient()
78 {
79     return m_client;
80 }
81
82 WebCommonWorkerClient* WebWorkerImpl::commonClient()
83 {
84     return m_client;
85 }
86
87 void WebWorkerImpl::postMessageToWorkerContextTask(WebCore::ScriptExecutionContext* context,
88                                                    WebWorkerImpl* thisPtr,
89                                                    const String& message,
90                                                    PassOwnPtr<MessagePortChannelArray> channels)
91 {
92     ASSERT(context->isWorkerContext());
93     DedicatedWorkerContext* workerContext =
94         static_cast<DedicatedWorkerContext*>(context);
95
96     OwnPtr<MessagePortArray> ports =
97         MessagePort::entanglePorts(*context, channels);
98     RefPtr<SerializedScriptValue> serializedMessage =
99         SerializedScriptValue::createFromWire(message);
100     workerContext->dispatchEvent(MessageEvent::create(
101         ports.release(), serializedMessage.release()));
102     thisPtr->confirmMessageFromWorkerObject(workerContext->hasPendingActivity());
103 }
104
105 // WebWorker -------------------------------------------------------------------
106
107 void WebWorkerImpl::startWorkerContext(const WebURL& scriptUrl,
108                                        const WebString& userAgent,
109                                        const WebString& sourceCode)
110 {
111     initializeLoader(scriptUrl);
112     setWorkerThread(DedicatedWorkerThread::create(scriptUrl, userAgent, sourceCode, *this, *this));
113     // Worker initialization means a pending activity.
114     reportPendingActivity(true);
115     workerThread()->start();
116 }
117
118 void WebWorkerImpl::terminateWorkerContext()
119 {
120     stopWorkerThread();
121 }
122
123 void WebWorkerImpl::postMessageToWorkerContext(const WebString& message,
124                                                const WebMessagePortChannelArray& webChannels)
125 {
126     OwnPtr<MessagePortChannelArray> channels;
127     if (webChannels.size()) {
128         channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
129         for (size_t i = 0; i < webChannels.size(); ++i) {
130             RefPtr<PlatformMessagePortChannel> platform_channel =
131                 PlatformMessagePortChannel::create(webChannels[i]);
132             webChannels[i]->setClient(platform_channel.get());
133             (*channels)[i] = MessagePortChannel::create(platform_channel);
134         }
135     }
136
137     workerThread()->runLoop().postTask(
138         createCallbackTask(&postMessageToWorkerContextTask,
139                            AllowCrossThreadAccess(this), String(message), channels.release()));
140 }
141
142 void WebWorkerImpl::workerObjectDestroyed()
143 {
144     // Worker object in the renderer was destroyed, perhaps a result of GC.
145     // For us, it's a signal to start terminating the WorkerContext too.
146     // FIXME: when 'kill a worker' html5 spec algorithm is implemented, it
147     // should be used here instead of 'terminate a worker'.
148     terminateWorkerContext();
149 }
150
151 void WebWorkerImpl::clientDestroyed()
152 {
153     m_client = 0;
154 }
155
156 static void connectToWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
157 {
158     ASSERT(context->isWorkerContext());
159     static_cast<WorkerContext*>(context)->workerInspectorController()->connectFrontend();
160 }
161
162 void WebWorkerImpl::attachDevTools()
163 {
164     workerThread()->runLoop().postTask(createCallbackTask(connectToWorkerContextInspectorTask, true));
165 }
166
167 static void disconnectFromWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
168 {
169     ASSERT(context->isWorkerContext());
170     static_cast<WorkerContext*>(context)->workerInspectorController()->disconnectFrontend();
171 }
172
173 void WebWorkerImpl::detachDevTools()
174 {
175     workerThread()->runLoop().postTaskForMode(createCallbackTask(disconnectFromWorkerContextInspectorTask, true), WorkerScriptDebugServer::debuggerTaskMode);
176 }
177
178 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
179 {
180     ASSERT(context->isWorkerContext());
181     static_cast<WorkerContext*>(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
182 }
183
184 void WebWorkerImpl::dispatchDevToolsMessage(const WebString& message)
185 {
186     workerThread()->runLoop().postTaskForMode(createCallbackTask(dispatchOnInspectorBackendTask, String(message)), WorkerScriptDebugServer::debuggerTaskMode);
187 }
188
189 #else
190
191 WebWorker* WebWorker::create(WebWorkerClient* client)
192 {
193     return 0;
194 }
195
196 #endif // ENABLE(WORKERS)
197
198 } // namespace WebKit