Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "modules/broadcastchannel/BroadcastChannel.h" | |
| 6 | |
| 7 #include "bindings/core/v8/SerializedScriptValue.h" | |
| 8 #include "core/dom/ExceptionCode.h" | |
| 9 #include "core/events/EventQueue.h" | |
| 10 #include "core/events/MessageEvent.h" | |
| 11 #include "platform/ThreadSafeFunctional.h" | |
| 12 #include "platform/mojo/MojoHelper.h" | |
| 13 #include "public/platform/Platform.h" | |
| 14 #include "public/platform/ServiceRegistry.h" | |
| 15 #include "public/platform/WebTaskRunner.h" | |
| 16 #include "public/platform/WebTraceLocation.h" | |
| 17 | |
| 18 namespace blink { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void connectToService(webmessaging::mojom::blink::BroadcastChannelServiceRequest request) | |
| 23 { | |
| 24 DCHECK(isMainThread()); | |
| 25 Platform::current()->serviceRegistry()->connectToRemoteService(std::move(req uest)); | |
| 26 } | |
| 27 | |
| 28 // To ensure proper ordering of messages send to/from multiple BroadcastChannel | |
| 29 // instances in the same thread this uses one BroadcastChannelService | |
| 30 // connection as basis for all connections to channels from the same thread. The | |
| 31 // actual connections used to send/receive messages are then created using | |
| 32 // associated interfaces, ensuring proper message ordering. | |
| 33 webmessaging::mojom::blink::BroadcastChannelServicePtr& getThreadSpecificService () | |
| 34 { | |
| 35 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<webmessaging::mojom::blink::B roadcastChannelServicePtr>, service, new ThreadSpecific<webmessaging::mojom::bli nk::BroadcastChannelServicePtr>); | |
| 36 if (!service.isSet()) { | |
| 37 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FR OM_HERE, | |
| 38 threadSafeBind(&connectToService, passed(mojo::GetProxy(&*service))) ); | |
|
kinuko
2016/06/23 15:29:57
I was looking into connectToRemoteService impl and
Marijn Kruisselbrink
2016/06/23 16:32:32
Ah yes, seems that was changed recently to work fr
| |
| 39 } | |
| 40 return *service; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 // static | |
| 46 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c onst String& name, ExceptionState& exceptionState) | |
| 47 { | |
| 48 if (executionContext->getSecurityOrigin()->isUnique()) { | |
| 49 // TODO(mek): Decide what to do here depending on https://github.com/wha twg/html/issues/1319 | |
| 50 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc astChannel in an opaque origin"); | |
| 51 return nullptr; | |
| 52 } | |
| 53 return new BroadcastChannel(executionContext, name); | |
| 54 } | |
| 55 | |
| 56 BroadcastChannel::~BroadcastChannel() | |
| 57 { | |
| 58 } | |
| 59 | |
| 60 void BroadcastChannel::dispose() | |
| 61 { | |
| 62 close(); | |
| 63 } | |
| 64 | |
| 65 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e xceptionState) | |
| 66 { | |
| 67 if (!m_binding.is_bound()) { | |
| 68 exceptionState.throwDOMException(InvalidStateError, "Channel is closed") ; | |
| 69 return; | |
| 70 } | |
| 71 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); | |
| 72 if (exceptionState.hadException()) | |
| 73 return; | |
| 74 | |
| 75 String data = value->toWireString(); | |
| 76 m_remoteClient->OnMessage(data); | |
| 77 } | |
| 78 | |
| 79 void BroadcastChannel::close() | |
| 80 { | |
| 81 m_remoteClient.reset(); | |
| 82 if (m_binding.is_bound()) | |
| 83 m_binding.Close(); | |
| 84 } | |
| 85 | |
| 86 const AtomicString& BroadcastChannel::interfaceName() const | |
| 87 { | |
| 88 return EventTargetNames::BroadcastChannel; | |
| 89 } | |
| 90 | |
| 91 bool BroadcastChannel::hasPendingActivity() const | |
| 92 { | |
| 93 return m_binding.is_bound() && hasEventListeners(EventTypeNames::message); | |
| 94 } | |
| 95 | |
| 96 void BroadcastChannel::contextDestroyed() | |
| 97 { | |
| 98 close(); | |
| 99 } | |
| 100 | |
| 101 DEFINE_TRACE(BroadcastChannel) | |
| 102 { | |
| 103 ContextLifecycleObserver::trace(visitor); | |
| 104 EventTargetWithInlineData::trace(visitor); | |
| 105 } | |
| 106 | |
| 107 void BroadcastChannel::OnMessage(const String& message) | |
| 108 { | |
| 109 // Queue a task to dispatch the event. | |
| 110 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message) ; | |
| 111 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec utionContext()->getSecurityOrigin()->toString()); | |
| 112 event->setTarget(this); | |
| 113 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); | |
| 114 DCHECK(success); | |
| 115 ALLOW_UNUSED_LOCAL(success); | |
| 116 } | |
| 117 | |
| 118 void BroadcastChannel::onError() | |
| 119 { | |
| 120 close(); | |
| 121 } | |
| 122 | |
| 123 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str ing& name) | |
| 124 : ActiveScriptWrappable(this) | |
| 125 , ContextLifecycleObserver(executionContext) | |
| 126 , m_origin(executionContext->getSecurityOrigin()) | |
| 127 , m_name(name) | |
| 128 , m_binding(this) | |
| 129 { | |
| 130 webmessaging::mojom::blink::BroadcastChannelServicePtr& service = getThreadS pecificService(); | |
| 131 | |
| 132 // Local BroadcastChannelClient for messages send from the browser to this c hannel. | |
| 133 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localCli entInfo; | |
| 134 m_binding.Bind(&localClientInfo, service.associated_group()); | |
| 135 m_binding.set_connection_error_handler(createBaseCallback(bind(&BroadcastCha nnel::onError, WeakPersistentThisPointer<BroadcastChannel>(this)))); | |
| 136 | |
| 137 // Remote BroadcastChannelClient for messages send from this channel to the browser. | |
| 138 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo remoteCl ientInfo; | |
| 139 mojo::AssociatedInterfaceRequest<webmessaging::mojom::blink::BroadcastChanne lClient> remoteCientRequest; | |
| 140 service.associated_group()->CreateAssociatedInterface(mojo::AssociatedGroup: :WILL_PASS_REQUEST, &remoteClientInfo, &remoteCientRequest); | |
| 141 m_remoteClient.Bind(std::move(remoteClientInfo)); | |
| 142 m_remoteClient.set_connection_error_handler(createBaseCallback(bind(&Broadca stChannel::onError, WeakPersistentThisPointer<BroadcastChannel>(this)))); | |
| 143 | |
| 144 service->ConnectToChannel(m_origin, m_name, std::move(localClientInfo), std: :move(remoteCientRequest)); | |
| 145 } | |
| 146 | |
| 147 } // namespace blink | |
| OLD | NEW |