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"
51 class ResourceResponse;
52 class WebApplicationCacheHost;
53 class WebApplicationCacheHostClient;
54 class WebWorkerClient;
55 class WebSecurityOrigin;
60 class WebSharedWorkerClient;
62 // This class is used by the worker process code to talk to the SharedWorker implementation.
63 // It can't use it directly since it uses WebKit types, so this class converts the data types.
64 // When the SharedWorker object wants to call WorkerReportingProxy, this class will
65 // convert to Chrome data types first and then call the supplied WebCommonWorkerClient.
66 class WebSharedWorkerImpl FINAL
67 : public WorkerReportingProxy
68 , public WorkerLoaderProxy
69 , public WebFrameClient
70 , public WebSharedWorker {
72 explicit WebSharedWorkerImpl(WebSharedWorkerClient*);
74 // WorkerReportingProxy methods:
75 virtual void reportException(
76 const WTF::String&, int, int, const WTF::String&) OVERRIDE;
77 virtual void reportConsoleMessage(PassRefPtrWillBeRawPtr<ConsoleMessage>) OVERRIDE;
78 virtual void postMessageToPageInspector(const WTF::String&) OVERRIDE;
79 virtual void updateInspectorStateCookie(const WTF::String&) OVERRIDE;
80 virtual void workerGlobalScopeStarted(WorkerGlobalScope*) OVERRIDE;
81 virtual void workerGlobalScopeClosed() OVERRIDE;
82 virtual void workerThreadTerminated() OVERRIDE;
83 virtual void willDestroyWorkerGlobalScope() OVERRIDE { }
85 // WorkerLoaderProxy methods:
86 virtual void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) OVERRIDE;
87 virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask>) OVERRIDE;
89 // WebFrameClient methods to support resource loading thru the 'shadow page'.
90 virtual WebApplicationCacheHost* createApplicationCacheHost(WebLocalFrame*, WebApplicationCacheHostClient*) OVERRIDE;
91 virtual void didFinishDocumentLoad(WebLocalFrame*) OVERRIDE;
93 // WebSharedWorker methods:
94 virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& contentSecurityPolicy, WebContentSecurityPolicyType) OVERRIDE;
95 virtual void connect(WebMessagePortChannel*) OVERRIDE;
96 virtual void terminateWorkerContext() OVERRIDE;
97 virtual void clientDestroyed() OVERRIDE;
99 virtual void pauseWorkerContextOnStart() OVERRIDE;
100 virtual void resumeWorkerContext() OVERRIDE;
101 virtual void attachDevTools() OVERRIDE;
102 virtual void reattachDevTools(const WebString& savedState) OVERRIDE;
103 virtual void detachDevTools() OVERRIDE;
104 virtual void dispatchDevToolsMessage(const WebString&) OVERRIDE;
109 virtual ~WebSharedWorkerImpl();
111 WebSharedWorkerClient* client() { return m_client->get(); }
113 void setWorkerThread(PassRefPtr<WorkerThread> thread) { m_workerThread = thread; }
114 WorkerThread* workerThread() { return m_workerThread.get(); }
116 // Shuts down the worker thread.
117 void stopWorkerThread();
119 // Creates the shadow loader used for worker network requests.
120 void initializeLoader(const WebURL&);
122 void didReceiveScriptLoaderResponse();
123 void onScriptLoaderFinished();
125 static void connectTask(ExecutionContext*, PassOwnPtr<WebMessagePortChannel>);
126 // Tasks that are run on the main thread.
127 void workerGlobalScopeClosedOnMainThread();
128 void workerThreadTerminatedOnMainThread();
130 // 'shadow page' - created to proxy loading requests from the worker.
131 RefPtrWillBePersistent<ExecutionContext> m_loadingDocument;
133 WebFrame* m_mainFrame;
134 bool m_askedToTerminate;
136 RefPtr<WorkerThread> m_workerThread;
138 // This one's initialized and bound to the main thread.
139 RefPtr<WeakReference<WebSharedWorkerClient> > m_client;
141 // Usually WeakPtr is created by WeakPtrFactory exposed by Client
142 // class itself, but here it's implemented by Chrome so we create
144 WeakPtr<WebSharedWorkerClient> m_clientWeakPtr;
146 bool m_pauseWorkerContextOnStart;
147 bool m_attachDevToolsOnStart;
149 // Kept around only while main script loading is ongoing.
150 OwnPtr<Loader> m_mainScriptLoader;
153 WebString m_contentSecurityPolicy;
154 WebContentSecurityPolicyType m_policyType;