Home | History | Annotate | Download | only in workers
      1 /*
      2  * Copyright (C) 2008 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  *
     25  */
     26 
     27 #ifndef WorkerMessagingProxy_h
     28 #define WorkerMessagingProxy_h
     29 
     30 #if ENABLE(WORKERS)
     31 
     32 #include "ScriptExecutionContext.h"
     33 #include "WorkerContextProxy.h"
     34 #include "WorkerLoaderProxy.h"
     35 #include "WorkerObjectProxy.h"
     36 #include <wtf/Noncopyable.h>
     37 #include <wtf/PassOwnPtr.h>
     38 #include <wtf/PassRefPtr.h>
     39 #include <wtf/RefPtr.h>
     40 #include <wtf/Vector.h>
     41 
     42 namespace WebCore {
     43 
     44     class DedicatedWorkerThread;
     45     class ScriptExecutionContext;
     46     class String;
     47     class Worker;
     48 
     49     class WorkerMessagingProxy : public WorkerContextProxy, public WorkerObjectProxy, public WorkerLoaderProxy, public Noncopyable {
     50     public:
     51         WorkerMessagingProxy(Worker*);
     52 
     53         // Implementations of WorkerContextProxy.
     54         // (Only use these methods in the worker object thread.)
     55         virtual void startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode);
     56         virtual void terminateWorkerContext();
     57         virtual void postMessageToWorkerContext(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
     58         virtual bool hasPendingActivity() const;
     59         virtual void workerObjectDestroyed();
     60 
     61         // Implementations of WorkerObjectProxy.
     62         // (Only use these methods in the worker context thread.)
     63         virtual void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
     64         virtual void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, const String& sourceURL);
     65         virtual void postConsoleMessageToWorkerObject(MessageDestination, MessageSource, MessageType, MessageLevel, const String& message, int lineNumber, const String& sourceURL);
     66         virtual void confirmMessageFromWorkerObject(bool hasPendingActivity);
     67         virtual void reportPendingActivity(bool hasPendingActivity);
     68         virtual void workerContextClosed();
     69         virtual void workerContextDestroyed();
     70 
     71         // Implementation of WorkerLoaderProxy.
     72         // These methods are called on different threads to schedule loading
     73         // requests and to send callbacks back to WorkerContext.
     74         virtual void postTaskToLoader(PassOwnPtr<ScriptExecutionContext::Task>);
     75         virtual void postTaskForModeToWorkerContext(PassOwnPtr<ScriptExecutionContext::Task>, const String& mode);
     76 
     77         void workerThreadCreated(PassRefPtr<DedicatedWorkerThread>);
     78 
     79         // Only use this method on the worker object thread.
     80         bool askedToTerminate() const { return m_askedToTerminate; }
     81 
     82     private:
     83         friend class MessageWorkerTask;
     84         friend class WorkerContextDestroyedTask;
     85         friend class WorkerExceptionTask;
     86         friend class WorkerThreadActivityReportTask;
     87 
     88         virtual ~WorkerMessagingProxy();
     89 
     90         void workerContextDestroyedInternal();
     91         void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
     92         Worker* workerObject() const { return m_workerObject; }
     93 
     94         RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
     95         Worker* m_workerObject;
     96         RefPtr<DedicatedWorkerThread> m_workerThread;
     97 
     98         unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
     99         bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
    100 
    101         bool m_askedToTerminate;
    102 
    103         Vector<OwnPtr<ScriptExecutionContext::Task> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
    104     };
    105 
    106 } // namespace WebCore
    107 
    108 #endif // ENABLE(WORKERS)
    109 
    110 #endif // WorkerMessagingProxy_h
    111