2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "WebWorkerImpl.h"
34 #include "CrossThreadTask.h"
35 #include "DedicatedWorkerContext.h"
36 #include "DedicatedWorkerThread.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>
50 #include "PlatformMessagePortChannel.h"
51 #include "WebMessagePortChannel.h"
52 #include "WebString.h"
54 #include "WebWorkerClient.h"
56 using namespace WebCore;
62 WebWorker* WebWorker::create(WebWorkerClient* client)
64 return new WebWorkerImpl(client);
68 WebWorkerImpl::WebWorkerImpl(WebWorkerClient* client)
73 WebWorkerImpl::~WebWorkerImpl()
77 NewWebCommonWorkerClient* WebWorkerImpl::newCommonClient()
82 WebCommonWorkerClient* WebWorkerImpl::commonClient()
87 void WebWorkerImpl::postMessageToWorkerContextTask(WebCore::ScriptExecutionContext* context,
88 WebWorkerImpl* thisPtr,
89 const String& message,
90 PassOwnPtr<MessagePortChannelArray> channels)
92 ASSERT(context->isWorkerContext());
93 DedicatedWorkerContext* workerContext =
94 static_cast<DedicatedWorkerContext*>(context);
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());
105 // WebWorker -------------------------------------------------------------------
107 void WebWorkerImpl::startWorkerContext(const WebURL& scriptUrl,
108 const WebString& userAgent,
109 const WebString& sourceCode)
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();
118 void WebWorkerImpl::terminateWorkerContext()
123 void WebWorkerImpl::postMessageToWorkerContext(const WebString& message,
124 const WebMessagePortChannelArray& webChannels)
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);
137 workerThread()->runLoop().postTask(
138 createCallbackTask(&postMessageToWorkerContextTask,
139 AllowCrossThreadAccess(this), String(message), channels.release()));
142 void WebWorkerImpl::workerObjectDestroyed()
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();
151 void WebWorkerImpl::clientDestroyed()
156 static void connectToWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
158 ASSERT(context->isWorkerContext());
159 static_cast<WorkerContext*>(context)->workerInspectorController()->connectFrontend();
162 void WebWorkerImpl::attachDevTools()
164 workerThread()->runLoop().postTask(createCallbackTask(connectToWorkerContextInspectorTask, true));
167 static void disconnectFromWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
169 ASSERT(context->isWorkerContext());
170 static_cast<WorkerContext*>(context)->workerInspectorController()->disconnectFrontend();
173 void WebWorkerImpl::detachDevTools()
175 workerThread()->runLoop().postTaskForMode(createCallbackTask(disconnectFromWorkerContextInspectorTask, true), WorkerScriptDebugServer::debuggerTaskMode);
178 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
180 ASSERT(context->isWorkerContext());
181 static_cast<WorkerContext*>(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
184 void WebWorkerImpl::dispatchDevToolsMessage(const WebString& message)
186 workerThread()->runLoop().postTaskForMode(createCallbackTask(dispatchOnInspectorBackendTask, String(message)), WorkerScriptDebugServer::debuggerTaskMode);
191 WebWorker* WebWorker::create(WebWorkerClient* client)
196 #endif // ENABLE(WORKERS)
198 } // namespace WebKit