HttpBackgroundChannelChild.h (5933B)
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 "mozilla/ipc/Endpoint.h" 13 #include "nsIRunnable.h" 14 #include "nsTArray.h" 15 16 using mozilla::ipc::IPCResult; 17 18 namespace mozilla { 19 namespace net { 20 21 class PBackgroundDataBridgeChild; 22 class BackgroundDataBridgeChild; 23 class HttpChannelChild; 24 25 class HttpBackgroundChannelChild final : public PHttpBackgroundChannelChild { 26 friend class BackgroundChannelCreateCallback; 27 friend class PHttpBackgroundChannelChild; 28 friend class HttpChannelChild; 29 friend class BackgroundDataBridgeChild; 30 31 public: 32 explicit HttpBackgroundChannelChild(); 33 34 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HttpBackgroundChannelChild, final) 35 36 // Associate this background channel with a HttpChannelChild and 37 // initiate the createion of the PBackground IPC channel. 38 nsresult Init(HttpChannelChild* aChannelChild); 39 40 // Callback while the associated HttpChannelChild is not going to 41 // handle any incoming messages over background channel. 42 void OnChannelClosed(); 43 44 // Return true if OnChannelClosed has been called. 45 bool ChannelClosed(); 46 47 // Callback when OnStartRequest is received and handled by HttpChannelChild. 48 // Enqueued messages in background channel will be flushed. 49 void OnStartRequestReceived(Maybe<uint32_t> aMultiPartID); 50 51 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED 52 bool IsQueueEmpty() const { return mQueuedRunnables.IsEmpty(); } 53 #endif 54 55 protected: 56 IPCResult RecvOnStartRequest(const nsHttpResponseHead& aResponseHead, 57 const bool& aUseResponseHead, 58 const nsHttpHeaderArray& aRequestHeaders, 59 const HttpChannelOnStartRequestArgs& aArgs, 60 const HttpChannelAltDataStream& aAltData, 61 const TimeStamp& aOnStartRequestStart); 62 63 IPCResult RecvOnTransportAndData(const nsresult& aChannelStatus, 64 const nsresult& aTransportStatus, 65 const uint64_t& aOffset, 66 const uint32_t& aCount, 67 const nsACString& aData, 68 const bool& aDataFromSocketProcess, 69 const TimeStamp& aOnDataAvailableStart); 70 71 IPCResult RecvOnStopRequest( 72 const nsresult& aChannelStatus, const ResourceTimingStructArgs& aTiming, 73 const TimeStamp& aLastActiveTabOptHit, 74 const nsHttpHeaderArray& aResponseTrailers, 75 nsTArray<ConsoleReportCollected>&& aConsoleReports, 76 const bool& aFromSocketProcess, const TimeStamp& aOnStopRequestStart); 77 78 IPCResult RecvOnConsoleReport( 79 nsTArray<ConsoleReportCollected>&& aConsoleReports); 80 81 IPCResult RecvOnAfterLastPart(const nsresult& aStatus); 82 83 IPCResult RecvOnProgress(const int64_t& aProgress, 84 const int64_t& aProgressMax); 85 86 IPCResult RecvOnStatus(const nsresult& aStatus); 87 88 IPCResult RecvNotifyClassificationFlags(const uint32_t& aClassificationFlags, 89 const bool& aIsThirdParty); 90 91 IPCResult RecvSetClassifierMatchedInfo(const ClassifierInfo& info); 92 93 IPCResult RecvSetClassifierMatchedTrackingInfo(const ClassifierInfo& info); 94 95 IPCResult RecvAttachStreamFilter( 96 Endpoint<extensions::PStreamFilterParent>&& aEndpoint); 97 98 IPCResult RecvDetachStreamFilters(); 99 100 void ActorDestroy(ActorDestroyReason aWhy) override; 101 102 void CreateDataBridge(Endpoint<PBackgroundDataBridgeChild>&& aEndpoint); 103 104 private: 105 virtual ~HttpBackgroundChannelChild(); 106 107 // Initiate the creation of the PBckground IPC channel. 108 // Return false if failed. 109 bool CreateBackgroundChannel(); 110 111 // Check OnStartRequest is sent by parent process over main thread IPC 112 // but not yet received on child process. 113 // return true before RecvOnStartRequestSent is invoked. 114 // return false if RecvOnStartRequestSent is invoked but not 115 // OnStartRequestReceived. 116 // return true after both RecvOnStartRequestSend and OnStartRequestReceived 117 // are invoked. 118 bool IsWaitingOnStartRequest(); 119 120 // Associated HttpChannelChild for handling the channel events. 121 // Will be removed while failed to create background channel, 122 // destruction of the background channel, or explicitly dissociation 123 // via OnChannelClosed callback. 124 RefPtr<HttpChannelChild> mChannelChild; 125 126 // True if OnStartRequest is received by HttpChannelChild. 127 // Should only access on STS thread. 128 bool mStartReceived = false; 129 130 // Store pending messages that require to be handled after OnStartRequest. 131 // Should be flushed after OnStartRequest is received and handled. 132 // Should only access on STS thread. 133 nsTArray<nsCOMPtr<nsIRunnable>> mQueuedRunnables; 134 135 enum ODASource { 136 ODA_PENDING = 0, // ODA is pending 137 ODA_FROM_PARENT = 1, // ODA from parent process. 138 ODA_FROM_SOCKET = 2 // response coming from the network 139 }; 140 // We need to know the first ODA will be from socket process or parent 141 // process. This information is from OnStartRequest message from parent 142 // process. 143 ODASource mFirstODASource; 144 145 // Indicate whether HttpChannelChild::ProcessOnStopRequest is called. 146 bool mOnStopRequestCalled = false; 147 148 // This is used when we receive the console report from parent process, but 149 // still not get the OnStopRequest from socket process. 150 std::function<void()> mConsoleReportTask; 151 }; 152 153 } // namespace net 154 } // namespace mozilla 155 156 #endif // mozilla_net_HttpBackgroundChannelChild_h