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