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 BroadcastChannel* channel = new BroadcastChannel(executionContext, name); | |
23 channel->suspendIfNeeded(); | |
haraken
2016/06/02 04:23:41
If you make BroadcastChannel a ContextLifecycleObs
Marijn Kruisselbrink
2016/06/02 18:33:29
Done
| |
24 return channel; | |
25 } | |
26 | |
27 BroadcastChannel::~BroadcastChannel() | |
28 { | |
29 close(); | |
30 } | |
31 | |
32 void BroadcastChannel::postMessage(const ScriptValue& message, ExceptionState& e xceptionState) | |
33 { | |
34 if (!m_connection) { | |
35 exceptionState.throwDOMException(InvalidStateError, "Channel is closed") ; | |
36 return; | |
37 } | |
38 RefPtr<SerializedScriptValue> value = SerializedScriptValue::serialize(messa ge.isolate(), message.v8Value(), nullptr, nullptr, exceptionState); | |
39 if (exceptionState.hadException()) | |
40 return; | |
41 | |
42 String data = value->toWireString(); | |
43 m_connection->broadcast(this, data); | |
44 } | |
45 | |
46 void BroadcastChannel::close() | |
47 { | |
48 if (m_connection) | |
49 m_connection->unregisterClient(this); | |
50 m_connection = nullptr; | |
51 } | |
52 | |
53 void BroadcastChannel::onMessage(const String& message) | |
54 { | |
55 // Queue a task to dispatch the event. | |
56 RefPtr<SerializedScriptValue> value = SerializedScriptValue::create(message) ; | |
57 MessageEvent* event = MessageEvent::create(nullptr, value.release(), getExec utionContext()->getSecurityOrigin()->toString()); | |
58 event->setTarget(this); | |
59 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); | |
60 DCHECK(success); | |
61 ALLOW_UNUSED_LOCAL(success); | |
62 } | |
63 | |
64 void BroadcastChannel::onError() | |
65 { | |
66 m_connection = nullptr; | |
67 close(); | |
68 } | |
69 | |
70 const AtomicString& BroadcastChannel::interfaceName() const | |
71 { | |
72 return EventTargetNames::BroadcastChannel; | |
73 } | |
74 | |
75 bool BroadcastChannel::hasPendingActivity() const | |
76 { | |
77 return m_connection && hasEventListeners(EventTypeNames::message); | |
78 } | |
79 | |
80 DEFINE_TRACE(BroadcastChannel) | |
81 { | |
82 ActiveDOMObject::trace(visitor); | |
83 EventTargetWithInlineData::trace(visitor); | |
84 } | |
85 | |
86 BroadcastChannel::BroadcastChannel(ExecutionContext* executionContext, const Str ing& name) | |
87 : ActiveScriptWrappable(this) | |
88 , ActiveDOMObject(executionContext) | |
89 , m_origin(executionContext->getSecurityOrigin()) | |
90 , m_name(name) | |
91 , m_connection(BroadcastChannelConnection::getForChannel(m_origin, m_name)) | |
92 { | |
93 m_connection->registerClient(this); | |
94 } | |
95 | |
96 } // namespace blink | |
OLD | NEW |