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/Forward.h>
37#include <wtf/Noncopyable.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/PassRefPtr.h>
40#include <wtf/RefPtr.h>
41#include <wtf/Vector.h>
42
43namespace WebCore {
44
45    class DedicatedWorkerThread;
46    class ScriptExecutionContext;
47    class Worker;
48
49    class WorkerMessagingProxy : public WorkerContextProxy, public WorkerObjectProxy, public WorkerLoaderProxy {
50        WTF_MAKE_NONCOPYABLE(WorkerMessagingProxy); WTF_MAKE_FAST_ALLOCATED;
51    public:
52        explicit WorkerMessagingProxy(Worker*);
53
54        // Implementations of WorkerContextProxy.
55        // (Only use these methods in the worker object thread.)
56        virtual void startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode) OVERRIDE;
57        virtual void terminateWorkerContext() OVERRIDE;
58        virtual void postMessageToWorkerContext(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>) OVERRIDE;
59        virtual bool hasPendingActivity() const OVERRIDE;
60        virtual void workerObjectDestroyed() OVERRIDE;
61#if ENABLE(INSPECTOR)
62        virtual void connectToInspector(WorkerContextProxy::PageInspector*) OVERRIDE;
63        virtual void disconnectFromInspector() OVERRIDE;
64        virtual void sendMessageToInspector(const String&) OVERRIDE;
65#endif
66
67        // Implementations of WorkerObjectProxy.
68        // (Only use these methods in the worker context thread.)
69        virtual void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>) OVERRIDE;
70        virtual void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL) OVERRIDE;
71        virtual void postConsoleMessageToWorkerObject(MessageSource, MessageLevel, const String& message, int lineNumber, int columnNumber, const String& sourceURL) OVERRIDE;
72#if ENABLE(INSPECTOR)
73        virtual void postMessageToPageInspector(const String&) OVERRIDE;
74        virtual void updateInspectorStateCookie(const String&) OVERRIDE;
75#endif
76        virtual void confirmMessageFromWorkerObject(bool hasPendingActivity) OVERRIDE;
77        virtual void reportPendingActivity(bool hasPendingActivity) OVERRIDE;
78        virtual void workerContextClosed() OVERRIDE;
79        virtual void workerContextDestroyed() OVERRIDE;
80
81        // Implementation of WorkerLoaderProxy.
82        // These methods are called on different threads to schedule loading
83        // requests and to send callbacks back to WorkerContext.
84        virtual void postTaskToLoader(PassOwnPtr<ScriptExecutionContext::Task>) OVERRIDE;
85        virtual bool postTaskForModeToWorkerContext(PassOwnPtr<ScriptExecutionContext::Task>, const String& mode) OVERRIDE;
86
87        void workerThreadCreated(PassRefPtr<DedicatedWorkerThread>);
88
89        // Only use this method on the worker object thread.
90        bool askedToTerminate() const { return m_askedToTerminate; }
91
92    protected:
93        virtual ~WorkerMessagingProxy();
94
95    private:
96        friend class MessageWorkerTask;
97        friend class PostMessageToPageInspectorTask;
98        friend class WorkerContextDestroyedTask;
99        friend class WorkerExceptionTask;
100        friend class WorkerThreadActivityReportTask;
101
102        void workerContextDestroyedInternal();
103        static void workerObjectDestroyedInternal(ScriptExecutionContext*, WorkerMessagingProxy*);
104        void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
105        Worker* workerObject() const { return m_workerObject; }
106
107        RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
108        Worker* m_workerObject;
109        bool m_mayBeDestroyed;
110        RefPtr<DedicatedWorkerThread> m_workerThread;
111
112        unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
113        bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
114
115        bool m_askedToTerminate;
116
117        Vector<OwnPtr<ScriptExecutionContext::Task> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
118#if ENABLE(INSPECTOR)
119        WorkerContextProxy::PageInspector* m_pageInspector;
120#endif
121    };
122
123} // namespace WebCore
124
125#endif // ENABLE(WORKERS)
126
127#endif // WorkerMessagingProxy_h
128