зеркало из https://github.com/mozilla/gecko-dev.git
b=387108. r=gavin. cleanup Extensions and Extension objects
This commit is contained in:
Родитель
6c31e9dfec
Коммит
ffd724f958
|
@ -411,11 +411,18 @@ Extension.prototype = {
|
|||
// for nsIObserver
|
||||
observe: function ext_observe(aSubject, aTopic, aData)
|
||||
{
|
||||
if ((aData == "item-uninstalled") &&
|
||||
(aSubject instanceof Ci.nsIUpdateItem) &&
|
||||
(aSubject.id == this._item.id))
|
||||
if ((aSubject instanceof Ci.nsIUpdateItem) && (aSubject.id == this._item.id))
|
||||
{
|
||||
this._events.dispatch("uninstall", this._item.id);
|
||||
if (aData == "item-uninstalled")
|
||||
this._events.dispatch("uninstall", this._item.id);
|
||||
else if (aData == "item-disabled")
|
||||
this._events.dispatch("disable", this._item.id);
|
||||
else if (aData == "item-enabled")
|
||||
this._events.dispatch("enable", this._item.id);
|
||||
else if (aData == "item-cancel-action")
|
||||
this._events.dispatch("cancel", this._item.id);
|
||||
else if (aData == "item-upgraded")
|
||||
this._events.dispatch("upgrade", this._item.id);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -454,7 +461,9 @@ Extension.prototype = {
|
|||
function Extensions() {
|
||||
this._extmgr = Components.classes["@mozilla.org/extensions/manager;1"]
|
||||
.getService(Ci.nsIExtensionManager);
|
||||
|
||||
|
||||
this._cache = {};
|
||||
|
||||
var self = this;
|
||||
gShutdown.push(function() { self._shutdown(); });
|
||||
}
|
||||
|
@ -462,8 +471,21 @@ function Extensions() {
|
|||
//=================================================
|
||||
// Extensions implementation
|
||||
Extensions.prototype = {
|
||||
_shutdown : function() {
|
||||
_shutdown : function exts_shutdown() {
|
||||
this._extmgr = null;
|
||||
this._cache = null;
|
||||
},
|
||||
|
||||
/*
|
||||
* Helper method to check cache before creating a new extension
|
||||
*/
|
||||
_get : function exts_get(aId) {
|
||||
if (this._cache.hasOwnProperty(aId))
|
||||
return this._cache[aId];
|
||||
|
||||
var newExt = new Extension(this._extmgr.getItemForID(aId));
|
||||
this._cache[aId] = newExt;
|
||||
return newExt;
|
||||
},
|
||||
|
||||
get all() {
|
||||
|
@ -481,7 +503,7 @@ Extensions.prototype = {
|
|||
var items = this._extmgr.getItemList(Ci.nsIUpdateItem.TYPE_EXTENSION, {});
|
||||
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
retVal.push(new Extension(items[i]));
|
||||
retVal.push(this._get(items[i].id));
|
||||
}
|
||||
|
||||
return retVal;
|
||||
|
@ -495,7 +517,7 @@ Extensions.prototype = {
|
|||
},
|
||||
|
||||
get : function exts_get(aId) {
|
||||
return this.has(aId) ? new Extension(this._extmgr.getItemForID(aId)) : null;
|
||||
return this.has(aId) ? this._get(aId) : null;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Testing Application.js</title>
|
||||
<title>Testing Extensions</title>
|
||||
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
|
||||
|
@ -17,6 +17,7 @@ var test = {
|
|||
inspectorid: "inspector@mozilla.org",
|
||||
inspectorname: "DOM Inspector"
|
||||
};
|
||||
var gLastEvent = "";
|
||||
|
||||
test_Extensions();
|
||||
|
||||
|
@ -44,7 +45,44 @@ function test_Extensions() {
|
|||
|
||||
// Reset the install event preference, so that we can test it again later
|
||||
inspector.prefs.get("install-event-fired").reset();
|
||||
|
||||
// Make sure the we are given the same extension (cached) so things like .storage work right
|
||||
inspector.storage.set("test", "simple check");
|
||||
ok(inspector.storage.has("test"), "Checking that extension storage worked");
|
||||
|
||||
var inspector2 = Application.extensions.get(test.inspectorid);
|
||||
is(inspector2.id, test.inspectorid, "Check 'Extension.id' for known extension - from cache");
|
||||
ok(inspector.storage.has("test"), "Checking that extension storage worked - from cache");
|
||||
is(inspector2.storage.get("test", "cache"), inspector.storage.get("test", "original"), "Checking that the storage of same extension is correct - from cache");
|
||||
|
||||
inspector.events.addListener("disable", onGenericEvent);
|
||||
inspector.events.addListener("enable", onGenericEvent);
|
||||
inspector.events.addListener("uninstall", onGenericEvent);
|
||||
inspector.events.addListener("cancel", onGenericEvent);
|
||||
|
||||
var extmgr = Components.classes["@mozilla.org/extensions/manager;1"]
|
||||
.getService(Components.interfaces.nsIExtensionManager);
|
||||
|
||||
extmgr.disableItem(test.inspectorid);
|
||||
is(gLastEvent, "disable", "Checking that disable event is fired");
|
||||
|
||||
// enabling after a disable will only fire a 'cancel' event
|
||||
// see - http://mxr.mozilla.org/seamonkey/source/toolkit/mozapps/extensions/src/nsExtensionManager.js.in#5216
|
||||
extmgr.enableItem(test.inspectorid);
|
||||
is(gLastEvent, "cancel", "Checking that enable (cancel) event is fired");
|
||||
|
||||
extmgr.uninstallItem(test.inspectorid);
|
||||
is(gLastEvent, "uninstall", "Checking that uninstall event is fired");
|
||||
|
||||
extmgr.cancelUninstallItem(test.inspectorid);
|
||||
is(gLastEvent, "cancel", "Checking that cancel event is fired");
|
||||
}
|
||||
|
||||
function onGenericEvent(event) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
gLastEvent = event.type;
|
||||
}
|
||||
|
||||
</script>
|
||||
</pre>
|
||||
</body>
|
||||
|
|
|
@ -21,6 +21,7 @@ var test = {
|
|||
inspectorid: "inspector@mozilla.org",
|
||||
inspectorname: "DOM Inspector"
|
||||
};
|
||||
var gLastEvent = "";
|
||||
|
||||
test_Extensions();
|
||||
|
||||
|
@ -46,6 +47,42 @@ function test_Extensions() {
|
|||
|
||||
// Reset the install event preference, so that we can test it again later
|
||||
inspector.prefs.get("install-event-fired").reset();
|
||||
|
||||
// Make sure the we are given the same extension (cached) so things like .storage work right
|
||||
inspector.storage.set("test", "simple check");
|
||||
ok(inspector.storage.has("test"), "Checking that extension storage worked");
|
||||
|
||||
var inspector2 = Application.extensions.get(test.inspectorid);
|
||||
is(inspector2.id, test.inspectorid, "Check 'Extension.id' for known extension - from cache");
|
||||
ok(inspector.storage.has("test"), "Checking that extension storage worked - from cache");
|
||||
is(inspector2.storage.get("test", "cache"), inspector.storage.get("test", "original"), "Checking that the storage of same extension is correct - from cache");
|
||||
|
||||
inspector.events.addListener("disable", onGenericEvent);
|
||||
inspector.events.addListener("enable", onGenericEvent);
|
||||
inspector.events.addListener("uninstall", onGenericEvent);
|
||||
inspector.events.addListener("cancel", onGenericEvent);
|
||||
|
||||
var extmgr = Components.classes["@mozilla.org/extensions/manager;1"]
|
||||
.getService(Components.interfaces.nsIExtensionManager);
|
||||
|
||||
extmgr.disableItem(test.inspectorid);
|
||||
is(gLastEvent, "disable", "Checking that disable event is fired");
|
||||
|
||||
// enabling after a disable will only fire a 'cancel' event
|
||||
// see - http://mxr.mozilla.org/seamonkey/source/toolkit/mozapps/extensions/src/nsExtensionManager.js.in#5216
|
||||
extmgr.enableItem(test.inspectorid);
|
||||
is(gLastEvent, "cancel", "Checking that enable (cancel) event is fired");
|
||||
|
||||
extmgr.uninstallItem(test.inspectorid);
|
||||
is(gLastEvent, "uninstall", "Checking that uninstall event is fired");
|
||||
|
||||
extmgr.cancelUninstallItem(test.inspectorid);
|
||||
is(gLastEvent, "cancel", "Checking that cancel event is fired");
|
||||
}
|
||||
|
||||
function onGenericEvent(event) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
gLastEvent = event.type;
|
||||
}
|
||||
|
||||
]]>
|
||||
|
|
Загрузка…
Ссылка в новой задаче