Bug 1885993 - Enable the BackupService initializer on Nightly by default. r=backup...
[gecko.git] / netwerk / base / nsAsyncRedirectVerifyHelper.h
blob3e1afa8c694940c382d250c6f7efbf31cb76aec6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsAsyncRedirectVerifyHelper_h
7 #define nsAsyncRedirectVerifyHelper_h
9 #include "nsIRunnable.h"
10 #include "nsIChannelEventSink.h"
11 #include "nsIAsyncVerifyRedirectCallback.h"
12 #include "nsINamed.h"
13 #include "nsCOMPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "mozilla/Attributes.h"
17 class nsIChannel;
19 namespace mozilla {
20 namespace net {
22 /**
23 * This class simplifies call of OnChannelRedirect of IOService and
24 * the sink bound with the channel being redirected while the result of
25 * redirect decision is returned through the callback.
27 class nsAsyncRedirectVerifyHelper final
28 : public nsIRunnable,
29 public nsINamed,
30 public nsIAsyncVerifyRedirectCallback {
31 NS_DECL_THREADSAFE_ISUPPORTS
32 NS_DECL_NSIRUNNABLE
33 NS_DECL_NSINAMED
34 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
36 public:
37 nsAsyncRedirectVerifyHelper() = default;
40 * Calls AsyncOnChannelRedirect() on the given sink with the given
41 * channels and flags. Keeps track of number of async callbacks to expect.
43 nsresult DelegateOnChannelRedirect(nsIChannelEventSink* sink,
44 nsIChannel* oldChannel,
45 nsIChannel* newChannel, uint32_t flags);
47 /**
48 * Initialize and run the chain of AsyncOnChannelRedirect calls. OldChannel
49 * is QI'ed for nsIAsyncVerifyRedirectCallback. The result of the redirect
50 * decision is passed through this interface back to the oldChannel.
52 * @param oldChan
53 * channel being redirected, MUST implement
54 * nsIAsyncVerifyRedirectCallback
55 * @param newChan
56 * target of the redirect channel
57 * @param flags
58 * redirect flags
59 * @param mainThreadEventTarget
60 * a labeled event target for dispatching runnables
61 * @param synchronize
62 * set to TRUE if you want the Init method wait synchronously for
63 * all redirect callbacks
65 nsresult Init(nsIChannel* oldChan, nsIChannel* newChan, uint32_t flags,
66 nsIEventTarget* mainThreadEventTarget,
67 bool synchronize = false);
69 protected:
70 nsCOMPtr<nsIChannel> mOldChan;
71 nsCOMPtr<nsIChannel> mNewChan;
72 uint32_t mFlags{0};
73 bool mWaitingForRedirectCallback{false};
74 nsCOMPtr<nsIEventTarget> mCallbackEventTarget;
75 bool mCallbackInitiated{false};
76 int32_t mExpectedCallbacks{0};
77 nsresult mResult{NS_OK}; // value passed to callback
79 void InitCallback();
81 /**
82 * Calls back to |oldChan| as described in Init()
84 void ExplicitCallback(nsresult result);
86 private:
87 ~nsAsyncRedirectVerifyHelper();
89 bool IsOldChannelCanceled();
93 * Helper to make the call-stack handle some control-flow for us
95 class nsAsyncRedirectAutoCallback {
96 public:
97 explicit nsAsyncRedirectAutoCallback(
98 nsIAsyncVerifyRedirectCallback* aCallback)
99 : mCallback(aCallback) {
100 mResult = NS_OK;
102 ~nsAsyncRedirectAutoCallback() {
103 if (mCallback) mCallback->OnRedirectVerifyCallback(mResult);
106 * Call this is you want it to call back with a different result-code
108 void SetResult(nsresult aRes) { mResult = aRes; }
110 * Call this is you want to avoid the callback
112 void DontCallback() { mCallback = nullptr; }
114 private:
115 nsIAsyncVerifyRedirectCallback* mCallback;
116 nsresult mResult;
119 } // namespace net
120 } // namespace mozilla
121 #endif