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 WebWorkerClientImpl_h 32 #define WebWorkerClientImpl_h 33 34 #if ENABLE(WORKERS) 35 36 #include "WebFileSystem.h" 37 #include "WebWorkerClient.h" 38 #include "WorkerContextProxy.h" 39 #include <wtf/PassOwnPtr.h> 40 #include <wtf/RefPtr.h> 41 42 namespace WebCore { 43 class ScriptExecutionContext; 44 } 45 46 namespace WebKit { 47 class WebWorker; 48 49 // The purpose of this class is to provide a WorkerContextProxy 50 // implementation that we can give to WebKit. Internally, it converts the 51 // data types to Chrome compatible ones so that renderer code can use it over 52 // IPC. 53 class WebWorkerClientImpl : public WebCore::WorkerContextProxy 54 , public WebWorkerClient { 55 public: 56 WebWorkerClientImpl(WebCore::Worker*); 57 58 // WebCore::WorkerContextProxy Factory. 59 static WebCore::WorkerContextProxy* createWorkerContextProxy(WebCore::Worker*); 60 void setWebWorker(WebWorker*); 61 62 // WebCore::WorkerContextProxy methods: 63 // These are called on the thread that created the worker. In the renderer 64 // process, this will be the main WebKit thread. In the worker process, this 65 // will be the thread of the executing worker (not the main WebKit thread). 66 virtual void startWorkerContext(const WebCore::KURL&, 67 const WTF::String&, 68 const WTF::String&); 69 virtual void terminateWorkerContext(); 70 virtual void postMessageToWorkerContext( 71 PassRefPtr<WebCore::SerializedScriptValue> message, 72 PassOwnPtr<WebCore::MessagePortChannelArray> channels); 73 virtual bool hasPendingActivity() const; 74 virtual void workerObjectDestroyed(); 75 76 // WebWorkerClient methods: 77 // These are called on the main WebKit thread. 78 virtual void postMessageToWorkerObject(const WebString&, const WebMessagePortChannelArray&); 79 virtual void postExceptionToWorkerObject(const WebString&, int, const WebString&); 80 81 // FIXME: the below is for compatibility only and should be 82 // removed once Chromium is updated to remove message 83 // destination parameter <http://webkit.org/b/37155>. 84 virtual void postConsoleMessageToWorkerObject(int, int, int, int, const WebString&, int, const WebString&); 85 virtual void postConsoleMessageToWorkerObject(int, int, int, const WebString&, int, const WebString&); 86 virtual void confirmMessageFromWorkerObject(bool); 87 virtual void reportPendingActivity(bool); 88 virtual void workerContextClosed(); 89 virtual void workerContextDestroyed(); createWorker(WebWorkerClient *)90 virtual WebWorker* createWorker(WebWorkerClient*) { return 0; } notificationPresenter()91 virtual WebNotificationPresenter* notificationPresenter() 92 { 93 // FIXME: Notifications not yet supported in workers. 94 return 0; 95 } createApplicationCacheHost(WebApplicationCacheHostClient *)96 virtual WebApplicationCacheHost* createApplicationCacheHost(WebApplicationCacheHostClient*) { return 0; } allowDatabase(WebFrame *,const WebString & name,const WebString & displayName,unsigned long estimatedSize)97 virtual bool allowDatabase(WebFrame*, const WebString& name, const WebString& displayName, unsigned long estimatedSize) 98 { 99 ASSERT_NOT_REACHED(); 100 return true; 101 } 102 103 private: 104 virtual ~WebWorkerClientImpl(); 105 106 // Methods used to support WebWorkerClientImpl being constructed on worker 107 // threads. 108 // These tasks are dispatched on the WebKit thread. 109 static void startWorkerContextTask(WebCore::ScriptExecutionContext* context, 110 WebWorkerClientImpl* thisPtr, 111 const WTF::String& scriptURL, 112 const WTF::String& userAgent, 113 const WTF::String& sourceCode); 114 static void terminateWorkerContextTask(WebCore::ScriptExecutionContext* context, 115 WebWorkerClientImpl* thisPtr); 116 static void postMessageToWorkerContextTask(WebCore::ScriptExecutionContext* context, 117 WebWorkerClientImpl* thisPtr, 118 const WTF::String& message, 119 PassOwnPtr<WebCore::MessagePortChannelArray> channels); 120 static void workerObjectDestroyedTask(WebCore::ScriptExecutionContext* context, 121 WebWorkerClientImpl* thisPtr); 122 123 // These tasks are dispatched on the thread that created the worker (i.e. 124 // main WebKit thread in renderer process, and the worker thread in the 125 // worker process). 126 static void postMessageToWorkerObjectTask(WebCore::ScriptExecutionContext* context, 127 WebWorkerClientImpl* thisPtr, 128 const WTF::String& message, 129 PassOwnPtr<WebCore::MessagePortChannelArray> channels); 130 static void postExceptionToWorkerObjectTask(WebCore::ScriptExecutionContext* context, 131 WebWorkerClientImpl* thisPtr, 132 const WTF::String& message, 133 int lineNumber, 134 const WTF::String& sourceURL); 135 static void postConsoleMessageToWorkerObjectTask(WebCore::ScriptExecutionContext* context, 136 WebWorkerClientImpl* thisPtr, 137 int sourceId, 138 int messageType, 139 int messageLevel, 140 const WTF::String& message, 141 int lineNumber, 142 const WTF::String& sourceURL); 143 static void confirmMessageFromWorkerObjectTask(WebCore::ScriptExecutionContext* context, 144 WebWorkerClientImpl* thisPtr); 145 static void reportPendingActivityTask(WebCore::ScriptExecutionContext* context, 146 WebWorkerClientImpl* thisPtr, 147 bool hasPendingActivity); 148 149 // Guard against context from being destroyed before a worker exits. 150 RefPtr<WebCore::ScriptExecutionContext> m_scriptExecutionContext; 151 152 WebCore::Worker* m_worker; 153 WebWorker* m_webWorker; 154 bool m_askedToTerminate; 155 unsigned m_unconfirmedMessageCount; 156 bool m_workerContextHadPendingActivity; 157 ThreadIdentifier m_workerThreadId; 158 }; 159 160 } // namespace WebKit; 161 162 #endif // ENABLE(WORKERS) 163 164 #endif 165