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 
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
8 #ifndef mozilla_net_HttpBackgroundChannelChild_h
9 #define mozilla_net_HttpBackgroundChannelChild_h
10 
11 #include "mozilla/net/PHttpBackgroundChannelChild.h"
12 #include "nsIRunnable.h"
13 #include "nsTArray.h"
14 
15 using mozilla::dom::ClassifierInfo;
16 using mozilla::ipc::IPCResult;
17 
18 namespace mozilla {
19 namespace net {
20 
21 class HttpChannelChild;
22 
23 class HttpBackgroundChannelChild final : public PHttpBackgroundChannelChild {
24   friend class BackgroundChannelCreateCallback;
25 
26  public:
27   explicit HttpBackgroundChannelChild();
28 
29   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HttpBackgroundChannelChild)
30 
31   // Associate this background channel with a HttpChannelChild and
32   // initiate the createion of the PBackground IPC channel.
33   nsresult Init(HttpChannelChild* aChannelChild);
34 
35   // Callback while the associated HttpChannelChild is not going to
36   // handle any incoming messages over background channel.
37   void OnChannelClosed();
38 
39   // Callback when OnStartRequest is received and handled by HttpChannelChild.
40   // Enqueued messages in background channel will be flushed.
41   void OnStartRequestReceived();
42 
43  protected:
44   IPCResult RecvOnTransportAndData(const nsresult& aChannelStatus,
45                                    const nsresult& aTransportStatus,
46                                    const uint64_t& aOffset,
47                                    const uint32_t& aCount,
48                                    const nsCString& aData) override;
49 
50   IPCResult RecvOnStopRequest(
51       const nsresult& aChannelStatus, const ResourceTimingStruct& aTiming,
52       const TimeStamp& aLastActiveTabOptHit,
53       const nsHttpHeaderArray& aResponseTrailers) override;
54 
55   IPCResult RecvOnProgress(const int64_t& aProgress,
56                            const int64_t& aProgressMax) override;
57 
58   IPCResult RecvOnStatus(const nsresult& aStatus) override;
59 
60   IPCResult RecvFlushedForDiversion() override;
61 
62   IPCResult RecvDivertMessages() override;
63 
64   IPCResult RecvOnStartRequestSent() override;
65 
66   IPCResult RecvNotifyTrackingProtectionDisabled() override;
67 
68   IPCResult RecvNotifyTrackingResource() override;
69 
70   IPCResult RecvSetClassifierMatchedInfo(const ClassifierInfo& info) override;
71 
72   void ActorDestroy(ActorDestroyReason aWhy) override;
73 
74  private:
75   virtual ~HttpBackgroundChannelChild();
76 
77   // Initiate the creation of the PBckground IPC channel.
78   // Return false if failed.
79   bool CreateBackgroundChannel();
80 
81   // Check OnStartRequest is sent by parent process over main thread IPC
82   // but not yet received on child process.
83   // return true before RecvOnStartRequestSent is invoked.
84   // return false if RecvOnStartRequestSent is invoked but not
85   // OnStartRequestReceived.
86   // return true after both RecvOnStartRequestSend and OnStartRequestReceived
87   // are invoked.
88   bool IsWaitingOnStartRequest();
89 
90   // Associated HttpChannelChild for handling the channel events.
91   // Will be removed while failed to create background channel,
92   // destruction of the background channel, or explicitly dissociation
93   // via OnChannelClosed callback.
94   RefPtr<HttpChannelChild> mChannelChild;
95 
96   // True if OnStartRequest is received by HttpChannelChild.
97   // Should only access on STS thread.
98   bool mStartReceived = false;
99 
100   // True if OnStartRequest is sent by HttpChannelParent.
101   // Should only access on STS thread.
102   bool mStartSent = false;
103 
104   // Store pending messages that require to be handled after OnStartRequest.
105   // Should be flushed after OnStartRequest is received and handled.
106   // Should only access on STS thread.
107   nsTArray<nsCOMPtr<nsIRunnable>> mQueuedRunnables;
108 };
109 
110 }  // namespace net
111 }  // namespace mozilla
112 
113 #endif  // mozilla_net_HttpBackgroundChannelChild_h
114