From 02211f8448876cd7c72784380b9e9229959392bb Mon Sep 17 00:00:00 2001 From: JustOff Date: Sat, 25 Aug 2018 17:50:02 +0300 Subject: Refresh nsStringBundleService and nsHttpHandler when the browser locale is changed --- intl/strres/nsStringBundle.cpp | 27 ++++++++++++++++++++++++ intl/strres/nsStringBundleService.h | 2 ++ netwerk/protocol/http/nsHttpHandler.cpp | 37 ++++++++++++++++++++------------- netwerk/protocol/http/nsHttpHandler.h | 3 ++- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/intl/strres/nsStringBundle.cpp b/intl/strres/nsStringBundle.cpp index ab840a469..717e368e2 100644 --- a/intl/strres/nsStringBundle.cpp +++ b/intl/strres/nsStringBundle.cpp @@ -25,6 +25,7 @@ #include "nsIErrorService.h" #include "nsICategoryManager.h" #include "nsContentUtils.h" +#include "mozilla/Preferences.h" // for async loading #ifdef ASYNC_LOADING @@ -32,6 +33,9 @@ #include "nsIStringStream.h" #endif +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) + using namespace mozilla; static NS_DEFINE_CID(kErrorServiceCID, NS_ERRORSERVICE_CID); @@ -529,6 +533,8 @@ nsStringBundleService::Init() os->AddObserver(this, "profile-do-change", true); os->AddObserver(this, "chrome-flush-caches", true); os->AddObserver(this, "xpcom-category-entry-added", true); + os->AddObserver(this, "selected-locale-has-changed", true); + os->AddObserver(this, "final-ui-startup", true); } // instantiate the override service, if there is any. @@ -550,6 +556,19 @@ nsStringBundleService::Observe(nsISupports* aSubject, { flushBundleCache(); } + else if (strcmp("selected-locale-has-changed", aTopic) == 0) + { + flushBundleCache(); + notifyBundlesFlushed(); + } + else if (strcmp("final-ui-startup", aTopic) == 0) + { + nsAdoptingCString ualocale = Preferences::GetCString("general.useragent.locale"); + if (!ualocale.EqualsLiteral(STR(MOZ_UI_LOCALE))) { + flushBundleCache(); + notifyBundlesFlushed(); + } + } else if (strcmp("xpcom-category-entry-added", aTopic) == 0 && NS_LITERAL_STRING("xpcom-autoregistration").Equals(aSomeData)) { @@ -559,6 +578,14 @@ nsStringBundleService::Observe(nsISupports* aSubject, return NS_OK; } +void +nsStringBundleService::notifyBundlesFlushed() +{ + nsCOMPtr obsSvc = mozilla::services::GetObserverService(); + NS_ASSERTION(obsSvc, "Couldn't get observer service."); + obsSvc->NotifyObservers(nullptr, "string-bundles-have-flushed", nullptr); +} + void nsStringBundleService::flushBundleCache() { diff --git a/intl/strres/nsStringBundleService.h b/intl/strres/nsStringBundleService.h index a192cdff8..e65c61f12 100644 --- a/intl/strres/nsStringBundleService.h +++ b/intl/strres/nsStringBundleService.h @@ -43,6 +43,8 @@ private: void flushBundleCache(); + void notifyBundlesFlushed(); + bundleCacheEntry_t *insertIntoCache(already_AddRefed aBundle, nsCString &aHashKey); diff --git a/netwerk/protocol/http/nsHttpHandler.cpp b/netwerk/protocol/http/nsHttpHandler.cpp index f9bcc391d..89f09190d 100644 --- a/netwerk/protocol/http/nsHttpHandler.cpp +++ b/netwerk/protocol/http/nsHttpHandler.cpp @@ -203,6 +203,7 @@ nsHttpHandler::nsHttpHandler() , mCompatFirefoxEnabled(false) , mCompatFirefoxVersion("52.9") , mUserAgentIsDirty(true) + , mAcceptLanguagesIsDirty(true) , mPromptTempRedirect(true) , mEnablePersistentHttpsCaching(false) , mDoNotTrackEnabled(false) @@ -419,6 +420,7 @@ nsHttpHandler::Init() obsService->AddObserver(this, "browser:purge-session-history", true); obsService->AddObserver(this, NS_NETWORK_LINK_TOPIC, true); obsService->AddObserver(this, "application-background", true); + obsService->AddObserver(this, "string-bundles-have-flushed", true); } MakeNewRequestTokenBucket(); @@ -489,8 +491,13 @@ nsHttpHandler::AddStandardRequestHeaders(nsHttpRequestHead *request, bool isSecu // Add the "Accept-Language" header. This header is also exposed to the // service worker. + if (mAcceptLanguagesIsDirty) { + rv = SetAcceptLanguages(); + MOZ_ASSERT(NS_SUCCEEDED(rv)); + } + + // Add the "Accept-Language" header if (!mAcceptLanguages.IsEmpty()) { - // Add the "Accept-Language" header rv = request->SetHeader(nsHttp::Accept_Language, mAcceptLanguages, false, nsHttpHeaderArray::eVarietyRequestOverride); @@ -1511,16 +1518,10 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) // if (PREF_CHANGED(INTL_ACCEPT_LANGUAGES)) { - nsCOMPtr pls; - prefs->GetComplexValue(INTL_ACCEPT_LANGUAGES, - NS_GET_IID(nsIPrefLocalizedString), - getter_AddRefs(pls)); - if (pls) { - nsXPIDLString uval; - pls->ToString(getter_Copies(uval)); - if (uval) - SetAcceptLanguages(NS_ConvertUTF16toUTF8(uval).get()); - } + // We don't want to set the new accept languages here since + // this pref is a complex type and it may be racy with flushing + // string resources. + mAcceptLanguagesIsDirty = true; } // @@ -1897,12 +1898,18 @@ PrepareAcceptLanguages(const char *i_AcceptLanguages, nsACString &o_AcceptLangua } nsresult -nsHttpHandler::SetAcceptLanguages(const char *aAcceptLanguages) +nsHttpHandler::SetAcceptLanguages() { + mAcceptLanguagesIsDirty = false; + + const nsAdoptingCString& acceptLanguages = + Preferences::GetLocalizedCString(INTL_ACCEPT_LANGUAGES); + nsAutoCString buf; - nsresult rv = PrepareAcceptLanguages(aAcceptLanguages, buf); - if (NS_SUCCEEDED(rv)) + nsresult rv = PrepareAcceptLanguages(acceptLanguages.get(), buf); + if (NS_SUCCEEDED(rv)) { mAcceptLanguages.Assign(buf); + } return rv; } @@ -2233,6 +2240,8 @@ nsHttpHandler::Observe(nsISupports *subject, if (mConnMgr) { mConnMgr->DoShiftReloadConnectionCleanup(nullptr); } + } else if (!strcmp(topic, "string-bundles-have-flushed")) { + mAcceptLanguagesIsDirty = true; } return NS_OK; diff --git a/netwerk/protocol/http/nsHttpHandler.h b/netwerk/protocol/http/nsHttpHandler.h index 0904af893..848dd25b1 100644 --- a/netwerk/protocol/http/nsHttpHandler.h +++ b/netwerk/protocol/http/nsHttpHandler.h @@ -384,7 +384,7 @@ private: void PrefsChanged(nsIPrefBranch *prefs, const char *pref); nsresult SetAccept(const char *); - nsresult SetAcceptLanguages(const char *); + nsresult SetAcceptLanguages(); nsresult SetAcceptEncodings(const char *, bool mIsSecure); nsresult InitConnectionMgr(); @@ -491,6 +491,7 @@ private: nsCString mUserAgent; nsXPIDLCString mUserAgentOverride; bool mUserAgentIsDirty; // true if mUserAgent should be rebuilt + bool mAcceptLanguagesIsDirty; bool mPromptTempRedirect; -- cgit v1.2.3