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/WorkerScriptLoaderClient.h"
40 #include "core/workers/WorkerThread.h"
41 #include "public/web/WebContentSecurityPolicy.h"
42 #include "public/web/WebFrameClient.h"
43 #include "public/web/WebSharedWorkerClient.h"
44 #include "wtf/PassOwnPtr.h"
45 #include "wtf/RefPtr.h"
46 #include "wtf/WeakPtr.h"
49 class ResourceResponse;
53 class WebApplicationCacheHost;
54 class WebApplicationCacheHostClient;
55 class WebWorkerClient;
56 class WebSecurityOrigin;
61 class WebSharedWorkerClient;
63 // 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.
67 class WebSharedWorkerImpl FINAL
68 : public WebCore::WorkerReportingProxy
69 , public WebCore::WorkerLoaderProxy
70 , public WebFrameClient
71 , public WebSharedWorker {
73 explicit WebSharedWorkerImpl(WebSharedWorkerClient*);
75 // WebCore::WorkerReportingProxy methods:
76 virtual void reportException(
77 const WTF::String&, int, int, const WTF::String&) OVERRIDE;
78 virtual void reportConsoleMessage(
79 WebCore::MessageSource, WebCore::MessageLevel,
80 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;
86 virtual void willDestroyWorkerGlobalScope() OVERRIDE { }
88 // WebCore::WorkerLoaderProxy methods:
89 virtual void postTaskToLoader(PassOwnPtr<WebCore::ExecutionContextTask>) OVERRIDE;
90 virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<WebCore::ExecutionContextTask>) OVERRIDE;
92 // WebFrameClient methods to support resource loading thru the 'shadow page'.
93 virtual WebApplicationCacheHost* createApplicationCacheHost(WebLocalFrame*, WebApplicationCacheHostClient*) OVERRIDE;
94 virtual void didFinishDocumentLoad(WebLocalFrame*) OVERRIDE;
96 // WebSharedWorker methods:
97 virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& contentSecurityPolicy, WebContentSecurityPolicyType) OVERRIDE;
98 virtual void connect(WebMessagePortChannel*) OVERRIDE;
99 virtual void terminateWorkerContext() OVERRIDE;
100 virtual void clientDestroyed() OVERRIDE;
102 virtual void pauseWorkerContextOnStart() OVERRIDE;
103 virtual void resumeWorkerContext() OVERRIDE;
104 virtual void attachDevTools() OVERRIDE;
105 virtual void reattachDevTools(const WebString& savedState) OVERRIDE;
106 virtual void detachDevTools() OVERRIDE;
107 virtual void dispatchDevToolsMessage(const WebString&) OVERRIDE;
112 virtual ~WebSharedWorkerImpl();
114 WebSharedWorkerClient* client() { return m_client->get(); }
116 void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; }
117 WebCore::WorkerThread* workerThread() { return m_workerThread.get(); }
119 // Shuts down the worker thread.
120 void stopWorkerThread();
122 // Creates the shadow loader used for worker network requests.
123 void initializeLoader(const WebURL&);
125 void didReceiveScriptLoaderResponse();
126 void onScriptLoaderFinished();
128 static void connectTask(WebCore::ExecutionContext*, PassOwnPtr<WebMessagePortChannel>);
129 // Tasks that are run on the main thread.
130 void workerGlobalScopeClosedOnMainThread();
131 void workerGlobalScopeDestroyedOnMainThread();
133 // 'shadow page' - created to proxy loading requests from the worker.
134 RefPtr<WebCore::ExecutionContext> m_loadingDocument;
136 WebFrame* m_mainFrame;
137 bool m_askedToTerminate;
139 RefPtr<WebCore::WorkerThread> m_workerThread;
141 // This one's initialized and bound to the main thread.
142 RefPtr<WeakReference<WebSharedWorkerClient> > m_client;
144 // Usually WeakPtr is created by WeakPtrFactory exposed by Client
145 // class itself, but here it's implemented by Chrome so we create
147 WeakPtr<WebSharedWorkerClient> m_clientWeakPtr;
149 bool m_pauseWorkerContextOnStart;
150 bool m_attachDevToolsOnStart;
152 // Kept around only while main script loading is ongoing.
153 OwnPtr<Loader> m_mainScriptLoader;
156 WebString m_contentSecurityPolicy;
157 WebContentSecurityPolicyType m_policyType;