blob: a13e5d7b2c0a40b78381fa64d1fefce0b6a4b929 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
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#ifndef WebSharedWorkerImpl_h
32#define WebSharedWorkerImpl_h
33
34#include "WebSharedWorker.h"
35
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000036#include "WebContentSecurityPolicy.h"
37#include "WebFrameClient.h"
38#include "WebSharedWorkerClient.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010039#include "core/dom/ExecutionContext.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040#include "core/workers/WorkerLoaderProxy.h"
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000041#include "core/workers/WorkerReportingProxy.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000042#include "core/workers/WorkerScriptLoaderClient.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043#include "core/workers/WorkerThread.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010044#include "wtf/PassOwnPtr.h"
45#include "wtf/RefPtr.h"
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000046#include "wtf/WeakPtr.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000047
Torne (Richard Coles)09380292014-02-21 12:17:33 +000048namespace WebCore {
49class ResourceResponse;
50}
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000051
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000052namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000053class WebApplicationCacheHost;
54class WebApplicationCacheHostClient;
55class WebWorkerClient;
56class WebSecurityOrigin;
57class WebString;
58class WebURL;
59class WebView;
60class WebWorker;
61class WebSharedWorkerClient;
Torne (Richard Coles)09380292014-02-21 12:17:33 +000062
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000063// This class is used by the worker process code to talk to the WebCore::SharedWorker implementation.
64// It can't use it directly since it uses WebKit types, so this class converts the data types.
65// When the WebCore::SharedWorker object wants to call WebCore::WorkerReportingProxy, this class will
66// convert to Chrome data types first and then call the supplied WebCommonWorkerClient.
Torne (Richard Coles)09380292014-02-21 12:17:33 +000067class WebSharedWorkerImpl FINAL
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000068 : public WebCore::WorkerReportingProxy
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000069 , public WebCore::WorkerLoaderProxy
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000070 , public WebFrameClient
71 , public WebSharedWorker {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000072public:
73 explicit WebSharedWorkerImpl(WebSharedWorkerClient*);
74
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000075 // WebCore::WorkerReportingProxy methods:
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000076 virtual void reportException(
Torne (Richard Coles)09380292014-02-21 12:17:33 +000077 const WTF::String&, int, int, const WTF::String&) OVERRIDE;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000078 virtual void reportConsoleMessage(
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000079 WebCore::MessageSource, WebCore::MessageLevel,
Torne (Richard Coles)09380292014-02-21 12:17:33 +000080 const WTF::String&, int, const WTF::String&) OVERRIDE;
81 virtual void postMessageToPageInspector(const WTF::String&) OVERRIDE;
82 virtual void updateInspectorStateCookie(const WTF::String&) OVERRIDE;
83 virtual void workerGlobalScopeStarted(WebCore::WorkerGlobalScope*) OVERRIDE;
84 virtual void workerGlobalScopeClosed() OVERRIDE;
85 virtual void workerGlobalScopeDestroyed() OVERRIDE;
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000086
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000087 // WebCore::WorkerLoaderProxy methods:
Torne (Richard Coles)09380292014-02-21 12:17:33 +000088 virtual void postTaskToLoader(PassOwnPtr<WebCore::ExecutionContextTask>) OVERRIDE;
89 virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<WebCore::ExecutionContextTask>) OVERRIDE;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000090
91 // WebFrameClient methods to support resource loading thru the 'shadow page'.
Torne (Richard Coles)09380292014-02-21 12:17:33 +000092 virtual WebApplicationCacheHost* createApplicationCacheHost(WebFrame*, WebApplicationCacheHostClient*) OVERRIDE;
93 virtual void didFinishDocumentLoad(WebFrame*) OVERRIDE;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000094
95 // WebSharedWorker methods:
Torne (Richard Coles)09380292014-02-21 12:17:33 +000096 virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& contentSecurityPolicy, WebContentSecurityPolicyType) OVERRIDE;
97 virtual void connect(WebMessagePortChannel*) OVERRIDE;
98 virtual void terminateWorkerContext() OVERRIDE;
99 virtual void clientDestroyed() OVERRIDE;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000100
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000101 virtual void pauseWorkerContextOnStart() OVERRIDE;
102 virtual void resumeWorkerContext() OVERRIDE;
103 virtual void attachDevTools() OVERRIDE;
104 virtual void reattachDevTools(const WebString& savedState) OVERRIDE;
105 virtual void detachDevTools() OVERRIDE;
106 virtual void dispatchDevToolsMessage(const WebString&) OVERRIDE;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000107
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000108private:
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000109 class Loader;
110
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000111 virtual ~WebSharedWorkerImpl();
112
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000113 WebSharedWorkerClient* client() { return m_client->get(); }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000114
115 void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; }
116 WebCore::WorkerThread* workerThread() { return m_workerThread.get(); }
117
118 // Shuts down the worker thread.
119 void stopWorkerThread();
120
121 // Creates the shadow loader used for worker network requests.
122 void initializeLoader(const WebURL&);
123
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000124 void didReceiveScriptLoaderResponse();
125 void onScriptLoaderFinished();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000126
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000127 static void connectTask(WebCore::ExecutionContext*, PassOwnPtr<WebMessagePortChannel>);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000128 // Tasks that are run on the main thread.
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000129 void workerGlobalScopeClosedOnMainThread();
130 void workerGlobalScopeDestroyedOnMainThread();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000131
132 // 'shadow page' - created to proxy loading requests from the worker.
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100133 RefPtr<WebCore::ExecutionContext> m_loadingDocument;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000134 WebView* m_webView;
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100135 WebFrame* m_mainFrame;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000136 bool m_askedToTerminate;
137
138 RefPtr<WebCore::WorkerThread> m_workerThread;
139
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000140 // This one's initialized and bound to the main thread.
141 RefPtr<WeakReference<WebSharedWorkerClient> > m_client;
142
143 // Usually WeakPtr is created by WeakPtrFactory exposed by Client
144 // class itself, but here it's implemented by Chrome so we create
145 // our own WeakPtr.
146 WeakPtr<WebSharedWorkerClient> m_clientWeakPtr;
147
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000148 bool m_pauseWorkerContextOnStart;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000149 bool m_attachDevToolsOnStart;
150
151 // Kept around only while main script loading is ongoing.
152 OwnPtr<Loader> m_mainScriptLoader;
153 WebURL m_url;
154 WebString m_name;
155 WebString m_contentSecurityPolicy;
156 WebContentSecurityPolicyType m_policyType;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000157};
158
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000159} // namespace blink
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000160
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000161#endif