WebSocketEventService.h (4486B)
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_net_WebSocketEventService_h 8 #define mozilla_net_WebSocketEventService_h 9 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "mozilla/Atomics.h" 12 #include "nsIWebSocketEventService.h" 13 #include "nsCOMPtr.h" 14 #include "nsClassHashtable.h" 15 #include "nsHashKeys.h" 16 #include "nsIObserver.h" 17 #include "nsISupportsImpl.h" 18 #include "nsTArray.h" 19 #include "nsIWeakReferenceUtils.h" 20 #include "nsTHashMap.h" 21 22 class nsIWebSocketImpl; 23 class nsIEventTarget; 24 25 namespace mozilla { 26 namespace net { 27 28 class WebSocketFrame; 29 class WebSocketEventListenerChild; 30 31 class WebSocketEventService final : public nsIWebSocketEventService, 32 public nsIObserver { 33 friend class WebSocketBaseRunnable; 34 35 public: 36 NS_DECL_ISUPPORTS 37 NS_DECL_NSIOBSERVER 38 NS_DECL_NSIWEBSOCKETEVENTSERVICE 39 40 static already_AddRefed<WebSocketEventService> Get(); 41 static already_AddRefed<WebSocketEventService> GetOrCreate(); 42 43 void WebSocketCreated(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID, 44 const nsAString& aURI, const nsACString& aProtocols, 45 nsIEventTarget* aTarget = nullptr); 46 47 void WebSocketOpened(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID, 48 const nsAString& aEffectiveURI, 49 const nsACString& aProtocols, 50 const nsACString& aExtensions, uint64_t aHttpChannelId, 51 nsIEventTarget* aTarget = nullptr); 52 53 void WebSocketMessageAvailable(uint32_t aWebSocketSerialID, 54 uint64_t aInnerWindowID, 55 const nsACString& aData, uint16_t aMessageType, 56 nsIEventTarget* aTarget = nullptr); 57 58 void WebSocketClosed(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID, 59 bool aWasClean, uint16_t aCode, const nsAString& aReason, 60 nsIEventTarget* aTarget = nullptr); 61 62 void FrameReceived(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID, 63 already_AddRefed<WebSocketFrame> aFrame, 64 nsIEventTarget* aTarget = nullptr); 65 66 void FrameSent(uint32_t aWebSocketSerialID, uint64_t aInnerWindowID, 67 already_AddRefed<WebSocketFrame> aFrame, 68 nsIEventTarget* aTarget = nullptr); 69 70 void AssociateWebSocketImplWithSerialID(nsIWebSocketImpl* aWebSocketImpl, 71 uint32_t aWebSocketSerialID); 72 73 already_AddRefed<WebSocketFrame> CreateFrameIfNeeded( 74 bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3, 75 uint8_t aOpCode, bool aMaskBit, uint32_t aMask, 76 const nsCString& aPayload); 77 78 already_AddRefed<WebSocketFrame> CreateFrameIfNeeded( 79 bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3, 80 uint8_t aOpCode, bool aMaskBit, uint32_t aMask, uint8_t* aPayload, 81 uint32_t aPayloadLength); 82 83 already_AddRefed<WebSocketFrame> CreateFrameIfNeeded( 84 bool aFinBit, bool aRsvBit1, bool aRsvBit2, bool aRsvBit3, 85 uint8_t aOpCode, bool aMaskBit, uint32_t aMask, uint8_t* aPayloadInHdr, 86 uint32_t aPayloadInHdrLength, uint8_t* aPayload, uint32_t aPayloadLength); 87 88 private: 89 WebSocketEventService(); 90 ~WebSocketEventService(); 91 92 bool HasListeners() const; 93 void Shutdown(); 94 95 using WindowListeners = nsTArray<nsCOMPtr<nsIWebSocketEventListener>>; 96 97 nsTHashMap<nsUint32HashKey, nsWeakPtr> mWebSocketImplMap; 98 99 struct WindowListener { 100 WindowListeners mListeners; 101 RefPtr<WebSocketEventListenerChild> mActor; 102 }; 103 104 void GetListeners(uint64_t aInnerWindowID, WindowListeners& aListeners) const; 105 106 void ShutdownActorListener(WindowListener* aListener); 107 108 // Used only on the main-thread. 109 nsClassHashtable<nsUint64HashKey, WindowListener> mWindows; 110 111 Atomic<uint64_t> mCountListeners; 112 }; 113 114 } // namespace net 115 } // namespace mozilla 116 117 /** 118 * Casting WebSocketEventService to nsISupports is ambiguous. 119 * This method handles that. 120 */ 121 inline nsISupports* ToSupports(mozilla::net::WebSocketEventService* p) { 122 return NS_ISUPPORTS_CAST(nsIWebSocketEventService*, p); 123 } 124 125 #endif // mozilla_net_WebSocketEventService_h