1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sts=2 sw=2 et tw=80: */
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 "mozilla/dom/ContentParent.h"
8 #include "RegistryMessageUtils.h"
9 #include "nsResProtocolHandler.h"
11 #include "nsChromeRegistryChrome.h"
15 #elif defined(XP_MACOSX)
16 # include <CoreServices/CoreServices.h>
19 #include "nsArrayEnumerator.h"
20 #include "nsComponentManager.h"
21 #include "nsEnumeratorUtils.h"
22 #include "nsNetUtil.h"
23 #include "nsStringEnumerator.h"
24 #include "nsTextFormatter.h"
25 #include "nsXPCOMCIDInternal.h"
27 #include "mozilla/LookAndFeel.h"
28 #include "mozilla/Unused.h"
30 #include "nsIAppStartup.h"
31 #include "nsIObserverService.h"
32 #include "mozilla/Preferences.h"
33 #include "nsIResProtocolHandler.h"
34 #include "nsIScriptError.h"
35 #include "nsIXULRuntime.h"
37 #define PACKAGE_OVERRIDE_BRANCH "chrome.override_package."
38 #define SKIN NS_LITERAL_CSTRING("classic/1.0")
40 using namespace mozilla
;
41 using mozilla::dom::ContentParent
;
42 using mozilla::dom::PContentParent
;
43 using mozilla::intl::LocaleService
;
45 // We use a "best-fit" algorithm for matching locales and themes.
46 // 1) the exact selected locale/theme
47 // 2) (locales only) same language, different country
48 // e.g. en-GB is the selected locale, only en-US is available
49 // 3) any available locale/theme
52 * Match the language-part of two lang-COUNTRY codes, hopefully but
53 * not guaranteed to be in the form ab-CD or abz-CD. "ab" should also
54 * work, any other garbage-in will produce undefined results as long
55 * as it does not crash.
57 static bool LanguagesMatch(const nsACString
& a
, const nsACString
& b
) {
58 if (a
.Length() < 2 || b
.Length() < 2) return false;
60 nsACString::const_iterator as
, ae
, bs
, be
;
67 if (*as
== '-') return true;
73 if (as
== ae
&& bs
== be
) return true;
76 if (as
== ae
) return (*bs
== '-');
79 if (bs
== be
) return (*as
== '-');
85 nsChromeRegistryChrome::nsChromeRegistryChrome()
86 : mProfileLoaded(false), mDynamicRegistration(true) {}
88 nsChromeRegistryChrome::~nsChromeRegistryChrome() {}
90 nsresult
nsChromeRegistryChrome::Init() {
91 nsresult rv
= nsChromeRegistry::Init();
92 if (NS_FAILED(rv
)) return rv
;
94 bool safeMode
= false;
95 nsCOMPtr
<nsIXULRuntime
> xulrun(do_GetService(XULAPPINFO_SERVICE_CONTRACTID
));
96 if (xulrun
) xulrun
->GetInSafeMode(&safeMode
);
98 nsCOMPtr
<nsIObserverService
> obsService
=
99 mozilla::services::GetObserverService();
101 obsService
->AddObserver(this, "profile-initial-state", true);
102 obsService
->AddObserver(this, "intl:app-locales-changed", true);
109 nsChromeRegistryChrome::GetLocalesForPackage(
110 const nsACString
& aPackage
, nsIUTF8StringEnumerator
** aResult
) {
111 nsCString realpackage
;
112 nsresult rv
= OverrideLocalePackage(aPackage
, realpackage
);
113 if (NS_FAILED(rv
)) return rv
;
115 nsTArray
<nsCString
>* a
= new nsTArray
<nsCString
>;
116 if (!a
) return NS_ERROR_OUT_OF_MEMORY
;
119 if (mPackagesHash
.Get(realpackage
, &entry
)) {
120 entry
->locales
.EnumerateToArray(a
);
123 rv
= NS_NewAdoptingUTF8StringEnumerator(aResult
, a
);
124 if (NS_FAILED(rv
)) delete a
;
130 nsChromeRegistryChrome::IsLocaleRTL(const nsACString
& package
, bool* aResult
) {
133 nsAutoCString locale
;
134 GetSelectedLocale(package
, false, locale
);
135 if (locale
.Length() < 2) return NS_OK
;
137 *aResult
= LocaleService::IsLocaleRTL(locale
);
142 * This method negotiates only between the app locale and the available
145 * If you want to get the current application's UI locale, please use
146 * LocaleService::GetAppLocaleAsLangTag.
148 nsresult
nsChromeRegistryChrome::GetSelectedLocale(const nsACString
& aPackage
,
150 nsACString
& aLocale
) {
151 nsAutoCString reqLocale
;
152 if (aPackage
.EqualsLiteral("global")) {
153 LocaleService::GetInstance()->GetAppLocaleAsLangTag(reqLocale
);
155 AutoTArray
<nsCString
, 10> requestedLocales
;
156 LocaleService::GetInstance()->GetRequestedLocales(requestedLocales
);
157 reqLocale
.Assign(requestedLocales
[0]);
160 nsCString realpackage
;
161 nsresult rv
= OverrideLocalePackage(aPackage
, realpackage
);
162 if (NS_FAILED(rv
)) return rv
;
164 if (!mPackagesHash
.Get(realpackage
, &entry
)) return NS_ERROR_FILE_NOT_FOUND
;
166 aLocale
= entry
->locales
.GetSelected(reqLocale
, nsProviderArray::LOCALE
);
167 if (aLocale
.IsEmpty()) return NS_ERROR_FAILURE
;
170 SanitizeForBCP47(aLocale
);
176 nsresult
nsChromeRegistryChrome::OverrideLocalePackage(
177 const nsACString
& aPackage
, nsACString
& aOverride
) {
178 const nsACString
& pref
=
179 NS_LITERAL_CSTRING(PACKAGE_OVERRIDE_BRANCH
) + aPackage
;
180 nsAutoCString override
;
181 nsresult rv
= mozilla::Preferences::GetCString(PromiseFlatCString(pref
).get(),
183 if (NS_SUCCEEDED(rv
)) {
184 aOverride
= override
;
186 aOverride
= aPackage
;
192 nsChromeRegistryChrome::Observe(nsISupports
* aSubject
, const char* aTopic
,
193 const char16_t
* someData
) {
196 if (!strcmp("profile-initial-state", aTopic
)) {
197 mProfileLoaded
= true;
198 } else if (!strcmp("intl:app-locales-changed", aTopic
)) {
199 if (mProfileLoaded
) {
203 NS_ERROR("Unexpected observer topic!");
210 nsChromeRegistryChrome::CheckForNewChrome() {
211 nsCOMPtr
<nsIAppStartup
> appStartup
= components::AppStartup::Service();
212 if (appStartup
->GetShuttingDown()) {
213 MOZ_ASSERT(false, "checking for new chrome during shutdown");
214 return NS_ERROR_UNEXPECTED
;
217 mPackagesHash
.Clear();
218 mOverrideTable
.Clear();
220 mDynamicRegistration
= false;
222 nsComponentManagerImpl::gComponentManager
->RereadChromeManifests();
224 mDynamicRegistration
= true;
226 SendRegisteredChrome(nullptr);
230 static void SerializeURI(nsIURI
* aURI
, SerializedURI
& aSerializedURI
) {
233 aURI
->GetSpec(aSerializedURI
.spec
);
236 void nsChromeRegistryChrome::SendRegisteredChrome(
237 mozilla::dom::PContentParent
* aParent
) {
238 nsTArray
<ChromePackage
> packages
;
239 nsTArray
<SubstitutionMapping
> resources
;
240 nsTArray
<OverrideMapping
> overrides
;
242 for (auto iter
= mPackagesHash
.Iter(); !iter
.Done(); iter
.Next()) {
243 ChromePackage chromePackage
;
244 ChromePackageFromPackageEntry(iter
.Key(), iter
.UserData(), &chromePackage
,
246 packages
.AppendElement(chromePackage
);
249 // If we were passed a parent then a new child process has been created and
250 // has requested all of the chrome so send it the resources too. Otherwise
251 // resource mappings are sent by the resource protocol handler dynamically.
253 nsCOMPtr
<nsIIOService
> io(do_GetIOService());
254 NS_ENSURE_TRUE_VOID(io
);
256 nsCOMPtr
<nsIProtocolHandler
> ph
;
257 nsresult rv
= io
->GetProtocolHandler("resource", getter_AddRefs(ph
));
258 NS_ENSURE_SUCCESS_VOID(rv
);
260 nsCOMPtr
<nsIResProtocolHandler
> irph(do_QueryInterface(ph
));
261 nsResProtocolHandler
* rph
= static_cast<nsResProtocolHandler
*>(irph
.get());
262 rv
= rph
->CollectSubstitutions(resources
);
263 NS_ENSURE_SUCCESS_VOID(rv
);
266 for (auto iter
= mOverrideTable
.Iter(); !iter
.Done(); iter
.Next()) {
267 SerializedURI chromeURI
, overrideURI
;
269 SerializeURI(iter
.Key(), chromeURI
);
270 SerializeURI(iter
.UserData(), overrideURI
);
272 OverrideMapping override
= {chromeURI
, overrideURI
};
273 overrides
.AppendElement(override
);
276 nsAutoCString appLocale
;
277 LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale
);
280 bool success
= aParent
->SendRegisterChrome(packages
, resources
, overrides
,
282 NS_ENSURE_TRUE_VOID(success
);
284 nsTArray
<ContentParent
*> parents
;
285 ContentParent::GetAll(parents
);
286 if (!parents
.Length()) return;
288 for (uint32_t i
= 0; i
< parents
.Length(); i
++) {
289 DebugOnly
<bool> success
= parents
[i
]->SendRegisterChrome(
290 packages
, resources
, overrides
, appLocale
, true);
291 NS_WARNING_ASSERTION(success
,
292 "couldn't reset a child's registered chrome");
298 void nsChromeRegistryChrome::ChromePackageFromPackageEntry(
299 const nsACString
& aPackageName
, PackageEntry
* aPackage
,
300 ChromePackage
* aChromePackage
, const nsCString
& aSelectedSkin
) {
301 nsAutoCString appLocale
;
302 LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale
);
304 SerializeURI(aPackage
->baseURI
, aChromePackage
->contentBaseURI
);
305 SerializeURI(aPackage
->locales
.GetBase(appLocale
, nsProviderArray::LOCALE
),
306 aChromePackage
->localeBaseURI
);
307 SerializeURI(aPackage
->skins
.GetBase(aSelectedSkin
, nsProviderArray::ANY
),
308 aChromePackage
->skinBaseURI
);
309 aChromePackage
->package
= aPackageName
;
310 aChromePackage
->flags
= aPackage
->flags
;
313 static bool CanLoadResource(nsIURI
* aResourceURI
) {
314 bool isLocalResource
= false;
315 (void)NS_URIChainHasFlags(aResourceURI
,
316 nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
,
318 return isLocalResource
;
321 nsIURI
* nsChromeRegistryChrome::GetBaseURIFromPackage(
322 const nsCString
& aPackage
, const nsCString
& aProvider
,
323 const nsCString
& aPath
) {
325 if (!mPackagesHash
.Get(aPackage
, &entry
)) {
326 if (!mInitialized
) return nullptr;
328 LogMessage("No chrome package registered for chrome://%s/%s/%s",
329 aPackage
.get(), aProvider
.get(), aPath
.get());
334 if (aProvider
.EqualsLiteral("locale")) {
335 nsAutoCString appLocale
;
336 LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale
);
337 return entry
->locales
.GetBase(appLocale
, nsProviderArray::LOCALE
);
338 } else if (aProvider
.EqualsLiteral("skin")) {
339 return entry
->skins
.GetBase(SKIN
, nsProviderArray::ANY
);
340 } else if (aProvider
.EqualsLiteral("content")) {
341 return entry
->baseURI
;
346 nsresult
nsChromeRegistryChrome::GetFlagsFromPackage(const nsCString
& aPackage
,
349 if (!mPackagesHash
.Get(aPackage
, &entry
)) return NS_ERROR_FILE_NOT_FOUND
;
351 *aFlags
= entry
->flags
;
355 nsChromeRegistryChrome::ProviderEntry
*
356 nsChromeRegistryChrome::nsProviderArray::GetProvider(
357 const nsACString
& aPreferred
, MatchType aType
) {
358 size_t i
= mArray
.Length();
359 if (!i
) return nullptr;
361 ProviderEntry
* found
= nullptr; // Only set if we find a partial-match locale
362 ProviderEntry
* entry
= nullptr;
366 if (aPreferred
.Equals(entry
->provider
)) return entry
;
368 if (aType
!= LOCALE
) continue;
370 if (LanguagesMatch(aPreferred
, entry
->provider
)) {
375 if (!found
&& entry
->provider
.EqualsLiteral("en-US")) found
= entry
;
378 if (!found
&& aType
!= EXACT
) return entry
;
383 nsIURI
* nsChromeRegistryChrome::nsProviderArray::GetBase(
384 const nsACString
& aPreferred
, MatchType aType
) {
385 ProviderEntry
* provider
= GetProvider(aPreferred
, aType
);
387 if (!provider
) return nullptr;
389 return provider
->baseURI
;
392 const nsACString
& nsChromeRegistryChrome::nsProviderArray::GetSelected(
393 const nsACString
& aPreferred
, MatchType aType
) {
394 ProviderEntry
* entry
= GetProvider(aPreferred
, aType
);
396 if (entry
) return entry
->provider
;
398 return EmptyCString();
401 void nsChromeRegistryChrome::nsProviderArray::SetBase(
402 const nsACString
& aProvider
, nsIURI
* aBaseURL
) {
403 ProviderEntry
* provider
= GetProvider(aProvider
, EXACT
);
406 provider
->baseURI
= aBaseURL
;
410 // no existing entries, add a new one
411 mArray
.AppendElement(ProviderEntry(aProvider
, aBaseURL
));
414 void nsChromeRegistryChrome::nsProviderArray::EnumerateToArray(
415 nsTArray
<nsCString
>* a
) {
416 int32_t i
= mArray
.Length();
418 a
->AppendElement(mArray
[i
].provider
);
422 nsIURI
* nsChromeRegistry::ManifestProcessingContext::GetManifestURI() {
425 mFile
.GetURIString(uri
);
426 NS_NewURI(getter_AddRefs(mManifestURI
), uri
);
431 already_AddRefed
<nsIURI
>
432 nsChromeRegistry::ManifestProcessingContext::ResolveURI(const char* uri
) {
433 nsIURI
* baseuri
= GetManifestURI();
434 if (!baseuri
) return nullptr;
436 nsCOMPtr
<nsIURI
> resolved
;
437 nsresult rv
= NS_NewURI(getter_AddRefs(resolved
), uri
, baseuri
);
438 if (NS_FAILED(rv
)) return nullptr;
440 return resolved
.forget();
443 static void EnsureLowerCase(char* aBuf
) {
444 for (; *aBuf
; ++aBuf
) {
446 if (ch
>= 'A' && ch
<= 'Z') *aBuf
= ch
+ 'a' - 'A';
450 static void SendManifestEntry(const ChromeRegistryItem
& aItem
) {
451 nsTArray
<ContentParent
*> parents
;
452 ContentParent::GetAll(parents
);
453 if (!parents
.Length()) return;
455 for (uint32_t i
= 0; i
< parents
.Length(); i
++) {
456 Unused
<< parents
[i
]->SendRegisterChromeItem(aItem
);
460 void nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext
& cx
,
461 int lineno
, char* const* argv
,
463 char* package
= argv
[0];
466 EnsureLowerCase(package
);
468 nsCOMPtr
<nsIURI
> resolved
= cx
.ResolveURI(uri
);
470 LogMessageWithContext(
471 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
472 "During chrome registration, unable to create URI '%s'.", uri
);
476 if (!CanLoadResource(resolved
)) {
477 LogMessageWithContext(resolved
, lineno
, nsIScriptError::warningFlag
,
478 "During chrome registration, cannot register "
479 "non-local URI '%s' as content.",
484 nsDependentCString
packageName(package
);
485 PackageEntry
* entry
= mPackagesHash
.LookupOrAdd(packageName
);
486 entry
->baseURI
= resolved
;
487 entry
->flags
= flags
;
489 if (mDynamicRegistration
) {
490 ChromePackage chromePackage
;
491 ChromePackageFromPackageEntry(packageName
, entry
, &chromePackage
, SKIN
);
492 SendManifestEntry(chromePackage
);
496 void nsChromeRegistryChrome::ManifestLocale(ManifestProcessingContext
& cx
,
497 int lineno
, char* const* argv
,
499 char* package
= argv
[0];
500 char* provider
= argv
[1];
503 EnsureLowerCase(package
);
505 nsCOMPtr
<nsIURI
> resolved
= cx
.ResolveURI(uri
);
507 LogMessageWithContext(
508 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
509 "During chrome registration, unable to create URI '%s'.", uri
);
513 if (!CanLoadResource(resolved
)) {
514 LogMessageWithContext(resolved
, lineno
, nsIScriptError::warningFlag
,
515 "During chrome registration, cannot register "
516 "non-local URI '%s' as content.",
521 nsDependentCString
packageName(package
);
522 PackageEntry
* entry
= mPackagesHash
.LookupOrAdd(packageName
);
523 entry
->locales
.SetBase(nsDependentCString(provider
), resolved
);
525 if (mDynamicRegistration
) {
526 ChromePackage chromePackage
;
527 ChromePackageFromPackageEntry(packageName
, entry
, &chromePackage
, SKIN
);
528 SendManifestEntry(chromePackage
);
531 // We use mainPackage as the package we track for reporting new locales being
532 // registered. For most cases it will be "global", but for Fennec it will be
534 nsAutoCString mainPackage
;
536 OverrideLocalePackage(NS_LITERAL_CSTRING("global"), mainPackage
);
542 void nsChromeRegistryChrome::ManifestSkin(ManifestProcessingContext
& cx
,
543 int lineno
, char* const* argv
,
545 char* package
= argv
[0];
546 char* provider
= argv
[1];
549 EnsureLowerCase(package
);
551 nsCOMPtr
<nsIURI
> resolved
= cx
.ResolveURI(uri
);
553 LogMessageWithContext(
554 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
555 "During chrome registration, unable to create URI '%s'.", uri
);
559 if (!CanLoadResource(resolved
)) {
560 LogMessageWithContext(resolved
, lineno
, nsIScriptError::warningFlag
,
561 "During chrome registration, cannot register "
562 "non-local URI '%s' as content.",
567 nsDependentCString
packageName(package
);
568 PackageEntry
* entry
= mPackagesHash
.LookupOrAdd(packageName
);
569 entry
->skins
.SetBase(nsDependentCString(provider
), resolved
);
571 if (mDynamicRegistration
) {
572 ChromePackage chromePackage
;
573 ChromePackageFromPackageEntry(packageName
, entry
, &chromePackage
, SKIN
);
574 SendManifestEntry(chromePackage
);
578 void nsChromeRegistryChrome::ManifestOverride(ManifestProcessingContext
& cx
,
579 int lineno
, char* const* argv
,
581 char* chrome
= argv
[0];
582 char* resolved
= argv
[1];
584 nsCOMPtr
<nsIURI
> chromeuri
= cx
.ResolveURI(chrome
);
585 nsCOMPtr
<nsIURI
> resolveduri
= cx
.ResolveURI(resolved
);
586 if (!chromeuri
|| !resolveduri
) {
587 LogMessageWithContext(cx
.GetManifestURI(), lineno
,
588 nsIScriptError::warningFlag
,
589 "During chrome registration, unable to create URI.");
593 if (cx
.mType
== NS_SKIN_LOCATION
) {
594 bool chromeSkinOnly
=
595 chromeuri
->SchemeIs("chrome") && resolveduri
->SchemeIs("chrome");
596 if (chromeSkinOnly
) {
597 nsAutoCString chromePath
, resolvedPath
;
598 chromeuri
->GetPathQueryRef(chromePath
);
599 resolveduri
->GetPathQueryRef(resolvedPath
);
601 StringBeginsWith(chromePath
, NS_LITERAL_CSTRING("/skin/")) &&
602 StringBeginsWith(resolvedPath
, NS_LITERAL_CSTRING("/skin/"));
604 if (!chromeSkinOnly
) {
605 LogMessageWithContext(
606 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
607 "Cannot register non-chrome://.../skin/ URIs '%s' and '%s' as "
608 "overrides and/or to be overridden from a skin manifest.",
614 if (!CanLoadResource(resolveduri
)) {
615 LogMessageWithContext(
616 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
617 "Cannot register non-local URI '%s' for an override.", resolved
);
620 mOverrideTable
.Put(chromeuri
, resolveduri
);
622 if (mDynamicRegistration
) {
623 SerializedURI serializedChrome
;
624 SerializedURI serializedOverride
;
626 SerializeURI(chromeuri
, serializedChrome
);
627 SerializeURI(resolveduri
, serializedOverride
);
629 OverrideMapping override
= {serializedChrome
, serializedOverride
};
630 SendManifestEntry(override
);
634 void nsChromeRegistryChrome::ManifestResource(ManifestProcessingContext
& cx
,
635 int lineno
, char* const* argv
,
637 char* package
= argv
[0];
640 EnsureLowerCase(package
);
641 nsDependentCString
host(package
);
643 nsCOMPtr
<nsIIOService
> io
= mozilla::services::GetIOService();
645 NS_WARNING("No IO service trying to process chrome manifests");
649 nsCOMPtr
<nsIProtocolHandler
> ph
;
650 nsresult rv
= io
->GetProtocolHandler("resource", getter_AddRefs(ph
));
651 if (NS_FAILED(rv
)) return;
653 nsCOMPtr
<nsIResProtocolHandler
> rph
= do_QueryInterface(ph
);
655 nsCOMPtr
<nsIURI
> resolved
= cx
.ResolveURI(uri
);
657 LogMessageWithContext(
658 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
659 "During chrome registration, unable to create URI '%s'.", uri
);
663 if (!CanLoadResource(resolved
)) {
664 LogMessageWithContext(
665 cx
.GetManifestURI(), lineno
, nsIScriptError::warningFlag
,
666 "Warning: cannot register non-local URI '%s' as a resource.", uri
);
670 // By default, Firefox resources are not content-accessible unless the
671 // manifests opts in.
672 bool contentAccessible
= (flags
& nsChromeRegistry::CONTENT_ACCESSIBLE
);
674 uint32_t substitutionFlags
= 0;
675 if (contentAccessible
) {
676 substitutionFlags
|= nsIResProtocolHandler::ALLOW_CONTENT_ACCESS
;
678 rv
= rph
->SetSubstitutionWithFlags(host
, resolved
, substitutionFlags
);
680 LogMessageWithContext(cx
.GetManifestURI(), lineno
,
681 nsIScriptError::warningFlag
,
682 "Warning: cannot set substitution for '%s'.", uri
);