Skip to content

Commit 4e2f285

Browse files
committed
Bug 939318 - Have protocolproxy service act on network change. r=mcmanus
Reloads the PAC on network change, but avoids the reload if the specified PAC URL is a file: or a data: one - as those are not likely to have changed just because the network changed.
1 parent 4626439 commit 4e2f285

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

netwerk/base/src/nsProtocolProxyService.cpp

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "mozilla/Mutex.h"
3030
#include "mozilla/CondVar.h"
3131
#include "nsISystemProxySettings.h"
32+
#include "nsINetworkLinkService.h"
3233

3334
//----------------------------------------------------------------------------
3435

@@ -339,7 +340,7 @@ proxy_GetIntPref(nsIPrefBranch *aPrefBranch,
339340
{
340341
int32_t temp;
341342
nsresult rv = aPrefBranch->GetIntPref(aPref, &temp);
342-
if (NS_FAILED(rv))
343+
if (NS_FAILED(rv))
343344
aResult = -1;
344345
else
345346
aResult = temp;
@@ -352,7 +353,7 @@ proxy_GetBoolPref(nsIPrefBranch *aPrefBranch,
352353
{
353354
bool temp;
354355
nsresult rv = aPrefBranch->GetBoolPref(aPref, &temp);
355-
if (NS_FAILED(rv))
356+
if (NS_FAILED(rv))
356357
aResult = false;
357358
else
358359
aResult = temp;
@@ -414,14 +415,66 @@ nsProtocolProxyService::Init()
414415
PrefsChanged(prefBranch, nullptr);
415416
}
416417

417-
// register for shutdown notification so we can clean ourselves up properly.
418418
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
419-
if (obs)
419+
if (obs) {
420+
// register for shutdown notification so we can clean ourselves up
421+
// properly.
420422
obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
421423

424+
obs->AddObserver(this, NS_NETWORK_LINK_TOPIC, false);
425+
}
426+
422427
return NS_OK;
423428
}
424429

430+
// ReloadNetworkPAC() checks if there's a non-networked PAC in use then avoids
431+
// to call ReloadPAC()
432+
nsresult
433+
nsProtocolProxyService::ReloadNetworkPAC()
434+
{
435+
nsCOMPtr<nsIPrefBranch> prefs =
436+
do_GetService(NS_PREFSERVICE_CONTRACTID);
437+
if (!prefs) {
438+
return NS_OK;
439+
}
440+
441+
int32_t type;
442+
nsresult rv = prefs->GetIntPref(PROXY_PREF("type"), &type);
443+
if (NS_FAILED(rv)) {
444+
return NS_OK;
445+
}
446+
447+
if (type == PROXYCONFIG_PAC) {
448+
nsXPIDLCString pacSpec;
449+
prefs->GetCharPref(PROXY_PREF("autoconfig_url"),
450+
getter_Copies(pacSpec));
451+
if (!pacSpec.IsEmpty()) {
452+
nsCOMPtr<nsIURI> pacURI;
453+
rv = NS_NewURI(getter_AddRefs(pacURI), pacSpec);
454+
if(!NS_SUCCEEDED(rv)) {
455+
return rv;
456+
}
457+
458+
nsProtocolInfo pac;
459+
rv = GetProtocolInfo(pacURI, &pac);
460+
if(!NS_SUCCEEDED(rv)) {
461+
return rv;
462+
}
463+
464+
if (!pac.scheme.EqualsLiteral("file") &&
465+
!pac.scheme.EqualsLiteral("data")) {
466+
LOG((": received network changed event, reload PAC"));
467+
ReloadPAC();
468+
}
469+
}
470+
} else if ((type == PROXYCONFIG_WPAD) || (type == PROXYCONFIG_SYSTEM)) {
471+
ReloadPAC();
472+
}
473+
474+
return NS_OK;
475+
}
476+
477+
425478
NS_IMETHODIMP
426479
nsProtocolProxyService::Observe(nsISupports *aSubject,
427480
const char *aTopic,
@@ -440,6 +493,12 @@ nsProtocolProxyService::Observe(nsISupports *aSubject,
440493
mPACMan->Shutdown();
441494
mPACMan = nullptr;
442495
}
496+
} else if (strcmp(aTopic, NS_NETWORK_LINK_TOPIC) == 0) {
497+
nsCString converted = NS_ConvertUTF16toUTF8(aData);
498+
const char *state = converted.get();
499+
if (!strcmp(state, NS_NETWORK_LINK_DATA_CHANGED)) {
500+
ReloadNetworkPAC();
501+
}
443502
}
444503
else {
445504
NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
@@ -513,7 +572,7 @@ nsProtocolProxyService::PrefsChanged(nsIPrefBranch *prefBranch,
513572

514573
if (!pref || !strcmp(pref, PROXY_PREF("socks")))
515574
proxy_GetStringPref(prefBranch, PROXY_PREF("socks"), mSOCKSProxyHost);
516-
575+
517576
if (!pref || !strcmp(pref, PROXY_PREF("socks_port")))
518577
proxy_GetIntPref(prefBranch, PROXY_PREF("socks_port"), mSOCKSProxyPort);
519578

@@ -587,14 +646,14 @@ nsProtocolProxyService::PrefsChanged(nsIPrefBranch *prefBranch,
587646
}
588647

589648
bool
590-
nsProtocolProxyService::CanUseProxy(nsIURI *aURI, int32_t defaultPort)
649+
nsProtocolProxyService::CanUseProxy(nsIURI *aURI, int32_t defaultPort)
591650
{
592651
if (mHostFiltersArray.Length() == 0)
593652
return true;
594653

595654
int32_t port;
596655
nsAutoCString host;
597-
656+
598657
nsresult rv = aURI->GetAsciiHost(host);
599658
if (NS_FAILED(rv) || host.IsEmpty())
600659
return false;
@@ -624,7 +683,7 @@ nsProtocolProxyService::CanUseProxy(nsIURI *aURI, int32_t defaultPort)
624683
return true; // allow proxying
625684
}
626685
}
627-
686+
628687
// Don't use proxy for local hosts (plain hostname, no dots)
629688
if (!is_ipaddr && mFilterLocalHosts && (kNotFound == host.FindChar('.'))) {
630689
LOG(("Not using proxy for this local host [%s]!\n", host.get()));
@@ -810,7 +869,7 @@ nsProtocolProxyService::GetProxyKey(nsProxyInfo *pi, nsCString &key)
810869
key.Append(':');
811870
key.AppendInt(pi->mPort);
812871
}
813-
}
872+
}
814873

815874
uint32_t
816875
nsProtocolProxyService::SecondsSinceSessionStart()
@@ -1331,7 +1390,7 @@ nsProtocolProxyService::LoadHostFilters(const char *filters)
13311390
return; // fail silently...
13321391

13331392
//
1334-
// filter = ( host | domain | ipaddr ["/" mask] ) [":" port]
1393+
// filter = ( host | domain | ipaddr ["/" mask] ) [":" port]
13351394
// filters = filter *( "," LWS filter)
13361395
//
13371396
// Reset mFilterLocalHosts - will be set to true if "<local>" is in pref string
@@ -1343,7 +1402,7 @@ nsProtocolProxyService::LoadHostFilters(const char *filters)
13431402

13441403
const char *starthost = filters;
13451404
const char *endhost = filters + 1; // at least that...
1346-
const char *portLocation = 0;
1405+
const char *portLocation = 0;
13471406
const char *maskLocation = 0;
13481407

13491408
while (*endhost && (*endhost != ',' && !IS_ASCII_SPACE(*endhost))) {

netwerk/base/src/nsProtocolProxyService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class nsProtocolProxyService MOZ_FINAL : public nsIProtocolProxyService2
280280
private:
281281
nsresult SetupPACThread();
282282
nsresult ResetPACThread();
283+
nsresult ReloadNetworkPAC();
283284

284285
public:
285286
// The Sun Forte compiler and others implement older versions of the

0 commit comments

Comments
 (0)