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 "WebSharedWorkerImpl.h"
34 #include "CrossThreadTask.h"
36 #include "MessageEvent.h"
37 #include "MessagePortChannel.h"
38 #include "PlatformMessagePortChannel.h"
39 #include "ScriptExecutionContext.h"
40 #include "SharedWorkerContext.h"
41 #include "SharedWorkerThread.h"
42 #include "WorkerDebuggerAgent.h"
43 #include "WorkerInspectorController.h"
45 #include "WebMessagePortChannel.h"
46 #include "WebString.h"
49 using namespace WebCore;
53 #if ENABLE(SHARED_WORKERS)
55 WebSharedWorkerImpl::WebSharedWorkerImpl(WebCommonWorkerClient* client)
57 , m_pauseWorkerContextOnStart(false)
61 WebSharedWorkerImpl::~WebSharedWorkerImpl()
65 bool WebSharedWorkerImpl::isStarted()
67 // Should not ever be called from the worker thread (this API is only called on WebSharedWorkerProxy on the renderer thread).
69 return workerThread();
72 void WebSharedWorkerImpl::connect(WebMessagePortChannel* webChannel, ConnectListener* listener)
74 // Convert the WebMessagePortChanel to a WebCore::MessagePortChannel.
75 RefPtr<PlatformMessagePortChannel> platform_channel =
76 PlatformMessagePortChannel::create(webChannel);
77 webChannel->setClient(platform_channel.get());
78 OwnPtr<MessagePortChannel> channel =
79 MessagePortChannel::create(platform_channel);
81 workerThread()->runLoop().postTask(
82 createCallbackTask(&connectTask, channel.release()));
84 listener->connected();
87 void WebSharedWorkerImpl::connectTask(ScriptExecutionContext* context, PassOwnPtr<MessagePortChannel> channel)
89 // Wrap the passed-in channel in a MessagePort, and send it off via a connect event.
90 RefPtr<MessagePort> port = MessagePort::create(*context);
91 port->entangle(channel);
92 ASSERT(context->isWorkerContext());
93 WorkerContext* workerContext = static_cast<WorkerContext*>(context);
94 ASSERT(workerContext->isSharedWorkerContext());
95 workerContext->dispatchEvent(createConnectEvent(port));
98 void WebSharedWorkerImpl::startWorkerContext(const WebURL& url, const WebString& name, const WebString& userAgent, const WebString& sourceCode, long long)
100 initializeLoader(url);
101 WorkerThreadStartMode startMode = m_pauseWorkerContextOnStart ? PauseWorkerContextOnStart : DontPauseWorkerContextOnStart;
102 setWorkerThread(SharedWorkerThread::create(name, url, userAgent, sourceCode, *this, *this, startMode));
103 workerThread()->start();
106 void WebSharedWorkerImpl::terminateWorkerContext()
111 void WebSharedWorkerImpl::clientDestroyed()
116 void WebSharedWorkerImpl::pauseWorkerContextOnStart()
118 m_pauseWorkerContextOnStart = true;
121 static void resumeWorkerContextTask(ScriptExecutionContext* context, bool)
123 ASSERT(context->isWorkerContext());
124 static_cast<WorkerContext*>(context)->workerInspectorController()->resume();
127 void WebSharedWorkerImpl::resumeWorkerContext()
129 m_pauseWorkerContextOnStart = false;
131 workerThread()->runLoop().postTaskForMode(createCallbackTask(resumeWorkerContextTask, true), WorkerDebuggerAgent::debuggerTaskMode);
134 static void connectToWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
136 ASSERT(context->isWorkerContext());
137 static_cast<WorkerContext*>(context)->workerInspectorController()->connectFrontend();
140 void WebSharedWorkerImpl::attachDevTools()
142 workerThread()->runLoop().postTaskForMode(createCallbackTask(connectToWorkerContextInspectorTask, true), WorkerDebuggerAgent::debuggerTaskMode);
145 static void reconnectToWorkerContextInspectorTask(ScriptExecutionContext* context, const String& savedState)
147 ASSERT(context->isWorkerContext());
148 WorkerInspectorController* ic = static_cast<WorkerContext*>(context)->workerInspectorController();
149 ic->restoreInspectorStateFromCookie(savedState);
153 void WebSharedWorkerImpl::reattachDevTools(const WebString& savedState)
155 workerThread()->runLoop().postTaskForMode(createCallbackTask(reconnectToWorkerContextInspectorTask, String(savedState)), WorkerDebuggerAgent::debuggerTaskMode);
158 static void disconnectFromWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
160 ASSERT(context->isWorkerContext());
161 static_cast<WorkerContext*>(context)->workerInspectorController()->disconnectFrontend();
164 void WebSharedWorkerImpl::detachDevTools()
166 workerThread()->runLoop().postTaskForMode(createCallbackTask(disconnectFromWorkerContextInspectorTask, true), WorkerDebuggerAgent::debuggerTaskMode);
169 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
171 ASSERT(context->isWorkerContext());
172 static_cast<WorkerContext*>(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
175 void WebSharedWorkerImpl::dispatchDevToolsMessage(const WebString& message)
177 workerThread()->runLoop().postTaskForMode(createCallbackTask(dispatchOnInspectorBackendTask, String(message)), WorkerDebuggerAgent::debuggerTaskMode);
180 WebWorkerClient* WebSharedWorkerImpl::client()
182 // We should never be asked for a WebWorkerClient (only dedicated workers have an associated WebWorkerClient).
183 // It should not be possible for SharedWorkerContext to generate an API call outside those supported by WebCommonWorkerClient.
184 ASSERT_NOT_REACHED();
188 WebSharedWorker* WebSharedWorker::create(WebCommonWorkerClient* client)
190 return new WebSharedWorkerImpl(client);
193 #endif // ENABLE(SHARED_WORKERS)
195 } // namespace WebKit