Attachment #8845514: P1 Update the MessageEvent webidl and implementation class. r=baku for bug #1311324

View | Details | Raw Unified | Return to bug 1311324
Collapse All | Expand All

(-)a/dom/base/PostMessageEvent.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 140-156   PostMessageEvent::Run() Link Here 
140
  }
140
  }
141
141
142
  // Create the event
142
  // Create the event
143
  nsCOMPtr<mozilla::dom::EventTarget> eventTarget = do_QueryObject(targetWindow);
143
  nsCOMPtr<mozilla::dom::EventTarget> eventTarget = do_QueryObject(targetWindow);
144
  RefPtr<MessageEvent> event =
144
  RefPtr<MessageEvent> event =
145
    new MessageEvent(eventTarget, nullptr, nullptr);
145
    new MessageEvent(eventTarget, nullptr, nullptr);
146
146
147
147
148
  Nullable<WindowProxyOrMessagePort> source;
148
  Nullable<WindowProxyOrMessagePortOrServiceWorker> source;
149
  source.SetValue().SetAsWindowProxy() = mSource ? mSource->AsOuter() : nullptr;
149
  source.SetValue().SetAsWindowProxy() = mSource ? mSource->AsOuter() : nullptr;
150
150
151
  Sequence<OwningNonNull<MessagePort>> ports;
151
  Sequence<OwningNonNull<MessagePort>> ports;
152
  if (!TakeTransferredPortsAsSequence(ports)) {
152
  if (!TakeTransferredPortsAsSequence(ports)) {
153
    return NS_ERROR_OUT_OF_MEMORY;
153
    return NS_ERROR_OUT_OF_MEMORY;
154
  }
154
  }
