initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WebSharedWorkerImpl.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 "WebSharedWorkerImpl.h"
33
34 #include "CrossThreadTask.h"
35 #include "KURL.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"
44
45 #include "WebMessagePortChannel.h"
46 #include "WebString.h"
47 #include "WebURL.h"
48
49 using namespace WebCore;
50
51 namespace WebKit {
52
53 #if ENABLE(SHARED_WORKERS)
54
55 WebSharedWorkerImpl::WebSharedWorkerImpl(WebCommonWorkerClient* client)
56     : m_client(client)
57 {
58 }
59
60 WebSharedWorkerImpl::~WebSharedWorkerImpl()
61 {
62 }
63
64 bool WebSharedWorkerImpl::isStarted()
65 {
66     // Should not ever be called from the worker thread (this API is only called on WebSharedWorkerProxy on the renderer thread).
67     ASSERT_NOT_REACHED();
68     return workerThread();
69 }
70
71 void WebSharedWorkerImpl::connect(WebMessagePortChannel* webChannel, ConnectListener* listener)
72 {
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);
79
80     workerThread()->runLoop().postTask(
81         createCallbackTask(&connectTask, channel.release()));
82     if (listener)
83         listener->connected();
84 }
85
86 void WebSharedWorkerImpl::connectTask(ScriptExecutionContext* context, PassOwnPtr<MessagePortChannel> channel)
87 {
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));
95 }
96
97 void WebSharedWorkerImpl::startWorkerContext(const WebURL& url, const WebString& name, const WebString& userAgent, const WebString& sourceCode, long long)
98 {
99     initializeLoader(url);
100     setWorkerThread(SharedWorkerThread::create(name, url, userAgent, sourceCode, *this, *this));
101     workerThread()->start();
102 }
103
104 void WebSharedWorkerImpl::terminateWorkerContext()
105 {
106     stopWorkerThread();
107 }
108
109 void WebSharedWorkerImpl::clientDestroyed()
110 {
111     m_client = 0;
112 }
113
114 static void connectToWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
115 {
116     ASSERT(context->isWorkerContext());
117     static_cast<WorkerContext*>(context)->workerInspectorController()->connectFrontend();
118 }
119
120 void WebSharedWorkerImpl::attachDevTools()
121 {
122     workerThread()->runLoop().postTask(createCallbackTask(connectToWorkerContextInspectorTask, true));
123 }
124
125 static void disconnectFromWorkerContextInspectorTask(ScriptExecutionContext* context, bool)
126 {
127     ASSERT(context->isWorkerContext());
128     static_cast<WorkerContext*>(context)->workerInspectorController()->disconnectFrontend();
129 }
130
131 void WebSharedWorkerImpl::detachDevTools()
132 {
133     workerThread()->runLoop().postTaskForMode(createCallbackTask(disconnectFromWorkerContextInspectorTask, true), WorkerScriptDebugServer::debuggerTaskMode);
134 }
135
136 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
137 {
138     ASSERT(context->isWorkerContext());
139     static_cast<WorkerContext*>(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
140 }
141
142 void WebSharedWorkerImpl::dispatchDevToolsMessage(const WebString& message)
143 {
144     workerThread()->runLoop().postTaskForMode(createCallbackTask(dispatchOnInspectorBackendTask, String(message)), WorkerScriptDebugServer::debuggerTaskMode);
145 }
146
147 WebWorkerClient* WebSharedWorkerImpl::client()
148 {
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();
152     return 0;
153 }
154
155 WebSharedWorker* WebSharedWorker::create(WebCommonWorkerClient* client)
156 {
157     return new WebSharedWorkerImpl(client);
158 }
159
160 #endif // ENABLE(SHARED_WORKERS)
161
162 } // namespace WebKit