Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / netwerk / base / src / nsProtocolProxyService.h
blob65ad0ae5ad3763ff66a150cca50e27b7a8a8056d
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
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/
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.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Malcolm Smith <malsmith@cs.rmit.edu.au>
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 ***** */
39 #ifndef nsProtocolProxyService_h__
40 #define nsProtocolProxyService_h__
42 #include "nsString.h"
43 #include "nsCOMPtr.h"
44 #include "nsAutoPtr.h"
45 #include "nsVoidArray.h"
46 #include "nsIPrefBranch.h"
47 #include "nsIProtocolProxyService2.h"
48 #include "nsIProtocolProxyFilter.h"
49 #include "nsIProxyAutoConfig.h"
50 #include "nsISystemProxySettings.h"
51 #include "nsIProxyInfo.h"
52 #include "nsIObserver.h"
53 #include "nsDataHashtable.h"
54 #include "nsHashKeys.h"
55 #include "nsPACMan.h"
56 #include "prtime.h"
57 #include "prmem.h"
58 #include "prio.h"
60 typedef nsDataHashtable<nsCStringHashKey, PRUint32> nsFailedProxyTable;
62 class nsProxyInfo;
63 struct nsProtocolInfo;
65 class nsProtocolProxyService : public nsIProtocolProxyService2
66 , public nsIObserver
68 public:
69 NS_DECL_ISUPPORTS
70 NS_DECL_NSIPROTOCOLPROXYSERVICE2
71 NS_DECL_NSIPROTOCOLPROXYSERVICE
72 NS_DECL_NSIOBSERVER
74 nsProtocolProxyService() NS_HIDDEN;
76 NS_HIDDEN_(nsresult) Init();
78 protected:
79 friend class nsAsyncResolveRequest;
81 ~nsProtocolProxyService() NS_HIDDEN;
83 /**
84 * This method is called whenever a preference may have changed or
85 * to initialize all preferences.
87 * @param prefs
88 * This must be a pointer to the root pref branch.
89 * @param name
90 * This can be the name of a fully-qualified preference, or it can
91 * be null, in which case all preferences will be initialized.
93 NS_HIDDEN_(void) PrefsChanged(nsIPrefBranch *prefs, const char *name);
95 /**
96 * This method is called to create a nsProxyInfo instance from the given
97 * PAC-style proxy string. It parses up to the end of the string, or to
98 * the next ';' character.
100 * @param proxy
101 * The PAC-style proxy string to parse. This must not be null.
102 * @param result
103 * Upon return this points to a newly allocated nsProxyInfo or null
104 * if the proxy string was invalid.
106 * @return A pointer beyond the parsed proxy string (never null).
108 NS_HIDDEN_(const char *) ExtractProxyInfo(const char *proxy,
109 nsProxyInfo **result);
112 * Load the specified PAC file.
114 * @param pacURI
115 * The URI spec of the PAC file to load.
117 NS_HIDDEN_(nsresult) ConfigureFromPAC(const nsCString &pacURI, PRBool forceReload);
120 * This method builds a list of nsProxyInfo objects from the given PAC-
121 * style string.
123 * @param pacString
124 * The PAC-style proxy string to parse. This may be empty.
125 * @param result
126 * The resulting list of proxy info objects.
128 NS_HIDDEN_(void) ProcessPACString(const nsCString &pacString,
129 nsIProxyInfo **result);
132 * This method generates a string valued identifier for the given
133 * nsProxyInfo object.
135 * @param pi
136 * The nsProxyInfo object from which to generate the key.
137 * @param result
138 * Upon return, this parameter holds the generated key.
140 NS_HIDDEN_(void) GetProxyKey(nsProxyInfo *pi, nsCString &result);
143 * @return Seconds since start of session.
145 NS_HIDDEN_(PRUint32) SecondsSinceSessionStart();
148 * This method removes the specified proxy from the disabled list.
150 * @param pi
151 * The nsProxyInfo object identifying the proxy to enable.
153 NS_HIDDEN_(void) EnableProxy(nsProxyInfo *pi);
156 * This method adds the specified proxy to the disabled list.
158 * @param pi
159 * The nsProxyInfo object identifying the proxy to disable.
161 NS_HIDDEN_(void) DisableProxy(nsProxyInfo *pi);
164 * This method tests to see if the given proxy is disabled.
166 * @param pi
167 * The nsProxyInfo object identifying the proxy to test.
169 * @return True if the specified proxy is disabled.
171 NS_HIDDEN_(PRBool) IsProxyDisabled(nsProxyInfo *pi);
174 * This method queries the protocol handler for the given scheme to check
175 * for the protocol flags and default port.
177 * @param uri
178 * The URI to query.
179 * @param info
180 * Holds information about the protocol upon return. Pass address
181 * of structure when you call this method. This parameter must not
182 * be null.
184 NS_HIDDEN_(nsresult) GetProtocolInfo(nsIURI *uri, nsProtocolInfo *result);
187 * This method is an internal version nsIProtocolProxyService::newProxyInfo
188 * that expects a string literal for the type.
190 * @param type
191 * The proxy type.
192 * @param host
193 * The proxy host name (UTF-8 ok).
194 * @param port
195 * The proxy port number.
196 * @param flags
197 * The proxy flags (nsIProxyInfo::flags).
198 * @param timeout
199 * The failover timeout for this proxy.
200 * @param next
201 * The next proxy to try if this one fails.
202 * @param result
203 * The resulting nsIProxyInfo object.
205 NS_HIDDEN_(nsresult) NewProxyInfo_Internal(const char *type,
206 const nsACString &host,
207 PRInt32 port,
208 PRUint32 flags,
209 PRUint32 timeout,
210 nsIProxyInfo *next,
211 nsIProxyInfo **result);
214 * This method is an internal version of Resolve that does not query PAC.
215 * It performs all of the built-in processing, and reports back to the
216 * caller with either the proxy info result or a flag to instruct the
217 * caller to use PAC instead.
219 * @param uri
220 * The URI to test.
221 * @param info
222 * Information about the URI's protocol.
223 * @param usePAC
224 * If this flag is set upon return, then PAC should be queried to
225 * resolve the proxy info.
226 * @param result
227 * The resulting proxy info or null.
229 NS_HIDDEN_(nsresult) Resolve_Internal(nsIURI *uri,
230 const nsProtocolInfo &info,
231 PRBool *usePAC,
232 nsIProxyInfo **result);
235 * This method applies the registered filters to the given proxy info
236 * list, and returns a possibly modified list.
238 * @param uri
239 * The URI corresponding to this proxy info list.
240 * @param info
241 * Information about the URI's protocol.
242 * @param proxyInfo
243 * The proxy info list to be modified. This is an inout param.
245 NS_HIDDEN_(void) ApplyFilters(nsIURI *uri, const nsProtocolInfo &info,
246 nsIProxyInfo **proxyInfo);
249 * This method is a simple wrapper around ApplyFilters that takes the
250 * proxy info list inout param as a nsCOMPtr.
252 inline void ApplyFilters(nsIURI *uri, const nsProtocolInfo &info,
253 nsCOMPtr<nsIProxyInfo> &proxyInfo)
255 nsIProxyInfo *pi = nsnull;
256 proxyInfo.swap(pi);
257 ApplyFilters(uri, info, &pi);
258 proxyInfo.swap(pi);
262 * This method prunes out disabled and disallowed proxies from a given
263 * proxy info list.
265 * @param info
266 * Information about the URI's protocol.
267 * @param proxyInfo
268 * The proxy info list to be modified. This is an inout param.
270 NS_HIDDEN_(void) PruneProxyInfo(const nsProtocolInfo &info,
271 nsIProxyInfo **proxyInfo);
274 * This method populates mHostFiltersArray from the given string.
276 * @param hostFilters
277 * A "no-proxy-for" exclusion list.
279 NS_HIDDEN_(void) LoadHostFilters(const char *hostFilters);
282 * This method checks the given URI against mHostFiltersArray.
284 * @param uri
285 * The URI to test.
286 * @param defaultPort
287 * The default port for the given URI.
289 * @return True if the URI can use the specified proxy.
291 NS_HIDDEN_(PRBool) CanUseProxy(nsIURI *uri, PRInt32 defaultPort);
293 static PRBool CleanupFilterArray(void *aElement, void *aData);
295 public:
296 // The Sun Forte compiler and others implement older versions of the
297 // C++ standard's rules on access and nested classes. These structs
298 // need to be public in order to deal with those compilers.
300 struct HostInfoIP {
301 PRUint16 family;
302 PRUint16 mask_len;
303 PRIPv6Addr addr; // possibly IPv4-mapped address
306 struct HostInfoName {
307 char *host;
308 PRUint32 host_len;
311 protected:
313 enum ProxyConfig {
314 eProxyConfig_Direct,
315 eProxyConfig_Manual,
316 eProxyConfig_PAC,
317 eProxyConfig_Direct4x,
318 eProxyConfig_WPAD,
319 eProxyConfig_System, // use system proxy settings if available, otherwise DIRECT
320 eProxyConfig_Last
323 // simplified array of filters defined by this struct
324 struct HostInfo {
325 PRBool is_ipaddr;
326 PRInt32 port;
327 union {
328 HostInfoIP ip;
329 HostInfoName name;
332 HostInfo()
333 : is_ipaddr(PR_FALSE)
334 { /* other members intentionally uninitialized */ }
335 ~HostInfo() {
336 if (!is_ipaddr && name.host)
337 nsMemory::Free(name.host);
341 // This structure is allocated for each registered nsIProtocolProxyFilter.
342 struct FilterLink {
343 struct FilterLink *next;
344 PRUint32 position;
345 nsCOMPtr<nsIProtocolProxyFilter> filter;
347 FilterLink(PRUint32 p, nsIProtocolProxyFilter *f)
348 : next(nsnull), position(p), filter(f) {}
350 // Chain deletion to simplify cleaning up the filter links
351 ~FilterLink() { if (next) delete next; }
354 // Holds an array of HostInfo objects
355 nsVoidArray mHostFiltersArray;
357 // Points to the start of a sorted by position, singly linked list
358 // of FilterLink objects.
359 FilterLink *mFilters;
361 ProxyConfig mProxyConfig;
363 nsCString mHTTPProxyHost;
364 PRInt32 mHTTPProxyPort;
366 nsCString mFTPProxyHost;
367 PRInt32 mFTPProxyPort;
369 nsCString mGopherProxyHost;
370 PRInt32 mGopherProxyPort;
372 nsCString mHTTPSProxyHost;
373 PRInt32 mHTTPSProxyPort;
375 nsCString mSOCKSProxyHost;
376 PRInt32 mSOCKSProxyPort;
377 PRInt32 mSOCKSProxyVersion;
378 PRBool mSOCKSProxyRemoteDNS;
380 nsRefPtr<nsPACMan> mPACMan; // non-null if we are using PAC
381 nsCOMPtr<nsISystemProxySettings> mSystemProxySettings;
383 PRTime mSessionStart;
384 nsFailedProxyTable mFailedProxies;
385 PRInt32 mFailedProxyTimeout;
388 #endif // !nsProtocolProxyService_h__