1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et ft=cpp : */
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 file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_net_WebSocketConnectionParent_h
8 #define mozilla_net_WebSocketConnectionParent_h
9 
10 #include "mozilla/net/PWebSocketConnectionParent.h"
11 #include "mozilla/net/WebSocketConnectionBase.h"
12 #include "nsISupportsImpl.h"
13 #include "WebSocketConnectionBase.h"
14 
15 class nsIHttpUpgradeListener;
16 
17 namespace mozilla {
18 namespace net {
19 
20 class WebSocketConnectionListener;
21 
22 // WebSocketConnectionParent implements WebSocketConnectionBase and provides
23 // interface for WebSocketChannel to send/receive data. The ownership model for
24 // WebSocketConnectionParent is that IPDL and WebSocketChannel hold strong
25 // reference of this object. When Close() is called, a __delete__ message will
26 // be sent and the IPC actor will be deallocated as well.
27 
28 class WebSocketConnectionParent final : public PWebSocketConnectionParent,
29                                         public WebSocketConnectionBase {
30  public:
31   NS_DECL_THREADSAFE_ISUPPORTS
32 
33   explicit WebSocketConnectionParent(nsIHttpUpgradeListener* aListener);
34 
35   mozilla::ipc::IPCResult RecvOnTransportAvailable(
36       const nsCString& aSecurityInfoSerialization);
37   mozilla::ipc::IPCResult RecvOnError(const nsresult& aStatus);
38   mozilla::ipc::IPCResult RecvOnTCPClosed();
39   mozilla::ipc::IPCResult RecvOnDataReceived(nsTArray<uint8_t>&& aData);
40   mozilla::ipc::IPCResult RecvOnUpgradeFailed(const nsresult& aReason);
41 
42   void ActorDestroy(ActorDestroyReason aWhy) override;
43 
44   nsresult Init(WebSocketConnectionListener* aListener) override;
45   void GetIoTarget(nsIEventTarget** aTarget) override;
46   void Close() override;
47   nsresult WriteOutputData(const uint8_t* aHdrBuf, uint32_t aHdrBufLength,
48                            const uint8_t* aPayloadBuf,
49                            uint32_t aPayloadBufLength) override;
50   nsresult StartReading() override;
51   void DrainSocketData() override;
52   nsresult GetSecurityInfo(nsISupports** aSecurityInfo) override;
53 
54  private:
55   virtual ~WebSocketConnectionParent();
56 
57   nsCOMPtr<nsIHttpUpgradeListener> mUpgradeListener;
58   RefPtr<WebSocketConnectionListener> mListener;
59   nsCOMPtr<nsIEventTarget> mBackgroundThread;
60   nsCOMPtr<nsISupports> mSecurityInfo;
61   Atomic<bool> mClosed{false};
62   Mutex mMutex{"WebSocketConnectionParent::mMutex"};
63 };
64 
65 }  // namespace net
66 }  // namespace mozilla
67 
68 #endif  // mozilla_net_WebSocketConnectionParent_h
69