Bug 1804798 - Explicitly set auto page name (and corresponding debug flag) when inser...
[gecko.git] / netwerk / base / nsProtocolProxyService.h
blob394450ca6e83e2eeb2456db2022dcfa0207c12ec
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__
9 #include "nsString.h"
10 #include "nsCOMPtr.h"
11 #include "nsTArray.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"
18 #include "nsITimer.h"
19 #include "prio.h"
20 #include "mozilla/Attributes.h"
22 class nsIPrefBranch;
23 class nsISystemProxySettings;
25 namespace mozilla {
26 namespace net {
28 using nsFailedProxyTable = nsTHashMap<nsCStringHashKey, uint32_t>;
30 class nsPACMan;
31 class nsProxyInfo;
32 struct nsProtocolInfo;
34 // CID for the nsProtocolProxyService class
35 // 091eedd8-8bae-4fe3-ad62-0c87351e640d
36 #define NS_PROTOCOL_PROXY_SERVICE_IMPL_CID \
37 { \
38 0x091eedd8, 0x8bae, 0x4fe3, { \
39 0xad, 0x62, 0x0c, 0x87, 0x35, 0x1e, 0x64, 0x0d \
40 } \
43 class nsProtocolProxyService final : public nsIProtocolProxyService2,
44 public nsIObserver,
45 public nsITimerCallback,
46 public nsINamed {
47 public:
48 NS_DECL_ISUPPORTS
49 NS_DECL_NSIPROTOCOLPROXYSERVICE2
50 NS_DECL_NSIPROTOCOLPROXYSERVICE
51 NS_DECL_NSIOBSERVER
52 NS_DECL_NSITIMERCALLBACK
53 NS_DECL_NSINAMED
55 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROTOCOL_PROXY_SERVICE_IMPL_CID)
57 nsProtocolProxyService();
59 nsresult Init();
61 public:
62 // An instance of this struct is allocated for each registered
63 // nsIProtocolProxyFilter and each nsIProtocolProxyChannelFilter.
64 class FilterLink {
65 public:
66 NS_INLINE_DECL_REFCOUNTING(FilterLink)
68 uint32_t position;
69 nsCOMPtr<nsIProtocolProxyFilter> filter;
70 nsCOMPtr<nsIProtocolProxyChannelFilter> channelFilter;
72 FilterLink(uint32_t p, nsIProtocolProxyFilter* f);
73 FilterLink(uint32_t p, nsIProtocolProxyChannelFilter* cf);
75 private:
76 ~FilterLink();
79 protected:
80 friend class nsAsyncResolveRequest;
81 friend class TestProtocolProxyService_LoadHostFilters_Test; // for gtest
83 ~nsProtocolProxyService();
85 /**
86 * This method is called whenever a preference may have changed or
87 * to initialize all preferences.
89 * @param prefs
90 * This must be a pointer to the root pref branch.
91 * @param name
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);
97 /**
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.
102 * @param proxy
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
106 * proxyInfo.
107 * @param result
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.
119 * @param pacURI
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-
126 * style string.
128 * @param pacString
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
132 * proxyInfo.
133 * @param result
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.
143 * @param pi
144 * The nsProxyInfo object from which to generate the key.
145 * @param result
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.
158 * @param pi
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.
166 * @param pi
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.
174 * @param pi
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.
185 * @param uri
186 * The URI to query.
187 * @param info
188 * Holds information about the protocol upon return. Pass address
189 * of structure when you call this method. This parameter must not
190 * be null.
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.
198 * @param type
199 * The proxy type.
200 * @param host
201 * The proxy host name (UTF-8 ok).
202 * @param port
203 * The proxy port number.
204 * @param username
205 * The username for the proxy (ASCII). May be "", but not null.
206 * @param password
207 * The password for the proxy (ASCII). May be "", but not null.
208 * @param flags
209 * The proxy flags (nsIProxyInfo::flags).
210 * @param timeout
211 * The failover timeout for this proxy.
212 * @param next
213 * The next proxy to try if this one fails.
214 * @param aResolveFlags
215 * The flags passed to resolve (from nsIProtocolProxyService).
216 * @param result
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.
234 * @param channel
235 * The channel to test.
236 * @param info
237 * Information about the URI's protocol.
238 * @param flags
239 * The flags passed to either the resolve or the asyncResolve method.
240 * @param usePAC
241 * If this flag is set upon return, then PAC should be queried to
242 * resolve the proxy info.
243 * @param result
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
268 * proxy info list.
270 * @param info
271 * Information about the URI's protocol.
272 * @param proxyInfo
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;
284 proxyInfo.swap(pi);
285 PruneProxyInfo(info, &pi);
286 proxyInfo.swap(pi);
290 * This method populates mHostFiltersArray from the given string.
292 * @param hostFilters
293 * A "no-proxy-for" exclusion list.
295 void LoadHostFilters(const nsACString& aFilters);
298 * This method checks the given URI against mHostFiltersArray.
300 * @param uri
301 * The URI to test.
302 * @param defaultPort
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.
312 * @param aProxy
313 * The proxy information
315 void MaybeDisableDNSPrefetch(nsIProxyInfo* aProxy);
317 private:
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);
327 public:
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.
332 struct HostInfoIP {
333 uint16_t family;
334 uint16_t mask_len;
335 PRIPv6Addr addr; // possibly IPv4-mapped address
338 struct HostInfoName {
339 char* host;
340 uint32_t host_len;
343 protected:
344 // simplified array of filters defined by this struct
345 struct HostInfo {
346 bool is_ipaddr{false};
347 int32_t port{0};
348 // other members intentionally uninitialized
349 union {
350 HostInfoIP ip;
351 HostInfoName name;
354 HostInfo() = default;
355 ~HostInfo() {
356 if (!is_ipaddr && name.host) {
357 free(name.host);
362 private:
363 // Private methods to insert and remove FilterLinks from the FilterLink chain.
364 nsresult InsertFilterLink(RefPtr<FilterLink>&& link);
365 nsresult RemoveFilterLink(nsISupports* givenObject);
367 protected:
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;
402 // 30 minute default
403 int32_t mFailedProxyTimeout{30 * 60};
405 private:
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)
417 } // namespace net
418 } // namespace mozilla
420 #endif // !nsProtocolProxyService_h__