dom/events/MessageEvent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/MessageEvent.h"
michael@0 7 #include "mozilla/dom/MessageEventBinding.h"
michael@0 8 #include "mozilla/dom/MessagePort.h"
michael@0 9 #include "mozilla/dom/MessagePortBinding.h"
michael@0 10 #include "mozilla/dom/MessagePortList.h"
michael@0 11 #include "mozilla/dom/UnionTypes.h"
michael@0 12
michael@0 13 #include "mozilla/HoldDropJSObjects.h"
michael@0 14 #include "jsapi.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace dom {
michael@0 18
michael@0 19 NS_IMPL_CYCLE_COLLECTION_CLASS(MessageEvent)
michael@0 20
michael@0 21 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessageEvent, Event)
michael@0 22 tmp->mData = JSVAL_VOID;
michael@0 23 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindowSource)
michael@0 24 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPortSource)
michael@0 25 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPorts)
michael@0 26 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 27
michael@0 28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessageEvent, Event)
michael@0 29 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindowSource)
michael@0 30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPortSource)
michael@0 31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPorts)
michael@0 32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 33
michael@0 34 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MessageEvent, Event)
michael@0 35 NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mData)
michael@0 36 NS_IMPL_CYCLE_COLLECTION_TRACE_END
michael@0 37
michael@0 38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessageEvent)
michael@0 39 NS_INTERFACE_MAP_ENTRY(nsIDOMMessageEvent)
michael@0 40 NS_INTERFACE_MAP_END_INHERITING(Event)
michael@0 41
michael@0 42 NS_IMPL_ADDREF_INHERITED(MessageEvent, Event)
michael@0 43 NS_IMPL_RELEASE_INHERITED(MessageEvent, Event)
michael@0 44
michael@0 45 MessageEvent::MessageEvent(EventTarget* aOwner,
michael@0 46 nsPresContext* aPresContext,
michael@0 47 WidgetEvent* aEvent)
michael@0 48 : Event(aOwner, aPresContext, aEvent)
michael@0 49 , mData(JSVAL_VOID)
michael@0 50 {
michael@0 51 }
michael@0 52
michael@0 53 MessageEvent::~MessageEvent()
michael@0 54 {
michael@0 55 mData = JSVAL_VOID;
michael@0 56 DropJSObjects(this);
michael@0 57 }
michael@0 58
michael@0 59 JSObject*
michael@0 60 MessageEvent::WrapObject(JSContext* aCx)
michael@0 61 {
michael@0 62 return mozilla::dom::MessageEventBinding::Wrap(aCx, this);
michael@0 63 }
michael@0 64
michael@0 65 NS_IMETHODIMP
michael@0 66 MessageEvent::GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData)
michael@0 67 {
michael@0 68 ErrorResult rv;
michael@0 69 GetData(aCx, aData, rv);
michael@0 70 return rv.ErrorCode();
michael@0 71 }
michael@0 72
michael@0 73 void
michael@0 74 MessageEvent::GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
michael@0 75 ErrorResult& aRv)
michael@0 76 {
michael@0 77 JS::ExposeValueToActiveJS(mData);
michael@0 78 aData.set(mData);
michael@0 79 if (!JS_WrapValue(aCx, aData)) {
michael@0 80 aRv.Throw(NS_ERROR_FAILURE);
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 NS_IMETHODIMP
michael@0 85 MessageEvent::GetOrigin(nsAString& aOrigin)
michael@0 86 {
michael@0 87 aOrigin = mOrigin;
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90
michael@0 91 NS_IMETHODIMP
michael@0 92 MessageEvent::GetLastEventId(nsAString& aLastEventId)
michael@0 93 {
michael@0 94 aLastEventId = mLastEventId;
michael@0 95 return NS_OK;
michael@0 96 }
michael@0 97
michael@0 98 NS_IMETHODIMP
michael@0 99 MessageEvent::GetSource(nsIDOMWindow** aSource)
michael@0 100 {
michael@0 101 NS_IF_ADDREF(*aSource = mWindowSource);
michael@0 102 return NS_OK;
michael@0 103 }
michael@0 104
michael@0 105 void
michael@0 106 MessageEvent::GetSource(Nullable<OwningWindowProxyOrMessagePort>& aValue) const
michael@0 107 {
michael@0 108 if (mWindowSource) {
michael@0 109 aValue.SetValue().SetAsWindowProxy() = mWindowSource;
michael@0 110 } else if (mPortSource) {
michael@0 111 aValue.SetValue().SetAsMessagePort() = mPortSource;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 /* static */ already_AddRefed<MessageEvent>
michael@0 116 MessageEvent::Constructor(const GlobalObject& aGlobal,
michael@0 117 JSContext* aCx, const nsAString& aType,
michael@0 118 const MessageEventInit& aParam,
michael@0 119 ErrorResult& aRv)
michael@0 120 {
michael@0 121 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
michael@0 122 nsRefPtr<MessageEvent> event = new MessageEvent(t, nullptr, nullptr);
michael@0 123
michael@0 124 aRv = event->InitEvent(aType, aParam.mBubbles, aParam.mCancelable);
michael@0 125 if (aRv.Failed()) {
michael@0 126 return nullptr;
michael@0 127 }
michael@0 128
michael@0 129 bool trusted = event->Init(t);
michael@0 130 event->SetTrusted(trusted);
michael@0 131
michael@0 132 event->mData = aParam.mData;
michael@0 133
michael@0 134 mozilla::HoldJSObjects(event.get());
michael@0 135
michael@0 136 if (aParam.mOrigin.WasPassed()) {
michael@0 137 event->mOrigin = aParam.mOrigin.Value();
michael@0 138 }
michael@0 139
michael@0 140 if (aParam.mLastEventId.WasPassed()) {
michael@0 141 event->mLastEventId = aParam.mLastEventId.Value();
michael@0 142 }
michael@0 143
michael@0 144 if (!aParam.mSource.IsNull()) {
michael@0 145 if (aParam.mSource.Value().IsWindowProxy()) {
michael@0 146 event->mWindowSource = aParam.mSource.Value().GetAsWindowProxy();
michael@0 147 } else {
michael@0 148 event->mPortSource = aParam.mSource.Value().GetAsMessagePort();
michael@0 149 }
michael@0 150
michael@0 151 MOZ_ASSERT(event->mWindowSource || event->mPortSource);
michael@0 152 }
michael@0 153
michael@0 154 if (aParam.mPorts.WasPassed() && !aParam.mPorts.Value().IsNull()) {
michael@0 155 nsTArray<nsRefPtr<MessagePortBase>> ports;
michael@0 156 for (uint32_t i = 0, len = aParam.mPorts.Value().Value().Length(); i < len; ++i) {
michael@0 157 ports.AppendElement(aParam.mPorts.Value().Value()[i].get());
michael@0 158 }
michael@0 159
michael@0 160 event->mPorts = new MessagePortList(static_cast<EventBase*>(event), ports);
michael@0 161 }
michael@0 162
michael@0 163 return event.forget();
michael@0 164 }
michael@0 165
michael@0 166 NS_IMETHODIMP
michael@0 167 MessageEvent::InitMessageEvent(const nsAString& aType,
michael@0 168 bool aCanBubble,
michael@0 169 bool aCancelable,
michael@0 170 JS::Handle<JS::Value> aData,
michael@0 171 const nsAString& aOrigin,
michael@0 172 const nsAString& aLastEventId,
michael@0 173 nsIDOMWindow* aSource)
michael@0 174 {
michael@0 175 nsresult rv = Event::InitEvent(aType, aCanBubble, aCancelable);
michael@0 176 NS_ENSURE_SUCCESS(rv, rv);
michael@0 177
michael@0 178 mData = aData;
michael@0 179 mozilla::HoldJSObjects(this);
michael@0 180 mOrigin = aOrigin;
michael@0 181 mLastEventId = aLastEventId;
michael@0 182 mWindowSource = aSource;
michael@0 183
michael@0 184 return NS_OK;
michael@0 185 }
michael@0 186
michael@0 187 void
michael@0 188 MessageEvent::SetPorts(MessagePortList* aPorts)
michael@0 189 {
michael@0 190 MOZ_ASSERT(!mPorts && aPorts);
michael@0 191 mPorts = aPorts;
michael@0 192 }
michael@0 193
michael@0 194 } // namespace dom
michael@0 195 } // namespace mozilla
michael@0 196
michael@0 197 using namespace mozilla;
michael@0 198 using namespace mozilla::dom;
michael@0 199
michael@0 200 nsresult
michael@0 201 NS_NewDOMMessageEvent(nsIDOMEvent** aInstancePtrResult,
michael@0 202 EventTarget* aOwner,
michael@0 203 nsPresContext* aPresContext,
michael@0 204 WidgetEvent* aEvent)
michael@0 205 {
michael@0 206 MessageEvent* it = new MessageEvent(aOwner, aPresContext, aEvent);
michael@0 207 NS_ADDREF(it);
michael@0 208 *aInstancePtrResult = static_cast<Event*>(it);
michael@0 209 return NS_OK;
michael@0 210 }

mercurial