Home | History | Annotate | Download | only in src
      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 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     void openFileSystemForWorker(WebFileSystem::Type, long long size, bool create, WebFileSystemCallbacks*, bool synchronous);
     96 #endif
     97 
     98     // Executes the given task on the main thread.
     99     static void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
    100 
    101 protected:
    102     virtual WebWorkerClient* client() = 0;
    103     virtual WebCommonWorkerClient* commonClient() = 0;
    104 
    105     void setWorkerThread(PassRefPtr<WebCore::WorkerThread> thread) { m_workerThread = thread; }
    106     WebCore::WorkerThread* workerThread() { return m_workerThread.get(); }
    107 
    108     // Shuts down the worker thread.
    109     void stopWorkerThread();
    110 
    111     // Creates the shadow loader used for worker network requests.
    112     void initializeLoader(const WebURL&);
    113 
    114 private:
    115     // Function used to invoke tasks on the main thread.
    116     static void invokeTaskMethod(void*);
    117 
    118     // Tasks that are run on the main thread.
    119     static void postMessageTask(
    120         WebCore::ScriptExecutionContext* context,
    121         WebWorkerBase* thisPtr,
    122         WTF::String message,
    123         PassOwnPtr<WebCore::MessagePortChannelArray> channels);
    124     static void postExceptionTask(
    125         WebCore::ScriptExecutionContext* context,
    126         WebWorkerBase* thisPtr,
    127         const WTF::String& message,
    128         int lineNumber,
    129         const WTF::String& sourceURL);
    130     static void postConsoleMessageTask(
    131         WebCore::ScriptExecutionContext* context,
    132         WebWorkerBase* thisPtr,
    133         int source,
    134         int type,
    135         int level,
    136         const WTF::String& message,
    137         int lineNumber,
    138         const WTF::String& sourceURL);
    139     static void confirmMessageTask(
    140         WebCore::ScriptExecutionContext* context,
    141         WebWorkerBase* thisPtr,
    142         bool hasPendingActivity);
    143     static void reportPendingActivityTask(
    144         WebCore::ScriptExecutionContext* context,
    145         WebWorkerBase* thisPtr,
    146         bool hasPendingActivity);
    147     static void workerContextClosedTask(
    148         WebCore::ScriptExecutionContext* context,
    149         WebWorkerBase* thisPtr);
    150     static void workerContextDestroyedTask(
    151         WebCore::ScriptExecutionContext* context,
    152         WebWorkerBase* thisPtr);
    153 
    154     // 'shadow page' - created to proxy loading requests from the worker.
    155     RefPtr<WebCore::ScriptExecutionContext> m_loadingDocument;
    156     WebView* m_webView;
    157     bool m_askedToTerminate;
    158 
    159     RefPtr<WebCore::WorkerThread> m_workerThread;
    160 };
    161 
    162 } // namespace WebKit
    163 
    164 #endif // ENABLE(WORKERS)
    165 
    166 #endif
    167