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 | |
12 namespace blink { | |
13 | |
14 // static | |
15 BroadcastChannel* BroadcastChannel::create(ExecutionContext* executionContext, c onst String& name, ExceptionState& exceptionState) | |
16 { | |
17 if (executionContext->getSecurityOrigin()->isUnique()) { | |
18 // TODO(mek): Decide what to do here depending on https://github.com/wha twg/html/issues/1319 | |
19 exceptionState.throwDOMException(NotSupportedError, "Can't create Broadc astChannel in an opaque origin"); | |
20 return nullptr; | |
21 } | |
22 return new BroadcastChannel(executionContext, name); | |
23 } | |
24 | |
25 BroadcastChannel::~BroadcastChannel() | |
26 { | |
27 } | |
28 | |
29 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e xceptionState) | |
30 { | |
31 if (!m_connection) { | |
32 exceptionState.throwDOMException(InvalidStateError, "Channel is closed") ; | |
33 return; | |
34 } | |
35 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); | |
36 if (exceptionState.hadException()) | |
37 return; | |
38 | |
39 String data = value->toWireString(); | |
40 m_connection->broadcast(this, data); | |
41 } | |
42 | |
43 void BroadcastChannel::close() | |
44 { | |
45 if (m_connection) | |
46 m_connection->unregisterClient(this); | |
47 m_connection = nullptr; | |
48 } | |
49 | |
50 void BroadcastChannel::onMessage(const String& message) | |
51 { | |
52 // Queue a task to dispatch the event. | |
53 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message) ; | |
54 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec utionContext()->getSecurityOrigin()->toString()); | |
55 event->setTarget(this); | |
56 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); | |
57 DCHECK(success); | |
58 ALLOW_UNUSED_LOCAL(success); | |
haraken
2016/06/06 02:05:47
Nit: Do we need this?
Marijn Kruisselbrink
2016/06/06 20:05:29
Not sure. Assertions.h seems to suggest so though
| |
59 } | |
60 | |
61 void BroadcastChannel::onError() | |
62 { | |
63 m_connection = nullptr; | |
haraken
2016/06/06 02:05:47
Do you really want to clear m_connection before ca
Marijn Kruisselbrink
2016/06/06 20:05:29
Good question. That pretty much turns close() into
| |
64 close(); | |
65 } | |
66 | |
67 const AtomicString& BroadcastChannel::interfaceName() const | |
68 { | |
69 return EventTargetNames::BroadcastChannel; | |
70 } | |
71 | |
72 bool BroadcastChannel::hasPendingActivity() const | |
73 { | |
74 return m_connection && hasEventListeners(EventTypeNames::message); | |
75 } | |
76 | |
77 DEFINE_TRACE(BroadcastChannel) | |
78 { | |
79 ContextLifecycleObserver::trace(visitor); | |
80 EventTargetWithInlineData::trace(visitor); | |
81 visitor->trace(m_connection); | |
82 } | |
83 | |
84 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str ing& name) | |
85 : ActiveScriptWrappable(this) | |
86 , ContextLifecycleObserver(executionContext) | |
haraken
2016/06/06 02:05:47
The fact that you're observing ContextLifecycleObs
haraken
2016/06/07 00:53:26
Any idea on this?
Marijn Kruisselbrink
2016/06/07 01:18:39
Sorry about missing this one. Yes, contextDestroye
haraken
2016/06/07 01:29:55
Don't know. But I think MessagePort should inherit
| |
87 , m_origin(executionContext->getSecurityOrigin()) | |
88 , m_name(name) | |
89 , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name)) | |
90 { | |
91 m_connection->registerClient(this); | |
92 } | |
93 | |
94 } // namespace blink | |
OLD | NEW |