Backed out 2 changesets (bug 1855992) for causing talos failures @ mozilla::net:...
[gecko.git] / netwerk / base / nsPACMan.cpp
blob0cf66c8ee6daefb32ca4c579e1ddf5951b52051c
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 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsPACMan.h"
9 #include "mozilla/Preferences.h"
10 #include "nsContentUtils.h"
11 #include "nsComponentManagerUtils.h"
12 #include "nsIAsyncVerifyRedirectCallback.h"
13 #include "nsIAuthPrompt.h"
14 #include "nsIDHCPClient.h"
15 #include "nsIHttpChannel.h"
16 #include "nsIPrefBranch.h"
17 #include "nsIPromptFactory.h"
18 #include "nsIProtocolProxyService.h"
19 #include "nsISystemProxySettings.h"
20 #include "nsIOService.h"
21 #include "nsNetUtil.h"
22 #include "nsThreadUtils.h"
23 #include "mozilla/ResultExtensions.h"
24 #include "mozilla/StaticPrefs_network.h"
25 #include "mozilla/Telemetry.h"
26 #include "mozilla/Try.h"
28 //-----------------------------------------------------------------------------
30 namespace mozilla {
31 namespace net {
33 LazyLogModule gProxyLog("proxy");
35 #undef LOG
36 #define LOG(args) MOZ_LOG(gProxyLog, LogLevel::Debug, args)
37 #define MOZ_WPAD_URL "http://wpad/wpad.dat"
38 #define MOZ_DHCP_WPAD_OPTION 252
40 // These pointers are declared in nsProtocolProxyService.cpp
41 extern const char kProxyType_HTTPS[];
42 extern const char kProxyType_DIRECT[];
44 // The PAC thread does evaluations of both PAC files and
45 // nsISystemProxySettings because they can both block the calling thread and we
46 // don't want that on the main thread
48 // Check to see if the underlying request was not an error page in the case of
49 // a HTTP request. For other types of channels, just return true.
50 static bool HttpRequestSucceeded(nsIStreamLoader* loader) {
51 nsCOMPtr<nsIRequest> request;
52 loader->GetRequest(getter_AddRefs(request));
54 bool result = true; // default to assuming success
56 nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
57 if (httpChannel) {
58 // failsafe
59 Unused << httpChannel->GetRequestSucceeded(&result);
62 return result;
65 // Read preference setting of extra JavaScript context heap size.
66 // PrefService tends to be run on main thread, where ProxyAutoConfig runs on
67 // ProxyResolution thread, so it's read here and passed to ProxyAutoConfig.
68 static uint32_t GetExtraJSContextHeapSize() {
69 MOZ_ASSERT(NS_IsMainThread());
71 static int32_t extraSize = -1;
73 if (extraSize < 0) {
74 nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
75 int32_t value;
77 if (prefs &&
78 NS_SUCCEEDED(prefs->GetIntPref(
79 "network.proxy.autoconfig_extra_jscontext_heap_size", &value))) {
80 LOG(("autoconfig_extra_jscontext_heap_size: %d\n", value));
82 extraSize = value;
86 return extraSize < 0 ? 0 : extraSize;
89 // Read network proxy type from preference
90 // Used to verify that the preference is WPAD in nsPACMan::ConfigureWPAD
91 nsresult GetNetworkProxyTypeFromPref(int32_t* type) {
92 *type = 0;
93 nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
95 if (!prefs) {
96 LOG(("Failed to get a preference service object"));
97 return NS_ERROR_FACTORY_NOT_REGISTERED;
99 nsresult rv = prefs->GetIntPref("network.proxy.type", type);
100 if (NS_FAILED(rv)) {
101 LOG(("Failed to retrieve network.proxy.type from prefs"));
102 return rv;
104 LOG(("network.proxy.type pref retrieved: %d\n", *type));
105 return NS_OK;
108 //-----------------------------------------------------------------------------
110 // The ExecuteCallback runnable is triggered by
111 // nsPACManCallback::OnQueryComplete on the Main thread when its completion is
112 // discovered on the pac thread
114 class ExecuteCallback final : public Runnable {
115 public:
116 ExecuteCallback(nsPACManCallback* aCallback, nsresult status)
117 : Runnable("net::ExecuteCallback"),
118 mCallback(aCallback),
119 mStatus(status) {}
121 void SetPACString(const nsACString& pacString) { mPACString = pacString; }
123 void SetPACURL(const nsACString& pacURL) { mPACURL = pacURL; }
125 NS_IMETHOD Run() override {
126 mCallback->OnQueryComplete(mStatus, mPACString, mPACURL);
127 mCallback = nullptr;
128 return NS_OK;
131 private:
132 RefPtr<nsPACManCallback> mCallback;
133 nsresult mStatus;
134 nsCString mPACString;
135 nsCString mPACURL;
138 //-----------------------------------------------------------------------------
140 // The PAC thread must be deleted from the main thread, this class
141 // acts as a proxy to do that, as the PACMan is reference counted
142 // and might be destroyed on either thread
144 class ShutdownThread final : public Runnable {
145 public:
146 explicit ShutdownThread(nsIThread* thread)
147 : Runnable("net::ShutdownThread"), mThread(thread) {}
149 NS_IMETHOD Run() override {
150 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
151 mThread->Shutdown();
152 return NS_OK;
155 private:
156 nsCOMPtr<nsIThread> mThread;
159 // Dispatch this to wait until the PAC thread shuts down.
161 class WaitForThreadShutdown final : public Runnable {
162 public:
163 explicit WaitForThreadShutdown(nsPACMan* aPACMan)
164 : Runnable("net::WaitForThreadShutdown"), mPACMan(aPACMan) {}
166 NS_IMETHOD Run() override {
167 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
168 if (mPACMan->mPACThread) {
169 mPACMan->mPACThread->Shutdown();
170 mPACMan->mPACThread = nullptr;
172 return NS_OK;
175 private:
176 RefPtr<nsPACMan> mPACMan;
179 //-----------------------------------------------------------------------------
181 // PACLoadComplete allows the PAC thread to tell the main thread that
182 // the javascript PAC file has been installed (perhaps unsuccessfully)
183 // and that there is no reason to queue executions anymore
185 class PACLoadComplete final : public Runnable {
186 public:
187 explicit PACLoadComplete(nsPACMan* aPACMan)
188 : Runnable("net::PACLoadComplete"), mPACMan(aPACMan) {}
190 NS_IMETHOD Run() override {
191 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
193 auto loader = mPACMan->mLoader.Lock();
194 loader.ref() = nullptr;
196 mPACMan->PostProcessPendingQ();
197 return NS_OK;
200 private:
201 RefPtr<nsPACMan> mPACMan;
204 //-----------------------------------------------------------------------------
206 // ConfigureWPADComplete allows the PAC thread to tell the main thread that
207 // the URL for the PAC file has been found
208 class ConfigureWPADComplete final : public Runnable {
209 public:
210 ConfigureWPADComplete(nsPACMan* aPACMan, const nsACString& aPACURISpec)
211 : Runnable("net::ConfigureWPADComplete"),
212 mPACMan(aPACMan),
213 mPACURISpec(aPACURISpec) {}
215 NS_IMETHOD Run() override {
216 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
217 mPACMan->AssignPACURISpec(mPACURISpec);
218 mPACMan->ContinueLoadingAfterPACUriKnown();
219 return NS_OK;
222 private:
223 RefPtr<nsPACMan> mPACMan;
224 nsCString mPACURISpec;
227 //-----------------------------------------------------------------------------
229 // ExecutePACThreadAction is used to proxy actions from the main
230 // thread onto the PAC thread. There are 4 options: process the queue,
231 // cancel the queue, query DHCP for the PAC option
232 // and setup the javascript context with a new PAC file
234 class ExecutePACThreadAction final : public Runnable {
235 public:
236 // by default we just process the queue
237 explicit ExecutePACThreadAction(nsPACMan* aPACMan)
238 : Runnable("net::ExecutePACThreadAction"),
239 mPACMan(aPACMan),
240 mCancel(false),
241 mCancelStatus(NS_OK),
242 mSetupPAC(false),
243 mExtraHeapSize(0),
244 mConfigureWPAD(false),
245 mShutdown(false) {}
247 void CancelQueue(nsresult status, bool aShutdown) {
248 mCancel = true;
249 mCancelStatus = status;
250 mShutdown = aShutdown;
253 void SetupPAC(const char* data, uint32_t dataLen, const nsACString& pacURI,
254 uint32_t extraHeapSize) {
255 mSetupPAC = true;
256 mSetupPACData.Assign(data, dataLen);
257 mSetupPACURI = pacURI;
258 mExtraHeapSize = extraHeapSize;
261 void ConfigureWPAD() { mConfigureWPAD = true; }
263 NS_IMETHOD Run() override {
264 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
265 if (mCancel) {
266 mPACMan->CancelPendingQ(mCancelStatus, mShutdown);
267 mCancel = false;
268 return NS_OK;
271 if (mSetupPAC) {
272 mSetupPAC = false;
274 nsCOMPtr<nsISerialEventTarget> target = mPACMan->GetNeckoTarget();
275 mPACMan->mPAC->ConfigurePAC(mSetupPACURI, mSetupPACData,
276 mPACMan->mIncludePath, mExtraHeapSize,
277 target);
279 RefPtr<PACLoadComplete> runnable = new PACLoadComplete(mPACMan);
280 mPACMan->Dispatch(runnable.forget());
281 return NS_OK;
284 if (mConfigureWPAD) {
285 nsAutoCString spec;
286 mConfigureWPAD = false;
287 mPACMan->ConfigureWPAD(spec);
288 RefPtr<ConfigureWPADComplete> runnable =
289 new ConfigureWPADComplete(mPACMan, spec);
290 mPACMan->Dispatch(runnable.forget());
291 return NS_OK;
294 mPACMan->ProcessPendingQ();
295 return NS_OK;
298 private:
299 RefPtr<nsPACMan> mPACMan;
301 bool mCancel;
302 nsresult mCancelStatus;
304 bool mSetupPAC;
305 uint32_t mExtraHeapSize;
306 nsCString mSetupPACData;
307 nsCString mSetupPACURI;
308 bool mConfigureWPAD;
309 bool mShutdown;
312 //-----------------------------------------------------------------------------
314 PendingPACQuery::PendingPACQuery(nsPACMan* pacMan, nsIURI* uri,
315 nsPACManCallback* callback, uint32_t flags,
316 bool mainThreadResponse)
317 : Runnable("net::PendingPACQuery"),
318 mPort(0),
319 mFlags(flags),
320 mPACMan(pacMan),
321 mCallback(callback),
322 mOnMainThreadOnly(mainThreadResponse) {
323 uri->GetAsciiSpec(mSpec);
324 uri->GetAsciiHost(mHost);
325 uri->GetScheme(mScheme);
326 uri->GetPort(&mPort);
329 void PendingPACQuery::Complete(nsresult status, const nsACString& pacString) {
330 if (!mCallback) return;
331 RefPtr<ExecuteCallback> runnable = new ExecuteCallback(mCallback, status);
332 runnable->SetPACString(pacString);
333 if (mOnMainThreadOnly) {
334 mPACMan->Dispatch(runnable.forget());
335 } else {
336 runnable->Run();
340 void PendingPACQuery::UseAlternatePACFile(const nsACString& pacURL) {
341 if (!mCallback) return;
343 RefPtr<ExecuteCallback> runnable = new ExecuteCallback(mCallback, NS_OK);
344 runnable->SetPACURL(pacURL);
345 if (mOnMainThreadOnly) {
346 mPACMan->Dispatch(runnable.forget());
347 } else {
348 runnable->Run();
352 NS_IMETHODIMP
353 PendingPACQuery::Run() {
354 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
355 mPACMan->PostQuery(this);
356 return NS_OK;
359 //-----------------------------------------------------------------------------
361 static bool sThreadLocalSetup = false;
362 static uint32_t sThreadLocalIndex = 0xdeadbeef; // out of range
364 static const char* kPACIncludePath =
365 "network.proxy.autoconfig_url.include_path";
367 nsPACMan::nsPACMan(nsISerialEventTarget* mainThreadEventTarget)
368 : NeckoTargetHolder(mainThreadEventTarget),
369 mLoader("nsPACMan::mLoader"),
370 mLoadPending(false),
371 mShutdown(false),
372 mLoadFailureCount(0),
373 mInProgress(false),
374 mAutoDetect(false),
375 mWPADOverDHCPEnabled(false),
376 mProxyConfigType(0) {
377 MOZ_ASSERT(NS_IsMainThread(), "pacman must be created on main thread");
378 mIncludePath = Preferences::GetBool(kPACIncludePath, false);
379 if (StaticPrefs::network_proxy_parse_pac_on_socket_process() &&
380 gIOService->SocketProcessReady()) {
381 mPAC = MakeUnique<RemoteProxyAutoConfig>();
382 } else {
383 mPAC = MakeUnique<ProxyAutoConfig>();
384 if (!sThreadLocalSetup) {
385 sThreadLocalSetup = true;
386 PR_NewThreadPrivateIndex(&sThreadLocalIndex, nullptr);
388 mPAC->SetThreadLocalIndex(sThreadLocalIndex);
392 nsPACMan::~nsPACMan() {
393 MOZ_ASSERT(mShutdown, "Shutdown must be called before dtor.");
395 if (mPACThread) {
396 if (NS_IsMainThread()) {
397 mPACThread->Shutdown();
398 mPACThread = nullptr;
399 } else {
400 RefPtr<ShutdownThread> runnable = new ShutdownThread(mPACThread);
401 Dispatch(runnable.forget());
405 #ifdef DEBUG
407 auto loader = mLoader.Lock();
408 NS_ASSERTION(loader.ref() == nullptr, "pac man not shutdown properly");
410 #endif
412 NS_ASSERTION(mPendingQ.isEmpty(), "pac man not shutdown properly");
415 void nsPACMan::Shutdown() {
416 MOZ_ASSERT(NS_IsMainThread(), "pacman must be shutdown on main thread");
417 if (mShutdown) {
418 return;
421 CancelExistingLoad();
423 if (mPACThread) {
424 PostCancelPendingQ(NS_ERROR_ABORT, /*aShutdown =*/true);
426 // Shutdown is initiated from an observer. We don't want to block the
427 // observer service on thread shutdown so we post a shutdown runnable that
428 // will run after we return instead.
429 RefPtr<WaitForThreadShutdown> runnable = new WaitForThreadShutdown(this);
430 Dispatch(runnable.forget());
433 mShutdown = true;
436 nsresult nsPACMan::DispatchToPAC(already_AddRefed<nsIRunnable> aEvent,
437 bool aSync) {
438 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
440 nsCOMPtr<nsIRunnable> e(aEvent);
442 if (mShutdown) {
443 return NS_ERROR_NOT_AVAILABLE;
446 // Lazily create the PAC thread. This method is main-thread only so we don't
447 // have to worry about threading issues here.
448 if (!mPACThread) {
449 MOZ_TRY(NS_NewNamedThread("ProxyResolution", getter_AddRefs(mPACThread)));
450 nsresult rv = mPAC->Init(mPACThread);
451 if (NS_FAILED(rv)) {
452 return rv;
456 if (aSync) {
457 return NS_DispatchAndSpinEventLoopUntilComplete(
458 "nsPACMan::DispatchToPAC"_ns, mPACThread, e.forget());
459 } else {
460 return mPACThread->Dispatch(e.forget());
464 nsresult nsPACMan::AsyncGetProxyForURI(nsIURI* uri, nsPACManCallback* callback,
465 uint32_t flags,
466 bool mainThreadResponse) {
467 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
468 if (mShutdown) return NS_ERROR_NOT_AVAILABLE;
470 // Maybe Reload PAC
471 if (!mPACURISpec.IsEmpty() && !mScheduledReload.IsNull() &&
472 TimeStamp::Now() > mScheduledReload) {
473 LOG(("nsPACMan::AsyncGetProxyForURI reload as scheduled\n"));
475 LoadPACFromURI(mAutoDetect ? ""_ns : mPACURISpec, false);
478 RefPtr<PendingPACQuery> query =
479 new PendingPACQuery(this, uri, callback, flags, mainThreadResponse);
481 if (IsPACURI(uri)) {
482 // deal with this directly instead of queueing it
483 query->Complete(NS_OK, ""_ns);
484 return NS_OK;
487 return DispatchToPAC(query.forget());
490 nsresult nsPACMan::PostQuery(PendingPACQuery* query) {
491 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
493 if (mShutdown) {
494 query->Complete(NS_ERROR_NOT_AVAILABLE, ""_ns);
495 return NS_OK;
498 // add a reference to the query while it is in the pending list
499 RefPtr<PendingPACQuery> addref(query);
500 mPendingQ.insertBack(addref.forget().take());
501 ProcessPendingQ();
502 return NS_OK;
505 nsresult nsPACMan::LoadPACFromURI(const nsACString& aSpec) {
506 return LoadPACFromURI(aSpec, true);
509 nsresult nsPACMan::LoadPACFromURI(const nsACString& aSpec,
510 bool aResetLoadFailureCount) {
511 NS_ENSURE_STATE(!mShutdown);
513 nsCOMPtr<nsIStreamLoader> loader =
514 do_CreateInstance(NS_STREAMLOADER_CONTRACTID);
515 NS_ENSURE_STATE(loader);
517 LOG(("nsPACMan::LoadPACFromURI aSpec: %s, aResetLoadFailureCount: %s\n",
518 aSpec.BeginReading(), aResetLoadFailureCount ? "true" : "false"));
520 CancelExistingLoad();
523 auto locked = mLoader.Lock();
524 locked.ref() = loader.forget();
526 mPACURIRedirectSpec.Truncate();
527 mNormalPACURISpec.Truncate(); // set at load time
528 if (aResetLoadFailureCount) {
529 mLoadFailureCount = 0;
531 mAutoDetect = aSpec.IsEmpty();
532 mPACURISpec.Assign(aSpec);
534 // reset to Null
535 mScheduledReload = TimeStamp();
537 // if we're on the main thread here so we can get hold of prefs,
538 // we check that we have WPAD preffed on if we're auto-detecting
539 if (mAutoDetect && NS_IsMainThread()) {
540 nsresult rv = GetNetworkProxyTypeFromPref(&mProxyConfigType);
541 if (NS_FAILED(rv)) {
542 return rv;
544 if (mProxyConfigType != nsIProtocolProxyService::PROXYCONFIG_WPAD) {
545 LOG(
546 ("LoadPACFromURI - Aborting WPAD autodetection because the pref "
547 "doesn't match anymore"));
548 return NS_BINDING_ABORTED;
551 // Since we might get called from nsProtocolProxyService::Init, we need to
552 // post an event back to the main thread before we try to use the IO service.
554 // But, we need to flag ourselves as loading, so that we queue up any PAC
555 // queries the enter between now and when we actually load the PAC file.
557 if (!mLoadPending) {
558 nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod(
559 "nsPACMan::StartLoading", this, &nsPACMan::StartLoading);
560 nsresult rv =
561 NS_IsMainThread()
562 ? Dispatch(runnable.forget())
563 : GetCurrentSerialEventTarget()->Dispatch(runnable.forget());
564 if (NS_FAILED(rv)) return rv;
565 mLoadPending = true;
568 return NS_OK;
571 nsresult nsPACMan::GetPACFromDHCP(nsACString& aSpec) {
572 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
573 if (!mDHCPClient) {
574 LOG(
575 ("nsPACMan::GetPACFromDHCP DHCP option %d query failed because there "
576 "is no DHCP client available\n",
577 MOZ_DHCP_WPAD_OPTION));
578 return NS_ERROR_NOT_IMPLEMENTED;
580 nsresult rv;
581 rv = mDHCPClient->GetOption(MOZ_DHCP_WPAD_OPTION, aSpec);
582 if (NS_FAILED(rv)) {
583 LOG((
584 "nsPACMan::GetPACFromDHCP DHCP option %d query failed with result %d\n",
585 MOZ_DHCP_WPAD_OPTION, (uint32_t)rv));
586 } else {
587 LOG(
588 ("nsPACMan::GetPACFromDHCP DHCP option %d query succeeded, finding PAC "
589 "URL %s\n",
590 MOZ_DHCP_WPAD_OPTION, aSpec.BeginReading()));
592 return rv;
595 nsresult nsPACMan::ConfigureWPAD(nsACString& aSpec) {
596 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
598 if (mProxyConfigType != nsIProtocolProxyService::PROXYCONFIG_WPAD) {
599 LOG(
600 ("ConfigureWPAD - Aborting WPAD autodetection because the pref "
601 "doesn't match anymore"));
602 return NS_BINDING_ABORTED;
605 aSpec.Truncate();
606 if (mWPADOverDHCPEnabled) {
607 GetPACFromDHCP(aSpec);
610 if (aSpec.IsEmpty()) {
611 // We diverge from the WPAD spec here in that we don't walk the
612 // hosts's FQDN, stripping components until we hit a TLD. Doing so
613 // is dangerous in the face of an incomplete list of TLDs, and TLDs
614 // get added over time. We could consider doing only a single
615 // substitution of the first component, if that proves to help
616 // compatibility.
617 aSpec.AssignLiteral(MOZ_WPAD_URL);
619 return NS_OK;
622 void nsPACMan::AssignPACURISpec(const nsACString& aSpec) {
623 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
624 mPACURISpec.Assign(aSpec);
627 void nsPACMan::StartLoading() {
628 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
629 mLoadPending = false;
632 // CancelExistingLoad was called...
633 nsCOMPtr<nsIStreamLoader> loader;
635 auto locked = mLoader.Lock();
636 loader = locked.ref();
638 if (!loader) {
639 PostCancelPendingQ(NS_ERROR_ABORT);
640 return;
644 if (mAutoDetect) {
645 nsresult rv = GetNetworkProxyTypeFromPref(&mProxyConfigType);
646 if (NS_FAILED(rv)) {
647 NS_WARNING(
648 "Could not retrieve Network Proxy Type pref when auto-detecting "
649 "proxy. Halting.");
650 return;
652 RefPtr<ExecutePACThreadAction> wpadConfigurer =
653 new ExecutePACThreadAction(this);
654 wpadConfigurer->ConfigureWPAD();
655 DispatchToPAC(wpadConfigurer.forget());
656 } else {
657 ContinueLoadingAfterPACUriKnown();
661 void nsPACMan::ContinueLoadingAfterPACUriKnown() {
662 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
664 nsCOMPtr<nsIStreamLoader> loader;
666 auto locked = mLoader.Lock();
667 loader = locked.ref();
670 // CancelExistingLoad was called...
671 if (!loader) {
672 PostCancelPendingQ(NS_ERROR_ABORT);
673 return;
675 if (NS_SUCCEEDED(loader->Init(this, nullptr))) {
676 // Always hit the origin server when loading PAC.
677 nsCOMPtr<nsIIOService> ios = do_GetIOService();
678 if (ios) {
679 nsCOMPtr<nsIChannel> channel;
680 nsCOMPtr<nsIURI> pacURI;
681 NS_NewURI(getter_AddRefs(pacURI), mPACURISpec);
683 // NOTE: This results in GetProxyForURI being called
684 if (pacURI) {
685 nsresult rv = pacURI->GetSpec(mNormalPACURISpec);
686 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
687 NS_NewChannel(getter_AddRefs(channel), pacURI,
688 nsContentUtils::GetSystemPrincipal(),
689 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
690 nsIContentPolicy::TYPE_OTHER,
691 nullptr, // nsICookieJarSettings
692 nullptr, // PerformanceStorage
693 nullptr, // aLoadGroup
694 nullptr, // aCallbacks
695 nsIRequest::LOAD_NORMAL, ios);
696 } else {
697 LOG(("nsPACMan::StartLoading Failed pacspec uri conversion %s\n",
698 mPACURISpec.get()));
701 if (channel) {
702 // allow deprecated HTTP request from SystemPrincipal
703 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
704 loadInfo->SetAllowDeprecatedSystemRequests(true);
705 loadInfo->SetHttpsOnlyStatus(nsILoadInfo::HTTPS_ONLY_EXEMPT);
707 channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE);
708 channel->SetNotificationCallbacks(this);
709 if (NS_SUCCEEDED(channel->AsyncOpen(loader))) return;
714 CancelExistingLoad();
715 PostCancelPendingQ(NS_ERROR_UNEXPECTED);
718 void nsPACMan::OnLoadFailure() {
719 int32_t minInterval = 5; // 5 seconds
720 int32_t maxInterval = 300; // 5 minutes
722 nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
723 if (prefs) {
724 prefs->GetIntPref("network.proxy.autoconfig_retry_interval_min",
725 &minInterval);
726 prefs->GetIntPref("network.proxy.autoconfig_retry_interval_max",
727 &maxInterval);
730 int32_t interval = minInterval << mLoadFailureCount++; // seconds
731 if (!interval || interval > maxInterval) interval = maxInterval;
733 mScheduledReload = TimeStamp::Now() + TimeDuration::FromSeconds(interval);
735 LOG(("OnLoadFailure: retry in %d seconds (%d fails)\n", interval,
736 (uint32_t)mLoadFailureCount));
738 // while we wait for the retry queued members should try direct
739 // even if that means fast failure.
740 PostCancelPendingQ(NS_ERROR_NOT_AVAILABLE);
743 void nsPACMan::CancelExistingLoad() {
744 nsCOMPtr<nsIStreamLoader> loader;
746 auto locked = mLoader.Lock();
747 loader.swap(*locked);
749 if (loader) {
750 nsCOMPtr<nsIRequest> request;
751 loader->GetRequest(getter_AddRefs(request));
752 if (request) {
753 request->Cancel(NS_ERROR_ABORT);
758 void nsPACMan::PostProcessPendingQ() {
759 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
760 RefPtr<ExecutePACThreadAction> pending = new ExecutePACThreadAction(this);
761 DispatchToPAC(pending.forget());
764 void nsPACMan::PostCancelPendingQ(nsresult status, bool aShutdown) {
765 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
766 RefPtr<ExecutePACThreadAction> pending = new ExecutePACThreadAction(this);
767 pending->CancelQueue(status, aShutdown);
768 DispatchToPAC(pending.forget());
771 void nsPACMan::CancelPendingQ(nsresult status, bool aShutdown) {
772 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
773 RefPtr<PendingPACQuery> query;
775 while (!mPendingQ.isEmpty()) {
776 query = dont_AddRef(mPendingQ.popLast());
777 query->Complete(status, ""_ns);
780 if (aShutdown) {
781 mPAC->Shutdown();
785 void nsPACMan::ProcessPendingQ() {
786 MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
787 while (ProcessPending()) {
791 if (mShutdown) {
792 mPAC->Shutdown();
793 } else {
794 // do GC while the thread has nothing pending
795 mPAC->GC();
799 // returns true if progress was made by shortening the queue
800 bool nsPACMan::ProcessPending() {
801 if (mPendingQ.isEmpty()) return false;
803 // queue during normal load, but if we are retrying a failed load then
804 // fast fail the queries
805 if (mInProgress || (IsLoading() && !mLoadFailureCount)) return false;
807 RefPtr<PendingPACQuery> query(dont_AddRef(mPendingQ.popFirst()));
809 // Having |mLoadFailureCount > 0| means we haven't had a sucessful PAC load
810 // yet. We should use DIRECT instead.
811 if (mShutdown || IsLoading() || mLoadFailureCount > 0) {
812 query->Complete(NS_ERROR_NOT_AVAILABLE, ""_ns);
813 return true;
816 nsAutoCString pacString;
817 bool completed = false;
818 mInProgress = true;
819 nsAutoCString PACURI;
821 // first we need to consider the system proxy changing the pac url
822 if (mSystemProxySettings &&
823 NS_SUCCEEDED(mSystemProxySettings->GetPACURI(PACURI)) &&
824 !PACURI.IsEmpty() && !PACURI.Equals(mPACURISpec)) {
825 query->UseAlternatePACFile(PACURI);
826 LOG(("Use PAC from system settings: %s\n", PACURI.get()));
827 completed = true;
830 // now try the system proxy settings for this particular url if
831 // PAC was not specified
832 if (!completed && mSystemProxySettings && PACURI.IsEmpty() &&
833 NS_SUCCEEDED(mSystemProxySettings->GetProxyForURI(
834 query->mSpec, query->mScheme, query->mHost, query->mPort,
835 pacString))) {
836 if (query->mFlags & nsIProtocolProxyService::RESOLVE_PREFER_SOCKS_PROXY &&
837 query->mFlags & nsIProtocolProxyService::RESOLVE_PREFER_HTTPS_PROXY) {
838 if (StringBeginsWith(pacString, nsDependentCString(kProxyType_DIRECT),
839 nsCaseInsensitiveUTF8StringComparator)) {
840 // DIRECT indicates that system proxy settings are not configured to use
841 // SOCKS proxy. Try https proxy as a secondary preferrable proxy. This
842 // is mainly for websocket whose precedence is SOCKS > HTTPS > DIRECT.
843 NS_SUCCEEDED(mSystemProxySettings->GetProxyForURI(
844 query->mSpec, nsDependentCString(kProxyType_HTTPS), query->mHost,
845 query->mPort, pacString));
848 LOG(("Use proxy from system settings: %s\n", pacString.get()));
849 query->Complete(NS_OK, pacString);
850 completed = true;
853 // the systemproxysettings didn't complete the resolution. try via PAC
854 if (!completed) {
855 auto callback = [query(query)](nsresult aStatus,
856 const nsACString& aResult) {
857 LOG(("Use proxy from PAC: %s\n", PromiseFlatCString(aResult).get()));
858 query->Complete(aStatus, aResult);
860 mPAC->GetProxyForURIWithCallback(query->mSpec, query->mHost,
861 std::move(callback));
864 mInProgress = false;
865 return true;
868 NS_IMPL_ISUPPORTS(nsPACMan, nsIStreamLoaderObserver, nsIInterfaceRequestor,
869 nsIChannelEventSink)
871 NS_IMETHODIMP
872 nsPACMan::OnStreamComplete(nsIStreamLoader* loader, nsISupports* context,
873 nsresult status, uint32_t dataLen,
874 const uint8_t* data) {
875 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
877 bool loadSucceeded = NS_SUCCEEDED(status) && HttpRequestSucceeded(loader);
879 auto locked = mLoader.Lock();
880 if (locked.ref() != loader) {
881 // If this happens, then it means that LoadPACFromURI was called more
882 // than once before the initial call completed. In this case, status
883 // should be NS_ERROR_ABORT, and if so, then we know that we can and
884 // should delay any processing.
885 LOG(("OnStreamComplete: called more than once\n"));
886 if (status == NS_ERROR_ABORT) {
887 return NS_OK;
889 } else if (!loadSucceeded) {
890 // We have to clear the loader to indicate that we are not loading PAC
891 // currently.
892 // Note that we can only clear the loader when |loader| and |mLoader| are
893 // the same one.
894 locked.ref() = nullptr;
898 LOG(("OnStreamComplete: entry\n"));
900 if (loadSucceeded) {
901 // Get the URI spec used to load this PAC script.
902 nsAutoCString pacURI;
904 nsCOMPtr<nsIRequest> request;
905 loader->GetRequest(getter_AddRefs(request));
906 nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
907 if (channel) {
908 nsCOMPtr<nsIURI> uri;
909 channel->GetURI(getter_AddRefs(uri));
910 if (uri) uri->GetAsciiSpec(pacURI);
914 nsCOMPtr<nsIProtocolProxyService> pps =
915 do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID);
916 MOZ_ASSERT(pps);
917 if (pps) {
918 pps->NotifyProxyConfigChangedInternal();
921 // We succeeded in loading the pac file using a bunch of interfaces that are
922 // main thread only. Unfortunately, we have to initialize the instance of
923 // the PAC evaluator (NS_PROXYAUTOCONFIG_CONTRACTID) on the PAC thread,
924 // because that's where it will be used.
925 RefPtr<ExecutePACThreadAction> pending = new ExecutePACThreadAction(this);
926 pending->SetupPAC(reinterpret_cast<const char*>(data), dataLen, pacURI,
927 GetExtraJSContextHeapSize());
928 DispatchToPAC(pending.forget());
930 LOG(("OnStreamComplete: process the PAC contents\n"));
932 // Even if the PAC file could not be parsed, we did succeed in loading the
933 // data for it.
934 mLoadFailureCount = 0;
935 } else {
936 // We were unable to load the PAC file (presumably because of a network
937 // failure). Try again a little later.
938 LOG(("OnStreamComplete: unable to load PAC, retry later\n"));
939 OnLoadFailure();
942 if (NS_SUCCEEDED(status)) {
943 PostProcessPendingQ();
944 } else {
945 PostCancelPendingQ(status);
948 return NS_OK;
951 NS_IMETHODIMP
952 nsPACMan::GetInterface(const nsIID& iid, void** result) {
953 // In case loading the PAC file requires authentication.
954 if (iid.Equals(NS_GET_IID(nsIAuthPrompt))) {
955 nsCOMPtr<nsIPromptFactory> promptFac =
956 do_GetService("@mozilla.org/prompter;1");
957 NS_ENSURE_TRUE(promptFac, NS_ERROR_NO_INTERFACE);
958 nsresult rv =
959 promptFac->GetPrompt(nullptr, iid, reinterpret_cast<void**>(result));
960 if (NS_FAILED(rv)) {
961 return NS_ERROR_NO_INTERFACE;
963 return NS_OK;
966 // In case loading the PAC file results in a redirect.
967 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
968 NS_ADDREF_THIS();
969 *result = static_cast<nsIChannelEventSink*>(this);
970 return NS_OK;
973 return NS_ERROR_NO_INTERFACE;
976 NS_IMETHODIMP
977 nsPACMan::AsyncOnChannelRedirect(nsIChannel* oldChannel, nsIChannel* newChannel,
978 uint32_t flags,
979 nsIAsyncVerifyRedirectCallback* callback) {
980 MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
982 nsresult rv = NS_OK;
983 nsCOMPtr<nsIURI> pacURI;
984 if (NS_FAILED((rv = newChannel->GetURI(getter_AddRefs(pacURI))))) return rv;
986 rv = pacURI->GetSpec(mPACURIRedirectSpec);
987 if (NS_FAILED(rv)) return rv;
989 LOG(("nsPACMan redirect from original %s to redirected %s\n",
990 mPACURISpec.get(), mPACURIRedirectSpec.get()));
992 // do not update mPACURISpec - that needs to stay as the
993 // configured URI so that we can determine when the config changes.
994 // However do track the most recent URI in the redirect change
995 // as mPACURIRedirectSpec so that URI can be allowed to bypass
996 // the proxy and actually fetch the pac file.
998 callback->OnRedirectVerifyCallback(NS_OK);
999 return NS_OK;
1002 nsresult nsPACMan::Init(nsISystemProxySettings* systemProxySettings) {
1003 mSystemProxySettings = systemProxySettings;
1004 mDHCPClient = do_GetService(NS_DHCPCLIENT_CONTRACTID);
1005 return NS_OK;
1008 } // namespace net
1009 } // namespace mozilla