1/*
2 * Copyright (C) 2008-2017 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#pragma once
27
28#include "WorkerGlobalScopeProxy.h"
29#include "WorkerDebuggerProxy.h"
30#include "WorkerLoaderProxy.h"
31#include "WorkerObjectProxy.h"
32#include <wtf/MonotonicTime.h>
33#include <wtf/ThreadSafeRefCounted.h>
34
35namespace WebCore {
36
37class DedicatedWorkerThread;
38class WorkerInspectorProxy;
39
40class WorkerMessagingProxy final : public ThreadSafeRefCounted<WorkerMessagingProxy>, public WorkerGlobalScopeProxy, public WorkerObjectProxy, public WorkerLoaderProxy, public WorkerDebuggerProxy {
41 WTF_MAKE_FAST_ALLOCATED;
42public:
43 explicit WorkerMessagingProxy(Worker&);
44 virtual ~WorkerMessagingProxy();
45
46private:
47 // Implementations of WorkerGlobalScopeProxy.
48 // (Only use these functions in the worker object thread.)
49 void startWorkerGlobalScope(const URL& scriptURL, const String& name, const String& userAgent, bool isOnline, const String& sourceCode, const ContentSecurityPolicyResponseHeaders&, bool shouldBypassMainWorldContentSecurityPolicy, MonotonicTime timeOrigin, JSC::RuntimeFlags, PAL::SessionID) final;
50 void terminateWorkerGlobalScope() final;
51 void postMessageToWorkerGlobalScope(MessageWithMessagePorts&&) final;
52 bool hasPendingActivity() const final;
53 void workerObjectDestroyed() final;
54 void notifyNetworkStateChange(bool isOnline) final;
55
56 // Implementation of WorkerObjectProxy.
57 // (Only use these functions in the worker context thread.)
58 void postMessageToWorkerObject(MessageWithMessagePorts&&) final;
59 void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL) final;
60 void confirmMessageFromWorkerObject(bool hasPendingActivity) final;
61 void reportPendingActivity(bool hasPendingActivity) final;
62 void workerGlobalScopeClosed() final;
63 void workerGlobalScopeDestroyed() final;
64
65 // Implementation of WorkerDebuggerProxy.
66 // (Only use these functions in the worker context thread.)
67 void postMessageToDebugger(const String&) final;
68 void setResourceCachingDisabled(bool) final;
69
70 // Implementation of WorkerLoaderProxy.
71 // These functions are called on different threads to schedule loading
72 // requests and to send callbacks back to WorkerGlobalScope.
73 void postTaskToLoader(ScriptExecutionContext::Task&&) final;
74 bool postTaskForModeToWorkerGlobalScope(ScriptExecutionContext::Task&&, const String& mode) final;
75 Ref<CacheStorageConnection> createCacheStorageConnection() final;
76
77 void workerThreadCreated(DedicatedWorkerThread&);
78
79 // Only use this method on the worker object thread.
80 bool askedToTerminate() const { return m_askedToTerminate; }
81
82 void workerGlobalScopeDestroyedInternal();
83 void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
84 Worker* workerObject() const { return m_workerObject; }
85
86 RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
87 std::unique_ptr<WorkerInspectorProxy> m_inspectorProxy;
88 Worker* m_workerObject;
89 bool m_mayBeDestroyed { false };
90 RefPtr<DedicatedWorkerThread> m_workerThread;
91
92 unsigned m_unconfirmedMessageCount { 0 }; // Unconfirmed messages from worker object to worker thread.
93 bool m_workerThreadHadPendingActivity { false }; // The latest confirmation from worker thread reported that it was still active.
94
95 bool m_askedToTerminate { false };
96
97 Vector<std::unique_ptr<ScriptExecutionContext::Task>> m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
98};
99
100} // namespace WebCore
101