Bug 693577 - port mockprovider.js to specialpowers. r=ted

This commit is contained in:
Joel Maher 2011-10-14 07:52:02 -04:00
Родитель 6fd75c54df
Коммит 88bf07f48f
2 изменённых файлов: 37 добавлений и 26 удалений

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

@ -676,5 +676,34 @@ SpecialPowersAPI.prototype = {
withCaret ? ctx.DRAWWINDOW_DRAW_CARET : 0);
return el;
},
swapFactoryRegistration: function(cid, contractID, newFactory, oldFactory) {
var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
var unregisterFactory = newFactory;
var registerFactory = oldFactory;
if (cid == null) {
if (contractID != null) {
cid = componentRegistrar.contractIDToCID(contractID);
oldFactory = Components.manager.getClassObject(Components.classes[contractID],
Components.interfaces.nsIFactory);
} else {
return {'error': "trying to register a new contract ID: Missing contractID"};
}
unregisterFactory = oldFactory;
registerFactory = newFactory;
}
componentRegistrar.unregisterFactory(cid,
unregisterFactory);
// Restore the original factory.
componentRegistrar.registerFactory(cid,
"",
contractID,
registerFactory);
return {'cid':cid, 'originalFactory':oldFactory};
},
};

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

@ -65,7 +65,6 @@ MockObjectRegisterer.prototype = {
* to ensure that unregister() is called.
*/
register: function MOR_register() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
if (this._originalFactory)
throw new Exception("Invalid object state when calling register()");
@ -79,41 +78,24 @@ MockObjectRegisterer.prototype = {
}
};
var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
// Get the component CID.
this._cid = componentRegistrar.contractIDToCID(this._contractID);
// ... and make sure we correctly replace the original factory with the mock one.
this._originalFactory = Components.manager.getClassObject(Components.classes[this._contractID],
Components.interfaces.nsIFactory);
componentRegistrar.unregisterFactory(this._cid, this._originalFactory);
componentRegistrar.registerFactory(this._cid,
"",
this._contractID,
this._mockFactory);
var retVal = SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
if ('error' in retVal) {
throw new Exception("ERROR: " + retVal.error);
} else {
this._cid = retVal.cid;
this._originalFactory = retVal.originalFactory;
}
},
/**
* Restores the original factory.
*/
unregister: function MOR_unregister() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
if (!this._originalFactory)
throw new Exception("Invalid object state when calling unregister()");
// Free references to the mock factory.
var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
componentRegistrar.unregisterFactory(this._cid,
this._mockFactory);
// Restore the original factory.
componentRegistrar.registerFactory(this._cid,
"",
this._contractID,
this._originalFactory);
SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
// Allow registering a mock factory again later.
this._cid = null;