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                        uint64_t aInnerWindowID, nsIWebSocketListener* aListener,
36                        nsISupports* aContext) override;
37   NS_IMETHOD Close(uint16_t code, const nsACString& reason) override;
38   NS_IMETHOD SendMsg(const nsACString& aMsg) override;
39   NS_IMETHOD SendBinaryMsg(const nsACString& aMsg) override;
40   NS_IMETHOD SendBinaryStream(nsIInputStream* aStream,
41                               uint32_t aLength) override;
42   NS_IMETHOD GetSecurityInfo(nsISupports** aSecurityInfo) override;
43 
44   void AddIPDLReference();
45   void ReleaseIPDLReference();
46 
47   // Off main thread URI access.
48   void GetEffectiveURL(nsAString& aEffectiveURL) const override;
49   bool IsEncrypted() const override;
50 
51  private:
52   ~WebSocketChannelChild();
53 
54   mozilla::ipc::IPCResult RecvOnStart(const nsCString& aProtocol,
55                                       const nsCString& aExtensions,
56                                       const nsString& aEffectiveURL,
57                                       const bool& aEncrypted,
58                                       const uint64_t& aHttpChannelId);
59   mozilla::ipc::IPCResult RecvOnStop(const nsresult& aStatusCode);
60   mozilla::ipc::IPCResult RecvOnMessageAvailable(
61       const nsDependentCSubstring& aMsg, const bool& aMoreData);
62   mozilla::ipc::IPCResult RecvOnBinaryMessageAvailable(
63       const nsDependentCSubstring& aMsg, const bool& aMoreData);
64   mozilla::ipc::IPCResult RecvOnAcknowledge(const uint32_t& aSize);
65   mozilla::ipc::IPCResult RecvOnServerClose(const uint16_t& aCode,
66                                             const nsCString& aReason);
67 
68   void OnStart(const nsCString& aProtocol, const nsCString& aExtensions,
69                const nsString& aEffectiveURL, const bool& aEncrypted,
70                const uint64_t& aHttpChannelId);
71   void OnStop(const nsresult& aStatusCode);
72   void OnMessageAvailable(const nsCString& aMsg);
73   void OnBinaryMessageAvailable(const nsCString& aMsg);
74   void OnAcknowledge(const uint32_t& aSize);
75   void OnServerClose(const uint16_t& aCode, const nsCString& aReason);
76   void AsyncOpenFailed();
77 
78   bool IsOnTargetThread();
79 
80   void MaybeReleaseIPCObject();
81 
82   // This function tries to get a labeled event target for |mNeckoTarget|.
83   void SetupNeckoTarget();
84 
85   bool RecvOnMessageAvailableInternal(const nsDependentCSubstring& aMsg,
86                                       bool aMoreData, bool aBinary);
87 
88   void OnError();
89 
90   RefPtr<ChannelEventQueue> mEventQ;
91   nsString mEffectiveURL;
92   nsCString mReceivedMsgBuffer;
93 
94   // This variable is protected by mutex.
95   enum { Opened, Closing, Closed } mIPCState;
96 
97   mozilla::Mutex mMutex;
98 
99   friend class StartEvent;
100   friend class StopEvent;
101   friend class MessageEvent;
102   friend class AcknowledgeEvent;
103   friend class ServerCloseEvent;
104   friend class AsyncOpenFailedEvent;
105   friend class OnErrorEvent;
106 };
107 
108 }  // namespace net
109 }  // namespace mozilla
110 
111 #endif  // mozilla_net_WebSocketChannelChild_h
112