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_WebSocketChannelChild_h
8 #define mozilla_net_WebSocketChannelChild_h
9 
10 #include "mozilla/net/NeckoTargetHolder.h"
11 #include "mozilla/net/PWebSocketChild.h"
12 #include "mozilla/net/BaseWebSocketChannel.h"
13 #include "nsString.h"
14 
15 namespace mozilla {
16 
17 namespace net {
18 
19 class ChannelEvent;
20 class ChannelEventQueue;
21 
22 class WebSocketChannelChild final : public BaseWebSocketChannel,
23                                     public PWebSocketChild,
24                                     public NeckoTargetHolder {
25   friend class PWebSocketChild;
26 
27  public:
28   explicit WebSocketChannelChild(bool aEncrypted);
29 
30   NS_DECL_THREADSAFE_ISUPPORTS
31 
32   // nsIWebSocketChannel methods BaseWebSocketChannel didn't implement for us
33   //
34   NS_IMETHOD AsyncOpen(nsIURI* aURI, const nsACString& aOrigin,
35                        JS::HandleValue aOriginAttributes,
36                        uint64_t aInnerWindowID, nsIWebSocketListener* aListener,
37                        nsISupports* aContext, JSContext* aCx) override;
38   NS_IMETHOD AsyncOpenNative(nsIURI* aURI, const nsACString& aOrigin,
39                              const OriginAttributes& aOriginAttributes,
40                              uint64_t aInnerWindowID,
41                              nsIWebSocketListener* aListener,
42                              nsISupports* aContext) override;
43   NS_IMETHOD Close(uint16_t code, const nsACString& reason) override;
44   NS_IMETHOD SendMsg(const nsACString& aMsg) override;
45   NS_IMETHOD SendBinaryMsg(const nsACString& aMsg) override;
46   NS_IMETHOD SendBinaryStream(nsIInputStream* aStream,
47                               uint32_t aLength) override;
48   NS_IMETHOD GetSecurityInfo(nsISupports** aSecurityInfo) override;
49 
50   void AddIPDLReference();
51   void ReleaseIPDLReference();
52 
53   // Off main thread URI access.
54   void GetEffectiveURL(nsAString& aEffectiveURL) const override;
55   bool IsEncrypted() const override;
56 
57  private:
58   ~WebSocketChannelChild();
59 
60   mozilla::ipc::IPCResult RecvOnStart(const nsCString& aProtocol,
61                                       const nsCString& aExtensions,
62                                       const nsString& aEffectiveURL,
63                                       const bool& aEncrypted,
64                                       const uint64_t& aHttpChannelId);
65   mozilla::ipc::IPCResult RecvOnStop(const nsresult& aStatusCode);
66   mozilla::ipc::IPCResult RecvOnMessageAvailable(
67       const nsDependentCSubstring& aMsg, const bool& aMoreData);
68   mozilla::ipc::IPCResult RecvOnBinaryMessageAvailable(
69       const nsDependentCSubstring& aMsg, const bool& aMoreData);
70   mozilla::ipc::IPCResult RecvOnAcknowledge(const uint32_t& aSize);
71   mozilla::ipc::IPCResult RecvOnServerClose(const uint16_t& aCode,
72                                             const nsCString& aReason);
73 
74   void OnStart(const nsCString& aProtocol, const nsCString& aExtensions,
75                const nsString& aEffectiveURL, const bool& aEncrypted,
76                const uint64_t& aHttpChannelId);
77   void OnStop(const nsresult& aStatusCode);
78   void OnMessageAvailable(const nsCString& aMsg);
79   void OnBinaryMessageAvailable(const nsCString& aMsg);
80   void OnAcknowledge(const uint32_t& aSize);
81   void OnServerClose(const uint16_t& aCode, const nsCString& aReason);
82   void AsyncOpenFailed();
83 
84   void MaybeReleaseIPCObject();
85 
86   // This function tries to get a labeled event target for |mNeckoTarget|.
87   void SetupNeckoTarget();
88 
89   bool RecvOnMessageAvailableInternal(const nsDependentCSubstring& aMsg,
90                                       bool aMoreData, bool aBinary);
91 
92   void OnError();
93 
94   RefPtr<ChannelEventQueue> mEventQ;
95   nsString mEffectiveURL;
96   nsCString mReceivedMsgBuffer;
97 
98   // This variable is protected by mutex.
99   enum { Opened, Closing, Closed } mIPCState;
100 
101   mozilla::Mutex mMutex;
102 
103   friend class StartEvent;
104   friend class StopEvent;
105   friend class MessageEvent;
106   friend class AcknowledgeEvent;
107   friend class ServerCloseEvent;
108   friend class AsyncOpenFailedEvent;
109   friend class OnErrorEvent;
110 };
111 
112 }  // namespace net
113 }  // namespace mozilla
114 
115 #endif  // mozilla_net_WebSocketChannelChild_h
116