/src/mozilla-central/dom/events/MessageEvent.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/MessageEvent.h" |
8 | | #include "mozilla/dom/MessageEventBinding.h" |
9 | | #include "mozilla/dom/MessagePort.h" |
10 | | #include "mozilla/dom/MessagePortBinding.h" |
11 | | #include "mozilla/dom/ServiceWorker.h" |
12 | | |
13 | | #include "mozilla/HoldDropJSObjects.h" |
14 | | #include "jsapi.h" |
15 | | #include "nsGlobalWindow.h" // So we can assign an nsGlobalWindow* to mWindowSource |
16 | | |
17 | | namespace mozilla { |
18 | | namespace dom { |
19 | | |
20 | | NS_IMPL_CYCLE_COLLECTION_CLASS(MessageEvent) |
21 | | |
22 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessageEvent, Event) |
23 | 0 | tmp->mData.setUndefined(); |
24 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindowSource) |
25 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mPortSource) |
26 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mServiceWorkerSource) |
27 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mPorts) |
28 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
29 | | |
30 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessageEvent, Event) |
31 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindowSource) |
32 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPortSource) |
33 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mServiceWorkerSource) |
34 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPorts) |
35 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
36 | | |
37 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MessageEvent, Event) |
38 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData) |
39 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
40 | | |
41 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageEvent) |
42 | 0 | NS_INTERFACE_MAP_END_INHERITING(Event) |
43 | | |
44 | | NS_IMPL_ADDREF_INHERITED(MessageEvent, Event) |
45 | | NS_IMPL_RELEASE_INHERITED(MessageEvent, Event) |
46 | | |
47 | | MessageEvent::MessageEvent(EventTarget* aOwner, |
48 | | nsPresContext* aPresContext, |
49 | | WidgetEvent* aEvent) |
50 | | : Event(aOwner, aPresContext, aEvent) |
51 | | , mData(JS::UndefinedValue()) |
52 | 0 | { |
53 | 0 | } |
54 | | |
55 | | MessageEvent::~MessageEvent() |
56 | 0 | { |
57 | 0 | mData.setUndefined(); |
58 | 0 | DropJSObjects(this); |
59 | 0 | } |
60 | | |
61 | | JSObject* |
62 | | MessageEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
63 | 0 | { |
64 | 0 | return mozilla::dom::MessageEvent_Binding::Wrap(aCx, this, aGivenProto); |
65 | 0 | } |
66 | | |
67 | | void |
68 | | MessageEvent::GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData, |
69 | | ErrorResult& aRv) |
70 | 0 | { |
71 | 0 | aData.set(mData); |
72 | 0 | if (!JS_WrapValue(aCx, aData)) { |
73 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | void |
78 | | MessageEvent::GetOrigin(nsAString& aOrigin) const |
79 | 0 | { |
80 | 0 | aOrigin = mOrigin; |
81 | 0 | } |
82 | | |
83 | | void |
84 | | MessageEvent::GetLastEventId(nsAString& aLastEventId) const |
85 | 0 | { |
86 | 0 | aLastEventId = mLastEventId; |
87 | 0 | } |
88 | | |
89 | | void |
90 | | MessageEvent::GetSource(Nullable<OwningWindowProxyOrMessagePortOrServiceWorker>& aValue) const |
91 | 0 | { |
92 | 0 | if (mWindowSource) { |
93 | 0 | aValue.SetValue().SetAsWindowProxy() = mWindowSource; |
94 | 0 | } else if (mPortSource) { |
95 | 0 | aValue.SetValue().SetAsMessagePort() = mPortSource; |
96 | 0 | } else if (mServiceWorkerSource) { |
97 | 0 | aValue.SetValue().SetAsServiceWorker() = mServiceWorkerSource; |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | /* static */ already_AddRefed<MessageEvent> |
102 | | MessageEvent::Constructor(const GlobalObject& aGlobal, |
103 | | const nsAString& aType, |
104 | | const MessageEventInit& aParam, |
105 | | ErrorResult& aRv) |
106 | 0 | { |
107 | 0 | nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports()); |
108 | 0 | return Constructor(t, aType, aParam); |
109 | 0 | } |
110 | | |
111 | | /* static */ already_AddRefed<MessageEvent> |
112 | | MessageEvent::Constructor(EventTarget* aEventTarget, |
113 | | const nsAString& aType, |
114 | | const MessageEventInit& aParam) |
115 | 0 | { |
116 | 0 | RefPtr<MessageEvent> event = new MessageEvent(aEventTarget, nullptr, nullptr); |
117 | 0 |
|
118 | 0 | event->InitEvent(aType, aParam.mBubbles, aParam.mCancelable); |
119 | 0 | bool trusted = event->Init(aEventTarget); |
120 | 0 | event->SetTrusted(trusted); |
121 | 0 |
|
122 | 0 | event->mData = aParam.mData; |
123 | 0 |
|
124 | 0 | mozilla::HoldJSObjects(event.get()); |
125 | 0 |
|
126 | 0 | event->mOrigin = aParam.mOrigin; |
127 | 0 | event->mLastEventId = aParam.mLastEventId; |
128 | 0 |
|
129 | 0 | if (!aParam.mSource.IsNull()) { |
130 | 0 | if (aParam.mSource.Value().IsWindowProxy()) { |
131 | 0 | event->mWindowSource = aParam.mSource.Value().GetAsWindowProxy(); |
132 | 0 | } else if (aParam.mSource.Value().IsMessagePort()) { |
133 | 0 | event->mPortSource = aParam.mSource.Value().GetAsMessagePort(); |
134 | 0 | } else { |
135 | 0 | event->mServiceWorkerSource = aParam.mSource.Value().GetAsServiceWorker(); |
136 | 0 | } |
137 | 0 |
|
138 | 0 | MOZ_ASSERT(event->mWindowSource || event->mPortSource || event->mServiceWorkerSource); |
139 | 0 | } |
140 | 0 |
|
141 | 0 | event->mPorts.AppendElements(aParam.mPorts); |
142 | 0 |
|
143 | 0 | return event.forget(); |
144 | 0 | } |
145 | | |
146 | | void |
147 | | MessageEvent::InitMessageEvent(JSContext* aCx, const nsAString& aType, |
148 | | mozilla::CanBubble aCanBubble, |
149 | | mozilla::Cancelable aCancelable, |
150 | | JS::Handle<JS::Value> aData, |
151 | | const nsAString& aOrigin, |
152 | | const nsAString& aLastEventId, |
153 | | const Nullable<WindowProxyOrMessagePortOrServiceWorker>& aSource, |
154 | | const Sequence<OwningNonNull<MessagePort>>& aPorts) |
155 | 0 | { |
156 | 0 | NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched); |
157 | 0 |
|
158 | 0 | Event::InitEvent(aType, aCanBubble, aCancelable); |
159 | 0 | mData = aData; |
160 | 0 | mozilla::HoldJSObjects(this); |
161 | 0 | mOrigin = aOrigin; |
162 | 0 | mLastEventId = aLastEventId; |
163 | 0 |
|
164 | 0 | mWindowSource = nullptr; |
165 | 0 | mPortSource = nullptr; |
166 | 0 | mServiceWorkerSource = nullptr; |
167 | 0 |
|
168 | 0 | if (!aSource.IsNull()) { |
169 | 0 | if (aSource.Value().IsWindowProxy()) { |
170 | 0 | mWindowSource = aSource.Value().GetAsWindowProxy(); |
171 | 0 | } else if (aSource.Value().IsMessagePort()) { |
172 | 0 | mPortSource = &aSource.Value().GetAsMessagePort(); |
173 | 0 | } else { |
174 | 0 | mServiceWorkerSource = &aSource.Value().GetAsServiceWorker(); |
175 | 0 | } |
176 | 0 | } |
177 | 0 |
|
178 | 0 | mPorts.Clear(); |
179 | 0 | mPorts.AppendElements(aPorts); |
180 | 0 | MessageEvent_Binding::ClearCachedPortsValue(this); |
181 | 0 | } |
182 | | |
183 | | void |
184 | | MessageEvent::GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts) |
185 | 0 | { |
186 | 0 | aPorts = mPorts; |
187 | 0 | } |
188 | | |
189 | | } // namespace dom |
190 | | } // namespace mozilla |