diff --git a/chrome/src/nsChromeRegistry.cpp b/chrome/src/nsChromeRegistry.cpp index d9a30480d89..a8248ea8492 100644 --- a/chrome/src/nsChromeRegistry.cpp +++ b/chrome/src/nsChromeRegistry.cpp @@ -1136,6 +1136,40 @@ nsChromeRegistry::AllowScriptsForPackage(nsIURI* aChromeURI, PRBool *aResult) return NS_OK; } +NS_IMETHODIMP +nsChromeRegistry::AllowContentToAccess(nsIURI *aURI, PRBool *aResult) +{ + nsresult rv; + + *aResult = PR_FALSE; + +#ifdef DEBUG + PRBool isChrome; + aURI->SchemeIs("chrome", &isChrome); + NS_ASSERTION(isChrome, "Non-chrome URI passed to AllowContentToAccess!"); +#endif + + nsCOMPtr url = do_QueryInterface(aURI); + if (!url) { + NS_ERROR("Chrome URL doesn't implement nsIURL."); + return NS_ERROR_UNEXPECTED; + } + + nsCAutoString package; + rv = url->GetHostPort(package); + NS_ENSURE_SUCCESS(rv, rv); + + PackageEntry *entry = + static_cast(PL_DHashTableOperate(&mPackagesHash, + & (nsACString&) package, + PL_DHASH_LOOKUP)); + + if (PL_DHASH_ENTRY_IS_BUSY(entry)) { + *aResult = !!(entry->flags & PackageEntry::CONTENT_ACCESSIBLE); + } + return NS_OK; +} + static PLDHashOperator RemoveAll(PLDHashTable *table, PLDHashEntryHdr *entry, PRUint32 number, void *arg) { @@ -2130,6 +2164,7 @@ nsChromeRegistry::ProcessManifestBuffer(char *buf, PRInt32 length, NS_NAMED_LITERAL_STRING(kPlatform, "platform"); NS_NAMED_LITERAL_STRING(kXPCNativeWrappers, "xpcnativewrappers"); + NS_NAMED_LITERAL_STRING(kContentAccessible, "contentaccessible"); NS_NAMED_LITERAL_STRING(kApplication, "application"); NS_NAMED_LITERAL_STRING(kAppVersion, "appversion"); NS_NAMED_LITERAL_STRING(kOs, "os"); @@ -2234,6 +2269,7 @@ nsChromeRegistry::ProcessManifestBuffer(char *buf, PRInt32 length, PRBool platform = PR_FALSE; PRBool xpcNativeWrappers = PR_TRUE; + PRBool contentAccessible = PR_FALSE; TriState stAppVersion = eUnspecified; TriState stApp = eUnspecified; TriState stOsVersion = eUnspecified; @@ -2248,6 +2284,7 @@ nsChromeRegistry::ProcessManifestBuffer(char *buf, PRInt32 length, if (CheckFlag(kPlatform, wtoken, platform) || CheckFlag(kXPCNativeWrappers, wtoken, xpcNativeWrappers) || + CheckFlag(kContentAccessible, wtoken, contentAccessible) || CheckStringFlag(kApplication, wtoken, appID, stApp) || CheckStringFlag(kOs, wtoken, osTarget, stOs) || CheckVersionFlag(kOsVersion, wtoken, osVersion, vc, stOsVersion) || @@ -2283,6 +2320,8 @@ nsChromeRegistry::ProcessManifestBuffer(char *buf, PRInt32 length, entry->flags |= PackageEntry::PLATFORM_PACKAGE; if (xpcNativeWrappers) entry->flags |= PackageEntry::XPCNATIVEWRAPPERS; + if (contentAccessible) + entry->flags |= PackageEntry::CONTENT_ACCESSIBLE; if (xpc) { nsCAutoString urlp("chrome://"); urlp.Append(package); diff --git a/chrome/src/nsChromeRegistry.h b/chrome/src/nsChromeRegistry.h index 6c97354a385..ecd3f186a64 100644 --- a/chrome/src/nsChromeRegistry.h +++ b/chrome/src/nsChromeRegistry.h @@ -185,7 +185,10 @@ public: // This package should use the new XPCNativeWrappers to separate // content from chrome. This flag is currently unused (because we call // into xpconnect at registration time). - XPCNATIVEWRAPPERS = 1 << 1 + XPCNATIVEWRAPPERS = 1 << 1, + + // Content script may access files in this package + CONTENT_ACCESSIBLE = 1 << 2 }; nsCString package; diff --git a/chrome/test/unit/data/test_bug292789.manifest b/chrome/test/unit/data/test_bug292789.manifest new file mode 100644 index 00000000000..df67aa15c8b --- /dev/null +++ b/chrome/test/unit/data/test_bug292789.manifest @@ -0,0 +1,4 @@ +content test1 test/ +content test2 test/ contentaccessible=yes +content test3 test/ contentaccessible=no +content test4 test/ contentaccessible diff --git a/chrome/test/unit/head_crtestutils.js b/chrome/test/unit/head_crtestutils.js new file mode 100644 index 00000000000..1616ea93ef5 --- /dev/null +++ b/chrome/test/unit/head_crtestutils.js @@ -0,0 +1,69 @@ +const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; +const XULAPPINFO_CID = Components.ID("{48a4e946-1f9f-4224-b4b0-9a54183cb81e}"); + +const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; + +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cr = Components.results; + +function ArrayEnumerator(array) +{ + this.array = array; +} + +ArrayEnumerator.prototype = { + pos: 0, + + hasMoreElements: function() { + return this.pos < this.array.length; + }, + + getNext: function() { + if (this.pos < this.array.length) + return this.array[this.pos++]; + throw Cr.NS_ERROR_FAILURE; + }, + + QueryInterface: function(iid) { + if (iid.equals(Ci.nsISimpleEnumerator) + || iid.equals(Ci.nsISupports)) + return this; + + throw Cr.NS_ERROR_NO_INTERFACE; + } +}; + +function ChromeProvider(manifests) +{ + this._manifests = manifests; +} + +ChromeProvider.prototype = { + getFile: function(prop, persistent) { + throw Cr.NS_ERROR_FAILURE; + }, + + getFiles: function(prop) { + if (prop == NS_CHROME_MANIFESTS_FILE_LIST) { + return new ArrayEnumerator(this._manifests); + } + throw Cr.NS_ERROR_FAILURE; + }, + + QueryInterface: function(iid) { + if (iid.equals(Ci.nsIDirectoryServiceProvider) + || iid.equals(Ci.nsIDirectoryServiceProvider2) + || iid.equals(Ci.nsISupports)) + return this; + + throw Components.results.NS_ERROR_NO_INTERFACE; + } +}; + +function registerManifests(manifests) +{ + var dirSvc = Cc["@mozilla.org/file/directory_service;1"]. + getService(Ci.nsIDirectoryService); + dirSvc.registerProvider(new ChromeProvider(manifests)); +} diff --git a/chrome/test/unit/test_bug292789.js b/chrome/test/unit/test_bug292789.js new file mode 100644 index 00000000000..dff6bfd0f61 --- /dev/null +++ b/chrome/test/unit/test_bug292789.js @@ -0,0 +1,70 @@ +/* ***** 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 + * Dave Townsend . + * + * Portions created by the Initial Developer are Copyright (C) 2007 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * 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 MANIFESTS = [ + do_get_file("chrome/test/unit/data/test_bug292789.manifest") +]; + +registerManifests(MANIFESTS); + +var gIOS; +var gCR; + +function check_accessibility(spec, desired) +{ + var uri = gIOS.newURI(spec, null, null); + var actual = gCR.allowContentToAccess(uri); + do_check_eq(desired, actual); +} + +function run_test() +{ + gIOS = Cc["@mozilla.org/network/io-service;1"]. + getService(Ci.nsIIOService); + gCR = Cc["@mozilla.org/chrome/chrome-registry;1"]. + getService(Ci.nsIXULChromeRegistry); + gCR.checkForNewChrome(); + + check_accessibility("chrome://test1/content/", false); + check_accessibility("chrome://test1/content/foo.js", false); + check_accessibility("chrome://test2/content/", true); + check_accessibility("chrome://test2/content/foo.js", true); + check_accessibility("chrome://test3/content/", false); + check_accessibility("chrome://test3/content/foo.js", false); + check_accessibility("chrome://test4/content/", true); +} diff --git a/chrome/test/unit/test_bug380398.js b/chrome/test/unit/test_bug380398.js index 44b859af153..32b8d14bf47 100644 --- a/chrome/test/unit/test_bug380398.js +++ b/chrome/test/unit/test_bug380398.js @@ -36,69 +36,12 @@ * ***** END LICENSE BLOCK ***** */ -const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; -const XULAPPINFO_CID = Components.ID("{48a4e946-1f9f-4224-b4b0-9a54183cb81e}"); - -const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; - -const Cc = Components.classes; -const Ci = Components.interfaces; var MANIFESTS = [ do_get_file("chrome/test/unit/data/test_bug380398.manifest") ]; -function ArrayEnumerator(array) -{ - this.array = array; -} - -ArrayEnumerator.prototype = { - pos: 0, - - hasMoreElements: function() { - return this.pos < this.array.length; - }, - - getNext: function() { - if (this.pos < this.array.length) - return this.array[this.pos++]; - throw Components.results.NS_ERROR_FAILURE; - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISimpleEnumerator) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; - -var ChromeProvider = { - getFile: function(prop, persistent) { - throw Components.results.NS_ERROR_FAILURE - }, - - getFiles: function(prop) { - if (prop == NS_CHROME_MANIFESTS_FILE_LIST) { - return new ArrayEnumerator(MANIFESTS); - } - throw Components.results.NS_ERROR_FAILURE - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsIDirectoryServiceProvider) - || iid.equals(Ci.nsIDirectoryServiceProvider2) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; -var dirSvc = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIDirectoryService); -dirSvc.registerProvider(ChromeProvider); +registerManifests(MANIFESTS); var XULAppInfo = { vendor: "Mozilla", diff --git a/chrome/test/unit/test_bug397073.js b/chrome/test/unit/test_bug397073.js index 1d52df7f331..2ec326b22e7 100644 --- a/chrome/test/unit/test_bug397073.js +++ b/chrome/test/unit/test_bug397073.js @@ -36,69 +36,12 @@ * ***** END LICENSE BLOCK ***** */ -const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; -const XULAPPINFO_CID = Components.ID("{48a4e946-1f9f-4224-b4b0-9a54183cb81e}"); - -const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; - -const Cc = Components.classes; -const Ci = Components.interfaces; - var MANIFESTS = [ do_get_file("chrome/test/unit/data/test_bug397073.manifest") ]; -function ArrayEnumerator(array) -{ - this.array = array; -} -ArrayEnumerator.prototype = { - pos: 0, - - hasMoreElements: function() { - return this.pos < this.array.length; - }, - - getNext: function() { - if (this.pos < this.array.length) - return this.array[this.pos++]; - throw Components.results.NS_ERROR_FAILURE; - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISimpleEnumerator) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; - -var ChromeProvider = { - getFile: function(prop, persistent) { - throw Components.results.NS_ERROR_FAILURE - }, - - getFiles: function(prop) { - if (prop == NS_CHROME_MANIFESTS_FILE_LIST) { - return new ArrayEnumerator(MANIFESTS); - } - throw Components.results.NS_ERROR_FAILURE - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsIDirectoryServiceProvider) - || iid.equals(Ci.nsIDirectoryServiceProvider2) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; -var dirSvc = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIDirectoryService); -dirSvc.registerProvider(ChromeProvider); +registerManifests(MANIFESTS); var XULAppInfo = { vendor: "Mozilla", diff --git a/chrome/test/unit/test_bug399707.js b/chrome/test/unit/test_bug399707.js index d29b6939f15..ee3d3bb760a 100644 --- a/chrome/test/unit/test_bug399707.js +++ b/chrome/test/unit/test_bug399707.js @@ -36,69 +36,11 @@ * ***** END LICENSE BLOCK ***** */ -const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; -const XULAPPINFO_CID = Components.ID("{48a4e946-1f9f-4224-b4b0-9a54183cb81e}"); - -const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; - -const Cc = Components.classes; -const Ci = Components.interfaces; - var MANIFESTS = [ do_get_file("chrome/test/unit/data/test_bug399707.manifest") ]; -function ArrayEnumerator(array) -{ - this.array = array; -} - -ArrayEnumerator.prototype = { - pos: 0, - - hasMoreElements: function() { - return this.pos < this.array.length; - }, - - getNext: function() { - if (this.pos < this.array.length) - return this.array[this.pos++]; - throw Components.results.NS_ERROR_FAILURE; - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISimpleEnumerator) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; - -var ChromeProvider = { - getFile: function(prop, persistent) { - throw Components.results.NS_ERROR_FAILURE - }, - - getFiles: function(prop) { - if (prop == NS_CHROME_MANIFESTS_FILE_LIST) { - return new ArrayEnumerator(MANIFESTS); - } - throw Components.results.NS_ERROR_FAILURE - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsIDirectoryServiceProvider) - || iid.equals(Ci.nsIDirectoryServiceProvider2) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; -var dirSvc = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIDirectoryService); -dirSvc.registerProvider(ChromeProvider); +registerManifests(MANIFESTS); var XULAppInfo = { vendor: "Mozilla", diff --git a/chrome/test/unit/test_bug401153.js b/chrome/test/unit/test_bug401153.js index dd4a9c308c4..13bed57130a 100644 --- a/chrome/test/unit/test_bug401153.js +++ b/chrome/test/unit/test_bug401153.js @@ -36,69 +36,11 @@ * ***** END LICENSE BLOCK ***** */ -const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; -const XULAPPINFO_CID = Components.ID("{48a4e946-1f9f-4224-b4b0-9a54183cb81e}"); - -const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; - -const Cc = Components.classes; -const Ci = Components.interfaces; - var MANIFESTS = [ do_get_file("chrome/test/unit/data/test_bug401153.manifest") ]; -function ArrayEnumerator(array) -{ - this.array = array; -} - -ArrayEnumerator.prototype = { - pos: 0, - - hasMoreElements: function() { - return this.pos < this.array.length; - }, - - getNext: function() { - if (this.pos < this.array.length) - return this.array[this.pos++]; - throw Components.results.NS_ERROR_FAILURE; - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISimpleEnumerator) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; - -var ChromeProvider = { - getFile: function(prop, persistent) { - throw Components.results.NS_ERROR_FAILURE - }, - - getFiles: function(prop) { - if (prop == NS_CHROME_MANIFESTS_FILE_LIST) { - return new ArrayEnumerator(MANIFESTS); - } - throw Components.results.NS_ERROR_FAILURE - }, - - QueryInterface: function(iid) { - if (iid.equals(Ci.nsIDirectoryServiceProvider) - || iid.equals(Ci.nsIDirectoryServiceProvider2) - || iid.equals(Ci.nsISupports)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; - } -}; -var dirSvc = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIDirectoryService); -dirSvc.registerProvider(ChromeProvider); +registerManifests(MANIFESTS); var XULAppInfo = { vendor: "Mozilla", diff --git a/chrome/test/unit/test_bug415367.js b/chrome/test/unit/test_bug415367.js index 92a554efdd4..063afc7bf59 100644 --- a/chrome/test/unit/test_bug415367.js +++ b/chrome/test/unit/test_bug415367.js @@ -36,9 +36,6 @@ * ***** END LICENSE BLOCK ***** */ -const Cc = Components.classes; -const Ci = Components.interfaces; - var gIOS = Cc["@mozilla.org/network/io-service;1"] .getService(Ci.nsIIOService); diff --git a/content/base/public/nsIChromeRegistry.idl b/content/base/public/nsIChromeRegistry.idl index 7ba2855392b..ae7a5ca4222 100755 --- a/content/base/public/nsIChromeRegistry.idl +++ b/content/base/public/nsIChromeRegistry.idl @@ -75,7 +75,7 @@ interface nsIChromeRegistry : nsISupports void checkForNewChrome(); }; -[scriptable, uuid(3e51f40b-b4b0-4e60-ac45-6c63477ebe41)] +[scriptable, uuid(2860e205-490e-4b06-90b6-87160d35a5a7)] interface nsIXULChromeRegistry : nsIChromeRegistry { /* Should be called when locales change to reload all chrome (including XUL). */ @@ -93,6 +93,13 @@ interface nsIXULChromeRegistry : nsIChromeRegistry * method. */ boolean allowScriptsForPackage(in nsIURI url); + + /** + * Content should only be allowed to load chrome JS from certain packages. + * This method reflects the contentaccessible flag on packages. + * Do not pass non-chrome URIs to this method. + */ + boolean allowContentToAccess(in nsIURI url); }; %{ C++