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 "WebContentSecurityPolicy.h" 37 #include "WebFrameClient.h" 38 #include "WebSharedWorkerClient.h" 39 #include "core/dom/ExecutionContext.h" 40 #include "core/workers/WorkerLoaderProxy.h" 41 #include "core/workers/WorkerReportingProxy.h" 42 #include "core/workers/WorkerThread.h" 43 #include "wtf/PassOwnPtr.h" 44 #include "wtf/RefPtr.h" 45 #include "wtf/WeakPtr.h" 46 47 48 namespace blink { 49 class WebApplicationCacheHost; 50 class WebApplicationCacheHostClient; 51 class WebWorkerClient; 52 class WebSecurityOrigin; 53 class WebString; 54 class WebURL; 55 class WebView; 56 class WebWorker; 57 class WebSharedWorkerClient; 58 // This class is used by the worker process code to talk to the WebCore::SharedWorker implementation. 59 // It can't use it directly since it uses WebKit types, so this class converts the data types. 60 // When the WebCore::SharedWorker object wants to call WebCore::WorkerReportingProxy, this class will 61 // convert to Chrome data types first and then call the supplied WebCommonWorkerClient. 62 class WebSharedWorkerImpl 63 : public WebCore::WorkerReportingProxy 64 , public WebCore::WorkerLoaderProxy 65 , public WebFrameClient 66 , public WebSharedWorker { 67 public: 68 explicit WebSharedWorkerImpl(WebSharedWorkerClient*); 69 70 // WebCore::WorkerReportingProxy methods: 71 virtual void reportException( 72 const WTF::String&, int, int, const WTF::String&); 73 virtual void reportConsoleMessage( 74 WebCore::MessageSource, WebCore::MessageLevel, 75 const WTF::String&, int, const WTF::String&); 76 virtual void postMessageToPageInspector(const WTF::String&); 77 virtual void updateInspectorStateCookie(const WTF::String&); 78 virtual void workerGlobalScopeStarted(); 79 virtual void workerGlobalScopeClosed(); 80 virtual void workerGlobalScopeDestroyed(); 81 82 // WebCore::WorkerLoaderProxy methods: 83 virtual void postTaskToLoader(PassOwnPtr<WebCore::ExecutionContextTask>); 84 virtual bool postTaskForModeToWorkerGlobalScope( 85 PassOwnPtr<WebCore::ExecutionContextTask>, const WTF::String& mode); 86 87 // WebFrameClient methods to support resource loading thru the 'shadow page'. 88 virtual void didCreateDataSource(WebFrame*, WebDataSource*); 89 virtual WebApplicationCacheHost* createApplicationCacheHost(WebFrame*, WebApplicationCacheHostClient*); 90 91 // WebSharedWorkerConnector methods: 92 virtual bool isStarted(); 93 virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& userAgent, const WebString& sourceCode, const WebString& contentSecurityPolicy, WebContentSecurityPolicyType, long long cacheId); 94 virtual void connect(WebMessagePortChannel*, ConnectListener*); 95 96 // WebSharedWorker methods: 97 virtual void terminateWorkerContext(); 98 virtual void clientDestroyed(); 99 100 virtual void pauseWorkerContextOnStart(); 101 virtual void resumeWorkerContext(); 102 virtual void attachDevTools(); 103 virtual void reattachDevTools(const WebString& savedState); 104 virtual void detachDevTools(); 105 virtual void dispatchDevToolsMessage(const WebString&); 106 107 private: 108 virtual ~WebSharedWorkerImpl(); 109 110 WebSharedWorkerClient* client() { return m_client->get(); } 111 112 void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; } 113 WebCore::WorkerThread* workerThread() { return m_workerThread.get(); } 114 115 // Shuts down the worker thread. 116 void stopWorkerThread(); 117 118 // Creates the shadow loader used for worker network requests. 119 void initializeLoader(const WebURL&); 120 121 122 static void connectTask(WebCore::ExecutionContext*, PassOwnPtr<WebMessagePortChannel>); 123 // Tasks that are run on the main thread. 124 void workerGlobalScopeClosedOnMainThread(); 125 void workerGlobalScopeDestroyedOnMainThread(); 126 127 // 'shadow page' - created to proxy loading requests from the worker. 128 RefPtr<WebCore::ExecutionContext> m_loadingDocument; 129 WebView* m_webView; 130 WebFrame* m_mainFrame; 131 bool m_askedToTerminate; 132 133 RefPtr<WebCore::WorkerThread> m_workerThread; 134 135 // This one's initialized and bound to the main thread. 136 RefPtr<WeakReference<WebSharedWorkerClient> > m_client; 137 138 // Usually WeakPtr is created by WeakPtrFactory exposed by Client 139 // class itself, but here it's implemented by Chrome so we create 140 // our own WeakPtr. 141 WeakPtr<WebSharedWorkerClient> m_clientWeakPtr; 142 143 bool m_pauseWorkerContextOnStart; 144 }; 145 146 } // namespace blink 147 148 #endif 149