Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: third_party/WebKit/Source/core/workers/InProcessWorkerObjectProxy.cpp

Issue 2716853002: (WIP) Worker: Merge ParentFrameTaskRunners into TaskRunnerHelper
Patch Set: WIP Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/workers/InProcessWorkerObjectProxy.h" 31 #include "core/workers/InProcessWorkerObjectProxy.h"
32 32
33 #include <memory>
33 #include "bindings/core/v8/SerializedScriptValue.h" 34 #include "bindings/core/v8/SerializedScriptValue.h"
34 #include "bindings/core/v8/SourceLocation.h" 35 #include "bindings/core/v8/SourceLocation.h"
35 #include "bindings/core/v8/V8GCController.h" 36 #include "bindings/core/v8/V8GCController.h"
36 #include "core/dom/Document.h" 37 #include "core/dom/Document.h"
37 #include "core/dom/ExecutionContext.h" 38 #include "core/dom/ExecutionContext.h"
39 #include "core/dom/TaskRunnerHelper.h"
38 #include "core/events/MessageEvent.h" 40 #include "core/events/MessageEvent.h"
39 #include "core/inspector/ConsoleMessage.h" 41 #include "core/inspector/ConsoleMessage.h"
40 #include "core/workers/InProcessWorkerMessagingProxy.h" 42 #include "core/workers/InProcessWorkerMessagingProxy.h"
41 #include "core/workers/ParentFrameTaskRunners.h"
42 #include "core/workers/WorkerGlobalScope.h" 43 #include "core/workers/WorkerGlobalScope.h"
43 #include "core/workers/WorkerThread.h" 44 #include "core/workers/WorkerThread.h"
44 #include "platform/CrossThreadFunctional.h" 45 #include "platform/CrossThreadFunctional.h"
45 #include "platform/WebTaskRunner.h" 46 #include "platform/WebTaskRunner.h"
46 #include "public/platform/Platform.h" 47 #include "public/platform/Platform.h"
47 #include "wtf/Functional.h" 48 #include "wtf/Functional.h"
48 #include "wtf/PtrUtil.h" 49 #include "wtf/PtrUtil.h"
49 #include <memory>
50 50
51 namespace blink { 51 namespace blink {
52 52
53 const double kDefaultIntervalInSec = 1; 53 const double kDefaultIntervalInSec = 1;
54 const double kMaxIntervalInSec = 30; 54 const double kMaxIntervalInSec = 30;
55 55
56 std::unique_ptr<InProcessWorkerObjectProxy> InProcessWorkerObjectProxy::create( 56 std::unique_ptr<InProcessWorkerObjectProxy> InProcessWorkerObjectProxy::create(
57 const WeakPtr<InProcessWorkerMessagingProxy>& messagingProxyWeakPtr, 57 const WeakPtr<InProcessWorkerMessagingProxy>& messagingProxyWeakPtr) {
58 ParentFrameTaskRunners* parentFrameTaskRunners) {
59 DCHECK(messagingProxyWeakPtr); 58 DCHECK(messagingProxyWeakPtr);
60 return WTF::wrapUnique(new InProcessWorkerObjectProxy( 59 return WTF::wrapUnique(new InProcessWorkerObjectProxy(messagingProxyWeakPtr));
61 messagingProxyWeakPtr, parentFrameTaskRunners));
62 } 60 }
63 61
64 InProcessWorkerObjectProxy::~InProcessWorkerObjectProxy() {} 62 InProcessWorkerObjectProxy::~InProcessWorkerObjectProxy() {}
65 63
66 void InProcessWorkerObjectProxy::postMessageToWorkerObject( 64 void InProcessWorkerObjectProxy::postMessageToWorkerObject(
67 PassRefPtr<SerializedScriptValue> message, 65 PassRefPtr<SerializedScriptValue> message,
68 MessagePortChannelArray channels) { 66 MessagePortChannelArray channels) {
69 getParentFrameTaskRunners() 67 FrameTaskRunnerHelper::get(TaskType::PostedMessage, m_workerGlobalScope.get())
70 ->get(TaskType::PostedMessage)
71 ->postTask(BLINK_FROM_HERE, 68 ->postTask(BLINK_FROM_HERE,
72 crossThreadBind( 69 crossThreadBind(
73 &InProcessWorkerMessagingProxy::postMessageToWorkerObject, 70 &InProcessWorkerMessagingProxy::postMessageToWorkerObject,
74 m_messagingProxyWeakPtr, std::move(message), 71 m_messagingProxyWeakPtr, std::move(message),
75 WTF::passed(std::move(channels)))); 72 WTF::passed(std::move(channels))));
76 } 73 }
77 74
78 void InProcessWorkerObjectProxy::processMessageFromWorkerObject( 75 void InProcessWorkerObjectProxy::processMessageFromWorkerObject(
79 PassRefPtr<SerializedScriptValue> message, 76 PassRefPtr<SerializedScriptValue> message,
80 MessagePortChannelArray channels, 77 MessagePortChannelArray channels,
81 WorkerThread* workerThread) { 78 WorkerThread* workerThread) {
82 WorkerGlobalScope* globalScope = 79 WorkerGlobalScope* globalScope =
83 toWorkerGlobalScope(workerThread->globalScope()); 80 toWorkerGlobalScope(workerThread->globalScope());
84 MessagePortArray* ports = 81 MessagePortArray* ports =
85 MessagePort::entanglePorts(*globalScope, std::move(channels)); 82 MessagePort::entanglePorts(*globalScope, std::move(channels));
86 globalScope->dispatchEvent(MessageEvent::create(ports, std::move(message))); 83 globalScope->dispatchEvent(MessageEvent::create(ports, std::move(message)));
87 84
88 getParentFrameTaskRunners() 85 FrameTaskRunnerHelper::get(TaskType::UnspecedTimer, globalScope)
89 ->get(TaskType::UnspecedTimer)
90 ->postTask( 86 ->postTask(
91 BLINK_FROM_HERE, 87 BLINK_FROM_HERE,
92 crossThreadBind( 88 crossThreadBind(
93 &InProcessWorkerMessagingProxy::confirmMessageFromWorkerObject, 89 &InProcessWorkerMessagingProxy::confirmMessageFromWorkerObject,
94 m_messagingProxyWeakPtr)); 90 m_messagingProxyWeakPtr));
95 91
96 startPendingActivityTimer(); 92 startPendingActivityTimer();
97 } 93 }
98 94
99 void InProcessWorkerObjectProxy::processUnhandledException( 95 void InProcessWorkerObjectProxy::processUnhandledException(
100 int exceptionId, 96 int exceptionId,
101 WorkerThread* workerThread) { 97 WorkerThread* workerThread) {
102 WorkerGlobalScope* globalScope = 98 WorkerGlobalScope* globalScope =
103 toWorkerGlobalScope(workerThread->globalScope()); 99 toWorkerGlobalScope(workerThread->globalScope());
104 globalScope->exceptionUnhandled(exceptionId); 100 globalScope->exceptionUnhandled(exceptionId);
105 } 101 }
106 102
107 void InProcessWorkerObjectProxy::reportException( 103 void InProcessWorkerObjectProxy::reportException(
108 const String& errorMessage, 104 const String& errorMessage,
109 std::unique_ptr<SourceLocation> location, 105 std::unique_ptr<SourceLocation> location,
110 int exceptionId) { 106 int exceptionId) {
111 getParentFrameTaskRunners() 107 FrameTaskRunnerHelper::get(TaskType::UnspecedTimer, m_workerGlobalScope.get())
112 ->get(TaskType::UnspecedTimer)
113 ->postTask( 108 ->postTask(
114 BLINK_FROM_HERE, 109 BLINK_FROM_HERE,
115 crossThreadBind(&InProcessWorkerMessagingProxy::dispatchErrorEvent, 110 crossThreadBind(&InProcessWorkerMessagingProxy::dispatchErrorEvent,
116 m_messagingProxyWeakPtr, errorMessage, 111 m_messagingProxyWeakPtr, errorMessage,
117 WTF::passed(location->clone()), exceptionId)); 112 WTF::passed(location->clone()), exceptionId));
118 } 113 }
119 114
120 void InProcessWorkerObjectProxy::didCreateWorkerGlobalScope( 115 void InProcessWorkerObjectProxy::didCreateWorkerGlobalScope(
121 WorkerOrWorkletGlobalScope* globalScope) { 116 WorkerOrWorkletGlobalScope* globalScope) {
122 DCHECK(!m_workerGlobalScope); 117 DCHECK(!m_workerGlobalScope);
118 ThreadedObjectProxyBase::didCreateWorkerGlobalScope(globalScope);
123 m_workerGlobalScope = toWorkerGlobalScope(globalScope); 119 m_workerGlobalScope = toWorkerGlobalScope(globalScope);
124 m_timer = WTF::makeUnique<TaskRunnerTimer<InProcessWorkerObjectProxy>>( 120 m_timer = WTF::makeUnique<TaskRunnerTimer<InProcessWorkerObjectProxy>>(
125 Platform::current()->currentThread()->getWebTaskRunner(), this, 121 Platform::current()->currentThread()->getWebTaskRunner(), this,
126 &InProcessWorkerObjectProxy::checkPendingActivity); 122 &InProcessWorkerObjectProxy::checkPendingActivity);
127 } 123 }
128 124
129 void InProcessWorkerObjectProxy::didEvaluateWorkerScript(bool) { 125 void InProcessWorkerObjectProxy::didEvaluateWorkerScript(bool) {
130 startPendingActivityTimer(); 126 startPendingActivityTimer();
131 } 127 }
132 128
133 void InProcessWorkerObjectProxy::willDestroyWorkerGlobalScope() { 129 void InProcessWorkerObjectProxy::willDestroyWorkerGlobalScope() {
134 m_timer.reset(); 130 m_timer.reset();
135 m_workerGlobalScope = nullptr; 131 m_workerGlobalScope = nullptr;
136 } 132 }
137 133
138 InProcessWorkerObjectProxy::InProcessWorkerObjectProxy( 134 InProcessWorkerObjectProxy::InProcessWorkerObjectProxy(
139 const WeakPtr<InProcessWorkerMessagingProxy>& messagingProxyWeakPtr, 135 const WeakPtr<InProcessWorkerMessagingProxy>& messagingProxyWeakPtr)
140 ParentFrameTaskRunners* parentFrameTaskRunners) 136 : m_messagingProxyWeakPtr(messagingProxyWeakPtr),
141 : ThreadedObjectProxyBase(parentFrameTaskRunners),
142 m_messagingProxyWeakPtr(messagingProxyWeakPtr),
143 m_defaultIntervalInSec(kDefaultIntervalInSec), 137 m_defaultIntervalInSec(kDefaultIntervalInSec),
144 m_nextIntervalInSec(kDefaultIntervalInSec), 138 m_nextIntervalInSec(kDefaultIntervalInSec),
145 m_maxIntervalInSec(kMaxIntervalInSec) {} 139 m_maxIntervalInSec(kMaxIntervalInSec) {}
146 140
147 void InProcessWorkerObjectProxy::startPendingActivityTimer() { 141 void InProcessWorkerObjectProxy::startPendingActivityTimer() {
148 if (m_timer->isActive()) { 142 if (m_timer->isActive()) {
149 // Reset the next interval duration to check new activity state timely. 143 // Reset the next interval duration to check new activity state timely.
150 // For example, a long-running activity can be cancelled by a message 144 // For example, a long-running activity can be cancelled by a message
151 // event. 145 // event.
152 m_nextIntervalInSec = kDefaultIntervalInSec; 146 m_nextIntervalInSec = kDefaultIntervalInSec;
153 return; 147 return;
154 } 148 }
155 m_timer->startOneShot(m_nextIntervalInSec, BLINK_FROM_HERE); 149 m_timer->startOneShot(m_nextIntervalInSec, BLINK_FROM_HERE);
156 m_nextIntervalInSec = std::min(m_nextIntervalInSec * 1.5, m_maxIntervalInSec); 150 m_nextIntervalInSec = std::min(m_nextIntervalInSec * 1.5, m_maxIntervalInSec);
157 } 151 }
158 152
159 void InProcessWorkerObjectProxy::checkPendingActivity(TimerBase*) { 153 void InProcessWorkerObjectProxy::checkPendingActivity(TimerBase*) {
160 bool hasPendingActivity = V8GCController::hasPendingActivity( 154 bool hasPendingActivity = V8GCController::hasPendingActivity(
161 m_workerGlobalScope->thread()->isolate(), m_workerGlobalScope); 155 m_workerGlobalScope->thread()->isolate(), m_workerGlobalScope);
162 if (!hasPendingActivity) { 156 if (!hasPendingActivity) {
163 // Report all activities are done. 157 // Report all activities are done.
164 getParentFrameTaskRunners() 158 FrameTaskRunnerHelper::get(TaskType::UnspecedTimer,
165 ->get(TaskType::UnspecedTimer) 159 m_workerGlobalScope.get())
166 ->postTask(BLINK_FROM_HERE, 160 ->postTask(BLINK_FROM_HERE,
167 crossThreadBind( 161 crossThreadBind(
168 &InProcessWorkerMessagingProxy::pendingActivityFinished, 162 &InProcessWorkerMessagingProxy::pendingActivityFinished,
169 m_messagingProxyWeakPtr)); 163 m_messagingProxyWeakPtr));
170 164
171 // Don't schedule a timer. It will be started again when a message event 165 // Don't schedule a timer. It will be started again when a message event
172 // is dispatched. 166 // is dispatched.
173 m_nextIntervalInSec = m_defaultIntervalInSec; 167 m_nextIntervalInSec = m_defaultIntervalInSec;
174 return; 168 return;
175 } 169 }
176 170
177 // There is still a pending activity. Check it later. 171 // There is still a pending activity. Check it later.
178 startPendingActivityTimer(); 172 startPendingActivityTimer();
179 } 173 }
180 174
181 WeakPtr<ThreadedMessagingProxyBase> 175 WeakPtr<ThreadedMessagingProxyBase>
182 InProcessWorkerObjectProxy::messagingProxyWeakPtr() { 176 InProcessWorkerObjectProxy::messagingProxyWeakPtr() {
183 return m_messagingProxyWeakPtr; 177 return m_messagingProxyWeakPtr;
184 } 178 }
185 179
186 } // namespace blink 180 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698