WebWorkerBase.h revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
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
43namespace WebCore {
44class WorkerThread;
45}
46
47namespace WebKit {
48class WebApplicationCacheHost;
49class WebApplicationCacheHostClient;
50class WebCommonWorkerClient;
51class WebSecurityOrigin;
52class WebString;
53class WebURL;
54class WebView;
55class WebWorker;
56class 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.
61class WebWorkerBase : public WebCore::WorkerObjectProxy
62                    , public WebCore::WorkerLoaderProxy
63                    , public WebFrameClient {
64public:
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 WTF::String&, int, const WTF::String&);
74    virtual void postConsoleMessageToWorkerObject(
75        WebCore::MessageSource, WebCore::MessageType,
76        WebCore::MessageLevel, const WTF::String&, int, const WTF::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 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    // 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#if ENABLE(FILE_SYSTEM)
95    // Requests to open a file system for this worker. (Note that this is not the implementation for WebFrameClient::openFileSystem.)
96    void openFileSystem(WebFileSystem::Type, long long size, WebFileSystemCallbacks*, bool synchronous);
97#endif
98
99    // Executes the given task on the main thread.
100    static void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
101
102protected:
103    virtual WebWorkerClient* client() = 0;
104    virtual WebCommonWorkerClient* commonClient() = 0;
105
106    void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; }
107    WebCore::WorkerThread* workerThread() { return m_workerThread.get(); }
108
109    // Shuts down the worker thread.
110    void stopWorkerThread();
111
112    // Creates the shadow loader used for worker network requests.
113    void initializeLoader(const WebURL&);
114
115private:
116    // Function used to invoke tasks on the main thread.
117    static void invokeTaskMethod(void*);
118
119    // Tasks that are run on the main thread.
120    static void postMessageTask(
121        WebCore::ScriptExecutionContext* context,
122        WebWorkerBase* thisPtr,
123        WTF::String message,
124        PassOwnPtr<WebCore::MessagePortChannelArray> channels);
125    static void postExceptionTask(
126        WebCore::ScriptExecutionContext* context,
127        WebWorkerBase* thisPtr,
128        const WTF::String& message,
129        int lineNumber,
130        const WTF::String& sourceURL);
131    static void postConsoleMessageTask(
132        WebCore::ScriptExecutionContext* context,
133        WebWorkerBase* thisPtr,
134        int source,
135        int type,
136        int level,
137        const WTF::String& message,
138        int lineNumber,
139        const WTF::String& sourceURL);
140    static void confirmMessageTask(
141        WebCore::ScriptExecutionContext* context,
142        WebWorkerBase* thisPtr,
143        bool hasPendingActivity);
144    static void reportPendingActivityTask(
145        WebCore::ScriptExecutionContext* context,
146        WebWorkerBase* thisPtr,
147        bool hasPendingActivity);
148    static void workerContextClosedTask(
149        WebCore::ScriptExecutionContext* context,
150        WebWorkerBase* thisPtr);
151    static void workerContextDestroyedTask(
152        WebCore::ScriptExecutionContext* context,
153        WebWorkerBase* thisPtr);
154
155    // 'shadow page' - created to proxy loading requests from the worker.
156    RefPtr<WebCore::ScriptExecutionContext> m_loadingDocument;
157    WebView* m_webView;
158    bool m_askedToTerminate;
159
160    RefPtr<WebCore::WorkerThread> m_workerThread;
161};
162
163} // namespace WebKit
164
165#endif // ENABLE(WORKERS)
166
167#endif
168