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 "mozilla/dom/Event.h"
11 #include "mozilla/dom/BindingUtils.h"
12 #include "nsCycleCollectionParticipant.h"
13 
14 namespace mozilla {
15 namespace dom {
16 
17 struct MessageEventInit;
18 class MessagePort;
19 class OwningWindowProxyOrMessagePort;
20 class WindowProxyOrMessagePort;
21 
22 /**
23  * Implements the MessageEvent event, used for cross-document messaging and
24  * server-sent events.
25  *
26  * See http://www.whatwg.org/specs/web-apps/current-work/#messageevent for
27  * further details.
28  */
29 class MessageEvent final : public Event
30 {
31 public:
32   MessageEvent(EventTarget* aOwner,
33                nsPresContext* aPresContext,
34                WidgetEvent* aEvent);
35 
36   NS_DECL_ISUPPORTS_INHERITED
37   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MessageEvent, Event)
38 
39   // Forward to base class
40   NS_FORWARD_TO_EVENT
41 
42   virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
43 
44   void GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
45                ErrorResult& aRv);
46   void GetOrigin(nsAString&) const;
47   void GetLastEventId(nsAString&) const;
48   void GetSource(Nullable<OwningWindowProxyOrMessagePort>& aValue) const;
49 
50   void GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts);
51 
52   static already_AddRefed<MessageEvent>
53   Constructor(const GlobalObject& aGlobal,
54               const nsAString& aType,
55               const MessageEventInit& aEventInit,
56               ErrorResult& aRv);
57 
58   static already_AddRefed<MessageEvent>
59   Constructor(EventTarget* aEventTarget,
60               const nsAString& aType,
61               const MessageEventInit& aEventInit,
62               ErrorResult& aRv);
63 
64   void InitMessageEvent(JSContext* aCx, const nsAString& aType, bool aCanBubble,
65                         bool aCancelable, JS::Handle<JS::Value> aData,
66                         const nsAString& aOrigin, const nsAString& aLastEventId,
67                         const Nullable<WindowProxyOrMessagePort>& aSource,
68                         const Sequence<OwningNonNull<MessagePort>>& aPorts);
69 
70 protected:
71   ~MessageEvent();
72 
73 private:
74   JS::Heap<JS::Value> mData;
75   nsString mOrigin;
76   nsString mLastEventId;
77   RefPtr<nsPIDOMWindowInner> mWindowSource;
78   RefPtr<MessagePort> mPortSource;
79 
80   nsTArray<RefPtr<MessagePort>> mPorts;
81 };
82 
83 } // namespace dom
84 } // namespace mozilla
85 
86 #endif // mozilla_dom_MessageEvent_h_
87