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.
31 #ifndef WebSharedWorkerImpl_h
32 #define WebSharedWorkerImpl_h
34 #include "public/web/WebSharedWorker.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/workers/WorkerLoaderProxy.h"
38 #include "core/workers/WorkerReportingProxy.h"
39 #include "core/workers/WorkerThread.h"
40 #include "public/web/WebContentSecurityPolicy.h"
41 #include "public/web/WebDevToolsAgentClient.h"
42 #include "public/web/WebFrameClient.h"
43 #include "public/web/WebSharedWorkerClient.h"
44 #include "wtf/PassOwnPtr.h"
45 #include "wtf/RefPtr.h"
50 class WebApplicationCacheHost
;
51 class WebApplicationCacheHostClient
;
52 class WebLocalFrameImpl
;
53 class WebServiceWorkerNetworkProvider
;
54 class WebSharedWorkerClient
;
58 class WorkerInspectorProxy
;
59 class WorkerScriptLoader
;
61 // This class is used by the worker process code to talk to the SharedWorker
63 // It can't use it directly since it uses WebKit types, so this class converts
64 // the data types. When the SharedWorker object wants to call
65 // WorkerReportingProxy, this class will convert to Chrome data types first and
66 // then call the supplied WebCommonWorkerClient.
67 class WebSharedWorkerImpl final
68 : public WorkerReportingProxy
69 , public WebFrameClient
70 , public WebSharedWorker
71 , public WebDevToolsAgentClient
72 , private WorkerLoaderProxyProvider
{
74 explicit WebSharedWorkerImpl(WebSharedWorkerClient
*);
76 // WorkerReportingProxy methods:
78 const WTF::String
&, int, int, const WTF::String
&, int) override
;
79 void reportConsoleMessage(PassRefPtrWillBeRawPtr
<ConsoleMessage
>) override
;
80 void postMessageToPageInspector(const WTF::String
&) override
;
81 void postWorkerConsoleAgentEnabled() override
{ }
82 void didEvaluateWorkerScript(bool success
) override
{ }
83 void workerGlobalScopeStarted(WorkerGlobalScope
*) override
;
84 void workerGlobalScopeClosed() override
;
85 void workerThreadTerminated() override
;
86 void willDestroyWorkerGlobalScope() override
{ }
88 // WebFrameClient methods to support resource loading thru the 'shadow page'.
89 WebApplicationCacheHost
* createApplicationCacheHost(WebLocalFrame
*, WebApplicationCacheHostClient
*) override
;
90 void willSendRequest(WebLocalFrame
*, unsigned identifier
, WebURLRequest
&, const WebURLResponse
& redirectResponse
) override
;
91 void didFinishDocumentLoad(WebLocalFrame
*, bool documentIsEmpty
) override
;
92 bool isControlledByServiceWorker(WebDataSource
&) override
;
93 int64_t serviceWorkerID(WebDataSource
&) override
;
95 // WebDevToolsAgentClient overrides.
96 void sendProtocolMessage(int callId
, const WebString
&, const WebString
&) override
;
97 void resumeStartup() override
;
99 // WebSharedWorker methods:
100 void startWorkerContext(const WebURL
&, const WebString
& name
, const WebString
& contentSecurityPolicy
, WebContentSecurityPolicyType
) override
;
101 void connect(WebMessagePortChannel
*) override
;
102 void terminateWorkerContext() override
;
104 void pauseWorkerContextOnStart() override
;
105 void attachDevTools(const WebString
& hostId
) override
;
106 void reattachDevTools(const WebString
& hostId
, const WebString
& savedState
) override
;
107 void detachDevTools() override
;
108 void dispatchDevToolsMessage(const WebString
&) override
;
111 ~WebSharedWorkerImpl() override
;
113 void setWorkerThread(PassRefPtr
<WorkerThread
> thread
) { m_workerThread
= thread
; }
114 WorkerThread
* workerThread() { return m_workerThread
.get(); }
116 // Shuts down the worker thread.
117 void terminateWorkerThread();
119 // Creates the shadow loader used for worker network requests.
120 void initializeLoader();
122 void loadShadowPage();
123 void didReceiveScriptLoaderResponse();
124 void onScriptLoaderFinished();
126 static void connectTask(PassOwnPtr
<WebMessagePortChannel
>, ExecutionContext
*);
127 // Tasks that are run on the main thread.
128 void workerGlobalScopeClosedOnMainThread();
129 void workerThreadTerminatedOnMainThread();
131 void postMessageToPageInspectorOnMainThread(const String
& message
);
133 // WorkerLoaderProxyProvider
134 void postTaskToLoader(PassOwnPtr
<ExecutionContextTask
>);
135 bool postTaskToWorkerGlobalScope(PassOwnPtr
<ExecutionContextTask
>);
137 // 'shadow page' - created to proxy loading requests from the worker.
138 RefPtrWillBePersistent
<ExecutionContext
> m_loadingDocument
;
140 RefPtrWillBePersistent
<WebLocalFrameImpl
> m_mainFrame
;
141 bool m_askedToTerminate
;
143 // This one is bound to and used only on the main thread.
144 OwnPtr
<WebServiceWorkerNetworkProvider
> m_networkProvider
;
146 OwnPtrWillBePersistent
<WorkerInspectorProxy
> m_workerInspectorProxy
;
148 RefPtr
<WorkerThread
> m_workerThread
;
150 WebSharedWorkerClient
* m_client
;
152 bool m_pauseWorkerContextOnStart
;
153 bool m_isPausedOnStart
;
155 // Kept around only while main script loading is ongoing.
156 RefPtr
<WorkerScriptLoader
> m_mainScriptLoader
;
158 RefPtr
<WorkerLoaderProxy
> m_loaderProxy
;
166 #endif // WebSharedWorkerImpl_h