BaseWebSocketChannel.h (5063B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et 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_BaseWebSocketChannel_h 8 #define mozilla_net_BaseWebSocketChannel_h 9 10 #include "mozilla/DataMutex.h" 11 #include "nsIWebSocketChannel.h" 12 #include "nsIWebSocketListener.h" 13 #include "nsIProtocolHandler.h" 14 #include "nsIThread.h" 15 #include "nsIThreadRetargetableRequest.h" 16 #include "nsCOMPtr.h" 17 #include "nsString.h" 18 19 namespace mozilla { 20 namespace net { 21 22 const static int32_t kDefaultWSPort = 80; 23 const static int32_t kDefaultWSSPort = 443; 24 25 class BaseWebSocketChannel : public nsIWebSocketChannel, 26 public nsIProtocolHandler, 27 public nsIThreadRetargetableRequest { 28 public: 29 BaseWebSocketChannel(); 30 31 NS_DECL_NSIPROTOCOLHANDLER 32 NS_DECL_NSITHREADRETARGETABLEREQUEST 33 34 NS_IMETHOD QueryInterface(const nsIID& uuid, void** result) override = 0; 35 NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override = 0; 36 NS_IMETHOD_(MozExternalRefCountType) Release(void) override = 0; 37 38 // Partial implementation of nsIWebSocketChannel 39 // 40 NS_IMETHOD GetOriginalURI(nsIURI** aOriginalURI) override; 41 NS_IMETHOD GetURI(nsIURI** aURI) override; 42 NS_IMETHOD GetNotificationCallbacks( 43 nsIInterfaceRequestor** aNotificationCallbacks) override; 44 NS_IMETHOD SetNotificationCallbacks( 45 nsIInterfaceRequestor* aNotificationCallbacks) override; 46 NS_IMETHOD GetLoadGroup(nsILoadGroup** aLoadGroup) override; 47 NS_IMETHOD SetLoadGroup(nsILoadGroup* aLoadGroup) override; 48 NS_IMETHOD SetLoadInfo(nsILoadInfo* aLoadInfo) override; 49 NS_IMETHOD GetLoadInfo(nsILoadInfo** aLoadInfo) override; 50 NS_IMETHOD GetExtensions(nsACString& aExtensions) override; 51 NS_IMETHOD GetProtocol(nsACString& aProtocol) override; 52 NS_IMETHOD SetProtocol(const nsACString& aProtocol) override; 53 NS_IMETHOD GetPingInterval(uint32_t* aSeconds) override; 54 NS_IMETHOD SetPingInterval(uint32_t aSeconds) override; 55 NS_IMETHOD GetPingTimeout(uint32_t* aSeconds) override; 56 NS_IMETHOD SetPingTimeout(uint32_t aSeconds) override; 57 NS_IMETHOD InitLoadInfoNative(nsINode* aLoadingNode, 58 nsIPrincipal* aLoadingPrincipal, 59 nsIPrincipal* aTriggeringPrincipal, 60 nsICookieJarSettings* aCookieJarSettings, 61 uint32_t aSecurityFlags, 62 nsContentPolicyType aContentPolicyType, 63 uint32_t aSandboxFlags) override; 64 NS_IMETHOD InitLoadInfo(nsINode* aLoadingNode, 65 nsIPrincipal* aLoadingPrincipal, 66 nsIPrincipal* aTriggeringPrincipal, 67 uint32_t aSecurityFlags, 68 nsContentPolicyType aContentPolicyType) override; 69 NS_IMETHOD GetSerial(uint32_t* aSerial) override; 70 NS_IMETHOD SetSerial(uint32_t aSerial) override; 71 NS_IMETHOD SetServerParameters( 72 nsITransportProvider* aProvider, 73 const nsACString& aNegotiatedExtensions) override; 74 NS_IMETHOD GetHttpChannelId(uint64_t* aHttpChannelId) override; 75 76 // Off main thread URI access. 77 virtual void GetEffectiveURL(nsAString& aEffectiveURL) const = 0; 78 virtual bool IsEncrypted() const = 0; 79 80 already_AddRefed<nsISerialEventTarget> GetTargetThread(); 81 bool IsOnTargetThread(); 82 83 class ListenerAndContextContainer final { 84 public: 85 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ListenerAndContextContainer) 86 87 ListenerAndContextContainer(nsIWebSocketListener* aListener, 88 nsISupports* aContext); 89 90 nsCOMPtr<nsIWebSocketListener> mListener; 91 nsCOMPtr<nsISupports> mContext; 92 93 private: 94 ~ListenerAndContextContainer(); 95 }; 96 97 protected: 98 virtual ~BaseWebSocketChannel(); 99 nsCOMPtr<nsIURI> mOriginalURI; 100 nsCOMPtr<nsIURI> mURI; 101 RefPtr<ListenerAndContextContainer> mListenerMT; 102 nsCOMPtr<nsIInterfaceRequestor> mCallbacks; 103 nsCOMPtr<nsILoadGroup> mLoadGroup; 104 nsCOMPtr<nsILoadInfo> mLoadInfo; 105 nsCOMPtr<nsITransportProvider> mServerTransportProvider; 106 107 // Used to ensure atomicity of mTargetThread. 108 // Set before AsyncOpen via RetargetDeliveryTo or in AsyncOpen, never changed 109 // after AsyncOpen 110 DataMutex<nsCOMPtr<nsISerialEventTarget>> mTargetThread{ 111 "BaseWebSocketChannel::EventTargetMutex"}; 112 113 nsCString mProtocol; 114 nsCString mOrigin; 115 116 nsCString mNegotiatedExtensions; 117 118 uint32_t mWasOpened : 1; 119 uint32_t mClientSetPingInterval : 1; 120 uint32_t mClientSetPingTimeout : 1; 121 122 Atomic<bool> mEncrypted; 123 bool mPingForced; 124 bool mIsServerSide; 125 126 Atomic<uint32_t> mPingInterval; /* milliseconds */ 127 uint32_t mPingResponseTimeout; /* milliseconds */ 128 129 uint32_t mSerial; 130 131 uint64_t mHttpChannelId; 132 }; 133 134 } // namespace net 135 } // namespace mozilla 136 137 #endif // mozilla_net_BaseWebSocketChannel_h