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(Platform::current()->mainThread()->isCurrentThread()); | |
|
kinuko
2016/06/09 09:14:58
nit: DCHECK(isMainThread()) ?
Marijn Kruisselbrink
2016/06/22 18:46:57
Done
| |
| 25 Platform::current()->serviceRegistry()->connectToRemoteService(std::move(req uest)); | |
| 26 } | |
| 27 | |
| 28 webmessaging::mojom::blink::BroadcastChannelServicePtr& getThreadSpecificService () | |
|
haraken
2016/06/09 02:09:36
Add a comment why you need to use a thread-local s
Marijn Kruisselbrink
2016/06/22 18:46:56
Done
| |
| 29 { | |
| 30 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<webmessaging::mojom::blink::B roadcastChannelServicePtr>, service, new ThreadSpecific<webmessaging::mojom::bli nk::BroadcastChannelServicePtr>); | |
| 31 if (!service.isSet()) { | |
| 32 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FR OM_HERE, | |
| 33 threadSafeBind(&connectToService, passed(mojo::GetProxy(&*service))) ); | |
| 34 } | |
| 35 return *service; | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 // static | |
| 41 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c onst String& name, ExceptionState& exceptionState) | |
| 42 { | |
| 43 if (executionContext->getSecurityOrigin()->isUnique()) { | |
| 44 // TODO(mek): Decide what to do here depending on https://github.com/wha twg/html/issues/1319 | |
| 45 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc astChannel in an opaque origin"); | |
| 46 return nullptr; | |
| 47 } | |
| 48 return new BroadcastChannel(executionContext, name); | |
| 49 } | |
| 50 | |
| 51 BroadcastChannel::~BroadcastChannel() | |
| 52 { | |
| 53 } | |
| 54 | |
| 55 void BroadcastChannel::dispose() | |
| 56 { | |
| 57 close(); | |
| 58 } | |
| 59 | |
| 60 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e xceptionState) | |
| 61 { | |
| 62 if (!m_binding.is_bound()) { | |
| 63 exceptionState.throwDOMException(InvalidStateError, "Channel is closed") ; | |
| 64 return; | |
| 65 } | |
| 66 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); | |
| 67 if (exceptionState.hadException()) | |
| 68 return; | |
| 69 | |
| 70 String data = value->toWireString(); | |
| 71 m_remoteClient->OnMessage(data); | |
| 72 } | |
| 73 | |
| 74 void BroadcastChannel::close() | |
| 75 { | |
| 76 m_remoteClient.reset(); | |
| 77 if (m_binding.is_bound()) | |
| 78 m_binding.Close(); | |
| 79 } | |
| 80 | |
| 81 const AtomicString& BroadcastChannel::interfaceName() const | |
| 82 { | |
| 83 return EventTargetNames::BroadcastChannel; | |
| 84 } | |
| 85 | |
| 86 bool BroadcastChannel::hasPendingActivity() const | |
| 87 { | |
| 88 return m_binding.is_bound() && hasEventListeners(EventTypeNames::message); | |
| 89 } | |
| 90 | |
| 91 void BroadcastChannel::contextDestroyed() | |
| 92 { | |
| 93 close(); | |
| 94 } | |
| 95 | |
| 96 DEFINE_TRACE(BroadcastChannel) | |
| 97 { | |
| 98 ContextLifecycleObserver::trace(visitor); | |
| 99 EventTargetWithInlineData::trace(visitor); | |
| 100 } | |
| 101 | |
| 102 void BroadcastChannel::OnMessage(const String& message) | |
| 103 { | |
| 104 // Queue a task to dispatch the event. | |
| 105 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message) ; | |
| 106 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec utionContext()->getSecurityOrigin()->toString()); | |
| 107 event->setTarget(this); | |
| 108 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); | |
| 109 DCHECK(success); | |
| 110 ALLOW_UNUSED_LOCAL(success); | |
| 111 } | |
| 112 | |
| 113 void BroadcastChannel::onError() | |
| 114 { | |
| 115 close(); | |
| 116 } | |
| 117 | |
| 118 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str ing& name) | |
| 119 : ActiveScriptWrappable(this) | |
| 120 , ContextLifecycleObserver(executionContext) | |
| 121 , m_origin(executionContext->getSecurityOrigin()) | |
| 122 , m_name(name) | |
| 123 , m_binding(this) | |
| 124 { | |
| 125 webmessaging::mojom::blink::BroadcastChannelServicePtr& service = getThreadS pecificService(); | |
| 126 | |
| 127 // Local BroadcastChannelClient for messages send from the browser to this c hannel. | |
| 128 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localCli entInfo; | |
| 129 m_binding.Bind(&localClientInfo, service.associated_group()); | |
| 130 m_binding.set_connection_error_handler(createBaseCallback(bind(&BroadcastCha nnel::onError, WeakPersistentThisPointer<BroadcastChannel>(this)))); | |
| 131 | |
| 132 // Remove BroadcastChannelClient for messages send from this channel to the browser. | |
|
kinuko
2016/06/09 09:14:58
Um... I guess s/Remove/Remote/ ?
Marijn Kruisselbrink
2016/06/22 18:46:57
Ah yes, done.
| |
| 133 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo remoteCl ientInfo; | |
| 134 mojo::AssociatedInterfaceRequest<webmessaging::mojom::blink::BroadcastChanne lClient> remoteCientRequest; | |
| 135 service.associated_group()->CreateAssociatedInterface(mojo::AssociatedGroup: :WILL_PASS_REQUEST, &remoteClientInfo, &remoteCientRequest); | |
| 136 m_remoteClient.Bind(std::move(remoteClientInfo)); | |
| 137 m_remoteClient.set_connection_error_handler(createBaseCallback(bind(&Broadca stChannel::onError, WeakPersistentThisPointer<BroadcastChannel>(this)))); | |
| 138 | |
| 139 service->ConnectToChannel(m_origin, m_name, std::move(localClientInfo), std: :move(remoteCientRequest)); | |
| 140 } | |
| 141 | |
| 142 } // namespace blink | |
| OLD | NEW |