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 "WorkerInspectorController.h"
43 #include "WorkerScriptDebugServer.h"
45 #include "WebMessagePortChannel.h"
46 #include "WebString.h"
49 using namespace WebCore;
53 #if ENABLE(SHARED_WORKERS)
55 WebSharedWorkerImpl::WebSharedWorkerImpl(WebCommonWorkerClient* client)
60 WebSharedWorkerImpl::~WebSharedWorkerImpl()
64 bool WebSharedWorkerImpl::isStarted()
66 // Should not ever be called from the worker thread (this API is only called on WebSharedWorkerProxy on the renderer thread).
68 return workerThread();
71 void WebSharedWorkerImpl::connect(WebMessagePortChannel* webChannel, ConnectListener* listener)
73 // Convert the WebMessagePortChanel to a WebCore::MessagePortChannel.
74 RefPtr<PlatformMessagePortChannel> platform_channel =
75 PlatformMessagePortChannel::create(webChannel);
76 webChannel->setClient(platform_channel.get());
77 OwnPtr<MessagePortChannel> channel =
78 MessagePortChannel::create(platform_channel);
80 workerThread()->runLoop().postTask(
81 createCallbackTask(&connectTask, channel.release()));
83 listener->connected();
86 void WebSharedWorkerImpl::connectTask(ScriptExecutionContext* context, PassOwnPtr<MessagePortChannel> channel)
88 // Wrap the passed-in channel in a MessagePort, and send it off via a connect event.
89 RefPtr<MessagePort> port = MessagePort::create(*context);
90 port->entangle(channel);
91 ASSERT(context->isWorkerContext());
92 WorkerContext* workerContext = static_cast<WorkerContext*>(context);
93 ASSERT(workerContext->isSharedWorkerContext());
94 workerContext->toSharedWorkerContext()->dispatchEvent(createConnectEvent(port));
97 void WebSharedWorkerImpl::startWorkerContext(const WebURL& url, const WebString& name, const WebString& userAgent, const WebString& sourceCode, long long)
99 initializeLoader(url);
100 setWorkerThread(SharedWorkerThread::create(name, url, userAgent, sourceCode, *this, *this));
101 workerThread()->start();
104 void WebSharedWorkerImpl::terminateWorkerContext()
109 void WebSharedWorkerImpl::clientDestroyed()
114 static void connectToWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
116 ASSERT(context->isWorkerContext());
117 static_cast<WorkerContext*>(context)->workerInspectorController()->connectFrontend();
120 void WebSharedWorkerImpl::attachDevTools()
122 workerThread()->runLoop().postTask(createCallbackTask(connectToWorkerContextInspectorTask, true));
125 static void disconnectFromWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
127 ASSERT(context->isWorkerContext());
128 static_cast<WorkerContext*>(context)->workerInspectorController()->disconnectFrontend();
131 void WebSharedWorkerImpl::detachDevTools()
133 workerThread()->runLoop().postTaskForMode(createCallbackTask(disconnectFromWorkerContextInspectorTask, true), WorkerScriptDebugServer::debuggerTaskMode);
136 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
138 ASSERT(context->isWorkerContext());
139 static_cast<WorkerContext*>(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
142 void WebSharedWorkerImpl::dispatchDevToolsMessage(const WebString& message)
144 workerThread()->runLoop().postTaskForMode(createCallbackTask(dispatchOnInspectorBackendTask, String(message)), WorkerScriptDebugServer::debuggerTaskMode);
147 WebWorkerClient* WebSharedWorkerImpl::client()
149 // We should never be asked for a WebWorkerClient (only dedicated workers have an associated WebWorkerClient).
150 // It should not be possible for SharedWorkerContext to generate an API call outside those supported by WebCommonWorkerClient.
151 ASSERT_NOT_REACHED();
155 WebSharedWorker* WebSharedWorker::create(WebCommonWorkerClient* client)
157 return new WebSharedWorkerImpl(client);
160 #endif // ENABLE(SHARED_WORKERS)
162 } // namespace WebKit