MessageEvent.h (3457B)
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/BasicEvents.h" 14 #include "mozilla/RefPtr.h" 15 #include "mozilla/dom/Event.h" 16 #include "nsCycleCollectionParticipant.h" 17 #include "nsISupports.h" 18 #include "nsStringFwd.h" 19 #include "nsTArray.h" 20 21 namespace mozilla::dom { 22 23 class BrowsingContext; 24 struct MessageEventInit; 25 class MessagePort; 26 class OwningWindowProxyOrMessagePortOrServiceWorker; 27 class ServiceWorker; 28 class WindowProxyOrMessagePortOrServiceWorker; 29 30 /** 31 * Implements the MessageEvent event, used for cross-document messaging and 32 * server-sent events. 33 * 34 * See http://www.whatwg.org/specs/web-apps/current-work/#messageevent for 35 * further details. 36 */ 37 class MessageEvent final : public Event { 38 public: 39 MessageEvent(EventTarget* aOwner, nsPresContext* aPresContext, 40 WidgetEvent* aEvent); 41 42 NS_DECL_ISUPPORTS_INHERITED 43 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MessageEvent, Event) 44 45 JSObject* WrapObjectInternal(JSContext* aCx, 46 JS::Handle<JSObject*> aGivenProto) override; 47 48 MessageEvent* AsMessageEvent() override { return this; } 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 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 mozilla::dom 100 101 #endif // mozilla_dom_MessageEvent_h_