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