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..54cbd4c9fba6df740cd18d1c812562601821d240 |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/StashedMessagePort.cpp |
| @@ -0,0 +1,79 @@ |
| +// 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) |
|
scheib
2015/05/06 21:44:30
definitions in the corresponding .cc file should b
Marijn Kruisselbrink
2015/05/12 06:56:40
yes, while that's true it's pretty common in at le
|
| + : MessagePort(executionContext), m_weakFactory(this), m_name(name) |
| +{ |
| + 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; |
| +} |
| + |
| +DEFINE_TRACE(StashedMessagePort) |
|
scheib
2015/05/06 21:44:30
Maybe use DEFINE_TRACE_INLINE for this in .h?
Marijn Kruisselbrink
2015/05/12 06:56:40
Done.
|
| +{ |
| + MessagePort::trace(visitor); |
| +} |
| + |
| +void StashedMessagePort::messageAvailable() |
| +{ |
| + ASSERT(executionContext()); |
| + executionContext()->postTask(FROM_HERE, createCrossThreadTask(&StashedMessagePort::dispatchMessages, m_weakFactory.createWeakPtr())); |
| +} |
| + |
| +void StashedMessagePort::dispatchMessages() |
|
scheib
2015/05/06 21:44:30
There's no need to check the closed, started state
Marijn Kruisselbrink
2015/05/12 06:56:40
!isEntangled() implies !closed(), and for now I'm
|
| +{ |
| + 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 |
| + |