1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/MessageEvent.h"
7 #include "mozilla/dom/MessageEventBinding.h"
8 #include "mozilla/dom/MessagePort.h"
9 #include "mozilla/dom/MessagePortBinding.h"
10 #include "mozilla/dom/MessagePortList.h"
11 #include "mozilla/dom/UnionTypes.h"
13 #include "mozilla/HoldDropJSObjects.h"
19 NS_IMPL_CYCLE_COLLECTION_CLASS(MessageEvent
)
21 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessageEvent
, Event
)
22 tmp
->mData
= JSVAL_VOID
;
23 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindowSource
)
24 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPortSource
)
25 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPorts
)
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessageEvent
, Event
)
29 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindowSource
)
30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPortSource
)
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPorts
)
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
34 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MessageEvent
, Event
)
35 NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mData
)
36 NS_IMPL_CYCLE_COLLECTION_TRACE_END
38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessageEvent
)
39 NS_INTERFACE_MAP_ENTRY(nsIDOMMessageEvent
)
40 NS_INTERFACE_MAP_END_INHERITING(Event
)
42 NS_IMPL_ADDREF_INHERITED(MessageEvent
, Event
)
43 NS_IMPL_RELEASE_INHERITED(MessageEvent
, Event
)
45 MessageEvent::MessageEvent(EventTarget
* aOwner
,
46 nsPresContext
* aPresContext
,
48 : Event(aOwner
, aPresContext
, aEvent
)
53 MessageEvent::~MessageEvent()
60 MessageEvent::WrapObject(JSContext
* aCx
)
62 return mozilla::dom::MessageEventBinding::Wrap(aCx
, this);
66 MessageEvent::GetData(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aData
)
69 GetData(aCx
, aData
, rv
);
70 return rv
.ErrorCode();
74 MessageEvent::GetData(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aData
,
77 JS::ExposeValueToActiveJS(mData
);
79 if (!JS_WrapValue(aCx
, aData
)) {
80 aRv
.Throw(NS_ERROR_FAILURE
);
85 MessageEvent::GetOrigin(nsAString
& aOrigin
)
92 MessageEvent::GetLastEventId(nsAString
& aLastEventId
)
94 aLastEventId
= mLastEventId
;
99 MessageEvent::GetSource(nsIDOMWindow
** aSource
)
101 NS_IF_ADDREF(*aSource
= mWindowSource
);
106 MessageEvent::GetSource(Nullable
<OwningWindowProxyOrMessagePort
>& aValue
) const
109 aValue
.SetValue().SetAsWindowProxy() = mWindowSource
;
110 } else if (mPortSource
) {
111 aValue
.SetValue().SetAsMessagePort() = mPortSource
;
115 /* static */ already_AddRefed
<MessageEvent
>
116 MessageEvent::Constructor(const GlobalObject
& aGlobal
,
117 JSContext
* aCx
, const nsAString
& aType
,
118 const MessageEventInit
& aParam
,
121 nsCOMPtr
<EventTarget
> t
= do_QueryInterface(aGlobal
.GetAsSupports());
122 nsRefPtr
<MessageEvent
> event
= new MessageEvent(t
, nullptr, nullptr);
124 aRv
= event
->InitEvent(aType
, aParam
.mBubbles
, aParam
.mCancelable
);
129 bool trusted
= event
->Init(t
);
130 event
->SetTrusted(trusted
);
132 event
->mData
= aParam
.mData
;
134 mozilla::HoldJSObjects(event
.get());
136 if (aParam
.mOrigin
.WasPassed()) {
137 event
->mOrigin
= aParam
.mOrigin
.Value();
140 if (aParam
.mLastEventId
.WasPassed()) {
141 event
->mLastEventId
= aParam
.mLastEventId
.Value();
144 if (!aParam
.mSource
.IsNull()) {
145 if (aParam
.mSource
.Value().IsWindowProxy()) {
146 event
->mWindowSource
= aParam
.mSource
.Value().GetAsWindowProxy();
148 event
->mPortSource
= aParam
.mSource
.Value().GetAsMessagePort();
151 MOZ_ASSERT(event
->mWindowSource
|| event
->mPortSource
);
154 if (aParam
.mPorts
.WasPassed() && !aParam
.mPorts
.Value().IsNull()) {
155 nsTArray
<nsRefPtr
<MessagePortBase
>> ports
;
156 for (uint32_t i
= 0, len
= aParam
.mPorts
.Value().Value().Length(); i
< len
; ++i
) {
157 ports
.AppendElement(aParam
.mPorts
.Value().Value()[i
].get());
160 event
->mPorts
= new MessagePortList(static_cast<EventBase
*>(event
), ports
);
163 return event
.forget();
167 MessageEvent::InitMessageEvent(const nsAString
& aType
,
170 JS::Handle
<JS::Value
> aData
,
171 const nsAString
& aOrigin
,
172 const nsAString
& aLastEventId
,
173 nsIDOMWindow
* aSource
)
175 nsresult rv
= Event::InitEvent(aType
, aCanBubble
, aCancelable
);
176 NS_ENSURE_SUCCESS(rv
, rv
);
179 mozilla::HoldJSObjects(this);
181 mLastEventId
= aLastEventId
;
182 mWindowSource
= aSource
;
188 MessageEvent::SetPorts(MessagePortList
* aPorts
)
190 MOZ_ASSERT(!mPorts
&& aPorts
);
195 } // namespace mozilla
197 using namespace mozilla
;
198 using namespace mozilla::dom
;
201 NS_NewDOMMessageEvent(nsIDOMEvent
** aInstancePtrResult
,
203 nsPresContext
* aPresContext
,
206 MessageEvent
* it
= new MessageEvent(aOwner
, aPresContext
, aEvent
);
208 *aInstancePtrResult
= static_cast<Event
*>(it
);