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