зеркало из https://github.com/mozilla/pjs.git
Bug 693577 - port mockprovider.js to specialpowers. r=ted
This commit is contained in:
Родитель
6fd75c54df
Коммит
88bf07f48f
|
@ -676,5 +676,34 @@ SpecialPowersAPI.prototype = {
|
||||||
withCaret ? ctx.DRAWWINDOW_DRAW_CARET : 0);
|
withCaret ? ctx.DRAWWINDOW_DRAW_CARET : 0);
|
||||||
return el;
|
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.
|
* to ensure that unregister() is called.
|
||||||
*/
|
*/
|
||||||
register: function MOR_register() {
|
register: function MOR_register() {
|
||||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
||||||
if (this._originalFactory)
|
if (this._originalFactory)
|
||||||
throw new Exception("Invalid object state when calling register()");
|
throw new Exception("Invalid object state when calling register()");
|
||||||
|
|
||||||
|
@ -79,41 +78,24 @@ MockObjectRegisterer.prototype = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
|
var retVal = SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
|
||||||
|
if ('error' in retVal) {
|
||||||
// Get the component CID.
|
throw new Exception("ERROR: " + retVal.error);
|
||||||
this._cid = componentRegistrar.contractIDToCID(this._contractID);
|
} else {
|
||||||
|
this._cid = retVal.cid;
|
||||||
// ... and make sure we correctly replace the original factory with the mock one.
|
this._originalFactory = retVal.originalFactory;
|
||||||
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);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restores the original factory.
|
* Restores the original factory.
|
||||||
*/
|
*/
|
||||||
unregister: function MOR_unregister() {
|
unregister: function MOR_unregister() {
|
||||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
||||||
if (!this._originalFactory)
|
if (!this._originalFactory)
|
||||||
throw new Exception("Invalid object state when calling unregister()");
|
throw new Exception("Invalid object state when calling unregister()");
|
||||||
|
|
||||||
// Free references to the mock factory.
|
// Free references to the mock factory.
|
||||||
var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
|
SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
|
||||||
componentRegistrar.unregisterFactory(this._cid,
|
|
||||||
this._mockFactory);
|
|
||||||
|
|
||||||
// Restore the original factory.
|
|
||||||
componentRegistrar.registerFactory(this._cid,
|
|
||||||
"",
|
|
||||||
this._contractID,
|
|
||||||
this._originalFactory);
|
|
||||||
|
|
||||||
// Allow registering a mock factory again later.
|
// Allow registering a mock factory again later.
|
||||||
this._cid = null;
|
this._cid = null;
|
||||||
|
|
Загрузка…
Ссылка в новой задаче