1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_MessageEvent_h_
8 #define mozilla_dom_MessageEvent_h_
9 
10 #include "js/RootingAPI.h"
11 #include "js/Value.h"
12 #include "mozilla/AlreadyAddRefed.h"
13 #include "mozilla/Assertions.h"
14 #include "mozilla/BasicEvents.h"
15 #include "mozilla/RefPtr.h"
16 #include "mozilla/dom/Event.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsISupports.h"
19 #include "nsStringFwd.h"
20 #include "nsTArray.h"
21 
22 namespace mozilla {
23 namespace dom {
24 
25 class BrowsingContext;
26 struct MessageEventInit;
27 class MessagePort;
28 class OwningWindowProxyOrMessagePortOrServiceWorker;
29 class ServiceWorker;
30 class WindowProxyOrMessagePortOrServiceWorker;
31 
32 /**
33  * Implements the MessageEvent event, used for cross-document messaging and
34  * server-sent events.
35  *
36  * See http://www.whatwg.org/specs/web-apps/current-work/#messageevent for
37  * further details.
38  */
39 class MessageEvent final : public Event {
40  public:
41   MessageEvent(EventTarget* aOwner, nsPresContext* aPresContext,
42                WidgetEvent* aEvent);
43 
44   NS_DECL_ISUPPORTS_INHERITED
45   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MessageEvent, Event)
46 
47   virtual JSObject* WrapObjectInternal(
48       JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
49 
50   void GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
51                ErrorResult& aRv);
52   void GetOrigin(nsAString&) const;
53   void GetLastEventId(nsAString&) const;
54   void GetSource(
55       Nullable<OwningWindowProxyOrMessagePortOrServiceWorker>& aValue) const;
56 
57   void GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts);
58 
59   static already_AddRefed<MessageEvent> Constructor(
60       const GlobalObject& aGlobal, const nsAString& aType,
61       const MessageEventInit& aEventInit);
62 
63   static already_AddRefed<MessageEvent> Constructor(
64       EventTarget* aEventTarget, const nsAString& aType,
65       const MessageEventInit& aEventInit);
66 
InitMessageEvent(JSContext * aCx,const nsAString & aType,bool aCanBubble,bool aCancelable,JS::Handle<JS::Value> aData,const nsAString & aOrigin,const nsAString & aLastEventId,const Nullable<WindowProxyOrMessagePortOrServiceWorker> & aSource,const Sequence<OwningNonNull<MessagePort>> & aPorts)67   void InitMessageEvent(
68       JSContext* aCx, const nsAString& aType, bool aCanBubble, bool aCancelable,
69       JS::Handle<JS::Value> aData, const nsAString& aOrigin,
70       const nsAString& aLastEventId,
71       const Nullable<WindowProxyOrMessagePortOrServiceWorker>& aSource,
72       const Sequence<OwningNonNull<MessagePort>>& aPorts) {
73     InitMessageEvent(aCx, aType, aCanBubble ? CanBubble::eYes : CanBubble::eNo,
74                      aCancelable ? Cancelable::eYes : Cancelable::eNo, aData,
75                      aOrigin, aLastEventId, aSource, aPorts);
76   }
77 
78   void InitMessageEvent(
79       JSContext* aCx, const nsAString& aType, mozilla::CanBubble,
80       mozilla::Cancelable, JS::Handle<JS::Value> aData,
81       const nsAString& aOrigin, const nsAString& aLastEventId,
82       const Nullable<WindowProxyOrMessagePortOrServiceWorker>& aSource,
83       const Sequence<OwningNonNull<MessagePort>>& aPorts);
84 
85  protected:
86   ~MessageEvent();
87 
88  private:
89   JS::Heap<JS::Value> mData;
90   nsString mOrigin;
91   nsString mLastEventId;
92   RefPtr<BrowsingContext> mWindowSource;
93   RefPtr<MessagePort> mPortSource;
94   RefPtr<ServiceWorker> mServiceWorkerSource;
95 
96   nsTArray<RefPtr<MessagePort>> mPorts;
97 };
98 
99 }  // namespace dom
100 }  // namespace mozilla
101 
102 #endif  // mozilla_dom_MessageEvent_h_
103