Source code

Revision control

Other Tools

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/. */
5
6
#ifndef nsAsyncRedirectVerifyHelper_h
7
#define nsAsyncRedirectVerifyHelper_h
8
9
#include "nsIRunnable.h"
10
#include "nsIThread.h"
11
#include "nsIChannelEventSink.h"
12
#include "nsIInterfaceRequestor.h"
13
#include "nsIAsyncVerifyRedirectCallback.h"
14
#include "nsCOMPtr.h"
15
#include "nsAutoPtr.h"
16
#include "nsCycleCollectionParticipant.h"
17
#include "mozilla/Attributes.h"
18
19
class nsIChannel;
20
21
namespace mozilla {
22
namespace net {
23
24
/**
25
* This class simplifies call of OnChannelRedirect of IOService and
26
* the sink bound with the channel being redirected while the result of
27
* redirect decision is returned through the callback.
28
*/
29
class nsAsyncRedirectVerifyHelper final
30
: public nsIRunnable,
31
public nsINamed,
32
public nsIAsyncVerifyRedirectCallback {
33
NS_DECL_THREADSAFE_ISUPPORTS
34
NS_DECL_NSIRUNNABLE
35
NS_DECL_NSINAMED
36
NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
37
38
public:
39
nsAsyncRedirectVerifyHelper();
40
41
/*
42
* Calls AsyncOnChannelRedirect() on the given sink with the given
43
* channels and flags. Keeps track of number of async callbacks to expect.
44
*/
45
nsresult DelegateOnChannelRedirect(nsIChannelEventSink* sink,
46
nsIChannel* oldChannel,
47
nsIChannel* newChannel, uint32_t flags);
48
49
/**
50
* Initialize and run the chain of AsyncOnChannelRedirect calls. OldChannel
51
* is QI'ed for nsIAsyncVerifyRedirectCallback. The result of the redirect
52
* decision is passed through this interface back to the oldChannel.
53
*
54
* @param oldChan
55
* channel being redirected, MUST implement
56
* nsIAsyncVerifyRedirectCallback
57
* @param newChan
58
* target of the redirect channel
59
* @param flags
60
* redirect flags
61
* @param mainThreadEventTarget
62
* a labeled event target for dispatching runnables
63
* @param synchronize
64
* set to TRUE if you want the Init method wait synchronously for
65
* all redirect callbacks
66
*/
67
nsresult Init(nsIChannel* oldChan, nsIChannel* newChan, uint32_t flags,
68
nsIEventTarget* mainThreadEventTarget,
69
bool synchronize = false);
70
71
protected:
72
nsCOMPtr<nsIChannel> mOldChan;
73
nsCOMPtr<nsIChannel> mNewChan;
74
uint32_t mFlags;
75
bool mWaitingForRedirectCallback;
76
nsCOMPtr<nsIEventTarget> mCallbackEventTarget;
77
bool mCallbackInitiated;
78
int32_t mExpectedCallbacks;
79
nsresult mResult; // value passed to callback
80
81
void InitCallback();
82
83
/**
84
* Calls back to |oldChan| as described in Init()
85
*/
86
void ExplicitCallback(nsresult result);
87
88
private:
89
~nsAsyncRedirectVerifyHelper();
90
91
bool IsOldChannelCanceled();
92
};
93
94
/*
95
* Helper to make the call-stack handle some control-flow for us
96
*/
97
class nsAsyncRedirectAutoCallback {
98
public:
99
explicit nsAsyncRedirectAutoCallback(
100
nsIAsyncVerifyRedirectCallback* aCallback)
101
: mCallback(aCallback) {
102
mResult = NS_OK;
103
}
104
~nsAsyncRedirectAutoCallback() {
105
if (mCallback) mCallback->OnRedirectVerifyCallback(mResult);
106
}
107
/*
108
* Call this is you want it to call back with a different result-code
109
*/
110
void SetResult(nsresult aRes) { mResult = aRes; }
111
/*
112
* Call this is you want to avoid the callback
113
*/
114
void DontCallback() { mCallback = nullptr; }
115
116
private:
117
nsIAsyncVerifyRedirectCallback* mCallback;
118
nsresult mResult;
119
};
120
121
} // namespace net
122
} // namespace mozilla
123
#endif