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"
14 #include "nsCycleCollectionParticipant.h"
15 #include "mozilla/Attributes.h"
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
30 public nsIAsyncVerifyRedirectCallback
{
31 NS_DECL_THREADSAFE_ISUPPORTS
34 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
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
);
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.
53 * channel being redirected, MUST implement
54 * nsIAsyncVerifyRedirectCallback
56 * target of the redirect channel
59 * @param mainThreadEventTarget
60 * a labeled event target for dispatching runnables
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);
70 nsCOMPtr
<nsIChannel
> mOldChan
;
71 nsCOMPtr
<nsIChannel
> mNewChan
;
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
82 * Calls back to |oldChan| as described in Init()
84 void ExplicitCallback(nsresult result
);
87 ~nsAsyncRedirectVerifyHelper();
89 bool IsOldChannelCanceled();
93 * Helper to make the call-stack handle some control-flow for us
95 class nsAsyncRedirectAutoCallback
{
97 explicit nsAsyncRedirectAutoCallback(
98 nsIAsyncVerifyRedirectCallback
* aCallback
)
99 : mCallback(aCallback
) {
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; }
115 nsIAsyncVerifyRedirectCallback
* mCallback
;
120 } // namespace mozilla