Chromium Code Reviews| Index: Source/modules/serviceworkers/StashedMessagePort.cpp |
| diff --git a/Source/modules/serviceworkers/StashedMessagePort.cpp b/Source/modules/serviceworkers/StashedMessagePort.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..901dc3c9e9acd627f5e3213be627f7472ec87f1f |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/StashedMessagePort.cpp |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2015 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 "config.h" |
| +#include "modules/serviceworkers/StashedMessagePort.h" |
| + |
| +#include "bindings/core/v8/SerializedScriptValue.h" |
| +#include "core/dom/CrossThreadTask.h" |
| +#include "core/events/MessageEvent.h" |
| +#include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
| +#include "modules/serviceworkers/StashedPortCollection.h" |
| +#include "public/platform/WebTraceLocation.h" |
| + |
| +namespace blink { |
| + |
| +PassRefPtrWillBeRawPtr<StashedMessagePort> StashedMessagePort::create(ExecutionContext& executionContext, PassOwnPtr<WebMessagePortChannel> remote, const String& name) |
| +{ |
| + RefPtrWillBeRawPtr<StashedMessagePort> port = adoptRefWillBeNoop(new StashedMessagePort(executionContext, remote, name)); |
| + port->suspendIfNeeded(); |
| + return port.release(); |
| +} |
| + |
| +StashedMessagePort::StashedMessagePort(ExecutionContext& executionContext, PassOwnPtr<WebMessagePortChannel> remote, const String& name) |
| + : MessagePort(executionContext), m_name(name), m_weakFactory(this) |
| +{ |
| + entangle(remote); |
| + start(); |
| +} |
| + |
| +StashedMessagePort::~StashedMessagePort() |
| +{ |
| +} |
| + |
| +PassOwnPtrWillBeRawPtr<StashedMessagePortArray> StashedMessagePort::toStashedMessagePortArray(ExecutionContext* context, const WebMessagePortChannelArray& webChannels, const WebVector<WebString>& channelKeys) |
| +{ |
| + OwnPtrWillBeRawPtr<StashedMessagePortArray> ports = adoptPtrWillBeNoop(new StashedMessagePortArray(webChannels.size())); |
| + for (size_t i = 0; i < webChannels.size(); ++i) { |
| + OwnPtr<WebMessagePortChannel> channel(adoptPtr(webChannels[i])); |
| + (*ports)[i] = StashedMessagePort::create(*context, channel.release(), channelKeys[i]); |
| + } |
| + return ports.release(); |
| +} |
| + |
| +String StashedMessagePort::name() const |
| +{ |
| + return m_name; |
| +} |
| + |
| +void StashedMessagePort::messageAvailable() |
| +{ |
| + ASSERT(executionContext()); |
| + executionContext()->postTask(FROM_HERE, createCrossThreadTask(&StashedMessagePort::dispatchMessages, m_weakFactory.createWeakPtr())); |
| +} |
| + |
| +void StashedMessagePort::dispatchMessages() |
| +{ |
| + if (!isEntangled()) |
| + return; |
| + ASSERT(executionContext()->isServiceWorkerGlobalScope()); |
| + RefPtr<StashedPortCollection> stashedPorts = toServiceWorkerGlobalScope(executionContext())->ports(); |
| + |
| + RefPtr<SerializedScriptValue> message; |
| + OwnPtr<MessagePortChannelArray> channels; |
| + while (tryGetMessage(message, channels)) { |
| + OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts(*executionContext(), channels.release()); |
| + RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), message.release(), String(), String(), this); |
| + |
| + stashedPorts->dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION); |
| + } |
| +} |
| + |
| +} // namespace blink |
| + |
|
nhiroki
2015/05/13 01:09:38
nit: an extra blank line
Marijn Kruisselbrink
2015/05/13 01:46:53
Done.
|