michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 tw=78 expandtab : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let manifests = [ michael@0: do_get_file("data/test_no_remote_registration.manifest"), michael@0: ]; michael@0: registerManifests(manifests); michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: let XULAppInfo = { michael@0: vendor: "Mozilla", michael@0: name: "XPCShell", michael@0: ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}", michael@0: version: "5", michael@0: appBuildID: "2007010101", michael@0: platformVersion: "1.9", michael@0: platformBuildID: "2007010101", michael@0: inSafeMode: false, michael@0: logConsoleErrors: true, michael@0: OS: "XPCShell", michael@0: XPCOMABI: "noarch-spidermonkey", michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIXULAppInfo, michael@0: Ci.nsIXULRuntime, michael@0: ]) michael@0: }; michael@0: michael@0: let XULAppInfoFactory = { michael@0: // These two are used when we register all our factories (and unregister) michael@0: CID: XULAPPINFO_CID, michael@0: scheme: "XULAppInfo", michael@0: contractID: XULAPPINFO_CONTRACTID, michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return XULAppInfo.QueryInterface(iid); michael@0: } michael@0: }; michael@0: michael@0: function ProtocolHandler(aScheme, aFlags) michael@0: { michael@0: this.scheme = aScheme; michael@0: this.protocolFlags = aFlags; michael@0: this.contractID = "@mozilla.org/network/protocol;1?name=" + aScheme; michael@0: } michael@0: michael@0: ProtocolHandler.prototype = michael@0: { michael@0: defaultPort: -1, michael@0: allowPort: function() false, michael@0: newURI: function(aSpec, aCharset, aBaseURI) michael@0: { michael@0: let uri = Cc["@mozilla.org/network/standard-url;1"]. michael@0: createInstance(Ci.nsIURI); michael@0: uri.spec = aSpec; michael@0: if (!uri.scheme) { michael@0: // We got a partial uri, so let's resolve it with the base one michael@0: uri.spec = aBaseURI.resolve(aSpec); michael@0: } michael@0: return uri; michael@0: }, michael@0: newChannel: function() { throw Cr.NS_ERROR_NOT_IMPLEMENTED }, michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIProtocolHandler michael@0: ]) michael@0: }; michael@0: michael@0: let testProtocols = [ michael@0: // It doesn't matter if it has this flag - the only flag we accept is michael@0: // URI_IS_LOCAL_RESOURCE. michael@0: {scheme: "moz-protocol-ui-resource", michael@0: flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE, michael@0: CID: Components.ID("{d6dedc93-558f-44fe-90f4-3b4bba8a0b14}"), michael@0: shouldRegister: false michael@0: }, michael@0: // It doesn't matter if it has this flag - the only flag we accept is michael@0: // URI_IS_LOCAL_RESOURCE. michael@0: {scheme: "moz-protocol-local-file", michael@0: flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE, michael@0: CID: Components.ID("{ee30d594-0a2d-4f47-89cc-d4cde320ab69}"), michael@0: shouldRegister: false michael@0: }, michael@0: // This clearly is non-local michael@0: {scheme: "moz-protocol-loadable-by-anyone", michael@0: flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, michael@0: CID: Components.ID("{c3735f23-3b0c-4a33-bfa0-79436dcd40b2}"), michael@0: shouldRegister: false michael@0: }, michael@0: // This should always be last (unless we add more flags that are OK) michael@0: {scheme: "moz-protocol-local-resource", michael@0: flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE, michael@0: CID: Components.ID("{b79e977c-f840-469a-b413-0125cc1b62a5}"), michael@0: shouldRegister: true michael@0: }, michael@0: ]; michael@0: function run_test() michael@0: { michael@0: // Create factories michael@0: let factories = []; michael@0: for (let i = 0; i < testProtocols.length; i++) { michael@0: factories[i] = { michael@0: scheme: testProtocols[i].scheme, michael@0: flags: testProtocols[i].flags, michael@0: CID: testProtocols[i].CID, michael@0: contractID: "@mozilla.org/network/protocol;1?name=" + testProtocols[i].scheme, michael@0: createInstance: function(aOuter, aIID) michael@0: { michael@0: if (aOuter != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: let handler = new ProtocolHandler(this.scheme, this.flags, this.CID); michael@0: return handler.QueryInterface(aIID); michael@0: } michael@0: }; michael@0: } michael@0: // Add our XULAppInfo factory michael@0: factories.push(XULAppInfoFactory); michael@0: michael@0: // Register our factories michael@0: for (let i = 0; i < factories.length; i++) { michael@0: let factory = factories[i]; michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .registerFactory(factory.CID, "test-" + factory.scheme, michael@0: factory.contractID, factory); michael@0: } michael@0: michael@0: // Check for new chrome michael@0: let cr = Cc["@mozilla.org/chrome/chrome-registry;1"]. michael@0: getService(Ci.nsIChromeRegistry); michael@0: cr.checkForNewChrome(); michael@0: michael@0: // See if our various things were able to register michael@0: let registrationTypes = [ michael@0: "content", michael@0: "locale", michael@0: "skin", michael@0: "override", michael@0: "resource", michael@0: ]; michael@0: for (let i = 0; i < testProtocols.length; i++) { michael@0: let protocol = testProtocols[i]; michael@0: for (let j = 0; j < registrationTypes.length; j++) { michael@0: let type = registrationTypes[j]; michael@0: dump("Testing protocol '" + protocol.scheme + "' with type '" + type + michael@0: "'\n"); michael@0: let expectedURI = protocol.scheme + "://foo/"; michael@0: let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/"; michael@0: switch (type) { michael@0: case "content": michael@0: expectedURI += protocol.scheme + ".xul"; michael@0: break; michael@0: case "locale": michael@0: expectedURI += protocol.scheme + ".dtd"; michael@0: break; michael@0: case "skin": michael@0: expectedURI += protocol.scheme + ".css"; michael@0: break; michael@0: case "override": michael@0: sourceURI = "chrome://good-package/content/override-" + michael@0: protocol.scheme + ".xul"; michael@0: break; michael@0: case "resource": michael@0: sourceURI = "resource://" + protocol.scheme + "/"; michael@0: break; michael@0: }; michael@0: try { michael@0: let ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: sourceURI = ios.newURI(sourceURI, null, null); michael@0: let uri; michael@0: if (type == "resource") { michael@0: // resources go about a slightly different way than everything else michael@0: let rph = ios.getProtocolHandler("resource"). michael@0: QueryInterface(Ci.nsIResProtocolHandler); michael@0: // this throws for packages that are not registered michael@0: uri = rph.resolveURI(sourceURI); michael@0: } michael@0: else { michael@0: // this throws for packages that are not registered michael@0: uri = cr.convertChromeURL(sourceURI).spec; michael@0: } michael@0: michael@0: if (protocol.shouldRegister) { michael@0: do_check_eq(expectedURI, uri); michael@0: } michael@0: else { michael@0: // Overrides will not throw, so we'll get to here. We want to make michael@0: // sure that the two strings are not the same in this situation. michael@0: do_check_neq(expectedURI, uri); michael@0: } michael@0: } michael@0: catch (e) { michael@0: if (protocol.shouldRegister) { michael@0: dump(e + "\n"); michael@0: do_throw("Should have registered our URI for protocol " + michael@0: protocol.scheme); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Unregister our factories so we do not leak michael@0: for (let i = 0; i < factories.length; i++) { michael@0: let factory = factories[i]; michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .unregisterFactory(factory.CID, factory); michael@0: } michael@0: }