155
155
156
  event->InitMessageEvent(nullptr, NS_LITERAL_STRING("message"),
156
  event->InitMessageEvent(nullptr, NS_LITERAL_STRING("message"),
(-)a/dom/events/MessageEvent.cpp (-6 / +13 lines)
Line     Link Here 
 Lines 3-18    Link Here 
3
/* This Source Code Form is subject to the terms of the Mozilla Public
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
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/. */
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
6
7
#include "mozilla/dom/MessageEvent.h"
7
#include "mozilla/dom/MessageEvent.h"
8
#include "mozilla/dom/MessageEventBinding.h"
8
#include "mozilla/dom/MessageEventBinding.h"
9
#include "mozilla/dom/MessagePort.h"
9
#include "mozilla/dom/MessagePort.h"
10
#include "mozilla/dom/MessagePortBinding.h"
10
#include "mozilla/dom/MessagePortBinding.h"
11
#include "mozilla/dom/workers/bindings/ServiceWorker.h"
11
12
12
#include "mozilla/HoldDropJSObjects.h"
13
#include "mozilla/HoldDropJSObjects.h"
13
#include "jsapi.h"
14
#include "jsapi.h"
14
#include "nsGlobalWindow.h" // So we can assign an nsGlobalWindow* to mWindowSource
15
#include "nsGlobalWindow.h" // So we can assign an nsGlobalWindow* to mWindowSource
15
16
16
namespace mozilla {
17
namespace mozilla {
17
namespace dom {
18
namespace dom {
18
19
 Lines 79-100   MessageEvent::GetOrigin(nsAString& aOrig Link Here 
79
80
80
void
81
void
81
MessageEvent::GetLastEventId(nsAString& aLastEventId) const
82
MessageEvent::GetLastEventId(nsAString& aLastEventId) const
82
{
83
{
83
  aLastEventId = mLastEventId;
84
  aLastEventId = mLastEventId;
84
}
85
}
85
86
86
void
87
void
87
MessageEvent::GetSource(Nullable<OwningWindowProxyOrMessagePort>& aValue) const
88
MessageEvent::GetSource(Nullable<OwningWindowProxyOrMessagePortOrServiceWorker>& aValue) const
88
{
89
{
89
  if (mWindowSource) {
90
  if (mWindowSource) {
90
    aValue.SetValue().SetAsWindowProxy() = mWindowSource->GetOuterWindow();
91
    aValue.SetValue().SetAsWindowProxy() = mWindowSource->GetOuterWindow();
91
  } else if (mPortSource) {
92
  } else if (mPortSource) {
92
    aValue.SetValue().SetAsMessagePort() = mPortSource;
93
    aValue.SetValue().SetAsMessagePort() = mPortSource;
94
  } else if (mServiceWorker) {
95
    aValue.SetValue().SetAsServiceWorker() = mServiceWorker;
93
  }
96
  }
94
}
97
}
95
98
96
/* static */ already_AddRefed<MessageEvent>
99
/* static */ already_AddRefed<MessageEvent>
97
MessageEvent::Constructor(const GlobalObject& aGlobal,
100
MessageEvent::Constructor(const GlobalObject& aGlobal,
98
                          const nsAString& aType,
101
                          const nsAString& aType,
99
                          const MessageEventInit& aParam,
102
                          const MessageEventInit& aParam,
100
                          ErrorResult& aRv)
103
                          ErrorResult& aRv)
 Lines 118-154   MessageEvent::Constructor(EventTarget* a Link Here 
118
  event->mData = aParam.mData;
121
  event->mData = aParam.mData;
119
122
120
  mozilla::HoldJSObjects(event.get());
123
  mozilla::HoldJSObjects(event.get());
121
124
122
  event->mOrigin = aParam.mOrigin;
125
  event->mOrigin = aParam.mOrigin;
123
  event->mLastEventId = aParam.mLastEventId;
126
  event->mLastEventId = aParam.mLastEventId;
124
127
125
  if (!aParam.mSource.IsNull()) {
128
  if (!aParam.mSource.IsNull()) {
126
    if (aParam.mSource.Value().IsWindow()) {
129
    if (aParam.mSource.Value().IsWindowProxy()) {
127
      event->mWindowSource = aParam.mSource.Value().GetAsWindow()->AsInner();
130
      event->mWindowSource = aParam.mSource.Value().GetAsWindowProxy()->AsInner();
131
    } else if (aParam.mSource.Value().IsMessagePort()) {
132
      event->mPortSource = aParam.mSource.Value().GetAsMessagePort();
128
    } else {
133
    } else {
129
      event->mPortSource = aParam.mSource.Value().GetAsMessagePort();
134
      event->mServiceWorker = aParam.mSource.Value().GetAsServiceWorker();
130
    }
135
    }
131
136
132
    MOZ_ASSERT(event->mWindowSource || event->mPortSource);
137
    MOZ_ASSERT(event->mWindowSource || event->mPortSource);
133
  }
138
  }
134
139
135
  event->mPorts.AppendElements(aParam.mPorts);
140
  event->mPorts.AppendElements(aParam.mPorts);
136
141
137
  return event.forget();
142
  return event.forget();
138
}
143
}
139
144
140
void
145
void
141
MessageEvent::InitMessageEvent(JSContext* aCx, const nsAString& aType,
146
MessageEvent::InitMessageEvent(JSContext* aCx, const nsAString& aType,
142
                               bool aCanBubble, bool aCancelable,
147
                               bool aCanBubble, bool aCancelable,
143
                               JS::Handle<JS::Value> aData,
148
                               JS::Handle<JS::Value> aData,
144
                               const nsAString& aOrigin,
149
                               const nsAString& aOrigin,
145
                               const nsAString& aLastEventId,
150
                               const nsAString& aLastEventId,
146
                               const Nullable<WindowProxyOrMessagePort>& aSource,
151
                               const Nullable<WindowProxyOrMessagePortOrServiceWorker>& aSource,
147
                               const Sequence<OwningNonNull<MessagePort>>& aPorts)
152
                               const Sequence<OwningNonNull<MessagePort>>& aPorts)
148
{
153
{
149
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
154
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
150
155
151
  Event::InitEvent(aType, aCanBubble, aCancelable);
156
  Event::InitEvent(aType, aCanBubble, aCancelable);
152
  mData = aData;
157
  mData = aData;
153
  mozilla::HoldJSObjects(this);
158
  mozilla::HoldJSObjects(this);
154
  mOrigin = aOrigin;
159
  mOrigin = aOrigin;
 Lines 156-173   MessageEvent::InitMessageEvent(JSContext Link Here 
156
161
157
  mWindowSource = nullptr;
162
  mWindowSource = nullptr;
158
  mPortSource = nullptr;
163
  mPortSource = nullptr;
159
164
160
  if (!aSource.IsNull()) {
165
  if (!aSource.IsNull()) {
161
    if (aSource.Value().IsWindowProxy()) {
166
    if (aSource.Value().IsWindowProxy()) {
162
      auto* windowProxy = aSource.Value().GetAsWindowProxy();
167
      auto* windowProxy = aSource.Value().GetAsWindowProxy();
163
      mWindowSource = windowProxy ? windowProxy->GetCurrentInnerWindow() : nullptr;
168
      mWindowSource = windowProxy ? windowProxy->GetCurrentInnerWindow() : nullptr;
169
    } else if (aSource.Value().IsMessagePort()) {
170
      mPortSource = &aSource.Value().GetAsMessagePort();
164
    } else {
171
    } else {
165
      mPortSource = &aSource.Value().GetAsMessagePort();
172
      mServiceWorker = &aSource.Value().GetAsServiceWorker();
166
    }
173
    }
167
  }
174
  }
168
175
169
  mPorts.Clear();
176
  mPorts.Clear();
170
  mPorts.AppendElements(aPorts);
177
  mPorts.AppendElements(aPorts);
171
  MessageEventBinding::ClearCachedPortsValue(this);
178
  MessageEventBinding::ClearCachedPortsValue(this);
172
}
179
}
173
180
(-)a/dom/events/MessageEvent.h (-4 / +9 lines)
Line     Link Here 
 Lines 11-28    Link Here 
11
#include "mozilla/dom/BindingUtils.h"
11
#include "mozilla/dom/BindingUtils.h"
12
#include "nsCycleCollectionParticipant.h"
12
#include "nsCycleCollectionParticipant.h"
13
13
14
namespace mozilla {
14
namespace mozilla {
15
namespace dom {
15
namespace dom {
16
16
17
struct MessageEventInit;
17
struct MessageEventInit;
18
class MessagePort;
18
class MessagePort;
19
class OwningWindowProxyOrMessagePort;
19
class OwningWindowProxyOrMessagePortOrServiceWorker;
20
class WindowProxyOrMessagePort;
20
class WindowProxyOrMessagePortOrServiceWorker;
21
22
namespace workers {
23
class ServiceWorker;
24
}
21
25
22
/**
26
/**
23
 * Implements the MessageEvent event, used for cross-document messaging and
27
 * Implements the MessageEvent event, used for cross-document messaging and
24
 * server-sent events.
28
 * server-sent events.
25
 *
29
 *
26
 * See http://www.whatwg.org/specs/web-apps/current-work/#messageevent for
30
 * See http://www.whatwg.org/specs/web-apps/current-work/#messageevent for
27
 * further details.
31
 * further details.
28
 */
32
 */
 Lines 40-56   public: Link Here 
40
  NS_FORWARD_TO_EVENT
44
  NS_FORWARD_TO_EVENT
41
45
42
  virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
46
  virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
43
47
44
  void GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
48
  void GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
45
               ErrorResult& aRv);
49
               ErrorResult& aRv);
46
  void GetOrigin(nsAString&) const;
50
  void GetOrigin(nsAString&) const;
47
  void GetLastEventId(nsAString&) const;
51
  void GetLastEventId(nsAString&) const;
48
  void GetSource(Nullable<OwningWindowProxyOrMessagePort>& aValue) const;
52
  void GetSource(Nullable<OwningWindowProxyOrMessagePortOrServiceWorker>& aValue) const;
49
53
50
  void GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts);
54
  void GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts);
51
55
52
  static already_AddRefed<MessageEvent>
56
  static already_AddRefed<MessageEvent>
53
  Constructor(const GlobalObject& aGlobal,
57
  Constructor(const GlobalObject& aGlobal,
54
              const nsAString& aType,
58
              const nsAString& aType,
55
              const MessageEventInit& aEventInit,
59
              const MessageEventInit& aEventInit,
56
              ErrorResult& aRv);
60
              ErrorResult& aRv);
 Lines 59-86   public: Link Here 
59
  Constructor(EventTarget* aEventTarget,
63
  Constructor(EventTarget* aEventTarget,
60
              const nsAString& aType,
64
              const nsAString& aType,
61
              const MessageEventInit& aEventInit,
65
              const MessageEventInit& aEventInit,
62
              ErrorResult& aRv);
66
              ErrorResult& aRv);
63
67
64
  void InitMessageEvent(JSContext* aCx, const nsAString& aType, bool aCanBubble,
68
  void InitMessageEvent(JSContext* aCx, const nsAString& aType, bool aCanBubble,
65
                        bool aCancelable, JS::Handle<JS::Value> aData,
69
                        bool aCancelable, JS::Handle<JS::Value> aData,
66
                        const nsAString& aOrigin, const nsAString& aLastEventId,
70
                        const nsAString& aOrigin, const nsAString& aLastEventId,
67
                        const Nullable<WindowProxyOrMessagePort>& aSource,
71
                        const Nullable<WindowProxyOrMessagePortOrServiceWorker>& aSource,
68
                        const Sequence<OwningNonNull<MessagePort>>& aPorts);
72
                        const Sequence<OwningNonNull<MessagePort>>& aPorts);
69
73
70
protected:
74
protected:
71
  ~MessageEvent();
75
  ~MessageEvent();
72
76
73
private:
77
private:
74
  JS::Heap<JS::Value> mData;
78
  JS::Heap<JS::Value> mData;
75
  nsString mOrigin;
79
  nsString mOrigin;
76
  nsString mLastEventId;
80
  nsString mLastEventId;
77
  RefPtr<nsPIDOMWindowInner> mWindowSource;
81
  RefPtr<nsPIDOMWindowInner> mWindowSource;
78
  RefPtr<MessagePort> mPortSource;
82
  RefPtr<MessagePort> mPortSource;
83
  RefPtr<mozilla::dom::workers::ServiceWorker> mServiceWorker;
79
84
80
  nsTArray<RefPtr<MessagePort>> mPorts;
85
  nsTArray<RefPtr<MessagePort>> mPorts;
81
};
86
};
82
87
83
} // namespace dom
88
} // namespace dom
84
} // namespace mozilla
89
} // namespace mozilla
85
90
86
#endif // mozilla_dom_MessageEvent_h_
91
#endif // mozilla_dom_MessageEvent_h_
(-)a/dom/webidl/MessageEvent.webidl (-4 / +6 lines)
Line     Link Here 
 Lines 17-58   interface MessageEvent : Event { Link Here 
17
  readonly attribute any data;
17
  readonly attribute any data;
18
18
19
  /**
19
  /**
20
   * The origin of the site from which this event originated, which is the
20
   * The origin of the site from which this event originated, which is the
21
   * scheme, ":", and if the URI has a host, "//" followed by the
21
   * scheme, ":", and if the URI has a host, "//" followed by the
22
   * host, and if the port is not the default for the given scheme,
22
   * host, and if the port is not the default for the given scheme,
23
   * ":" followed by that port.  This value does not have a trailing slash.
23
   * ":" followed by that port.  This value does not have a trailing slash.
24
   */
24
   */
25
  readonly attribute DOMString origin;
25
  readonly attribute USVString origin;
26
26
27
  /**
27
  /**
28
   * The last event ID string of the event source, for server-sent DOM events; this
28
   * The last event ID string of the event source, for server-sent DOM events; this
29
   * value is the empty string for cross-origin messaging.
29
   * value is the empty string for cross-origin messaging.
30
   */
30
   */
31
  readonly attribute DOMString lastEventId;
31
  readonly attribute DOMString lastEventId;
32
32
33
  /**
33
  /**
34
   * The window or port which originated this event.
34
   * The window or port which originated this event.
35
   */
35
   */
36
  readonly attribute (WindowProxy or MessagePort)? source;
36
  readonly attribute MessageEventSource? source;
37
37
38
  /**
38
  /**
39
   * Initializes this event with the given data, in a manner analogous to
39
   * Initializes this event with the given data, in a manner analogous to
40
   * the similarly-named method on the nsIDOMEvent interface, also setting the
40
   * the similarly-named method on the nsIDOMEvent interface, also setting the
41
   * data, origin, source, and lastEventId attributes of this appropriately.
41
   * data, origin, source, and lastEventId attributes of this appropriately.
42
   */
42
   */
43
  [Pure, Cached, Frozen]
43
  [Pure, Cached, Frozen]
44
  readonly attribute sequence<MessagePort> ports;
44
  readonly attribute sequence<MessagePort> ports;
45
45
46
  void initMessageEvent(DOMString type, boolean bubbles, boolean cancelable,
46
  void initMessageEvent(DOMString type, boolean bubbles, boolean cancelable,
47
                        any data, DOMString origin, DOMString lastEventId,
47
                        any data, DOMString origin, DOMString lastEventId,
48
                        (WindowProxy or MessagePort)? source,
48
                        MessageEventSource? source,
49
                        sequence<MessagePort> ports);
49
                        sequence<MessagePort> ports);
50
};
50
};
51
51
52
dictionary MessageEventInit : EventInit {
52
dictionary MessageEventInit : EventInit {
53
  any data = null;
53
  any data = null;
54
  DOMString origin = "";
54
  DOMString origin = "";
55
  DOMString lastEventId = "";
55
  DOMString lastEventId = "";
56
  (Window or MessagePort)? source = null;
56
  MessageEventSource? source = null;
57
  sequence<MessagePort> ports = [];
57
  sequence<MessagePort> ports = [];
58
};
58
};
59
60
typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource;

Return to bug 1311324