1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@meer.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include "nsIStreamLoader.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIChannelEventSink.h"
45 #include "nsIProxyAutoConfig.h"
52 * This class defines a callback interface used by AsyncGetProxyForURI.
54 class NS_NO_VTABLE nsPACManCallback
: public nsISupports
58 * This method is invoked on the same thread that called AsyncGetProxyForURI.
61 * This parameter indicates whether or not the PAC query succeeded.
63 * This parameter holds the value of the PAC string. It is empty when
64 * status is a failure code.
66 virtual void OnQueryComplete(nsresult status
, const nsCString
&pacString
) = 0;
70 * This class provides an abstraction layer above the PAC thread. The methods
71 * defined on this class are intended to be called on the main thread only.
73 class nsPACMan
: public nsIStreamLoaderObserver
74 , public nsIInterfaceRequestor
75 , public nsIChannelEventSink
83 * This method may be called to shutdown the PAC manager. Any async queries
84 * that have not yet completed will either finish normally or be canceled by
85 * the time this method returns.
90 * This method queries a PAC result synchronously.
95 * Holds the PAC result string upon return.
97 * @return NS_ERROR_IN_PROGRESS if the PAC file is not yet loaded.
98 * @return NS_ERROR_NOT_AVAILABLE if the PAC file could not be loaded.
100 nsresult
GetProxyForURI(nsIURI
*uri
, nsACString
&result
);
103 * This method queries a PAC result asynchronously. The callback runs on the
104 * calling thread. If the PAC file has not yet been loaded, then this method
105 * will queue up the request, and complete it once the PAC file has been
111 * The callback to run once the PAC result is available.
113 nsresult
AsyncGetProxyForURI(nsIURI
*uri
, nsPACManCallback
*callback
);
116 * This method may be called to reload the PAC file. While we are loading
117 * the PAC file, any asynchronous PAC queries will be queued up to be
118 * processed once the PAC file finishes loading.
121 * The nsIURI of the PAC file to load. If this parameter is null,
122 * then the previous PAC URI is simply reloaded.
124 nsresult
LoadPACFromURI(nsIURI
*pacURI
);
127 * Returns true if we are currently loading the PAC file.
129 PRBool
IsLoading() { return mLoader
!= nsnull
; }
132 * Returns true if the given URI matches the URI of our PAC file.
134 PRBool
IsPACURI(nsIURI
*uri
) {
136 return mPACURI
&& NS_SUCCEEDED(mPACURI
->Equals(uri
, &result
)) && result
;
140 NS_DECL_NSISTREAMLOADEROBSERVER
141 NS_DECL_NSIINTERFACEREQUESTOR
142 NS_DECL_NSICHANNELEVENTSINK
147 * Cancel any existing load if any.
149 void CancelExistingLoad();
152 * Process mPendingQ. If status is a failure code, then the pending queue
153 * will be emptied. If status is a success code, then the pending requests
154 * will be processed (i.e., their Start method will be called).
156 void ProcessPendingQ(nsresult status
);
159 * Start loading the PAC file.
164 * Reload the PAC file if there is reason to.
166 void MaybeReloadPAC();
169 * Called when we fail to load the PAC file.
171 void OnLoadFailure();
174 nsCOMPtr
<nsIProxyAutoConfig
> mPAC
;
175 nsCOMPtr
<nsIURI
> mPACURI
;
177 nsCOMPtr
<nsIStreamLoader
> mLoader
;
178 PRPackedBool mLoadPending
;
179 PRPackedBool mShutdown
;
180 PRTime mScheduledReload
;
181 PRUint32 mLoadFailureCount
;
184 #endif // nsPACMan_h__