nsChromeRegistryChrome.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. #include "mozilla/dom/ContentParent.h"
  6. #include "RegistryMessageUtils.h"
  7. #include "nsResProtocolHandler.h"
  8. #include "nsChromeRegistryChrome.h"
  9. #if defined(XP_WIN)
  10. #include <windows.h>
  11. #endif
  12. #include "nsArrayEnumerator.h"
  13. #include "nsComponentManager.h"
  14. #include "nsEnumeratorUtils.h"
  15. #include "nsNetUtil.h"
  16. #include "nsStringEnumerator.h"
  17. #include "nsTextFormatter.h"
  18. #include "nsXPCOMCIDInternal.h"
  19. #include "mozilla/LookAndFeel.h"
  20. #include "mozilla/Unused.h"
  21. #include "nsICommandLine.h"
  22. #include "nsILocaleService.h"
  23. #include "nsIObserverService.h"
  24. #include "nsIPrefBranch.h"
  25. #include "nsIPrefService.h"
  26. #include "mozilla/Preferences.h"
  27. #include "nsIResProtocolHandler.h"
  28. #include "nsIScriptError.h"
  29. #include "nsIXPConnect.h"
  30. #include "nsIXULRuntime.h"
  31. #define UILOCALE_CMD_LINE_ARG "UILocale"
  32. #define MATCH_OS_LOCALE_PREF "intl.locale.matchOS"
  33. #define SELECTED_LOCALE_PREF "general.useragent.locale"
  34. #define SELECTED_SKIN_PREF "general.skins.selectedSkin"
  35. #define PACKAGE_OVERRIDE_BRANCH "chrome.override_package."
  36. using namespace mozilla;
  37. using mozilla::dom::ContentParent;
  38. using mozilla::dom::PContentParent;
  39. // We use a "best-fit" algorithm for matching locales and themes.
  40. // 1) the exact selected locale/theme
  41. // 2) (locales only) same language, different country
  42. // e.g. en-GB is the selected locale, only en-US is available
  43. // 3) any available locale/theme
  44. /**
  45. * Match the language-part of two lang-COUNTRY codes, hopefully but
  46. * not guaranteed to be in the form ab-CD or abz-CD. "ab" should also
  47. * work, any other garbage-in will produce undefined results as long
  48. * as it does not crash.
  49. */
  50. static bool
  51. LanguagesMatch(const nsACString& a, const nsACString& b)
  52. {
  53. if (a.Length() < 2 || b.Length() < 2)
  54. return false;
  55. nsACString::const_iterator as, ae, bs, be;
  56. a.BeginReading(as);
  57. a.EndReading(ae);
  58. b.BeginReading(bs);
  59. b.EndReading(be);
  60. while (*as == *bs) {
  61. if (*as == '-')
  62. return true;
  63. ++as; ++bs;
  64. // reached the end
  65. if (as == ae && bs == be)
  66. return true;
  67. // "a" is short
  68. if (as == ae)
  69. return (*bs == '-');
  70. // "b" is short
  71. if (bs == be)
  72. return (*as == '-');
  73. }
  74. return false;
  75. }
  76. nsChromeRegistryChrome::nsChromeRegistryChrome()
  77. : mProfileLoaded(false)
  78. , mDynamicRegistration(true)
  79. {
  80. }
  81. nsChromeRegistryChrome::~nsChromeRegistryChrome()
  82. {
  83. }
  84. nsresult
  85. nsChromeRegistryChrome::Init()
  86. {
  87. nsresult rv = nsChromeRegistry::Init();
  88. if (NS_FAILED(rv))
  89. return rv;
  90. mSelectedLocale = NS_LITERAL_CSTRING("en-US");
  91. mSelectedSkin = NS_LITERAL_CSTRING("classic/1.0");
  92. bool safeMode = false;
  93. nsCOMPtr<nsIXULRuntime> xulrun (do_GetService(XULAPPINFO_SERVICE_CONTRACTID));
  94. if (xulrun)
  95. xulrun->GetInSafeMode(&safeMode);
  96. nsCOMPtr<nsIPrefService> prefserv (do_GetService(NS_PREFSERVICE_CONTRACTID));
  97. nsCOMPtr<nsIPrefBranch> prefs;
  98. if (prefserv) {
  99. if (safeMode) {
  100. prefserv->GetDefaultBranch(nullptr, getter_AddRefs(prefs));
  101. } else {
  102. prefs = do_QueryInterface(prefserv);
  103. }
  104. }
  105. if (!prefs) {
  106. NS_WARNING("Could not get pref service!");
  107. } else {
  108. nsXPIDLCString provider;
  109. rv = prefs->GetCharPref(SELECTED_SKIN_PREF, getter_Copies(provider));
  110. if (NS_SUCCEEDED(rv))
  111. mSelectedSkin = provider;
  112. SelectLocaleFromPref(prefs);
  113. rv = prefs->AddObserver(MATCH_OS_LOCALE_PREF, this, true);
  114. rv = prefs->AddObserver(SELECTED_LOCALE_PREF, this, true);
  115. rv = prefs->AddObserver(SELECTED_SKIN_PREF, this, true);
  116. }
  117. nsCOMPtr<nsIObserverService> obsService = mozilla::services::GetObserverService();
  118. if (obsService) {
  119. obsService->AddObserver(this, "command-line-startup", true);
  120. obsService->AddObserver(this, "profile-initial-state", true);
  121. }
  122. return NS_OK;
  123. }
  124. NS_IMETHODIMP
  125. nsChromeRegistryChrome::CheckForOSAccessibility()
  126. {
  127. int32_t useAccessibilityTheme =
  128. LookAndFeel::GetInt(LookAndFeel::eIntID_UseAccessibilityTheme, 0);
  129. if (useAccessibilityTheme) {
  130. /* Set the skin to classic and remove pref observers */
  131. if (!mSelectedSkin.EqualsLiteral("classic/1.0")) {
  132. mSelectedSkin.AssignLiteral("classic/1.0");
  133. RefreshSkins();
  134. }
  135. nsCOMPtr<nsIPrefBranch> prefs (do_GetService(NS_PREFSERVICE_CONTRACTID));
  136. if (prefs) {
  137. prefs->RemoveObserver(SELECTED_SKIN_PREF, this);
  138. }
  139. }
  140. return NS_OK;
  141. }
  142. NS_IMETHODIMP
  143. nsChromeRegistryChrome::GetLocalesForPackage(const nsACString& aPackage,
  144. nsIUTF8StringEnumerator* *aResult)
  145. {
  146. nsCString realpackage;
  147. nsresult rv = OverrideLocalePackage(aPackage, realpackage);
  148. if (NS_FAILED(rv))
  149. return rv;
  150. nsTArray<nsCString> *a = new nsTArray<nsCString>;
  151. if (!a)
  152. return NS_ERROR_OUT_OF_MEMORY;
  153. PackageEntry* entry;
  154. if (mPackagesHash.Get(realpackage, &entry)) {
  155. entry->locales.EnumerateToArray(a);
  156. }
  157. rv = NS_NewAdoptingUTF8StringEnumerator(aResult, a);
  158. if (NS_FAILED(rv))
  159. delete a;
  160. return rv;
  161. }
  162. static nsresult
  163. getUILangCountry(nsACString& aUILang)
  164. {
  165. nsresult rv;
  166. nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
  167. NS_ENSURE_SUCCESS(rv, rv);
  168. nsAutoString uiLang;
  169. rv = localeService->GetLocaleComponentForUserAgent(uiLang);
  170. NS_ENSURE_SUCCESS(rv, rv);
  171. CopyUTF16toUTF8(uiLang, aUILang);
  172. return NS_OK;
  173. }
  174. NS_IMETHODIMP
  175. nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
  176. {
  177. *aResult = false;
  178. nsAutoCString locale;
  179. GetSelectedLocale(package, false, locale);
  180. if (locale.Length() < 2)
  181. return NS_OK;
  182. *aResult = GetDirectionForLocale(locale);
  183. return NS_OK;
  184. }
  185. nsresult
  186. nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
  187. bool aAsBCP47,
  188. nsACString& aLocale)
  189. {
  190. nsCString realpackage;
  191. nsresult rv = OverrideLocalePackage(aPackage, realpackage);
  192. if (NS_FAILED(rv))
  193. return rv;
  194. PackageEntry* entry;
  195. if (!mPackagesHash.Get(realpackage, &entry))
  196. return NS_ERROR_FILE_NOT_FOUND;
  197. aLocale = entry->locales.GetSelected(mSelectedLocale, nsProviderArray::LOCALE);
  198. if (aLocale.IsEmpty())
  199. return NS_ERROR_FAILURE;
  200. if (aAsBCP47) {
  201. SanitizeForBCP47(aLocale);
  202. }
  203. return NS_OK;
  204. }
  205. nsresult
  206. nsChromeRegistryChrome::OverrideLocalePackage(const nsACString& aPackage,
  207. nsACString& aOverride)
  208. {
  209. const nsACString& pref = NS_LITERAL_CSTRING(PACKAGE_OVERRIDE_BRANCH) + aPackage;
  210. nsAdoptingCString override = mozilla::Preferences::GetCString(PromiseFlatCString(pref).get());
  211. if (override) {
  212. aOverride = override;
  213. }
  214. else {
  215. aOverride = aPackage;
  216. }
  217. return NS_OK;
  218. }
  219. nsresult
  220. nsChromeRegistryChrome::SelectLocaleFromPref(nsIPrefBranch* prefs)
  221. {
  222. nsresult rv;
  223. bool matchOSLocale = false;
  224. rv = prefs->GetBoolPref(MATCH_OS_LOCALE_PREF, &matchOSLocale);
  225. if (NS_SUCCEEDED(rv) && matchOSLocale) {
  226. // compute lang and region code only when needed!
  227. nsAutoCString uiLocale;
  228. rv = getUILangCountry(uiLocale);
  229. if (NS_SUCCEEDED(rv))
  230. mSelectedLocale = uiLocale;
  231. }
  232. else {
  233. nsXPIDLCString provider;
  234. rv = prefs->GetCharPref(SELECTED_LOCALE_PREF, getter_Copies(provider));
  235. if (NS_SUCCEEDED(rv)) {
  236. mSelectedLocale = provider;
  237. }
  238. }
  239. if (NS_FAILED(rv))
  240. NS_ERROR("Couldn't select locale from pref!");
  241. return rv;
  242. }
  243. NS_IMETHODIMP
  244. nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
  245. const char16_t *someData)
  246. {
  247. nsresult rv = NS_OK;
  248. if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) {
  249. nsCOMPtr<nsIPrefBranch> prefs (do_QueryInterface(aSubject));
  250. NS_ASSERTION(prefs, "Bad observer call!");
  251. NS_ConvertUTF16toUTF8 pref(someData);
  252. if (pref.EqualsLiteral(MATCH_OS_LOCALE_PREF) ||
  253. pref.EqualsLiteral(SELECTED_LOCALE_PREF)) {
  254. rv = UpdateSelectedLocale();
  255. if (NS_SUCCEEDED(rv) && mProfileLoaded)
  256. FlushAllCaches();
  257. }
  258. else if (pref.EqualsLiteral(SELECTED_SKIN_PREF)) {
  259. nsXPIDLCString provider;
  260. rv = prefs->GetCharPref(pref.get(), getter_Copies(provider));
  261. if (NS_FAILED(rv)) {
  262. NS_ERROR("Couldn't get new skin pref!");
  263. return rv;
  264. }
  265. mSelectedSkin = provider;
  266. RefreshSkins();
  267. } else {
  268. NS_ERROR("Unexpected pref!");
  269. }
  270. }
  271. else if (!strcmp("command-line-startup", aTopic)) {
  272. nsCOMPtr<nsICommandLine> cmdLine (do_QueryInterface(aSubject));
  273. if (cmdLine) {
  274. nsAutoString uiLocale;
  275. rv = cmdLine->HandleFlagWithParam(NS_LITERAL_STRING(UILOCALE_CMD_LINE_ARG),
  276. false, uiLocale);
  277. if (NS_SUCCEEDED(rv) && !uiLocale.IsEmpty()) {
  278. CopyUTF16toUTF8(uiLocale, mSelectedLocale);
  279. nsCOMPtr<nsIPrefBranch> prefs (do_GetService(NS_PREFSERVICE_CONTRACTID));
  280. if (prefs) {
  281. prefs->RemoveObserver(SELECTED_LOCALE_PREF, this);
  282. }
  283. }
  284. }
  285. }
  286. else if (!strcmp("profile-initial-state", aTopic)) {
  287. mProfileLoaded = true;
  288. }
  289. else {
  290. NS_ERROR("Unexpected observer topic!");
  291. }
  292. return rv;
  293. }
  294. NS_IMETHODIMP
  295. nsChromeRegistryChrome::CheckForNewChrome()
  296. {
  297. mPackagesHash.Clear();
  298. mOverlayHash.Clear();
  299. mStyleHash.Clear();
  300. mOverrideTable.Clear();
  301. mDynamicRegistration = false;
  302. nsComponentManagerImpl::gComponentManager->RereadChromeManifests();
  303. mDynamicRegistration = true;
  304. SendRegisteredChrome(nullptr);
  305. return NS_OK;
  306. }
  307. nsresult nsChromeRegistryChrome::UpdateSelectedLocale()
  308. {
  309. nsresult rv = NS_OK;
  310. nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
  311. if (prefs) {
  312. rv = SelectLocaleFromPref(prefs);
  313. if (NS_SUCCEEDED(rv)) {
  314. nsCOMPtr<nsIObserverService> obsSvc =
  315. mozilla::services::GetObserverService();
  316. NS_ASSERTION(obsSvc, "Couldn't get observer service.");
  317. obsSvc->NotifyObservers((nsIChromeRegistry*) this,
  318. "selected-locale-has-changed", nullptr);
  319. }
  320. }
  321. return rv;
  322. }
  323. static void
  324. SerializeURI(nsIURI* aURI,
  325. SerializedURI& aSerializedURI)
  326. {
  327. if (!aURI)
  328. return;
  329. aURI->GetSpec(aSerializedURI.spec);
  330. aURI->GetOriginCharset(aSerializedURI.charset);
  331. }
  332. void
  333. nsChromeRegistryChrome::SendRegisteredChrome(
  334. mozilla::dom::PContentParent* aParent)
  335. {
  336. InfallibleTArray<ChromePackage> packages;
  337. InfallibleTArray<SubstitutionMapping> resources;
  338. InfallibleTArray<OverrideMapping> overrides;
  339. for (auto iter = mPackagesHash.Iter(); !iter.Done(); iter.Next()) {
  340. ChromePackage chromePackage;
  341. ChromePackageFromPackageEntry(iter.Key(), iter.UserData(), &chromePackage,
  342. mSelectedLocale, mSelectedSkin);
  343. packages.AppendElement(chromePackage);
  344. }
  345. // If we were passed a parent then a new child process has been created and
  346. // has requested all of the chrome so send it the resources too. Otherwise
  347. // resource mappings are sent by the resource protocol handler dynamically.
  348. if (aParent) {
  349. nsCOMPtr<nsIIOService> io (do_GetIOService());
  350. NS_ENSURE_TRUE_VOID(io);
  351. nsCOMPtr<nsIProtocolHandler> ph;
  352. nsresult rv = io->GetProtocolHandler("resource", getter_AddRefs(ph));
  353. NS_ENSURE_SUCCESS_VOID(rv);
  354. nsCOMPtr<nsIResProtocolHandler> irph (do_QueryInterface(ph));
  355. nsResProtocolHandler* rph = static_cast<nsResProtocolHandler*>(irph.get());
  356. rv = rph->CollectSubstitutions(resources);
  357. NS_ENSURE_SUCCESS_VOID(rv);
  358. }
  359. for (auto iter = mOverrideTable.Iter(); !iter.Done(); iter.Next()) {
  360. SerializedURI chromeURI, overrideURI;
  361. SerializeURI(iter.Key(), chromeURI);
  362. SerializeURI(iter.UserData(), overrideURI);
  363. OverrideMapping override = { chromeURI, overrideURI };
  364. overrides.AppendElement(override);
  365. }
  366. if (aParent) {
  367. bool success = aParent->SendRegisterChrome(packages, resources, overrides,
  368. mSelectedLocale, false);
  369. NS_ENSURE_TRUE_VOID(success);
  370. } else {
  371. nsTArray<ContentParent*> parents;
  372. ContentParent::GetAll(parents);
  373. if (!parents.Length())
  374. return;
  375. for (uint32_t i = 0; i < parents.Length(); i++) {
  376. DebugOnly<bool> success =
  377. parents[i]->SendRegisterChrome(packages, resources, overrides,
  378. mSelectedLocale, true);
  379. NS_WARNING_ASSERTION(success,
  380. "couldn't reset a child's registered chrome");
  381. }
  382. }
  383. }
  384. /* static */ void
  385. nsChromeRegistryChrome::ChromePackageFromPackageEntry(const nsACString& aPackageName,
  386. PackageEntry* aPackage,
  387. ChromePackage* aChromePackage,
  388. const nsCString& aSelectedLocale,
  389. const nsCString& aSelectedSkin)
  390. {
  391. SerializeURI(aPackage->baseURI, aChromePackage->contentBaseURI);
  392. SerializeURI(aPackage->locales.GetBase(aSelectedLocale,
  393. nsProviderArray::LOCALE),
  394. aChromePackage->localeBaseURI);
  395. SerializeURI(aPackage->skins.GetBase(aSelectedSkin, nsProviderArray::ANY),
  396. aChromePackage->skinBaseURI);
  397. aChromePackage->package = aPackageName;
  398. aChromePackage->flags = aPackage->flags;
  399. }
  400. static bool
  401. CanLoadResource(nsIURI* aResourceURI)
  402. {
  403. bool isLocalResource = false;
  404. (void)NS_URIChainHasFlags(aResourceURI,
  405. nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
  406. &isLocalResource);
  407. return isLocalResource;
  408. }
  409. nsIURI*
  410. nsChromeRegistryChrome::GetBaseURIFromPackage(const nsCString& aPackage,
  411. const nsCString& aProvider,
  412. const nsCString& aPath)
  413. {
  414. PackageEntry* entry;
  415. if (!mPackagesHash.Get(aPackage, &entry)) {
  416. if (!mInitialized)
  417. return nullptr;
  418. LogMessage("No chrome package registered for chrome://%s/%s/%s",
  419. aPackage.get(), aProvider.get(), aPath.get());
  420. return nullptr;
  421. }
  422. if (aProvider.EqualsLiteral("locale")) {
  423. return entry->locales.GetBase(mSelectedLocale, nsProviderArray::LOCALE);
  424. }
  425. else if (aProvider.EqualsLiteral("skin")) {
  426. return entry->skins.GetBase(mSelectedSkin, nsProviderArray::ANY);
  427. }
  428. else if (aProvider.EqualsLiteral("content")) {
  429. return entry->baseURI;
  430. }
  431. return nullptr;
  432. }
  433. nsresult
  434. nsChromeRegistryChrome::GetFlagsFromPackage(const nsCString& aPackage,
  435. uint32_t* aFlags)
  436. {
  437. PackageEntry* entry;
  438. if (!mPackagesHash.Get(aPackage, &entry))
  439. return NS_ERROR_FILE_NOT_FOUND;
  440. *aFlags = entry->flags;
  441. return NS_OK;
  442. }
  443. nsChromeRegistryChrome::ProviderEntry*
  444. nsChromeRegistryChrome::nsProviderArray::GetProvider(const nsACString& aPreferred, MatchType aType)
  445. {
  446. size_t i = mArray.Length();
  447. if (!i)
  448. return nullptr;
  449. ProviderEntry* found = nullptr; // Only set if we find a partial-match locale
  450. ProviderEntry* entry = nullptr;
  451. while (i--) {
  452. entry = &mArray[i];
  453. if (aPreferred.Equals(entry->provider))
  454. return entry;
  455. if (aType != LOCALE)
  456. continue;
  457. if (LanguagesMatch(aPreferred, entry->provider)) {
  458. found = entry;
  459. continue;
  460. }
  461. if (!found && entry->provider.EqualsLiteral("en-US"))
  462. found = entry;
  463. }
  464. if (!found && aType != EXACT)
  465. return entry;
  466. return found;
  467. }
  468. nsIURI*
  469. nsChromeRegistryChrome::nsProviderArray::GetBase(const nsACString& aPreferred, MatchType aType)
  470. {
  471. ProviderEntry* provider = GetProvider(aPreferred, aType);
  472. if (!provider)
  473. return nullptr;
  474. return provider->baseURI;
  475. }
  476. const nsACString&
  477. nsChromeRegistryChrome::nsProviderArray::GetSelected(const nsACString& aPreferred, MatchType aType)
  478. {
  479. ProviderEntry* entry = GetProvider(aPreferred, aType);
  480. if (entry)
  481. return entry->provider;
  482. return EmptyCString();
  483. }
  484. void
  485. nsChromeRegistryChrome::nsProviderArray::SetBase(const nsACString& aProvider, nsIURI* aBaseURL)
  486. {
  487. ProviderEntry* provider = GetProvider(aProvider, EXACT);
  488. if (provider) {
  489. provider->baseURI = aBaseURL;
  490. return;
  491. }
  492. // no existing entries, add a new one
  493. mArray.AppendElement(ProviderEntry(aProvider, aBaseURL));
  494. }
  495. void
  496. nsChromeRegistryChrome::nsProviderArray::EnumerateToArray(nsTArray<nsCString> *a)
  497. {
  498. int32_t i = mArray.Length();
  499. while (i--) {
  500. a->AppendElement(mArray[i].provider);
  501. }
  502. }
  503. void
  504. nsChromeRegistryChrome::OverlayListEntry::AddURI(nsIURI* aURI)
  505. {
  506. int32_t i = mArray.Count();
  507. while (i--) {
  508. bool equals;
  509. if (NS_SUCCEEDED(aURI->Equals(mArray[i], &equals)) && equals)
  510. return;
  511. }
  512. mArray.AppendObject(aURI);
  513. }
  514. void
  515. nsChromeRegistryChrome::OverlayListHash::Add(nsIURI* aBase, nsIURI* aOverlay)
  516. {
  517. OverlayListEntry* entry = mTable.PutEntry(aBase);
  518. if (entry)
  519. entry->AddURI(aOverlay);
  520. }
  521. const nsCOMArray<nsIURI>*
  522. nsChromeRegistryChrome::OverlayListHash::GetArray(nsIURI* aBase)
  523. {
  524. OverlayListEntry* entry = mTable.GetEntry(aBase);
  525. if (!entry)
  526. return nullptr;
  527. return &entry->mArray;
  528. }
  529. #ifdef MOZ_XUL
  530. NS_IMETHODIMP
  531. nsChromeRegistryChrome::GetStyleOverlays(nsIURI *aChromeURL,
  532. nsISimpleEnumerator **aResult)
  533. {
  534. nsCOMPtr<nsIURI> chromeURLWithoutHash;
  535. if (aChromeURL) {
  536. aChromeURL->CloneIgnoringRef(getter_AddRefs(chromeURLWithoutHash));
  537. }
  538. const nsCOMArray<nsIURI>* parray = mStyleHash.GetArray(chromeURLWithoutHash);
  539. if (!parray)
  540. return NS_NewEmptyEnumerator(aResult);
  541. return NS_NewArrayEnumerator(aResult, *parray);
  542. }
  543. NS_IMETHODIMP
  544. nsChromeRegistryChrome::GetXULOverlays(nsIURI *aChromeURL,
  545. nsISimpleEnumerator **aResult)
  546. {
  547. nsCOMPtr<nsIURI> chromeURLWithoutHash;
  548. if (aChromeURL) {
  549. aChromeURL->CloneIgnoringRef(getter_AddRefs(chromeURLWithoutHash));
  550. }
  551. const nsCOMArray<nsIURI>* parray = mOverlayHash.GetArray(chromeURLWithoutHash);
  552. if (!parray)
  553. return NS_NewEmptyEnumerator(aResult);
  554. return NS_NewArrayEnumerator(aResult, *parray);
  555. }
  556. #endif // MOZ_XUL
  557. nsIURI*
  558. nsChromeRegistry::ManifestProcessingContext::GetManifestURI()
  559. {
  560. if (!mManifestURI) {
  561. nsCString uri;
  562. mFile.GetURIString(uri);
  563. NS_NewURI(getter_AddRefs(mManifestURI), uri);
  564. }
  565. return mManifestURI;
  566. }
  567. nsIXPConnect*
  568. nsChromeRegistry::ManifestProcessingContext::GetXPConnect()
  569. {
  570. if (!mXPConnect)
  571. mXPConnect = do_GetService("@mozilla.org/js/xpc/XPConnect;1");
  572. return mXPConnect;
  573. }
  574. already_AddRefed<nsIURI>
  575. nsChromeRegistry::ManifestProcessingContext::ResolveURI(const char* uri)
  576. {
  577. nsIURI* baseuri = GetManifestURI();
  578. if (!baseuri)
  579. return nullptr;
  580. nsCOMPtr<nsIURI> resolved;
  581. nsresult rv = NS_NewURI(getter_AddRefs(resolved), uri, baseuri);
  582. if (NS_FAILED(rv))
  583. return nullptr;
  584. return resolved.forget();
  585. }
  586. static void
  587. EnsureLowerCase(char *aBuf)
  588. {
  589. for (; *aBuf; ++aBuf) {
  590. char ch = *aBuf;
  591. if (ch >= 'A' && ch <= 'Z')
  592. *aBuf = ch + 'a' - 'A';
  593. }
  594. }
  595. static void
  596. SendManifestEntry(const ChromeRegistryItem &aItem)
  597. {
  598. nsTArray<ContentParent*> parents;
  599. ContentParent::GetAll(parents);
  600. if (!parents.Length())
  601. return;
  602. for (uint32_t i = 0; i < parents.Length(); i++) {
  603. Unused << parents[i]->SendRegisterChromeItem(aItem);
  604. }
  605. }
  606. void
  607. nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext& cx, int lineno,
  608. char *const * argv, int flags)
  609. {
  610. char* package = argv[0];
  611. char* uri = argv[1];
  612. EnsureLowerCase(package);
  613. nsCOMPtr<nsIURI> resolved = cx.ResolveURI(uri);
  614. if (!resolved) {
  615. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  616. "During chrome registration, unable to create URI '%s'.", uri);
  617. return;
  618. }
  619. if (!CanLoadResource(resolved)) {
  620. LogMessageWithContext(resolved, lineno, nsIScriptError::warningFlag,
  621. "During chrome registration, cannot register non-local URI '%s' as content.",
  622. uri);
  623. return;
  624. }
  625. nsDependentCString packageName(package);
  626. PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
  627. entry->baseURI = resolved;
  628. entry->flags = flags;
  629. if (mDynamicRegistration) {
  630. ChromePackage chromePackage;
  631. ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
  632. mSelectedLocale, mSelectedSkin);
  633. SendManifestEntry(chromePackage);
  634. }
  635. }
  636. void
  637. nsChromeRegistryChrome::ManifestLocale(ManifestProcessingContext& cx, int lineno,
  638. char *const * argv, int flags)
  639. {
  640. char* package = argv[0];
  641. char* provider = argv[1];
  642. char* uri = argv[2];
  643. EnsureLowerCase(package);
  644. nsCOMPtr<nsIURI> resolved = cx.ResolveURI(uri);
  645. if (!resolved) {
  646. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  647. "During chrome registration, unable to create URI '%s'.", uri);
  648. return;
  649. }
  650. if (!CanLoadResource(resolved)) {
  651. LogMessageWithContext(resolved, lineno, nsIScriptError::warningFlag,
  652. "During chrome registration, cannot register non-local URI '%s' as content.",
  653. uri);
  654. return;
  655. }
  656. nsDependentCString packageName(package);
  657. PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
  658. entry->locales.SetBase(nsDependentCString(provider), resolved);
  659. if (mDynamicRegistration) {
  660. ChromePackage chromePackage;
  661. ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
  662. mSelectedLocale, mSelectedSkin);
  663. SendManifestEntry(chromePackage);
  664. }
  665. }
  666. void
  667. nsChromeRegistryChrome::ManifestSkin(ManifestProcessingContext& cx, int lineno,
  668. char *const * argv, int flags)
  669. {
  670. char* package = argv[0];
  671. char* provider = argv[1];
  672. char* uri = argv[2];
  673. EnsureLowerCase(package);
  674. nsCOMPtr<nsIURI> resolved = cx.ResolveURI(uri);
  675. if (!resolved) {
  676. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  677. "During chrome registration, unable to create URI '%s'.", uri);
  678. return;
  679. }
  680. if (!CanLoadResource(resolved)) {
  681. LogMessageWithContext(resolved, lineno, nsIScriptError::warningFlag,
  682. "During chrome registration, cannot register non-local URI '%s' as content.",
  683. uri);
  684. return;
  685. }
  686. nsDependentCString packageName(package);
  687. PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
  688. entry->skins.SetBase(nsDependentCString(provider), resolved);
  689. if (mDynamicRegistration) {
  690. ChromePackage chromePackage;
  691. ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
  692. mSelectedLocale, mSelectedSkin);
  693. SendManifestEntry(chromePackage);
  694. }
  695. }
  696. void
  697. nsChromeRegistryChrome::ManifestOverlay(ManifestProcessingContext& cx, int lineno,
  698. char *const * argv, int flags)
  699. {
  700. char* base = argv[0];
  701. char* overlay = argv[1];
  702. nsCOMPtr<nsIURI> baseuri = cx.ResolveURI(base);
  703. nsCOMPtr<nsIURI> overlayuri = cx.ResolveURI(overlay);
  704. if (!baseuri || !overlayuri) {
  705. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  706. "During chrome registration, unable to create URI.");
  707. return;
  708. }
  709. if (!CanLoadResource(overlayuri)) {
  710. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  711. "Cannot register non-local URI '%s' as an overlay.", overlay);
  712. return;
  713. }
  714. nsCOMPtr<nsIURI> baseuriWithoutHash;
  715. baseuri->CloneIgnoringRef(getter_AddRefs(baseuriWithoutHash));
  716. mOverlayHash.Add(baseuriWithoutHash, overlayuri);
  717. }
  718. void
  719. nsChromeRegistryChrome::ManifestStyle(ManifestProcessingContext& cx, int lineno,
  720. char *const * argv, int flags)
  721. {
  722. char* base = argv[0];
  723. char* overlay = argv[1];
  724. nsCOMPtr<nsIURI> baseuri = cx.ResolveURI(base);
  725. nsCOMPtr<nsIURI> overlayuri = cx.ResolveURI(overlay);
  726. if (!baseuri || !overlayuri) {
  727. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  728. "During chrome registration, unable to create URI.");
  729. return;
  730. }
  731. if (!CanLoadResource(overlayuri)) {
  732. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  733. "Cannot register non-local URI '%s' as a style overlay.", overlay);
  734. return;
  735. }
  736. nsCOMPtr<nsIURI> baseuriWithoutHash;
  737. baseuri->CloneIgnoringRef(getter_AddRefs(baseuriWithoutHash));
  738. mStyleHash.Add(baseuriWithoutHash, overlayuri);
  739. }
  740. void
  741. nsChromeRegistryChrome::ManifestOverride(ManifestProcessingContext& cx, int lineno,
  742. char *const * argv, int flags)
  743. {
  744. char* chrome = argv[0];
  745. char* resolved = argv[1];
  746. nsCOMPtr<nsIURI> chromeuri = cx.ResolveURI(chrome);
  747. nsCOMPtr<nsIURI> resolveduri = cx.ResolveURI(resolved);
  748. if (!chromeuri || !resolveduri) {
  749. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  750. "During chrome registration, unable to create URI.");
  751. return;
  752. }
  753. if (cx.mType == NS_SKIN_LOCATION) {
  754. bool chromeSkinOnly = false;
  755. nsresult rv = chromeuri->SchemeIs("chrome", &chromeSkinOnly);
  756. chromeSkinOnly = chromeSkinOnly && NS_SUCCEEDED(rv);
  757. if (chromeSkinOnly) {
  758. rv = resolveduri->SchemeIs("chrome", &chromeSkinOnly);
  759. chromeSkinOnly = chromeSkinOnly && NS_SUCCEEDED(rv);
  760. }
  761. if (chromeSkinOnly) {
  762. nsAutoCString chromePath, resolvedPath;
  763. chromeuri->GetPath(chromePath);
  764. resolveduri->GetPath(resolvedPath);
  765. chromeSkinOnly = StringBeginsWith(chromePath, NS_LITERAL_CSTRING("/skin/")) &&
  766. StringBeginsWith(resolvedPath, NS_LITERAL_CSTRING("/skin/"));
  767. }
  768. if (!chromeSkinOnly) {
  769. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  770. "Cannot register non-chrome://.../skin/ URIs '%s' and '%s' as overrides and/or to be overridden from a skin manifest.",
  771. chrome, resolved);
  772. return;
  773. }
  774. }
  775. if (!CanLoadResource(resolveduri)) {
  776. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  777. "Cannot register non-local URI '%s' for an override.", resolved);
  778. return;
  779. }
  780. mOverrideTable.Put(chromeuri, resolveduri);
  781. if (mDynamicRegistration) {
  782. SerializedURI serializedChrome;
  783. SerializedURI serializedOverride;
  784. SerializeURI(chromeuri, serializedChrome);
  785. SerializeURI(resolveduri, serializedOverride);
  786. OverrideMapping override = { serializedChrome, serializedOverride };
  787. SendManifestEntry(override);
  788. }
  789. }
  790. void
  791. nsChromeRegistryChrome::ManifestResource(ManifestProcessingContext& cx, int lineno,
  792. char *const * argv, int flags)
  793. {
  794. char* package = argv[0];
  795. char* uri = argv[1];
  796. EnsureLowerCase(package);
  797. nsDependentCString host(package);
  798. nsCOMPtr<nsIIOService> io = mozilla::services::GetIOService();
  799. if (!io) {
  800. NS_WARNING("No IO service trying to process chrome manifests");
  801. return;
  802. }
  803. nsCOMPtr<nsIProtocolHandler> ph;
  804. nsresult rv = io->GetProtocolHandler("resource", getter_AddRefs(ph));
  805. if (NS_FAILED(rv))
  806. return;
  807. nsCOMPtr<nsIResProtocolHandler> rph = do_QueryInterface(ph);
  808. nsCOMPtr<nsIURI> resolved = cx.ResolveURI(uri);
  809. if (!resolved) {
  810. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  811. "During chrome registration, unable to create URI '%s'.", uri);
  812. return;
  813. }
  814. if (!CanLoadResource(resolved)) {
  815. LogMessageWithContext(cx.GetManifestURI(), lineno, nsIScriptError::warningFlag,
  816. "Warning: cannot register non-local URI '%s' as a resource.",
  817. uri);
  818. return;
  819. }
  820. rph->SetSubstitution(host, resolved);
  821. }