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 #ifndef WebSharedWorkerImpl_h 32 #define WebSharedWorkerImpl_h 33 34 #include "WebSharedWorker.h" 35 36 #include "WebCommonWorkerClient.h" 37 #include "WebContentSecurityPolicy.h" 38 #include "WebFrameClient.h" 39 #include "WebSharedWorkerClient.h" 40 #include "WebWorkerBase.h" 41 #include "core/dom/ScriptExecutionContext.h" 42 #include "core/workers/WorkerLoaderProxy.h" 43 #include "core/workers/WorkerObjectProxy.h" 44 #include "core/workers/WorkerThread.h" 45 #include "wtf/PassOwnPtr.h" 46 #include "wtf/RefPtr.h" 47 48 49 namespace WebKit { 50 class WebApplicationCacheHost; 51 class WebApplicationCacheHostClient; 52 class WebWorkerClient; 53 class WebSecurityOrigin; 54 class WebString; 55 class WebURL; 56 class WebView; 57 class WebWorker; 58 class WebSharedWorkerClient; 59 // This class is used by the worker process code to talk to the WebCore::SharedWorker implementation. 60 // It can't use it directly since it uses WebKit types, so this class converts the data types. 61 // When the WebCore::SharedWorker object wants to call WebCore::WorkerReportingProxy, this class will 62 // convert to Chrome data types first and then call the supplied WebCommonWorkerClient. 63 class WebSharedWorkerImpl 64 : public WebCore::WorkerObjectProxy 65 , public WebCore::WorkerLoaderProxy 66 , public WebWorkerBase 67 , public WebFrameClient 68 , public WebSharedWorker { 69 public: 70 explicit WebSharedWorkerImpl(WebSharedWorkerClient*); 71 72 virtual void postMessageToWorkerObject( 73 PassRefPtr<WebCore::SerializedScriptValue>, 74 PassOwnPtr<WebCore::MessagePortChannelArray>); 75 virtual void postExceptionToWorkerObject( 76 const WTF::String&, int, int, const WTF::String&); 77 virtual void postConsoleMessageToWorkerObject( 78 WebCore::MessageSource, WebCore::MessageLevel, 79 const WTF::String&, int, const WTF::String&); 80 virtual void postMessageToPageInspector(const WTF::String&); 81 virtual void updateInspectorStateCookie(const WTF::String&); 82 virtual void confirmMessageFromWorkerObject(bool); 83 virtual void reportPendingActivity(bool); 84 virtual void workerGlobalScopeClosed(); 85 virtual void workerGlobalScopeDestroyed(); 86 virtual WebView* view() const { return m_webView; } 87 88 // WebCore::WorkerLoaderProxy methods: 89 virtual void postTaskToLoader(PassOwnPtr<WebCore::ScriptExecutionContext::Task>); 90 virtual bool postTaskForModeToWorkerGlobalScope( 91 PassOwnPtr<WebCore::ScriptExecutionContext::Task>, const WTF::String& mode); 92 virtual WebWorkerBase* toWebWorkerBase() OVERRIDE; 93 94 // WebFrameClient methods to support resource loading thru the 'shadow page'. 95 virtual void didCreateDataSource(WebFrame*, WebDataSource*); 96 virtual WebApplicationCacheHost* createApplicationCacheHost(WebFrame*, WebApplicationCacheHostClient*); 97 98 99 // WebSharedWorker methods: 100 virtual bool isStarted(); 101 102 virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& userAgent, const WebString& sourceCode, const WebString& contentSecurityPolicy, WebContentSecurityPolicyType, long long cacheId); 103 104 virtual void connect(WebMessagePortChannel*, ConnectListener*); 105 virtual void terminateWorkerContext(); 106 virtual void clientDestroyed(); 107 108 virtual void pauseWorkerContextOnStart(); 109 virtual void resumeWorkerContext(); 110 virtual void attachDevTools(); 111 virtual void reattachDevTools(const WebString& savedState); 112 virtual void detachDevTools(); 113 virtual void dispatchDevToolsMessage(const WebString&); 114 115 116 // WebWorkerBase methods: 117 WebCore::WorkerLoaderProxy* workerLoaderProxy() { return this; } 118 WebCommonWorkerClient* commonClient() { return m_client; } 119 120 private: 121 virtual ~WebSharedWorkerImpl(); 122 123 WebSharedWorkerClient* client() { return m_client; } 124 125 void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; } 126 WebCore::WorkerThread* workerThread() { return m_workerThread.get(); } 127 128 // Shuts down the worker thread. 129 void stopWorkerThread(); 130 131 // Creates the shadow loader used for worker network requests. 132 void initializeLoader(const WebURL&); 133 134 135 static void connectTask(WebCore::ScriptExecutionContext*, PassOwnPtr<WebCore::MessagePortChannel>); 136 // Tasks that are run on the main thread. 137 static void postMessageTask( 138 WebCore::ScriptExecutionContext*, 139 WebSharedWorkerImpl* thisPtr, 140 WTF::String message, 141 PassOwnPtr<WebCore::MessagePortChannelArray> channels); 142 static void postExceptionTask( 143 WebCore::ScriptExecutionContext*, 144 WebSharedWorkerImpl* thisPtr, 145 const WTF::String& message, 146 int lineNumber, 147 const WTF::String& sourceURL); 148 static void postConsoleMessageTask( 149 WebCore::ScriptExecutionContext*, 150 WebSharedWorkerImpl* thisPtr, 151 int source, 152 int level, 153 const WTF::String& message, 154 int lineNumber, 155 const WTF::String& sourceURL); 156 static void postMessageToPageInspectorTask(WebCore::ScriptExecutionContext*, WebSharedWorkerImpl*, const WTF::String&); 157 static void updateInspectorStateCookieTask(WebCore::ScriptExecutionContext*, WebSharedWorkerImpl* thisPtr, const WTF::String& cookie); 158 static void confirmMessageTask( 159 WebCore::ScriptExecutionContext*, 160 WebSharedWorkerImpl* thisPtr, 161 bool hasPendingActivity); 162 static void reportPendingActivityTask( 163 WebCore::ScriptExecutionContext*, 164 WebSharedWorkerImpl* thisPtr, 165 bool hasPendingActivity); 166 static void workerGlobalScopeClosedTask( 167 WebCore::ScriptExecutionContext*, 168 WebSharedWorkerImpl* thisPtr); 169 static void workerGlobalScopeDestroyedTask( 170 WebCore::ScriptExecutionContext*, 171 WebSharedWorkerImpl* thisPtr); 172 173 // 'shadow page' - created to proxy loading requests from the worker. 174 RefPtr<WebCore::ScriptExecutionContext> m_loadingDocument; 175 WebView* m_webView; 176 bool m_askedToTerminate; 177 178 RefPtr<WebCore::WorkerThread> m_workerThread; 179 180 WebSharedWorkerClient* m_client; 181 bool m_pauseWorkerContextOnStart; 182 }; 183 184 } // namespace WebKit 185 186 #endif 187