2008-01-29 17:31:29 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
#include "nsDOMMessageEvent.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2013-09-11 07:10:01 -07:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
|
|
|
#include "mozilla/dom/MessagePortList.h"
|
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2013-08-16 13:10:17 -07:00
|
|
|
|
2013-09-10 08:29:43 -07:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2011-05-23 11:53:31 -07:00
|
|
|
#include "jsapi.h"
|
2008-01-29 17:31:29 -08:00
|
|
|
|
2013-05-02 02:12:45 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMMessageEvent)
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMMessageEvent, nsDOMEvent)
|
2013-03-18 08:50:37 -07:00
|
|
|
tmp->mData = JSVAL_VOID;
|
2013-09-11 07:10:01 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindowSource)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPortSource)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPorts)
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMMessageEvent, nsDOMEvent)
|
2013-09-11 07:10:01 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindowSource)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPortSource)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPorts)
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMMessageEvent, nsDOMEvent)
|
2012-06-10 16:44:50 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mData)
|
2011-05-23 11:53:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMMessageEvent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMessageEvent)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMMessageEvent, nsDOMEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMMessageEvent, nsDOMEvent)
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMMessageEvent::nsDOMMessageEvent(mozilla::dom::EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent* aEvent)
|
2013-03-09 03:34:29 -08:00
|
|
|
: nsDOMEvent(aOwner, aPresContext, aEvent),
|
2013-03-18 08:50:37 -07:00
|
|
|
mData(JSVAL_VOID)
|
2011-05-23 11:53:31 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMessageEvent::~nsDOMMessageEvent()
|
|
|
|
{
|
|
|
|
mData = JSVAL_VOID;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2011-05-23 11:53:31 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
JSObject*
|
|
|
|
nsDOMMessageEvent::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return mozilla::dom::MessageEventBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMETHODIMP
|
2013-03-16 16:39:51 -07:00
|
|
|
nsDOMMessageEvent::GetData(JSContext* aCx, JS::Value* aData)
|
2008-01-29 17:31:29 -08:00
|
|
|
{
|
2013-05-02 02:12:45 -07:00
|
|
|
ErrorResult rv;
|
|
|
|
*aData = GetData(aCx, rv);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
nsDOMMessageEvent::GetData(JSContext* aCx, ErrorResult& aRv)
|
|
|
|
{
|
2013-05-02 02:12:47 -07:00
|
|
|
JS::Rooted<JS::Value> data(aCx, mData);
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(aCx, &data)) {
|
2013-05-02 02:12:45 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
return data;
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-02-26 19:48:54 -08:00
|
|
|
nsDOMMessageEvent::GetOrigin(nsAString& aOrigin)
|
2008-01-29 17:31:29 -08:00
|
|
|
{
|
2008-02-26 19:48:54 -08:00
|
|
|
aOrigin = mOrigin;
|
2008-01-29 17:31:29 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMessageEvent::GetLastEventId(nsAString& aLastEventId)
|
|
|
|
{
|
|
|
|
aLastEventId = mLastEventId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMessageEvent::GetSource(nsIDOMWindow** aSource)
|
|
|
|
{
|
2013-09-11 07:10:01 -07:00
|
|
|
NS_IF_ADDREF(*aSource = mWindowSource);
|
2008-01-29 17:31:29 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:10:01 -07:00
|
|
|
void
|
2013-09-17 08:16:02 -07:00
|
|
|
nsDOMMessageEvent::GetSource(Nullable<mozilla::dom::OwningWindowProxyOrMessagePort>& aValue) const
|
2013-09-11 07:10:01 -07:00
|
|
|
{
|
|
|
|
if (mWindowSource) {
|
|
|
|
aValue.SetValue().SetAsWindowProxy() = mWindowSource;
|
|
|
|
} else if (mPortSource) {
|
|
|
|
aValue.SetValue().SetAsMessagePort() = mPortSource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsDOMMessageEvent>
|
|
|
|
nsDOMMessageEvent::Constructor(const mozilla::dom::GlobalObject& aGlobal,
|
|
|
|
JSContext* aCx, const nsAString& aType,
|
|
|
|
const mozilla::dom::MessageEventInit& aParam,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> t =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsRefPtr<nsDOMMessageEvent> event =
|
|
|
|
new nsDOMMessageEvent(t, nullptr, nullptr);
|
|
|
|
|
|
|
|
aRv = event->InitEvent(aType, aParam.mBubbles, aParam.mCancelable);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool trusted = event->Init(t);
|
|
|
|
event->SetTrusted(trusted);
|
|
|
|
|
|
|
|
if (aParam.mData.WasPassed()) {
|
|
|
|
event->mData = aParam.mData.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::HoldJSObjects(event.get());
|
|
|
|
|
|
|
|
if (aParam.mOrigin.WasPassed()) {
|
|
|
|
event->mOrigin = aParam.mOrigin.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aParam.mLastEventId.WasPassed()) {
|
|
|
|
event->mLastEventId = aParam.mLastEventId.Value();
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:51:03 -08:00
|
|
|
if (!aParam.mSource.IsNull()) {
|
|
|
|
if (aParam.mSource.Value().IsWindowProxy()) {
|
|
|
|
event->mWindowSource = aParam.mSource.Value().GetAsWindowProxy();
|
|
|
|
} else {
|
|
|
|
event->mPortSource = aParam.mSource.Value().GetAsMessagePort();
|
2013-09-11 07:10:01 -07:00
|
|
|
}
|
|
|
|
|
2013-12-20 00:51:03 -08:00
|
|
|
MOZ_ASSERT(event->mWindowSource || event->mPortSource);
|
2013-09-11 07:10:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aParam.mPorts.WasPassed() && !aParam.mPorts.Value().IsNull()) {
|
2013-11-05 06:16:26 -08:00
|
|
|
nsTArray<nsRefPtr<MessagePortBase>> ports;
|
2013-09-11 07:10:01 -07:00
|
|
|
for (uint32_t i = 0, len = aParam.mPorts.Value().Value().Length(); i < len; ++i) {
|
|
|
|
ports.AppendElement(aParam.mPorts.Value().Value()[i].get());
|
|
|
|
}
|
|
|
|
|
|
|
|
event->mPorts = new MessagePortList(static_cast<nsDOMEventBase*>(event),
|
|
|
|
ports);
|
|
|
|
}
|
|
|
|
|
|
|
|
return event.forget();
|
|
|
|
}
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMessageEvent::InitMessageEvent(const nsAString& aType,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
2013-03-16 16:39:51 -07:00
|
|
|
const JS::Value& aData,
|
2008-02-26 19:48:54 -08:00
|
|
|
const nsAString& aOrigin,
|
2008-05-02 12:26:47 -07:00
|
|
|
const nsAString& aLastEventId,
|
2008-01-29 17:31:29 -08:00
|
|
|
nsIDOMWindow* aSource)
|
|
|
|
{
|
|
|
|
nsresult rv = nsDOMEvent::InitEvent(aType, aCanBubble, aCancelable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mData = aData;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2008-02-26 19:48:54 -08:00
|
|
|
mOrigin = aOrigin;
|
2008-05-02 12:26:47 -07:00
|
|
|
mLastEventId = aLastEventId;
|
2013-09-11 07:10:01 -07:00
|
|
|
mWindowSource = aSource;
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
nsDOMMessageEvent::SetPorts(mozilla::dom::MessagePortList* aPorts)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mPorts && aPorts);
|
|
|
|
mPorts = aPorts;
|
|
|
|
}
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
nsresult
|
|
|
|
NS_NewDOMMessageEvent(nsIDOMEvent** aInstancePtrResult,
|
2013-03-09 03:34:29 -08:00
|
|
|
mozilla::dom::EventTarget* aOwner,
|
2008-01-29 17:31:29 -08:00
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent* aEvent)
|
2008-01-29 17:31:29 -08:00
|
|
|
{
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMMessageEvent* it = new nsDOMMessageEvent(aOwner, aPresContext, aEvent);
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
return CallQueryInterface(it, aInstancePtrResult);
|
|
|
|
}
|