Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Side by Side Diff: third_party/WebKit/Source/modules/broadcastchannel/BroadcastChannel.cpp

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address dcheng's comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/mojo/MojoHelper.h"
12 #include "public/platform/Platform.h"
13 #include "public/platform/ServiceRegistry.h"
14 #include "wtf/Functional.h"
15
16 namespace blink {
17
18 namespace {
19
20 // To ensure proper ordering of messages send to/from multiple BroadcastChannel
21 // instances in the same thread this uses one BroadcastChannelService
22 // connection as basis for all connections to channels from the same thread. The
23 // actual connections used to send/receive messages are then created using
24 // associated interfaces, ensuring proper message ordering.
25 webmessaging::mojom::blink::BroadcastChannelProviderPtr& getThreadSpecificProvid er()
26 {
27 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<webmessaging::mojom::blink::B roadcastChannelProviderPtr>, provider, new ThreadSpecific<webmessaging::mojom::b link::BroadcastChannelProviderPtr>);
28 if (!provider.isSet()) {
29 Platform::current()->serviceRegistry()->connectToRemoteService(mojo::Get Proxy(&*provider));
30 }
31 return *provider;
32 }
33
34 } // namespace
35
36 // static
37 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c onst String& name, ExceptionState& exceptionState)
38 {
39 if (executionContext->getSecurityOrigin()->isUnique()) {
40 // TODO(mek): Decide what to do here depending on https://github.com/wha twg/html/issues/1319
41 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc astChannel in an opaque origin");
42 return nullptr;
43 }
44 return new BroadcastChannel(executionContext, name);
45 }
46
47 BroadcastChannel::~BroadcastChannel()
48 {
49 }
50
51 void BroadcastChannel::dispose()
52 {
53 close();
54 }
55
56 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e xceptionState)
57 {
58 if (!m_binding.is_bound()) {
59 exceptionState.throwDOMException(InvalidStateError, "Channel is closed") ;
60 return;
61 }
62 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState);
63 if (exceptionState.hadException())
64 return;
65
66 String data = value->toWireString();
67 m_remoteClient->OnMessage(data);
68 }
69
70 void BroadcastChannel::close()
71 {
72 m_remoteClient.reset();
73 if (m_binding.is_bound())
74 m_binding.Close();
75 }
76
77 const AtomicString& BroadcastChannel::interfaceName() const
78 {
79 return EventTargetNames::BroadcastChannel;
80 }
81
82 bool BroadcastChannel::hasPendingActivity() const
83 {
84 return m_binding.is_bound() && hasEventListeners(EventTypeNames::message);
85 }
86
87 void BroadcastChannel::contextDestroyed()
88 {
89 close();
90 }
91
92 DEFINE_TRACE(BroadcastChannel)
93 {
94 ContextLifecycleObserver::trace(visitor);
95 EventTargetWithInlineData::trace(visitor);
96 }
97
98 void BroadcastChannel::OnMessage(const String& message)
99 {
100 // Queue a task to dispatch the event.
101 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message) ;
102 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec utionContext()->getSecurityOrigin()->toString());
103 event->setTarget(this);
104 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event);
105 DCHECK(success);
106 ALLOW_UNUSED_LOCAL(success);
107 }
108
109 void BroadcastChannel::onError()
110 {
111 close();
112 }
113
114 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str ing& name)
115 : ActiveScriptWrappable(this)
116 , ContextLifecycleObserver(executionContext)
117 , m_origin(executionContext->getSecurityOrigin())
118 , m_name(name)
119 , m_binding(this)
120 {
121 webmessaging::mojom::blink::BroadcastChannelProviderPtr& provider = getThrea dSpecificProvider();
122
123 // Local BroadcastChannelClient for messages send from the browser to this c hannel.
124 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo localCli entInfo;
125 m_binding.Bind(&localClientInfo, provider.associated_group());
126 m_binding.set_connection_error_handler(createBaseCallback(WTF::bind(&Broadca stChannel::onError, wrapWeakPersistent(this))));
127
128 // Remote BroadcastChannelClient for messages send from this channel to the browser.
129 webmessaging::mojom::blink::BroadcastChannelClientAssociatedPtrInfo remoteCl ientInfo;
130 mojo::AssociatedInterfaceRequest<webmessaging::mojom::blink::BroadcastChanne lClient> remoteCientRequest;
131 provider.associated_group()->CreateAssociatedInterface(mojo::AssociatedGroup ::WILL_PASS_REQUEST, &remoteClientInfo, &remoteCientRequest);
132 m_remoteClient.Bind(std::move(remoteClientInfo));
133 m_remoteClient.set_connection_error_handler(createBaseCallback(WTF::bind(&Br oadcastChannel::onError, wrapWeakPersistent(this))));
134
135 provider->ConnectToChannel(m_origin, m_name, std::move(localClientInfo), std ::move(remoteCientRequest));
136 }
137
138 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698