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