Bug 553070 - Allow XPCOMUtils to do category registration for specific apps. r=sayrer

This commit is contained in:
Marco Bonardo 2010-05-22 10:41:00 +02:00
Родитель 569d29edc3
Коммит 026c01b479
4 изменённых файлов: 236 добавлений и 50 удалений

Просмотреть файл

@ -1246,7 +1246,8 @@ BrowserGlue.prototype = {
// get this contractID registered for certain categories via XPCOMUtils
_xpcom_categories: [
// make BrowserGlue a startup observer
{ category: "app-startup", service: true }
{ category: "app-startup", service: true,
apps: [ /* Firefox */ "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}" ] }
]
}

Просмотреть файл

@ -77,7 +77,12 @@
* // optional, defaults to false. When set to true, and only if 'value'
* // is not specified, the concatenation of the string "service," and the
* // object's contractID is passed as aValue parameter of addCategoryEntry.
* service: true
* service: true,
* // optional, it can be an array of applications' IDs in the form:
* // [ "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", ... ]
* // If defined the component will be registered in this category only for
* // the provided applications.
* apps: [...]
* }],
*
* // QueryInterface implementation, e.g. using the generateQI helper
@ -146,7 +151,8 @@ var XPCOMUtils = {
* signature 'preUnregister(nsIComponentManager,
* nsIFile, componentsArray)'
*/
generateModule: function XPCU_generateModule(componentsArray, postRegister,
generateModule: function XPCU_generateModule(componentsArray,
postRegister,
preUnregister) {
let classes = [];
for each (let component in componentsArray) {
@ -159,6 +165,38 @@ var XPCOMUtils = {
});
}
function categoryRegistration(action, compMgr, fileSpec,
registrationFunc, hookFunc) {
debug("*** " + action + "ing " + fileSpec.leafName + ": [ ");
var componentCount = 0;
compMgr.QueryInterface(Ci.nsIComponentRegistrar);
if (action == "unregister" && preUnregister)
preUnregister(compMgr, fileSpec, componentsArray);
for each (let classDesc in classes) {
debug((componentCount++ ? ", " : "") + classDesc.className);
if (action == "register" && hookFunc)
hookFunc(classDesc);
if (classDesc.categories) {
for each (let cat in classDesc.categories) {
if ("apps" in cat && -1 == cat.apps.indexOf(XPCOMUtils._appID))
continue;
registrationFunc(cat, classDesc);
}
}
if (action == "unregister" && hookFunc)
hookFunc(classDesc);
}
if (action == "register" && postRegister)
postRegister(compMgr, fileSpec, componentsArray);
debug(" ]\n");
}
return { // nsIModule impl.
getClassObject: function(compMgr, cid, iid) {
// We only support nsIFactory queries, not nsIClassInfo
@ -174,56 +212,37 @@ var XPCOMUtils = {
},
registerSelf: function(compMgr, fileSpec, location, type) {
var componentCount = 0;
debug("*** registering " + fileSpec.leafName + ": [ ");
compMgr.QueryInterface(Ci.nsIComponentRegistrar);
for each (let classDesc in classes) {
debug((componentCount++ ? ", " : "") + classDesc.className);
compMgr.registerFactoryLocation(classDesc.cid,
classDesc.className,
classDesc.contractID,
fileSpec,
location,
type);
if (classDesc.categories) {
categoryRegistration("register", compMgr, fileSpec,
function(cat, classDesc) {
let defaultValue = (cat.service ? "service," : "") +
classDesc.contractID;
let catMan = XPCOMUtils.categoryManager;
for each (let cat in classDesc.categories) {
let defaultValue = (cat.service ? "service," : "") +
classDesc.contractID;
catMan.addCategoryEntry(cat.category,
cat.entry || classDesc.className,
cat.value || defaultValue,
true, true);
}
}
}
if (postRegister)
postRegister(compMgr, fileSpec, componentsArray);
debug(" ]\n");
catMan.addCategoryEntry(cat.category,
cat.entry || classDesc.className,
cat.value || defaultValue,
true, true);
},
function(classDesc) {
compMgr.registerFactoryLocation(classDesc.cid,
classDesc.className,
classDesc.contractID,
fileSpec,
location,
type);
});
},
unregisterSelf: function(compMgr, fileSpec, location) {
var componentCount = 0;
debug("*** unregistering " + fileSpec.leafName + ": [ ");
compMgr.QueryInterface(Ci.nsIComponentRegistrar);
if (preUnregister)
preUnregister(compMgr, fileSpec, componentsArray);
for each (let classDesc in classes) {
debug((componentCount++ ? ", " : "") + classDesc.className);
if (classDesc.categories) {
categoryRegistration("unregister", compMgr, fileSpec,
function(cat, classDesc) {
let catMan = XPCOMUtils.categoryManager;
for each (let cat in classDesc.categories) {
catMan.deleteCategoryEntry(cat.category,
cat.entry || classDesc.className,
true);
}
}
compMgr.unregisterFactoryLocation(classDesc.cid, fileSpec);
}
debug(" ]\n");
catMan.deleteCategoryEntry(cat.category,
cat.entry || classDesc.className,
true);
},
function (classDesc) {
compMgr.unregisterFactoryLocation(classDesc.cid, fileSpec);
});
},
canUnload: function(compMgr) {
@ -232,6 +251,13 @@ var XPCOMUtils = {
};
},
get _appID() {
delete this._appID;
let appInfo = Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULAppInfo);
return this._appID = appInfo.ID;
},
/**
* Defines a getter on a specified object that will be created upon first use.
*

Просмотреть файл

@ -0,0 +1,97 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: sw=4 ts=4 sts=4 et
* ***** 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 Necko Test Code.
*
* The Initial Developer of the Original Code is the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Marco Bonardo <mak77@bonardo.net> (Original Author)
*
* 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 ***** */
// Test components used in test_xpcomutils.js
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const CATEGORY_NAME = "test-cat";
// This component will register under the "test-cat" category.
function CatRegisteredComponent() {}
CatRegisteredComponent.prototype = {
classDescription: "CatRegisteredComponent",
classID: Components.ID("{163cd427-1f08-4416-a291-83ea71127b0e}"),
contractID: "@unit.test.com/cat-registered-component;1",
QueryInterface: XPCOMUtils.generateQI([]),
_xpcom_categories: [
{ category: CATEGORY_NAME }
]
};
// This component will register under the "test-cat" category.
function CatAppRegisteredComponent() {}
CatAppRegisteredComponent.prototype = {
classDescription: "CatAppRegisteredComponent",
classID: Components.ID("{b686dc84-f42e-4c94-94fe-89d0ac899578}"),
contractID: "@unit.test.com/cat-app-registered-component;1",
QueryInterface: XPCOMUtils.generateQI([]),
_xpcom_categories: [
{ category: CATEGORY_NAME,
apps: [ /* Our app */ "{adb42a9a-0d19-4849-bf4d-627614ca19be}" ]
}
]
};
// This component will not register under the "test-cat" category because
// it does not support our app.
function CatUnregisteredComponent() {}
CatUnregisteredComponent.prototype = {
classDescription: "CatUnregisteredComponent",
classID: Components.ID("{c31a552b-0228-4a1a-8cdf-d8aab7d4eff8}"),
contractID: "@unit.test.com/cat-unregistered-component;1",
QueryInterface: XPCOMUtils.generateQI([]),
_xpcom_categories: [
{ category: CATEGORY_NAME,
apps: [ /* Another app */ "{e84fce36-6ef6-435c-bf63-979a8811dcd4}" ]
}
]
};
let components = [
CatRegisteredComponent,
CatAppRegisteredComponent,
CatUnregisteredComponent,
];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}

Просмотреть файл

@ -15,8 +15,7 @@
*
* The Original Code is Necko Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* The Initial Developer of the Original Code is the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
@ -47,6 +46,7 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Cc = Components.classes;
const Ci = Components.interfaces;
////////////////////////////////////////////////////////////////////////////////
//// Tests
@ -75,6 +75,7 @@ function test_generateQI_string_names()
} catch(e) {}
}
function test_defineLazyGetter()
{
let accessCount = 0;
@ -100,6 +101,7 @@ function test_defineLazyGetter()
do_check_eq(accessCount, 1);
}
function test_defineLazyServiceGetter()
{
let obj = { };
@ -117,6 +119,66 @@ function test_defineLazyServiceGetter()
do_check_true(prop in obj.service);
}
function test_categoryRegistration()
{
const CATEGORY_NAME = "test-cat";
// Create a fake app entry for our category registration apps filter.
let XULAppInfo = {
vendor: "Mozilla",
name: "catRegTest",
ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}",
version: "1",
appBuildID: "2007010101",
platformVersion: "",
platformBuildID: "2007010101",
inSafeMode: false,
logConsoleErrors: true,
OS: "XPCShell",
XPCOMABI: "noarch-spidermonkey",
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIXULAppInfo,
Ci.nsIXULRuntime,
])
};
let XULAppInfoFactory = {
createInstance: function (outer, iid) {
if (outer != null)
throw Cr.NS_ERROR_NO_AGGREGATION;
return XULAppInfo.QueryInterface(iid);
}
};
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(
Components.ID("{6372ef9b-0827-4d18-954f-c0974f1a1573}"),
"XULAppInfo",
"@mozilla.org/xre/app-info;1",
XULAppInfoFactory
);
// Load test components.
do_load_module("CatRegistrationComponents.js");
const EXPECTED_ENTRIES = ["CatAppRegisteredComponent",
"CatRegisteredComponent"];
// Check who is registered in "test-cat" category.
let foundEntriesCount = 0;
let catMan = Cc["@mozilla.org/categorymanager;1"].
getService(Ci.nsICategoryManager);
let entries = catMan.enumerateCategory(CATEGORY_NAME);
while (entries.hasMoreElements()) {
foundEntriesCount++;
let entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
print("Check the found category entry (" + entry + ")is expected.");
do_check_true(EXPECTED_ENTRIES.indexOf(entry) != -1);
}
print("Check there are no more or less than expected entries.");
do_check_eq(foundEntriesCount, EXPECTED_ENTRIES.length);
}
////////////////////////////////////////////////////////////////////////////////
//// Test Runner