1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org networking code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Corporation
19 : * Portions created by the Initial Developer are Copyright (C) 2009
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Honza Bambas <honzab@firemni.cz>
24 : * Bjarne Geir Herland <bjarne@runitsoft.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsAsyncRedirectVerifyHelper_h
41 : #define nsAsyncRedirectVerifyHelper_h
42 :
43 : #include "nsIRunnable.h"
44 : #include "nsIThread.h"
45 : #include "nsIChannelEventSink.h"
46 : #include "nsIInterfaceRequestor.h"
47 : #include "nsIAsyncVerifyRedirectCallback.h"
48 : #include "nsCOMPtr.h"
49 : #include "nsAutoPtr.h"
50 : #include "nsCycleCollectionParticipant.h"
51 :
52 : class nsIChannel;
53 :
54 : /**
55 : * This class simplifies call of OnChannelRedirect of IOService and
56 : * the sink bound with the channel being redirected while the result of
57 : * redirect decision is returned through the callback.
58 : */
59 : class nsAsyncRedirectVerifyHelper : public nsIRunnable,
60 : public nsIAsyncVerifyRedirectCallback
61 : {
62 : NS_DECL_ISUPPORTS
63 : NS_DECL_NSIRUNNABLE
64 : NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
65 :
66 : public:
67 : nsAsyncRedirectVerifyHelper();
68 :
69 : /*
70 : * Calls AsyncOnChannelRedirect() on the given sink with the given
71 : * channels and flags. Keeps track of number of async callbacks to expect.
72 : */
73 : nsresult DelegateOnChannelRedirect(nsIChannelEventSink *sink,
74 : nsIChannel *oldChannel,
75 : nsIChannel *newChannel,
76 : PRUint32 flags);
77 :
78 : /**
79 : * Initialize and run the chain of AsyncOnChannelRedirect calls. OldChannel
80 : * is QI'ed for nsIAsyncVerifyRedirectCallback. The result of the redirect
81 : * decision is passed through this interface back to the oldChannel.
82 : *
83 : * @param oldChan
84 : * channel being redirected, MUST implement
85 : * nsIAsyncVerifyRedirectCallback
86 : * @param newChan
87 : * target of the redirect channel
88 : * @param flags
89 : * redirect flags
90 : * @param synchronize
91 : * set to TRUE if you want the Init method wait synchronously for
92 : * all redirect callbacks
93 : */
94 : nsresult Init(nsIChannel* oldChan,
95 : nsIChannel* newChan,
96 : PRUint32 flags,
97 : bool synchronize = false);
98 :
99 : protected:
100 : nsCOMPtr<nsIChannel> mOldChan;
101 : nsCOMPtr<nsIChannel> mNewChan;
102 : PRUint32 mFlags;
103 : bool mWaitingForRedirectCallback;
104 : nsCOMPtr<nsIThread> mCallbackThread;
105 : bool mCallbackInitiated;
106 : PRInt32 mExpectedCallbacks;
107 : nsresult mResult; // value passed to callback
108 :
109 : void InitCallback();
110 :
111 : /**
112 : * Calls back to |oldChan| as described in Init()
113 : */
114 : void ExplicitCallback(nsresult result);
115 :
116 : private:
117 : ~nsAsyncRedirectVerifyHelper();
118 :
119 : bool IsOldChannelCanceled();
120 : };
121 :
122 : /*
123 : * Helper to make the call-stack handle some control-flow for us
124 : */
125 : class nsAsyncRedirectAutoCallback
126 : {
127 : public:
128 153 : nsAsyncRedirectAutoCallback(nsIAsyncVerifyRedirectCallback* aCallback)
129 153 : : mCallback(aCallback)
130 : {
131 153 : mResult = NS_OK;
132 153 : }
133 153 : ~nsAsyncRedirectAutoCallback()
134 : {
135 153 : if (mCallback)
136 153 : mCallback->OnRedirectVerifyCallback(mResult);
137 153 : }
138 : /*
139 : * Call this is you want it to call back with a different result-code
140 : */
141 : void SetResult(nsresult aRes)
142 : {
143 : mResult = aRes;
144 : }
145 : /*
146 : * Call this is you want to avoid the callback
147 : */
148 0 : void DontCallback()
149 : {
150 0 : mCallback = nsnull;
151 0 : }
152 : private:
153 : nsIAsyncVerifyRedirectCallback* mCallback;
154 : nsresult mResult;
155 : };
156 :
157 : #endif
|