|
1 /* |
|
2 * Copyright (C) 2010 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 WebWorkerBase_h |
|
32 #define WebWorkerBase_h |
|
33 |
|
34 #if ENABLE(WORKERS) |
|
35 |
|
36 #include "ScriptExecutionContext.h" |
|
37 #include "WebFrameClient.h" |
|
38 #include "WorkerLoaderProxy.h" |
|
39 #include "WorkerObjectProxy.h" |
|
40 #include <wtf/PassOwnPtr.h> |
|
41 #include <wtf/RefPtr.h> |
|
42 |
|
43 namespace WebCore { |
|
44 class WorkerThread; |
|
45 } |
|
46 |
|
47 namespace WebKit { |
|
48 class WebApplicationCacheHost; |
|
49 class WebApplicationCacheHostClient; |
|
50 class WebCommonWorkerClient; |
|
51 class WebSecurityOrigin; |
|
52 class WebString; |
|
53 class WebURL; |
|
54 class WebView; |
|
55 class WebWorker; |
|
56 class WebWorkerClient; |
|
57 |
|
58 // Base class for WebSharedWorkerImpl and WebWorkerImpl. It contains common |
|
59 // code used by both implementation classes, including implementations of the |
|
60 // WorkerObjectProxy and WorkerLoaderProxy interfaces. |
|
61 class WebWorkerBase : public WebCore::WorkerObjectProxy |
|
62 , public WebCore::WorkerLoaderProxy |
|
63 , public WebFrameClient { |
|
64 public: |
|
65 WebWorkerBase(); |
|
66 virtual ~WebWorkerBase(); |
|
67 |
|
68 // WebCore::WorkerObjectProxy methods: |
|
69 virtual void postMessageToWorkerObject( |
|
70 PassRefPtr<WebCore::SerializedScriptValue>, |
|
71 PassOwnPtr<WebCore::MessagePortChannelArray>); |
|
72 virtual void postExceptionToWorkerObject( |
|
73 const WebCore::String&, int, const WebCore::String&); |
|
74 virtual void postConsoleMessageToWorkerObject( |
|
75 WebCore::MessageSource, WebCore::MessageType, |
|
76 WebCore::MessageLevel, const WebCore::String&, int, const WebCore::String&); |
|
77 virtual void confirmMessageFromWorkerObject(bool); |
|
78 virtual void reportPendingActivity(bool); |
|
79 virtual void workerContextClosed(); |
|
80 virtual void workerContextDestroyed(); |
|
81 |
|
82 // WebCore::WorkerLoaderProxy methods: |
|
83 virtual void postTaskToLoader(PassOwnPtr<WebCore::ScriptExecutionContext::Task>); |
|
84 virtual void postTaskForModeToWorkerContext( |
|
85 PassOwnPtr<WebCore::ScriptExecutionContext::Task>, const WebCore::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 // Controls whether access to Web Databases is allowed for this worker. |
|
92 virtual bool allowDatabase(WebFrame*, const WebString& name, const WebString& displayName, unsigned long estimatedSize); |
|
93 |
|
94 // Executes the given task on the main thread. |
|
95 static void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>); |
|
96 |
|
97 protected: |
|
98 virtual WebWorkerClient* client() = 0; |
|
99 virtual WebCommonWorkerClient* commonClient() = 0; |
|
100 |
|
101 void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; } |
|
102 WebCore::WorkerThread* workerThread() { return m_workerThread.get(); } |
|
103 |
|
104 // Shuts down the worker thread. |
|
105 void stopWorkerThread(); |
|
106 |
|
107 // Creates the shadow loader used for worker network requests. |
|
108 void initializeLoader(const WebURL&); |
|
109 |
|
110 private: |
|
111 // Function used to invoke tasks on the main thread. |
|
112 static void invokeTaskMethod(void*); |
|
113 |
|
114 // Tasks that are run on the main thread. |
|
115 static void postMessageTask( |
|
116 WebCore::ScriptExecutionContext* context, |
|
117 WebWorkerBase* thisPtr, |
|
118 WebCore::String message, |
|
119 PassOwnPtr<WebCore::MessagePortChannelArray> channels); |
|
120 static void postExceptionTask( |
|
121 WebCore::ScriptExecutionContext* context, |
|
122 WebWorkerBase* thisPtr, |
|
123 const WebCore::String& message, |
|
124 int lineNumber, |
|
125 const WebCore::String& sourceURL); |
|
126 static void postConsoleMessageTask( |
|
127 WebCore::ScriptExecutionContext* context, |
|
128 WebWorkerBase* thisPtr, |
|
129 int source, |
|
130 int type, |
|
131 int level, |
|
132 const WebCore::String& message, |
|
133 int lineNumber, |
|
134 const WebCore::String& sourceURL); |
|
135 static void confirmMessageTask( |
|
136 WebCore::ScriptExecutionContext* context, |
|
137 WebWorkerBase* thisPtr, |
|
138 bool hasPendingActivity); |
|
139 static void reportPendingActivityTask( |
|
140 WebCore::ScriptExecutionContext* context, |
|
141 WebWorkerBase* thisPtr, |
|
142 bool hasPendingActivity); |
|
143 static void workerContextClosedTask( |
|
144 WebCore::ScriptExecutionContext* context, |
|
145 WebWorkerBase* thisPtr); |
|
146 static void workerContextDestroyedTask( |
|
147 WebCore::ScriptExecutionContext* context, |
|
148 WebWorkerBase* thisPtr); |
|
149 |
|
150 // 'shadow page' - created to proxy loading requests from the worker. |
|
151 RefPtr<WebCore::ScriptExecutionContext> m_loadingDocument; |
|
152 WebView* m_webView; |
|
153 bool m_askedToTerminate; |
|
154 |
|
155 RefPtr<WebCore::WorkerThread> m_workerThread; |
|
156 }; |
|
157 |
|
158 } // namespace WebKit |
|
159 |
|
160 #endif // ENABLE(WORKERS) |
|
161 |
|
162 #endif |