Bug 986639: Use the __URI__ property of component and module globals to detect which add-on they come from. r=fitzgen

This commit is contained in:
Dave Townsend 2014-03-26 16:04:20 -07:00
parent fec0c9e2da
commit d633915be4
8 changed files with 191 additions and 3 deletions

View File

@ -0,0 +1,23 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { interfaces: Ci, classes: Cc } = Components;
function startup(aParams, aReason) {
Components.utils.import("resource://gre/modules/Services.jsm");
let res = Services.io.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
res.setSubstitution("browser_dbg_addon4", aParams.resourceURI);
// Load a JS module
Components.utils.import("resource://browser_dbg_addon4/test.jsm");
}
function shutdown(aParams, aReason) {
// Unload the JS module
Components.utils.unload("resource://browser_dbg_addon4/test.jsm");
let res = Services.io.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
res.setSubstitution("browser_dbg_addon4", null);
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>browser_dbg_addon4@tests.mozilla.org</em:id>
<em:version>1.0</em:version>
<em:name>Test add-on with JS Modules</em:name>
<em:bootstrap>true</em:bootstrap>
<em:targetApplication>
<Description>
<em:id>toolkit@mozilla.org</em:id>
<em:minVersion>0</em:minVersion>
<em:maxVersion>*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
</RDF>

View File

@ -0,0 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const EXPORTED_SYMBOLS = ["Foo"];
const Foo = {};

View File

@ -0,0 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const EXPORTED_SYMBOLS = ["Bar"];
const Bar = {};

Binary file not shown.

View File

@ -3,6 +3,7 @@ support-files =
addon1.xpi
addon2.xpi
addon3.xpi
addon4.xpi
code_binary_search.coffee
code_binary_search.js
code_binary_search.map
@ -83,6 +84,7 @@ support-files =
[browser_dbg_aaa_run_first_leaktest.js]
[browser_dbg_addonactor.js]
[browser_dbg_addon-sources.js]
[browser_dbg_addon-modules.js]
[browser_dbg_auto-pretty-print-01.js]
[browser_dbg_auto-pretty-print-02.js]
[browser_dbg_bfcache.js]

View File

@ -0,0 +1,114 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Make sure the add-on actor can see loaded JS Modules from an add-on
const ADDON4_URL = EXAMPLE_URL + "addon4.xpi";
let gAddon, gClient, gThreadClient, gDebugger, gSources;
function test() {
Task.spawn(function () {
if (!DebuggerServer.initialized) {
DebuggerServer.init(() => true);
DebuggerServer.addBrowserActors();
}
gBrowser.selectedTab = gBrowser.addTab();
let iframe = document.createElement("iframe");
document.documentElement.appendChild(iframe);
let transport = DebuggerServer.connectPipe();
gClient = new DebuggerClient(transport);
let connected = promise.defer();
gClient.connect(connected.resolve);
yield connected.promise;
yield installAddon();
let debuggerPanel = yield initAddonDebugger(gClient, ADDON4_URL, iframe);
gDebugger = debuggerPanel.panelWin;
gThreadClient = gDebugger.gThreadClient;
gSources = gDebugger.DebuggerView.Sources;
yield testSources(false);
Cu.import("resource://browser_dbg_addon4/test2.jsm", {});
yield testSources(true);
Cu.unload("resource://browser_dbg_addon4/test2.jsm");
yield uninstallAddon();
yield closeConnection();
yield debuggerPanel._toolbox.destroy();
iframe.remove();
finish();
});
}
function installAddon () {
return addAddon(ADDON4_URL).then(aAddon => {
gAddon = aAddon;
});
}
function testSources(expectSecondModule) {
let deferred = promise.defer();
let foundAddonModule = false;
let foundAddonModule2 = false;
let foundAddonBootstrap = false;
gThreadClient.getSources(({sources}) => {
ok(sources.length, "retrieved sources");
sources.forEach(source => {
let url = source.url.split(" -> ").pop();
let { label, group } = gSources.getItemByValue(source.url).attachment;
if (url.indexOf("resource://browser_dbg_addon4/test.jsm") === 0) {
is(label, "test.jsm", "correct label for addon code");
is(group, "resource://browser_dbg_addon4", "addon module is in its own group");
foundAddonModule = true;
} else if (url.indexOf("resource://browser_dbg_addon4/test2.jsm") === 0) {
is(label, "test2.jsm", "correct label for addon code");
is(group, "resource://browser_dbg_addon4", "addon module is in its own group");
foundAddonModule2 = true;
} else if (url.endsWith("/browser_dbg_addon4@tests.mozilla.org.xpi!/bootstrap.js")) {
is(label, "bootstrap.js", "correct label for bootstrap code");
is(group, "jar:", "addon bootstrap script is in its own group");
foundAddonBootstrap = true;
} else {
ok(false, "Saw an unexpected source: " + url);
}
});
ok(foundAddonModule, "found JS module for the addon in the list");
is(foundAddonModule2, expectSecondModule, "saw the second addon module");
ok(foundAddonBootstrap, "found bootstrap script for the addon in the list");
deferred.resolve();
});
return deferred.promise;
}
function uninstallAddon() {
return removeAddon(gAddon);
}
function closeConnection () {
let deferred = promise.defer();
gClient.close(deferred.resolve);
return deferred.promise;
}
registerCleanupFunction(function() {
gClient = null;
gAddon = null;
gThreadClient = null;
gDebugger = null;
gSources = null;
while (gBrowser.tabs.length > 1)
gBrowser.removeCurrentTab();
});

View File

@ -4638,6 +4638,8 @@ update(ChromeDebuggerActor.prototype, {
function AddonThreadActor(aConnect, aHooks, aAddonID) {
this.addonID = aAddonID;
this.addonManager = Cc["@mozilla.org/addons/integration;1"].
getService(Ci.amIAddonManager);
ThreadActor.call(this, aHooks);
}
@ -4708,14 +4710,30 @@ update(AddonThreadActor.prototype, {
* @param aGlobal Debugger.Object
*/
_checkGlobal: function ADA_checkGlobal(aGlobal) {
let metadata;
try {
// This will fail for non-Sandbox objects, hence the try-catch block.
metadata = Cu.getSandboxMetadata(aGlobal.unsafeDereference());
let metadata = Cu.getSandboxMetadata(aGlobal.unsafeDereference());
if (metadata)
return metadata.addonID === this.addonID;
} catch (e) {
}
return metadata && metadata.addonID === this.addonID;
// Check the global for a __URI__ property and then try to map that to an
// add-on
let uridescriptor = aGlobal.getOwnPropertyDescriptor("__URI__");
if (uridescriptor && "value" in uridescriptor) {
try {
let uri = Services.io.newURI(uridescriptor.value, null, null);
let id = {};
if (this.addonManager.mapURIToAddonID(uri, id))
return id.value === this.addonID;
}
catch (e) {
console.log("Unexpected URI " + uridescriptor.value);
}
}
return false;
}
});