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_BroadcastChannel_h 8 #define mozilla_dom_BroadcastChannel_h 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "nsAutoPtr.h" 13 #include "nsIIPCBackgroundChildCreateCallback.h" 14 #include "nsIObserver.h" 15 #include "nsTArray.h" 16 #include "mozilla/RefPtr.h" 17 18 class nsPIDOMWindowInner; 19 20 namespace mozilla { 21 22 namespace ipc { 23 class PrincipalInfo; 24 } // namespace ipc 25 26 namespace dom { 27 28 namespace workers { 29 class WorkerHolder; 30 } // namespace workers 31 32 class BroadcastChannelChild; 33 class BroadcastChannelMessage; 34 35 class BroadcastChannel final 36 : public DOMEventTargetHelper 37 , public nsIIPCBackgroundChildCreateCallback 38 , public nsIObserver 39 { 40 friend class BroadcastChannelChild; 41 42 NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK 43 NS_DECL_NSIOBSERVER 44 45 typedef mozilla::ipc::PrincipalInfo PrincipalInfo; 46 47 public: 48 NS_DECL_ISUPPORTS_INHERITED 49 50 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BroadcastChannel, 51 DOMEventTargetHelper) 52 53 virtual JSObject* 54 WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; 55 56 static already_AddRefed<BroadcastChannel> 57 Constructor(const GlobalObject& aGlobal, const nsAString& aChannel, 58 ErrorResult& aRv); 59 GetName(nsAString & aName)60 void GetName(nsAString& aName) const 61 { 62 aName = mChannel; 63 } 64 65 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, 66 ErrorResult& aRv); 67 68 void Close(); 69 70 EventHandlerNonNull* GetOnmessage(); 71 void SetOnmessage(EventHandlerNonNull* aCallback); 72 73 using nsIDOMEventTarget::AddEventListener; 74 using nsIDOMEventTarget::RemoveEventListener; 75 76 virtual void AddEventListener(const nsAString& aType, 77 EventListener* aCallback, 78 const AddEventListenerOptionsOrBoolean& aOptions, 79 const Nullable<bool>& aWantsUntrusted, 80 ErrorResult& aRv) override; 81 virtual void RemoveEventListener(const nsAString& aType, 82 EventListener* aCallback, 83 const EventListenerOptionsOrBoolean& aOptions, 84 ErrorResult& aRv) override; 85 86 void Shutdown(); 87 88 private: 89 BroadcastChannel(nsPIDOMWindowInner* aWindow, 90 const PrincipalInfo& aPrincipalInfo, 91 const nsACString& aOrigin, 92 const nsAString& aChannel); 93 94 ~BroadcastChannel(); 95 96 void PostMessageData(BroadcastChannelMessage* aData); 97 98 void PostMessageInternal(JSContext* aCx, JS::Handle<JS::Value> aMessage, 99 ErrorResult& aRv); 100 101 void UpdateMustKeepAlive(); 102 IsCertainlyAliveForCC()103 bool IsCertainlyAliveForCC() const override 104 { 105 return mIsKeptAlive; 106 } 107 108 void RemoveDocFromBFCache(); 109 110 RefPtr<BroadcastChannelChild> mActor; 111 nsTArray<RefPtr<BroadcastChannelMessage>> mPendingMessages; 112 113 nsAutoPtr<workers::WorkerHolder> mWorkerHolder; 114 115 nsAutoPtr<PrincipalInfo> mPrincipalInfo; 116 117 nsCString mOrigin; 118 nsString mChannel; 119 120 bool mIsKeptAlive; 121 122 uint64_t mInnerID; 123 124 enum { 125 StateActive, 126 StateClosing, 127 StateClosed 128 } mState; 129 }; 130 131 } // namespace dom 132 } // namespace mozilla 133 134 #endif // mozilla_dom_BroadcastChannel_h 135