OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/serviceworkers/StashedMessagePort.h" |
| 7 |
| 8 #include "bindings/core/v8/SerializedScriptValue.h" |
| 9 #include "core/dom/CrossThreadTask.h" |
| 10 #include "core/events/MessageEvent.h" |
| 11 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
| 12 #include "modules/serviceworkers/StashedPortCollection.h" |
| 13 #include "public/platform/WebTraceLocation.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 PassRefPtrWillBeRawPtr<StashedMessagePort> StashedMessagePort::create(ExecutionC
ontext& executionContext, PassOwnPtr<WebMessagePortChannel> remote, const String
& name) |
| 18 { |
| 19 RefPtrWillBeRawPtr<StashedMessagePort> port = adoptRefWillBeNoop(new Stashed
MessagePort(executionContext, remote, name)); |
| 20 port->suspendIfNeeded(); |
| 21 return port.release(); |
| 22 } |
| 23 |
| 24 StashedMessagePort::StashedMessagePort(ExecutionContext& executionContext, PassO
wnPtr<WebMessagePortChannel> remote, const String& name) |
| 25 : MessagePort(executionContext), m_name(name), m_weakFactory(this) |
| 26 { |
| 27 entangle(remote); |
| 28 start(); |
| 29 } |
| 30 |
| 31 StashedMessagePort::~StashedMessagePort() |
| 32 { |
| 33 } |
| 34 |
| 35 PassOwnPtrWillBeRawPtr<StashedMessagePortArray> StashedMessagePort::toStashedMes
sagePortArray(ExecutionContext* context, const WebMessagePortChannelArray& webCh
annels, const WebVector<WebString>& channelKeys) |
| 36 { |
| 37 OwnPtrWillBeRawPtr<StashedMessagePortArray> ports = adoptPtrWillBeNoop(new S
tashedMessagePortArray(webChannels.size())); |
| 38 for (size_t i = 0; i < webChannels.size(); ++i) { |
| 39 OwnPtr<WebMessagePortChannel> channel(adoptPtr(webChannels[i])); |
| 40 (*ports)[i] = StashedMessagePort::create(*context, channel.release(), ch
annelKeys[i]); |
| 41 } |
| 42 return ports.release(); |
| 43 } |
| 44 |
| 45 String StashedMessagePort::name() const |
| 46 { |
| 47 return m_name; |
| 48 } |
| 49 |
| 50 void StashedMessagePort::messageAvailable() |
| 51 { |
| 52 ASSERT(executionContext()); |
| 53 executionContext()->postTask(FROM_HERE, createCrossThreadTask(&StashedMessag
ePort::dispatchMessages, m_weakFactory.createWeakPtr())); |
| 54 } |
| 55 |
| 56 void StashedMessagePort::dispatchMessages() |
| 57 { |
| 58 if (!isEntangled()) |
| 59 return; |
| 60 ASSERT(executionContext()->isServiceWorkerGlobalScope()); |
| 61 RefPtr<StashedPortCollection> stashedPorts = toServiceWorkerGlobalScope(exec
utionContext())->ports(); |
| 62 |
| 63 RefPtr<SerializedScriptValue> message; |
| 64 OwnPtr<MessagePortChannelArray> channels; |
| 65 while (tryGetMessage(message, channels)) { |
| 66 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts(
*executionContext(), channels.release()); |
| 67 RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), me
ssage.release(), String(), String(), this); |
| 68 |
| 69 stashedPorts->dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION); |
| 70 } |
| 71 } |
| 72 |
| 73 } // namespace blink |
OLD | NEW |