Chromium Code Reviews| Index: third_party/WebKit/Source/core/workers/ThreadedMessagingProxyBase.cpp |
| diff --git a/third_party/WebKit/Source/core/workers/InProcessWorkerMessagingProxy.cpp b/third_party/WebKit/Source/core/workers/ThreadedMessagingProxyBase.cpp |
| similarity index 37% |
| copy from third_party/WebKit/Source/core/workers/InProcessWorkerMessagingProxy.cpp |
| copy to third_party/WebKit/Source/core/workers/ThreadedMessagingProxyBase.cpp |
| index ecfa490533d65703cc607645be3c90fb569a516c..6c47d0c2d511c5bbd9adf5e5ab3b13278e35f0a1 100644 |
| --- a/third_party/WebKit/Source/core/workers/InProcessWorkerMessagingProxy.cpp |
| +++ b/third_party/WebKit/Source/core/workers/ThreadedMessagingProxyBase.cpp |
| @@ -1,143 +1,67 @@ |
| -/* |
| - * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| - * Copyright (C) 2009 Google Inc. All Rights Reserved. |
| - * |
| - * Redistribution and use in source and binary forms, with or without |
| - * modification, are permitted provided that the following conditions |
| - * are met: |
| - * 1. Redistributions of source code must retain the above copyright |
| - * notice, this list of conditions and the following disclaimer. |
| - * 2. Redistributions in binary form must reproduce the above copyright |
| - * notice, this list of conditions and the following disclaimer in the |
| - * documentation and/or other materials provided with the distribution. |
| - * |
| - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| - * |
| - */ |
| - |
| -#include "core/workers/InProcessWorkerMessagingProxy.h" |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "core/workers/ThreadedMessagingProxyBase.h" |
| + |
| +#include "bindings/core/v8/SourceLocation.h" |
| #include "core/dom/Document.h" |
| -#include "core/dom/ExecutionContextTask.h" |
| -#include "core/dom/SecurityContext.h" |
| -#include "core/events/ErrorEvent.h" |
| -#include "core/events/MessageEvent.h" |
| -#include "core/frame/FrameConsole.h" |
| -#include "core/frame/LocalFrame.h" |
| -#include "core/frame/csp/ContentSecurityPolicy.h" |
| -#include "core/inspector/ConsoleMessage.h" |
| -#include "core/loader/DocumentLoadTiming.h" |
| #include "core/loader/DocumentLoader.h" |
| -#include "core/origin_trials/OriginTrialContext.h" |
| -#include "core/workers/InProcessWorkerBase.h" |
| -#include "core/workers/InProcessWorkerObjectProxy.h" |
| #include "core/workers/ParentFrameTaskRunners.h" |
| -#include "core/workers/WorkerClients.h" |
| -#include "core/workers/WorkerGlobalScope.h" |
| #include "core/workers/WorkerInspectorProxy.h" |
| #include "core/workers/WorkerThreadStartupData.h" |
| -#include "public/platform/WebTaskRunner.h" |
| -#include "wtf/WTF.h" |
| -#include <memory> |
| +#include "wtf/CurrentTime.h" |
| namespace blink { |
| namespace { |
| -void processUnhandledExceptionOnWorkerGlobalScope(int exceptionId, ExecutionContext* scriptContext) |
| -{ |
| - WorkerGlobalScope* globalScope = toWorkerGlobalScope(scriptContext); |
| - globalScope->exceptionUnhandled(exceptionId); |
| -} |
| - |
| -void processMessageOnWorkerGlobalScope(PassRefPtr<SerializedScriptValue> message, std::unique_ptr<MessagePortChannelArray> channels, InProcessWorkerObjectProxy* workerObjectProxy, ExecutionContext* scriptContext) |
| -{ |
| - WorkerGlobalScope* globalScope = toWorkerGlobalScope(scriptContext); |
| - MessagePortArray* ports = MessagePort::entanglePorts(*scriptContext, std::move(channels)); |
| - globalScope->dispatchEvent(MessageEvent::create(ports, message)); |
| - workerObjectProxy->confirmMessageFromWorkerObject(); |
| - workerObjectProxy->startPendingActivityTimer(); |
| -} |
| - |
| static int s_liveMessagingProxyCount = 0; |
| } // namespace |
| -InProcessWorkerMessagingProxy::InProcessWorkerMessagingProxy(InProcessWorkerBase* workerObject, WorkerClients* workerClients) |
| - : InProcessWorkerMessagingProxy(workerObject->getExecutionContext(), workerObject, workerClients) |
| +ThreadedMessagingProxyBase::ThreadedMessagingProxyBase(ExecutionContext* executionContext) |
| + : m_executionContext(executionContext) |
| + , m_mayBeDestroyed(false) |
| + , m_unconfirmedMessageCount(0) |
| + , m_workerGlobalScopeMayHavePendingActivity(false) |
| + , m_askedToTerminate(false) |
| + , m_workerInspectorProxy(WorkerInspectorProxy::create()) |
| + , m_parentFrameTaskRunners(ParentFrameTaskRunners::create(toDocument(m_executionContext.get())->frame())) |
| { |
| - DCHECK(m_workerObject); |
| + DCHECK(isParentContextThread()); |
| + s_liveMessagingProxyCount++; |
| } |
| -InProcessWorkerMessagingProxy::~InProcessWorkerMessagingProxy() |
| +ThreadedMessagingProxyBase::~ThreadedMessagingProxyBase() |
| { |
| DCHECK(isParentContextThread()); |
| - DCHECK(!m_workerObject); |
| if (m_loaderProxy) |
| m_loaderProxy->detachProvider(this); |
| s_liveMessagingProxyCount--; |
| } |
| -int InProcessWorkerMessagingProxy::proxyCount() |
| +int ThreadedMessagingProxyBase::proxyCount() |
| { |
| DCHECK(isMainThread()); |
| return s_liveMessagingProxyCount; |
| } |
| -void InProcessWorkerMessagingProxy::startWorkerGlobalScope(const KURL& scriptURL, const String& userAgent, const String& sourceCode) |
| +void ThreadedMessagingProxyBase::initializeWorkerThread(std::unique_ptr<WorkerThreadStartupData> startupData) |
| { |
| DCHECK(isParentContextThread()); |
| - if (m_askedToTerminate) { |
| - // Worker.terminate() could be called from JS before the thread was |
| - // created. |
| - return; |
| - } |
| Document* document = toDocument(getExecutionContext()); |
| - SecurityOrigin* starterOrigin = document->getSecurityOrigin(); |
| - |
| - ContentSecurityPolicy* csp = m_workerObject->contentSecurityPolicy() ? m_workerObject->contentSecurityPolicy() : document->contentSecurityPolicy(); |
| - DCHECK(csp); |
| - |
| - WorkerThreadStartMode startMode = m_workerInspectorProxy->workerStartMode(document); |
| - std::unique_ptr<WorkerSettings> workerSettings = wrapUnique(new WorkerSettings(document->settings())); |
| - std::unique_ptr<WorkerThreadStartupData> startupData = WorkerThreadStartupData::create(scriptURL, userAgent, sourceCode, nullptr, startMode, csp->headers().get(), m_workerObject->referrerPolicy(), starterOrigin, m_workerClients.release(), document->addressSpace(), OriginTrialContext::getTokens(document).get(), std::move(workerSettings)); |
| double originTime = document->loader() ? document->loader()->timing().referenceMonotonicTime() : monotonicallyIncreasingTime(); |
| m_loaderProxy = WorkerLoaderProxy::create(this); |
| m_workerThread = createWorkerThread(originTime); |
| m_workerThread->start(std::move(startupData)); |
| workerThreadCreated(); |
| - m_workerInspectorProxy->workerThreadCreated(document, m_workerThread.get(), scriptURL); |
| -} |
| - |
| -void InProcessWorkerMessagingProxy::postMessageToWorkerObject(PassRefPtr<SerializedScriptValue> message, std::unique_ptr<MessagePortChannelArray> channels) |
| -{ |
| - DCHECK(isParentContextThread()); |
| - if (!m_workerObject || m_askedToTerminate) |
| - return; |
| - |
| - MessagePortArray* ports = MessagePort::entanglePorts(*getExecutionContext(), std::move(channels)); |
| - m_workerObject->dispatchEvent(MessageEvent::create(ports, message)); |
| } |
| -void InProcessWorkerMessagingProxy::postMessageToWorkerGlobalScope(PassRefPtr<SerializedScriptValue> message, std::unique_ptr<MessagePortChannelArray> channels) |
| +void ThreadedMessagingProxyBase::postOrQueueTaskToWorkerGlobalScope(std::unique_ptr<ExecutionContextTask> task) |
|
nhiroki
2016/09/06 07:07:08
I'd prefer to keep 'Message(Event)' instead of 'Ta
ikilpatrick
2016/09/06 18:59:49
Acknowledged.
|
| { |
| - DCHECK(isParentContextThread()); |
| - if (m_askedToTerminate) |
| - return; |
| - |
| - std::unique_ptr<ExecutionContextTask> task = createCrossThreadTask(&processMessageOnWorkerGlobalScope, message, passed(std::move(channels)), crossThreadUnretained(&workerObjectProxy())); |
|
nhiroki
2016/09/06 07:07:08
Will Worklet support postMessage()? If it'll be su
ikilpatrick
2016/09/06 18:59:50
Yeah I wasn't sure what to do here initially. My i
|
| if (m_workerThread) { |
| // A message event is an activity and may initiate another activity. |
| m_workerGlobalScopeMayHavePendingActivity = true; |
| @@ -148,7 +72,7 @@ void InProcessWorkerMessagingProxy::postMessageToWorkerGlobalScope(PassRefPtr<Se |
| } |
| } |
| -void InProcessWorkerMessagingProxy::postTaskToWorkerGlobalScope(const WebTraceLocation& location, std::unique_ptr<ExecutionContextTask> task) |
| +void ThreadedMessagingProxyBase::postTaskToWorkerGlobalScope(const WebTraceLocation& location, std::unique_ptr<ExecutionContextTask> task) |
| { |
| if (m_askedToTerminate) |
| return; |
| @@ -157,7 +81,7 @@ void InProcessWorkerMessagingProxy::postTaskToWorkerGlobalScope(const WebTraceLo |
| m_workerThread->postTask(location, std::move(task)); |
| } |
| -void InProcessWorkerMessagingProxy::postTaskToLoader(const WebTraceLocation& location, std::unique_ptr<ExecutionContextTask> task) |
| +void ThreadedMessagingProxyBase::postTaskToLoader(const WebTraceLocation& location, std::unique_ptr<ExecutionContextTask> task) |
| { |
| DCHECK(getExecutionContext()->isDocument()); |
| // TODO(hiroshige,yuryu): Make this not use ExecutionContextTask and use |
| @@ -165,24 +89,7 @@ void InProcessWorkerMessagingProxy::postTaskToLoader(const WebTraceLocation& loc |
| getExecutionContext()->postTask(location, std::move(task)); |
| } |
| -void InProcessWorkerMessagingProxy::dispatchErrorEvent(const String& errorMessage, std::unique_ptr<SourceLocation> location, int exceptionId) |
| -{ |
| - DCHECK(isParentContextThread()); |
| - if (!m_workerObject) |
| - return; |
| - |
| - // We don't bother checking the askedToTerminate() flag here, because |
| - // exceptions should *always* be reported even if the thread is terminated. |
| - // This is intentionally different than the behavior in MessageWorkerTask, |
| - // because terminated workers no longer deliver messages (section 4.6 of the |
| - // WebWorker spec), but they do report exceptions. |
| - |
| - ErrorEvent* event = ErrorEvent::create(errorMessage, location->clone(), nullptr); |
| - if (m_workerObject->dispatchEvent(event) == DispatchEventResult::NotCanceled) |
| - postTaskToWorkerGlobalScope(BLINK_FROM_HERE, createCrossThreadTask(&processUnhandledExceptionOnWorkerGlobalScope, exceptionId)); |
| -} |
| - |
| -void InProcessWorkerMessagingProxy::reportConsoleMessage(MessageSource source, MessageLevel level, const String& message, std::unique_ptr<SourceLocation> location) |
| +void ThreadedMessagingProxyBase::reportConsoleMessage(MessageSource source, MessageLevel level, const String& message, std::unique_ptr<SourceLocation> location) |
| { |
| DCHECK(isParentContextThread()); |
| if (m_askedToTerminate) |
| @@ -191,7 +98,7 @@ void InProcessWorkerMessagingProxy::reportConsoleMessage(MessageSource source, M |
| m_workerInspectorProxy->addConsoleMessageFromWorker(level, message, std::move(location)); |
| } |
| -void InProcessWorkerMessagingProxy::workerThreadCreated() |
| +void ThreadedMessagingProxyBase::workerThreadCreated() |
| { |
| DCHECK(isParentContextThread()); |
| DCHECK(!m_askedToTerminate); |
| @@ -208,45 +115,24 @@ void InProcessWorkerMessagingProxy::workerThreadCreated() |
| m_queuedEarlyTasks.clear(); |
| } |
| -InProcessWorkerMessagingProxy::InProcessWorkerMessagingProxy(ExecutionContext* executionContext, InProcessWorkerBase* workerObject, WorkerClients* workerClients) |
| - : m_executionContext(executionContext) |
| - , m_workerObjectProxy(InProcessWorkerObjectProxy::create(this)) |
| - , m_workerObject(workerObject) |
| - , m_mayBeDestroyed(false) |
| - , m_unconfirmedMessageCount(0) |
| - , m_workerGlobalScopeMayHavePendingActivity(false) |
| - , m_askedToTerminate(false) |
| - , m_workerInspectorProxy(WorkerInspectorProxy::create()) |
| - , m_workerClients(workerClients) |
| - , m_parentFrameTaskRunners(ParentFrameTaskRunners::create(toDocument(m_executionContext.get())->frame())) |
| +void ThreadedMessagingProxyBase::parentObjectDestroyed() |
| { |
| DCHECK(isParentContextThread()); |
| - s_liveMessagingProxyCount++; |
| -} |
| - |
| -void InProcessWorkerMessagingProxy::workerObjectDestroyed() |
| -{ |
| - DCHECK(isParentContextThread()); |
| - |
| - // workerObjectDestroyed() is called in InProcessWorkerBase's destructor. |
| - // Thus it should be guaranteed that a weak pointer m_workerObject has been |
| - // cleared before this method gets called. |
| - DCHECK(!m_workerObject); |
| - m_parentFrameTaskRunners->get(TaskType::Internal)->postTask(BLINK_FROM_HERE, WTF::bind(&InProcessWorkerMessagingProxy::workerObjectDestroyedInternal, unretained(this))); |
| + m_parentFrameTaskRunners->get(TaskType::Internal)->postTask(BLINK_FROM_HERE, WTF::bind(&ThreadedMessagingProxyBase::parentObjectDestroyedInternal, unretained(this))); |
| } |
| -void InProcessWorkerMessagingProxy::workerObjectDestroyedInternal() |
| +void ThreadedMessagingProxyBase::parentObjectDestroyedInternal() |
| { |
| DCHECK(isParentContextThread()); |
| m_mayBeDestroyed = true; |
| if (m_workerThread) |
| - terminateWorkerGlobalScope(); |
| + terminateGlobalScope(); |
| else |
| workerThreadTerminated(); |
| } |
| -void InProcessWorkerMessagingProxy::workerThreadTerminated() |
| +void ThreadedMessagingProxyBase::workerThreadTerminated() |
| { |
| DCHECK(isParentContextThread()); |
| @@ -260,7 +146,7 @@ void InProcessWorkerMessagingProxy::workerThreadTerminated() |
| delete this; |
| } |
| -void InProcessWorkerMessagingProxy::terminateWorkerGlobalScope() |
| +void ThreadedMessagingProxyBase::terminateGlobalScope() |
| { |
| DCHECK(isParentContextThread()); |
| @@ -274,14 +160,14 @@ void InProcessWorkerMessagingProxy::terminateWorkerGlobalScope() |
| m_workerInspectorProxy->workerThreadTerminated(); |
| } |
| -void InProcessWorkerMessagingProxy::postMessageToPageInspector(const String& message) |
| +void ThreadedMessagingProxyBase::postMessageToPageInspector(const String& message) |
| { |
| DCHECK(isParentContextThread()); |
| if (m_workerInspectorProxy) |
| m_workerInspectorProxy->dispatchMessageFromWorker(message); |
| } |
| -void InProcessWorkerMessagingProxy::confirmMessageFromWorkerObject() |
| +void ThreadedMessagingProxyBase::confirmMessageFromWorkerObject() |
| { |
| DCHECK(isParentContextThread()); |
| if (m_askedToTerminate) |
| @@ -290,7 +176,7 @@ void InProcessWorkerMessagingProxy::confirmMessageFromWorkerObject() |
| --m_unconfirmedMessageCount; |
| } |
| -void InProcessWorkerMessagingProxy::pendingActivityFinished() |
| +void ThreadedMessagingProxyBase::pendingActivityFinished() |
| { |
| DCHECK(isParentContextThread()); |
| DCHECK(m_workerGlobalScopeMayHavePendingActivity); |
| @@ -302,7 +188,7 @@ void InProcessWorkerMessagingProxy::pendingActivityFinished() |
| m_workerGlobalScopeMayHavePendingActivity = false; |
| } |
| -bool InProcessWorkerMessagingProxy::hasPendingActivity() const |
| +bool ThreadedMessagingProxyBase::hasPendingActivity() const |
| { |
| DCHECK(isParentContextThread()); |
| if (m_askedToTerminate) |
| @@ -310,7 +196,7 @@ bool InProcessWorkerMessagingProxy::hasPendingActivity() const |
| return m_unconfirmedMessageCount || m_workerGlobalScopeMayHavePendingActivity; |
| } |
| -bool InProcessWorkerMessagingProxy::isParentContextThread() const |
| +bool ThreadedMessagingProxyBase::isParentContextThread() const |
| { |
| // TODO(nhiroki): Nested worker is not supported yet, so the parent context |
| // thread should be equal to the main thread (http://crbug.com/31666). |