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_HttpBackgroundChannelParent_h
9 #define mozilla_net_HttpBackgroundChannelParent_h
10 
11 #include "mozilla/net/PHttpBackgroundChannelParent.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/Mutex.h"
14 #include "nsID.h"
15 #include "nsISupportsImpl.h"
16 
17 class nsIEventTarget;
18 
19 namespace mozilla {
20 namespace net {
21 
22 class HttpChannelParent;
23 
24 class HttpBackgroundChannelParent final : public PHttpBackgroundChannelParent {
25  public:
26   explicit HttpBackgroundChannelParent();
27 
28   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HttpBackgroundChannelParent)
29 
30   // Try to find associated HttpChannelParent with the same
31   // channel Id.
32   nsresult Init(const uint64_t& aChannelId);
33 
34   // Callbacks for BackgroundChannelRegistrar to notify
35   // the associated HttpChannelParent is found.
36   void LinkToChannel(HttpChannelParent* aChannelParent);
37 
38   // Callbacks for HttpChannelParent to close the background
39   // IPC channel.
40   void OnChannelClosed();
41 
42   // To send OnStartRequestSend message over background channel.
43   bool OnStartRequestSent();
44 
45   // To send OnTransportAndData message over background channel.
46   bool OnTransportAndData(const nsresult& aChannelStatus,
47                           const nsresult& aTransportStatus,
48                           const uint64_t& aOffset, const uint32_t& aCount,
49                           const nsCString& aData);
50 
51   // To send OnStopRequest message over background channel.
52   bool OnStopRequest(const nsresult& aChannelStatus,
53                      const ResourceTimingStruct& aTiming,
54                      const nsHttpHeaderArray& aResponseTrailers);
55 
56   // To send OnProgress message over background channel.
57   bool OnProgress(const int64_t& aProgress, const int64_t& aProgressMax);
58 
59   // To send OnStatus message over background channel.
60   bool OnStatus(const nsresult& aStatus);
61 
62   // To send FlushedForDiversion and DivertMessages messages
63   // over background channel.
64   bool OnDiversion();
65 
66   // To send NotifyTrackingProtectionDisabled message over background channel.
67   bool OnNotifyTrackingProtectionDisabled();
68 
69   // To send NotifyTrackingResource message over background channel.
70   bool OnNotifyTrackingResource();
71 
72   // To send SetClassifierMatchedInfo message over background channel.
73   bool OnSetClassifierMatchedInfo(const nsACString& aList,
74                                   const nsACString& aProvider,
75                                   const nsACString& aFullHash);
76 
77  protected:
78   void ActorDestroy(ActorDestroyReason aWhy) override;
79 
80  private:
81   virtual ~HttpBackgroundChannelParent();
82 
83   Atomic<bool> mIPCOpened;
84 
85   // Used to ensure atomicity of mBackgroundThread
86   Mutex mBgThreadMutex;
87 
88   nsCOMPtr<nsIEventTarget> mBackgroundThread;
89 
90   // associated HttpChannelParent for generating the channel events
91   RefPtr<HttpChannelParent> mChannelParent;
92 };
93 
94 }  // namespace net
95 }  // namespace mozilla
96 
97 #endif  // mozilla_net_HttpBackgroundChannelParent_h
98