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/. */
6 #ifndef nsProtocolProxyService_h__
7 #define nsProtocolProxyService_h__
12 #include "nsIProtocolProxyService2.h"
13 #include "nsIProtocolProxyFilter.h"
14 #include "nsIProxyInfo.h"
15 #include "nsIObserver.h"
16 #include "nsTHashMap.h"
17 #include "nsHashKeys.h"
20 #include "mozilla/Attributes.h"
23 class nsISystemProxySettings
;
28 using nsFailedProxyTable
= nsTHashMap
<nsCStringHashKey
, uint32_t>;
32 struct nsProtocolInfo
;
34 // CID for the nsProtocolProxyService class
35 // 091eedd8-8bae-4fe3-ad62-0c87351e640d
36 #define NS_PROTOCOL_PROXY_SERVICE_IMPL_CID \
38 0x091eedd8, 0x8bae, 0x4fe3, { \
39 0xad, 0x62, 0x0c, 0x87, 0x35, 0x1e, 0x64, 0x0d \
43 class nsProtocolProxyService final
: public nsIProtocolProxyService2
,
45 public nsITimerCallback
,
49 NS_DECL_NSIPROTOCOLPROXYSERVICE2
50 NS_DECL_NSIPROTOCOLPROXYSERVICE
52 NS_DECL_NSITIMERCALLBACK
55 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROTOCOL_PROXY_SERVICE_IMPL_CID
)
57 nsProtocolProxyService();
62 // An instance of this struct is allocated for each registered
63 // nsIProtocolProxyFilter and each nsIProtocolProxyChannelFilter.
66 NS_INLINE_DECL_REFCOUNTING(FilterLink
)
69 nsCOMPtr
<nsIProtocolProxyFilter
> filter
;
70 nsCOMPtr
<nsIProtocolProxyChannelFilter
> channelFilter
;
72 FilterLink(uint32_t p
, nsIProtocolProxyFilter
* f
);
73 FilterLink(uint32_t p
, nsIProtocolProxyChannelFilter
* cf
);
80 friend class nsAsyncResolveRequest
;
81 friend class TestProtocolProxyService_LoadHostFilters_Test
; // for gtest
83 ~nsProtocolProxyService();
86 * This method is called whenever a preference may have changed or
87 * to initialize all preferences.
90 * This must be a pointer to the root pref branch.
92 * This can be the name of a fully-qualified preference, or it can
93 * be null, in which case all preferences will be initialized.
95 void PrefsChanged(nsIPrefBranch
* prefBranch
, const char* pref
);
98 * This method is called to create a nsProxyInfo instance from the given
99 * PAC-style proxy string. It parses up to the end of the string, or to
100 * the next ';' character.
103 * The PAC-style proxy string to parse. This must not be null.
104 * @param aResolveFlags
105 * The flags passed to Resolve or AsyncResolve that are stored in
108 * Upon return this points to a newly allocated nsProxyInfo or null
109 * if the proxy string was invalid.
111 * @return A pointer beyond the parsed proxy string (never null).
113 const char* ExtractProxyInfo(const char* start
, uint32_t aResolveFlags
,
114 nsProxyInfo
** result
);
117 * Load the specified PAC file.
120 * The URI spec of the PAC file to load.
122 nsresult
ConfigureFromPAC(const nsCString
& spec
, bool forceReload
);
125 * This method builds a list of nsProxyInfo objects from the given PAC-
129 * The PAC-style proxy string to parse. This may be empty.
130 * @param aResolveFlags
131 * The flags passed to Resolve or AsyncResolve that are stored in
134 * The resulting list of proxy info objects.
136 void ProcessPACString(const nsCString
& pacString
, uint32_t aResolveFlags
,
137 nsIProxyInfo
** result
);
140 * This method generates a string valued identifier for the given
141 * nsProxyInfo object.
144 * The nsProxyInfo object from which to generate the key.
146 * Upon return, this parameter holds the generated key.
148 void GetProxyKey(nsProxyInfo
* pi
, nsCString
& key
);
151 * @return Seconds since start of session.
153 uint32_t SecondsSinceSessionStart();
156 * This method removes the specified proxy from the disabled list.
159 * The nsProxyInfo object identifying the proxy to enable.
161 void EnableProxy(nsProxyInfo
* pi
);
164 * This method adds the specified proxy to the disabled list.
167 * The nsProxyInfo object identifying the proxy to disable.
169 void DisableProxy(nsProxyInfo
* pi
);
172 * This method tests to see if the given proxy is disabled.
175 * The nsProxyInfo object identifying the proxy to test.
177 * @return True if the specified proxy is disabled.
179 bool IsProxyDisabled(nsProxyInfo
* pi
);
182 * This method queries the protocol handler for the given scheme to check
183 * for the protocol flags and default port.
188 * Holds information about the protocol upon return. Pass address
189 * of structure when you call this method. This parameter must not
192 nsresult
GetProtocolInfo(nsIURI
* uri
, nsProtocolInfo
* info
);
195 * This method is an internal version nsIProtocolProxyService::newProxyInfo
196 * that expects a string literal for the type.
201 * The proxy host name (UTF-8 ok).
203 * The proxy port number.
205 * The username for the proxy (ASCII). May be "", but not null.
207 * The password for the proxy (ASCII). May be "", but not null.
209 * The proxy flags (nsIProxyInfo::flags).
211 * The failover timeout for this proxy.
213 * The next proxy to try if this one fails.
214 * @param aResolveFlags
215 * The flags passed to resolve (from nsIProtocolProxyService).
217 * The resulting nsIProxyInfo object.
219 nsresult
NewProxyInfo_Internal(const char* type
, const nsACString
& host
,
220 int32_t port
, const nsACString
& username
,
221 const nsACString
& password
,
222 const nsACString
& aProxyAuthorizationHeader
,
223 const nsACString
& aConnectionIsolationKey
,
224 uint32_t flags
, uint32_t timeout
,
225 nsIProxyInfo
* aFailoverProxy
,
226 uint32_t aResolveFlags
, nsIProxyInfo
** result
);
229 * This method is an internal version of Resolve that does not query PAC.
230 * It performs all of the built-in processing, and reports back to the
231 * caller with either the proxy info result or a flag to instruct the
232 * caller to use PAC instead.
235 * The channel to test.
237 * Information about the URI's protocol.
239 * The flags passed to either the resolve or the asyncResolve method.
241 * If this flag is set upon return, then PAC should be queried to
242 * resolve the proxy info.
244 * The resulting proxy info or null.
246 nsresult
Resolve_Internal(nsIChannel
* channel
, const nsProtocolInfo
& info
,
247 uint32_t flags
, bool* usePAC
,
248 nsIProxyInfo
** result
);
251 * Shallow copy of the current list of registered filters so that
252 * we can safely let them asynchronously process a single proxy
253 * resolution request.
255 void CopyFilters(nsTArray
<RefPtr
<FilterLink
>>& aCopy
);
258 * This method applies the provided filter to the given proxy info
259 * list, and expects |callback| be called on (synchronously or
260 * asynchronously) to provide the updated proxyinfo list.
262 bool ApplyFilter(FilterLink
const* filterLink
, nsIChannel
* channel
,
263 const nsProtocolInfo
& info
, nsCOMPtr
<nsIProxyInfo
> list
,
264 nsIProxyProtocolFilterResult
* callback
);
267 * This method prunes out disabled and disallowed proxies from a given
271 * Information about the URI's protocol.
273 * The proxy info list to be modified. This is an inout param.
275 void PruneProxyInfo(const nsProtocolInfo
& info
, nsIProxyInfo
** list
);
278 * This method is a simple wrapper around PruneProxyInfo that takes the
279 * proxy info list inout param as a nsCOMPtr.
281 void PruneProxyInfo(const nsProtocolInfo
& info
,
282 nsCOMPtr
<nsIProxyInfo
>& proxyInfo
) {
283 nsIProxyInfo
* pi
= nullptr;
285 PruneProxyInfo(info
, &pi
);
290 * This method populates mHostFiltersArray from the given string.
293 * A "no-proxy-for" exclusion list.
295 void LoadHostFilters(const nsACString
& aFilters
);
298 * This method checks the given URI against mHostFiltersArray.
303 * The default port for the given URI.
305 * @return True if the URI can use the specified proxy.
307 bool CanUseProxy(nsIURI
* uri
, int32_t defaultPort
);
310 * Disable Prefetch in the DNS service if a proxy is in use.
313 * The proxy information
315 void MaybeDisableDNSPrefetch(nsIProxyInfo
* aProxy
);
318 nsresult
SetupPACThread(
319 nsISerialEventTarget
* mainThreadEventTarget
= nullptr);
320 nsresult
ResetPACThread();
321 nsresult
ReloadNetworkPAC();
323 nsresult
AsyncConfigureFromPAC(bool aForceReload
, bool aResetPACThread
);
324 nsresult
OnAsyncGetPACURI(bool aForceReload
, bool aResetPACThread
,
325 nsresult aResult
, const nsACString
& aUri
);
328 // The Sun Forte compiler and others implement older versions of the
329 // C++ standard's rules on access and nested classes. These structs
330 // need to be public in order to deal with those compilers.
335 PRIPv6Addr addr
; // possibly IPv4-mapped address
338 struct HostInfoName
{
344 // simplified array of filters defined by this struct
346 bool is_ipaddr
{false};
348 // other members intentionally uninitialized
354 HostInfo() = default;
356 if (!is_ipaddr
&& name
.host
) {
363 // Private methods to insert and remove FilterLinks from the FilterLink chain.
364 nsresult
InsertFilterLink(RefPtr
<FilterLink
>&& link
);
365 nsresult
RemoveFilterLink(nsISupports
* givenObject
);
368 // Indicates if local hosts (plain hostnames, no dots) should use the proxy
369 bool mFilterLocalHosts
{false};
371 // Holds an array of HostInfo objects
372 nsTArray
<UniquePtr
<HostInfo
>> mHostFiltersArray
;
374 // Filters, always sorted by the position.
375 nsTArray
<RefPtr
<FilterLink
>> mFilters
;
377 nsTArray
<nsCOMPtr
<nsIProxyConfigChangedCallback
>>
378 mProxyConfigChangedCallbacks
;
380 uint32_t mProxyConfig
{PROXYCONFIG_DIRECT
};
382 nsCString mHTTPProxyHost
;
383 int32_t mHTTPProxyPort
{-1};
385 nsCString mHTTPSProxyHost
;
386 int32_t mHTTPSProxyPort
{-1};
388 // mSOCKSProxyTarget could be a host, a domain socket path,
389 // or a named-pipe name.
390 nsCString mSOCKSProxyTarget
;
391 int32_t mSOCKSProxyPort
{-1};
392 int32_t mSOCKSProxyVersion
{4};
393 bool mSOCKSProxyRemoteDNS
{false};
394 bool mProxyOverTLS
{true};
395 bool mWPADOverDHCPEnabled
{false};
397 RefPtr
<nsPACMan
> mPACMan
; // non-null if we are using PAC
398 nsCOMPtr
<nsISystemProxySettings
> mSystemProxySettings
;
400 PRTime mSessionStart
;
401 nsFailedProxyTable mFailedProxies
;
403 int32_t mFailedProxyTimeout
{30 * 60};
406 nsresult
AsyncResolveInternal(nsIChannel
* channel
, uint32_t flags
,
407 nsIProtocolProxyCallback
* callback
,
408 nsICancelable
** result
, bool isSyncOK
,
409 nsISerialEventTarget
* mainThreadEventTarget
);
410 bool mIsShutdown
{false};
411 nsCOMPtr
<nsITimer
> mReloadPACTimer
;
414 NS_DEFINE_STATIC_IID_ACCESSOR(nsProtocolProxyService
,
415 NS_PROTOCOL_PROXY_SERVICE_IMPL_CID
)
418 } // namespace mozilla
420 #endif // !nsProtocolProxyService_h__