Bug 949376 - MessageEvent::initMessageEvent, r=smaug

This commit is contained in:
Andrea Marchesini 2015-10-07 20:35:11 +01:00
parent e07aa7aa5f
commit 31da9d5c53
5 changed files with 87 additions and 16 deletions

View File

@ -199,6 +199,49 @@ MessageEvent::InitMessageEvent(const nsAString& aType,
return NS_OK;
}
void
MessageEvent::InitMessageEvent(JSContext* aCx, const nsAString& aType,
bool aCanBubble, bool aCancelable,
JS::Handle<JS::Value> aData,
const nsAString& aOrigin,
const nsAString& aLastEventId,
const Nullable<WindowProxyOrMessagePort>& aSource,
const Nullable<Sequence<OwningNonNull<MessagePort>>>& aPorts,
ErrorResult& aRv)
{
aRv = Event::InitEvent(aType, aCanBubble, aCancelable);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
mData = aData;
mozilla::HoldJSObjects(this);
mOrigin = aOrigin;
mLastEventId = aLastEventId;
mWindowSource = nullptr;
mPortSource = nullptr;
if (!aSource.IsNull()) {
if (aSource.Value().IsWindowProxy()) {
mWindowSource = aSource.Value().GetAsWindowProxy();
} else {
mPortSource = &aSource.Value().GetAsMessagePort();
}
}
mPorts = nullptr;
if (!aPorts.IsNull()) {
nsTArray<nsRefPtr<MessagePort>> ports;
for (uint32_t i = 0, len = aPorts.Value().Length(); i < len; ++i) {
ports.AppendElement(aPorts.Value()[i]);
}
mPorts = new MessagePortList(static_cast<Event*>(this), ports);
}
}
void
MessageEvent::SetPorts(MessagePortList* aPorts)
{
@ -227,7 +270,7 @@ using namespace mozilla::dom;
already_AddRefed<MessageEvent>
NS_NewDOMMessageEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetEvent* aEvent)
WidgetEvent* aEvent)
{
nsRefPtr<MessageEvent> it = new MessageEvent(aOwner, aPresContext, aEvent);
return it.forget();

View File

@ -8,9 +8,10 @@
#define mozilla_dom_MessageEvent_h_
#include "mozilla/dom/Event.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/MessagePortList.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIDOMMessageEvent.h"
#include "mozilla/dom/MessagePortList.h"
namespace mozilla {
namespace dom {
@ -19,6 +20,7 @@ struct MessageEventInit;
class MessagePort;
class MessagePortList;
class OwningWindowProxyOrMessagePortOrClient;
class WindowProxyOrMessagePort;
namespace workers {
@ -85,6 +87,13 @@ public:
const MessageEventInit& aEventInit,
ErrorResult& aRv);
void InitMessageEvent(JSContext* aCx, const nsAString& aType, bool aCanBubble,
bool aCancelable, JS::Handle<JS::Value> aData,
const nsAString& aOrigin, const nsAString& aLastEventId,
const Nullable<WindowProxyOrMessagePort>& aSource,
const Nullable<Sequence<OwningNonNull<MessagePort>>>& aPorts,
ErrorResult& aRv);
protected:
~MessageEvent();

View File

@ -13,6 +13,23 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=848294
</head>
<body>
<script type="application/javascript">
function testMessageEvent(e, test) {
ok(e, "MessageEvent created");
is(e.type, 'message', 'MessageEvent.type is right');
is(e.data, 'data' in test ? test.data : undefined, 'MessageEvent.data is ok');
is(e.origin, 'origin' in test ? test.origin : '', 'MessageEvent.origin is ok');
is(e.lastEventId, 'lastEventId' in test ? test.lastEventId : '', 'MessageEvent.lastEventId is ok');
is(e.source, 'source' in test ? test.source : null, 'MessageEvent.source is ok');
if (test.ports != undefined) {
is(e.ports.length, test.ports.length, 'MessageEvent.ports is ok');
is(e.ports, e.ports, 'MessageEvent.ports is ok');
} else {
ok(!('ports' in test) || test.ports == null, 'MessageEvent.ports is ok');
}
}
function runTest() {
var channel = new MessageChannel();
@ -33,20 +50,16 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=848294
var test = tests.shift();
var e = new MessageEvent('message', test);
ok(e, "MessageEvent created");
is(e.type, 'message', 'MessageEvent.type is right');
testMessageEvent(e, test);
is(e.data, 'data' in test ? test.data : undefined, 'MessageEvent.data is ok');
is(e.origin, 'origin' in test ? test.origin : '', 'MessageEvent.origin is ok');
is(e.lastEventId, 'lastEventId' in test ? test.lastEventId : '', 'MessageEvent.lastEventId is ok');
is(e.source, 'source' in test ? test.source : null, 'MessageEvent.source is ok');
if (test.ports != undefined) {
is(e.ports.length, test.ports.length, 'MessageEvent.ports is ok');
is(e.ports, e.ports, 'MessageEvent.ports is ok');
} else {
ok(!('ports' in test) || test.ports == null, 'MessageEvent.ports is ok');
}
e = new MessageEvent('message');
e.initMessageEvent('message', true, true,
'data' in test ? test.data : undefined,
'origin' in test ? test.origin : '',
'lastEventId' in test ? test.lastEventId : '',
'source' in test ? test.source : null,
'ports' in test ? test.ports : null);
testMessageEvent(e, test);
}
try {

View File

@ -66,7 +66,7 @@ public:
return mPorts[aIndex];
}
public:
private:
nsCOMPtr<nsISupports> mOwner;
nsTArray<nsRefPtr<MessagePort>> mPorts;
};

View File

@ -44,6 +44,12 @@ interface MessageEvent : Event {
* data, origin, source, and lastEventId attributes of this appropriately.
*/
readonly attribute MessagePortList? ports;
[Throws]
void initMessageEvent(DOMString type, boolean bubbles, boolean cancelable,
any data, DOMString origin, DOMString lastEventId,
(WindowProxy or MessagePort)? source,
sequence<MessagePort>? ports);
};
dictionary MessageEventInit : EventInit {