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 nsISerialEventTarget;
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, final)
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 OnStartRequest message over background channel.
43   bool OnStartRequest(const nsHttpResponseHead& aResponseHead,
44                       const bool& aUseResponseHead,
45                       const nsHttpHeaderArray& aRequestHeaders,
46                       const HttpChannelOnStartRequestArgs& aArgs,
47                       const nsCOMPtr<nsICacheEntry>& aCacheEntry);
48 
49   // To send OnTransportAndData message over background channel.
50   bool OnTransportAndData(const nsresult& aChannelStatus,
51                           const nsresult& aTransportStatus,
52                           const uint64_t& aOffset, const uint32_t& aCount,
53                           const nsCString& aData);
54 
55   // To send OnStopRequest message over background channel.
56   bool OnStopRequest(const nsresult& aChannelStatus,
57                      const ResourceTimingStructArgs& aTiming,
58                      const nsHttpHeaderArray& aResponseTrailers,
59                      const nsTArray<ConsoleReportCollected>& aConsoleReports);
60 
61   // When ODA and OnStopRequest are sending from socket process to child
62   // process, this is the last IPC message sent from parent process.
63   bool OnConsoleReport(const nsTArray<ConsoleReportCollected>& aConsoleReports);
64 
65   // To send OnAfterLastPart message over background channel.
66   bool OnAfterLastPart(const nsresult aStatus);
67 
68   // To send OnProgress message over background channel.
69   bool OnProgress(const int64_t aProgress, const int64_t aProgressMax);
70 
71   // To send OnStatus message over background channel.
72   bool OnStatus(const nsresult aStatus);
73 
74   // To send FlushedForDiversion and DivertMessages messages
75   // over background channel.
76   bool OnDiversion();
77 
78   // To send NotifyClassificationFlags message over background channel.
79   bool OnNotifyClassificationFlags(uint32_t aClassificationFlags,
80                                    bool aIsThirdParty);
81 
82   // To send NotifyFlashPluginStateChanged message over background channel.
83   bool OnNotifyFlashPluginStateChanged(nsIHttpChannel::FlashPluginState aState);
84 
85   // To send SetClassifierMatchedInfo message over background channel.
86   bool OnSetClassifierMatchedInfo(const nsACString& aList,
87                                   const nsACString& aProvider,
88                                   const nsACString& aFullHash);
89 
90   // To send SetClassifierMatchedTrackingInfo message over background channel.
91   bool OnSetClassifierMatchedTrackingInfo(const nsACString& aLists,
92                                           const nsACString& aFullHashes);
93 
94   nsISerialEventTarget* GetBackgroundTarget();
95 
96   using ChildEndpointPromise =
97       MozPromise<ipc::Endpoint<extensions::PStreamFilterChild>, bool, true>;
98   [[nodiscard]] RefPtr<ChildEndpointPromise> AttachStreamFilter(
99       Endpoint<extensions::PStreamFilterParent>&& aParentEndpoint,
100       Endpoint<extensions::PStreamFilterChild>&& aChildEndpoint);
101 
102  protected:
103   void ActorDestroy(ActorDestroyReason aWhy) override;
104 
105  private:
106   virtual ~HttpBackgroundChannelParent();
107 
108   Atomic<bool> mIPCOpened;
109 
110   // Used to ensure atomicity of mBackgroundThread
111   Mutex mBgThreadMutex;
112 
113   nsCOMPtr<nsISerialEventTarget> mBackgroundThread;
114 
115   // associated HttpChannelParent for generating the channel events
116   RefPtr<HttpChannelParent> mChannelParent;
117 };
118 
119 }  // namespace net
120 }  // namespace mozilla
121 
122 #endif  // mozilla_net_HttpBackgroundChannelParent_h
123