From 75db19c5a609d872bf32d8de05a157725d83c103 Mon Sep 17 00:00:00 2001 From: Hernan Rodriguez Colmeiro Date: Fri, 29 Jul 2011 11:20:47 +0200 Subject: [PATCH] Bug 564667: Allow bootstrapped add-ons to have chrome URLs. r=dtownsend, sr=bsmedberg --- chrome/test/unit/data/test_bug564667.xpi | Bin 0 -> 569 bytes .../unit/data/test_bug564667/chrome.manifest | 16 ++ .../unit/data/test_bug564667/loaded.manifest | 2 + chrome/test/unit/test_bug564667.js | 155 ++++++++++++++++++ chrome/test/unit/xpcshell.ini | 1 + xpcom/build/nsXULAppAPI.h | 3 +- xpcom/components/ManifestParser.cpp | 41 +++-- xpcom/components/nsComponentManager.cpp | 64 +++++++- xpcom/components/nsComponentManager.h | 18 +- xpcom/components/nsIComponentManager.idl | 24 ++- 10 files changed, 300 insertions(+), 24 deletions(-) create mode 100644 chrome/test/unit/data/test_bug564667.xpi create mode 100644 chrome/test/unit/data/test_bug564667/chrome.manifest create mode 100644 chrome/test/unit/data/test_bug564667/loaded.manifest create mode 100644 chrome/test/unit/test_bug564667.js diff --git a/chrome/test/unit/data/test_bug564667.xpi b/chrome/test/unit/data/test_bug564667.xpi new file mode 100644 index 0000000000000000000000000000000000000000..be1632d8ca90969c3835124b7d8308360d744cea GIT binary patch literal 569 zcmWIWW@Zs#U|`^2h-!Fax8`u4{6QdZ2O|RmKZ6WIaz;^pZmM2xVqRuiYH>+u2qy#c zoJ%r(+po&_l~!;wFtU6FsstO*I^iJKVFjMH_jaxeo`+W5E!_~DxM3~7!Yrf6RSU|t zNXXSsKe0ZVql>gbe7HpE*e0fxm6fN@^YQXe*ADPke(S7yx}zgg zZ~nG!v4pqZme21C+`||E4~e4a=XU3A>?~IUhJ+OmgF+%FKQSdW1vMn5{}RO(65i*| zYkQv64h+!n)YUT#YG4%bpRcWMZ)vC=%BVMU)^cenp2;i>0p5&Ea?H4bUIJ(>0|PK@ y8J0AHSQrt(3W*4`NI*6e(-+8waskc8@DtEbke@INWn}}ofe8p#0qIJRvlsw;im?~~ literal 0 HcmV?d00001 diff --git a/chrome/test/unit/data/test_bug564667/chrome.manifest b/chrome/test/unit/data/test_bug564667/chrome.manifest new file mode 100644 index 00000000000..b35692764b4 --- /dev/null +++ b/chrome/test/unit/data/test_bug564667/chrome.manifest @@ -0,0 +1,16 @@ +# Locally defined URLs +content test1 test/ +locale test1 en-US test/ +skin test1 test test/ + +# Test Override +content testOverride test/ +override chrome://testOverride/content file:///test1/override + + +# Load external manifest +manifest loaded.manifest + +# Failure Cases +overlay chrome://test1/content/overlay.xul chrome://test1/content/test1.xul +style chrome://test1/content/style.xul chrome://test1/content/test1.css diff --git a/chrome/test/unit/data/test_bug564667/loaded.manifest b/chrome/test/unit/data/test_bug564667/loaded.manifest new file mode 100644 index 00000000000..2f231856a4e --- /dev/null +++ b/chrome/test/unit/data/test_bug564667/loaded.manifest @@ -0,0 +1,2 @@ +content test2 test/ +locale test2 en-US test/ diff --git a/chrome/test/unit/test_bug564667.js b/chrome/test/unit/test_bug564667.js new file mode 100644 index 00000000000..6137ea800a0 --- /dev/null +++ b/chrome/test/unit/test_bug564667.js @@ -0,0 +1,155 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * the Mozilla Foundation. + * + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Hernan Rodriguez Colmeiro . + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** + */ + +const UNPACKAGED_ADDON = do_get_file("data/test_bug564667"); +const PACKAGED_ADDON = do_get_file("data/test_bug564667.xpi"); + +var gIOS = Cc["@mozilla.org/network/io-service;1"]. + getService(Ci.nsIIOService); + +var gCR = Cc["@mozilla.org/chrome/chrome-registry;1"]. + getService(Ci.nsIChromeRegistry). + QueryInterface(Ci.nsIXULOverlayProvider); + +/* + * Checks that a mapping was added + */ +function test_mapping(chromeURL, target) { + var uri = gIOS.newURI(chromeURL, null, null); + + try { + var result = gCR.convertChromeURL(uri); + do_check_eq(result.spec, target); + } + catch (ex) { + do_throw(chromeURL + " not Registered"); + } +} + +/* + * Checks that a mapping was removed + */ +function test_removed_mapping(chromeURL, target) { + var uri = gIOS.newURI(chromeURL, null, null); + try { + var result = gCR.convertChromeURL(uri); + do_throw(chromeURL + " not removed"); + } + catch (ex) { + // This should throw + } +} + +/* + * Checks if any overlay was added after loading + * the manifest files + * + * @param type The type of overlay: overlay|style + */ +function test_no_overlays(chromeURL, target, type) { + var type = type || "overlay"; + var uri = gIOS.newURI(chromeURL, null, null); + var present = false, elem; + + var overlays = (type == "overlay") ? + gCR.getXULOverlays(uri) : gCR.getStyleOverlays(uri); + + // We shouldn't be allowed to register overlays nor styles + if (overlays.hasMoreElements()) { + if (type == "styles") + do_throw("Style Registered: " + chromeURL); + else + do_throw("Overlay Registered: " + chromeURL); + } +} + +function testManifest(manifestPath, baseURI) { + + // ------------------ Add manifest file ------------------------ + Components.manager.addBootstrappedManifestLocation(manifestPath); + + // Test Adding Content URL + test_mapping("chrome://test1/content", baseURI + "test/test1.xul"); + + // Test Adding Locale URL + test_mapping("chrome://test1/locale", baseURI + "test/test1.dtd"); + + // Test Adding Skin URL + test_mapping("chrome://test1/skin", baseURI + "test/test1.css"); + + // Test Adding Manifest URL + test_mapping("chrome://test2/content", baseURI + "test/test2.xul"); + test_mapping("chrome://test2/locale", baseURI + "test/test2.dtd"); + + // Test Adding Override + test_mapping("chrome://testOverride/content", 'file:///test1/override') + + // Test Not-Adding Overlays + test_no_overlays("chrome://test1/content/overlay.xul", + "chrome://test1/content/test1.xul"); + + // Test Not-Adding Styles + test_no_overlays("chrome://test1/content/style.xul", + "chrome://test1/content/test1.css", "styles"); + + + // ------------------ Remove manifest file ------------------------ + Components.manager.removeBootstrappedManifestLocation(manifestPath); + + // Test Removing Content URL + test_removed_mapping("chrome://test1/content", baseURI + "test/test1.xul"); + + // Test Removing Content URL + test_removed_mapping("chrome://test1/locale", baseURI + "test/test1.dtd"); + + // Test Removing Skin URL + test_removed_mapping("chrome://test1/skin", baseURI + "test/test1.css"); + + // Test Removing Manifest URL + test_removed_mapping("chrome://test2/content", baseURI + "test/test2.xul"); + test_removed_mapping("chrome://test2/locale", baseURI + "test/test2.dtd"); +} + +function run_test() { + // Test an unpackaged addon + testManifest(UNPACKAGED_ADDON, gIOS.newFileURI(UNPACKAGED_ADDON).spec); + + // Test a packaged addon + testManifest(PACKAGED_ADDON, "jar:" + gIOS.newFileURI(PACKAGED_ADDON).spec + "!/"); +} diff --git a/chrome/test/unit/xpcshell.ini b/chrome/test/unit/xpcshell.ini index 1a5ec94acd6..da5f4df5973 100644 --- a/chrome/test/unit/xpcshell.ini +++ b/chrome/test/unit/xpcshell.ini @@ -10,6 +10,7 @@ tail = [test_bug401153.js] [test_bug415367.js] [test_bug519468.js] +[test_bug564667.js] [test_crlf.js] [test_data_protocol_registration.js] [test_no_remote_registration.js] diff --git a/xpcom/build/nsXULAppAPI.h b/xpcom/build/nsXULAppAPI.h index 7f27bc13bcd..f2251c1ef9f 100644 --- a/xpcom/build/nsXULAppAPI.h +++ b/xpcom/build/nsXULAppAPI.h @@ -373,7 +373,8 @@ XRE_API(nsresult, enum NSLocationType { NS_COMPONENT_LOCATION, - NS_SKIN_LOCATION + NS_SKIN_LOCATION, + NS_BOOTSTRAPPED_LOCATION }; XRE_API(nsresult, diff --git a/xpcom/components/ManifestParser.cpp b/xpcom/components/ManifestParser.cpp index adc26398785..d3b63b9176d 100644 --- a/xpcom/components/ManifestParser.cpp +++ b/xpcom/components/ManifestParser.cpp @@ -76,6 +76,8 @@ struct ManifestDirective bool ischrome; + bool allowbootstrap; + // The platform/contentaccessible flags only apply to content directives. bool contentflags; @@ -92,31 +94,31 @@ struct ManifestDirective bool isContract; }; static const ManifestDirective kParsingTable[] = { - { "manifest", 1, false, true, false, + { "manifest", 1, false, true, true, false, &nsComponentManagerImpl::ManifestManifest, NULL }, - { "binary-component", 1, true, false, false, + { "binary-component", 1, true, false, false, false, &nsComponentManagerImpl::ManifestBinaryComponent, NULL }, - { "interfaces", 1, true, false, false, + { "interfaces", 1, true, false, false, false, &nsComponentManagerImpl::ManifestXPT, NULL }, - { "component", 2, true, false, false, + { "component", 2, true, false, false, false, &nsComponentManagerImpl::ManifestComponent, NULL }, - { "contract", 2, true, false, false, + { "contract", 2, true, false, false, false, &nsComponentManagerImpl::ManifestContract, NULL, true}, - { "category", 3, true, false, false, + { "category", 3, true, false, false, false, &nsComponentManagerImpl::ManifestCategory, NULL }, - { "content", 2, true, true, true, + { "content", 2, true, true, true, true, NULL, &nsChromeRegistry::ManifestContent }, - { "locale", 3, true, true, false, + { "locale", 3, true, true, true, false, NULL, &nsChromeRegistry::ManifestLocale }, - { "skin", 3, false, true, false, + { "skin", 3, false, true, true, false, NULL, &nsChromeRegistry::ManifestSkin }, - { "overlay", 2, true, true, false, + { "overlay", 2, true, true, false, false, NULL, &nsChromeRegistry::ManifestOverlay }, - { "style", 2, false, true, false, + { "style", 2, false, true, false, false, NULL, &nsChromeRegistry::ManifestStyle }, - { "override", 2, true, true, false, + { "override", 2, true, true, true, false, NULL, &nsChromeRegistry::ManifestOverride }, - { "resource", 2, true, true, false, + { "resource", 2, true, true, false, false, NULL, &nsChromeRegistry::ManifestResource } }; @@ -450,7 +452,7 @@ ParseManifestCommon(NSLocationType aType, nsILocalFile* aFile, rv = xapp->GetVersion(s); if (NS_SUCCEEDED(rv)) CopyUTF8toUTF16(s, appVersion); - + nsCOMPtr xruntime (do_QueryInterface(xapp)); if (xruntime) { rv = xruntime->GetOS(s); @@ -538,13 +540,22 @@ ParseManifestCommon(NSLocationType aType, nsILocalFile* aFile, break; } } + if (!directive) { LogMessageWithContext(aFile, aPath, line, "Ignoring unrecognized chrome manifest directive '%s'.", token); continue; } - if (directive->componentonly && NS_COMPONENT_LOCATION != aType) { + + if (!directive->allowbootstrap && NS_BOOTSTRAPPED_LOCATION == aType) { + LogMessageWithContext(aFile, aPath, line, + "Bootstrapped manifest not allowed to use '%s' directive.", + token); + continue; + } + + if (directive->componentonly && NS_SKIN_LOCATION == aType) { LogMessageWithContext(aFile, aPath, line, "Skin manifest not allowed to use '%s' directive.", token); diff --git a/xpcom/components/nsComponentManager.cpp b/xpcom/components/nsComponentManager.cpp index 8d9b093a9b3..06737ffa054 100644 --- a/xpcom/components/nsComponentManager.cpp +++ b/xpcom/components/nsComponentManager.cpp @@ -89,6 +89,7 @@ #include "prio.h" #include "mozilla/FunctionTimer.h" #include "ManifestParser.h" +#include "mozilla/Services.h" #include "nsManifestLineReader.h" #include "mozilla/GenericFactory.h" @@ -406,7 +407,7 @@ nsresult nsComponentManagerImpl::Init() nsCOMPtr reader = do_CreateInstance(kZipReaderCID, &rv); rv = reader->Open(l.location); if (NS_SUCCEEDED(rv)) - RegisterJarManifest(reader, "chrome.manifest", false); + RegisterJarManifest(l.type, reader, "chrome.manifest", false); } nsCategoryManager::GetSingleton()->SuppressNotifications(false); @@ -542,7 +543,7 @@ LoadEntry(nsIZipReader* aReader, const char* aName) } void -nsComponentManagerImpl::RegisterJarManifest(nsIZipReader* aReader, +nsComponentManagerImpl::RegisterJarManifest(NSLocationType aType, nsIZipReader* aReader, const char* aPath, bool aChromeOnly) { nsCOMPtr is = LoadEntry(aReader, aPath); @@ -576,7 +577,7 @@ nsComponentManagerImpl::RegisterJarManifest(nsIZipReader* aReader, whole[flen] = '\0'; - ParseManifest(NS_COMPONENT_LOCATION, aReader, aPath, + ParseManifest(aType, aReader, aPath, whole, aChromeOnly); } @@ -674,7 +675,7 @@ nsComponentManagerImpl::ManifestManifest(ManifestProcessingContext& cx, int line nsCAutoString manifest(cx.mPath); AppendFileToManifestPath(manifest, file); - RegisterJarManifest(cx.mReader, manifest.get(), cx.mChromeOnly); + RegisterJarManifest(cx.mType, cx.mReader, manifest.get(), cx.mChromeOnly); } else { #ifdef TRANSLATE_SLASHES @@ -911,7 +912,7 @@ nsComponentManagerImpl::RereadChromeManifests() if (NS_SUCCEEDED(rv)) rv = reader->Open(l.location); if (NS_SUCCEEDED(rv)) - RegisterJarManifest(reader, "chrome.manifest", true); + RegisterJarManifest(l.type, reader, "chrome.manifest", true); } } @@ -2036,6 +2037,57 @@ XRE_AddStaticComponent(const mozilla::Module* aComponent) return NS_OK; } +NS_IMETHODIMP +nsComponentManagerImpl::AddBootstrappedManifestLocation(nsILocalFile* aLocation) +{ + nsString path; + nsresult rv = aLocation->GetPath(path); + if (NS_FAILED(rv)) + return rv; + + if (Substring(path, path.Length() - 4).Equals(NS_LITERAL_STRING(".xpi"))) { + return XRE_AddJarManifestLocation(NS_BOOTSTRAPPED_LOCATION, aLocation); + } + + nsCOMPtr manifest = + CloneAndAppend(aLocation, NS_LITERAL_CSTRING("chrome.manifest")); + return XRE_AddManifestLocation(NS_BOOTSTRAPPED_LOCATION, manifest); +} + +NS_IMETHODIMP +nsComponentManagerImpl::RemoveBootstrappedManifestLocation(nsILocalFile* aLocation) +{ + nsCOMPtr cr = mozilla::services::GetChromeRegistryService(); + if (!cr) + return NS_ERROR_FAILURE; + + bool isJar = false; + nsCOMPtr manifest; + nsString path; + nsresult rv = aLocation->GetPath(path); + if (NS_FAILED(rv)) + return rv; + + if (Substring(path, path.Length() - 4).Equals(NS_LITERAL_STRING(".xpi"))) { + isJar = true; + manifest = aLocation; + } else { + manifest = CloneAndAppend(aLocation, NS_LITERAL_CSTRING("chrome.manifest")); + } + + nsComponentManagerImpl::ComponentLocation elem = { + NS_BOOTSTRAPPED_LOCATION, + manifest, + isJar + }; + + // Remove reference. + nsComponentManagerImpl::sModuleLocations->RemoveElement(elem, ComponentLocationComparator()); + + rv = cr->CheckForNewChrome(); + return rv; +} + EXPORT_XPCOM_API(nsresult) XRE_AddManifestLocation(NSLocationType aType, nsILocalFile* aLocation) { @@ -2073,7 +2125,7 @@ XRE_AddJarManifestLocation(NSLocationType aType, nsILocalFile* aLocation) rv = reader->Open(c->location); if (NS_SUCCEEDED(rv)) - nsComponentManagerImpl::gComponentManager->RegisterJarManifest(reader, "chrome.manifest", false); + nsComponentManagerImpl::gComponentManager->RegisterJarManifest(aType, reader, "chrome.manifest", false); return NS_OK; } diff --git a/xpcom/components/nsComponentManager.h b/xpcom/components/nsComponentManager.h index 02e0c8b02c3..4090de81cbb 100644 --- a/xpcom/components/nsComponentManager.h +++ b/xpcom/components/nsComponentManager.h @@ -162,6 +162,22 @@ public: bool jar; }; + class ComponentLocationComparator + { + public: + PRBool Equals(const ComponentLocation& a, const ComponentLocation& b) const + { + if (a.type == b.type && a.jar == b.jar) { + PRBool res; + nsresult rv = a.location->Equals(b.location, &res); + NS_ASSERTION(NS_SUCCEEDED(rv), "Error comparing locations"); + return res; + } + + return PR_FALSE; + } + }; + static nsTArray* sStaticModules; static nsTArray* sModuleLocations; static nsTArray* sJarModuleLocations; @@ -240,7 +256,7 @@ public: KnownModule* aModule); void RegisterContractID(const mozilla::Module::ContractIDEntry* aEntry); - void RegisterJarManifest(nsIZipReader* aReader, + void RegisterJarManifest(NSLocationType aType, nsIZipReader* aReader, const char* aPath, bool aChromeOnly); void RegisterManifestFile(NSLocationType aType, nsILocalFile* aFile, diff --git a/xpcom/components/nsIComponentManager.idl b/xpcom/components/nsIComponentManager.idl index e5f700cd552..2101e7f6d4f 100644 --- a/xpcom/components/nsIComponentManager.idl +++ b/xpcom/components/nsIComponentManager.idl @@ -41,9 +41,10 @@ #include "nsISupports.idl" +interface nsILocalFile; interface nsIFactory; -[scriptable, uuid(a88e5a60-205a-4bb1-94e1-2628daf51eae)] +[scriptable, uuid(1d940426-5fe5-42c3-84ae-a300f2d9ebd5)] interface nsIComponentManager : nsISupports { /** @@ -99,6 +100,27 @@ interface nsIComponentManager : nsISupports in nsISupports aDelegate, in nsIIDRef aIID, [iid_is(aIID),retval] out nsQIResult result); + + /** + * addBootstrappedManifestLocation + * + * Adds a bootstrapped manifest location on runtime. + * + * @param aLocation : A directory where chrome.manifest resides, + * or an XPI with it on the root. + */ + void addBootstrappedManifestLocation(in nsILocalFile aLocation); + + /** + * removeBootstrappedManifestLocation + * + * Removes a bootstrapped manifest location on runtime. + * + * @param aLocation : A directory where chrome.manifest resides, + * or an XPI with it on the root. + */ + void removeBootstrappedManifestLocation(in nsILocalFile aLocation); + };