Skip to content

Commit

Permalink
Bug 1454456 - make getAddonBlocklistState API asynchronous, r=mossop
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 1HniSNjL3M0
  • Loading branch information
gijsk committed Apr 16, 2018
1 parent 7185557 commit df8eb8d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 54 deletions.
4 changes: 3 additions & 1 deletion browser/base/content/test/plugins/blocklist_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const kBlocklistServiceContractID = "@mozilla.org/extensions/blocklist;1";
const kBlocklistServiceFactory = Cm.getClassObject(Cc[kBlocklistServiceContractID], Ci.nsIFactory);

ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Timer.jsm");

/*
* A lightweight blocklist proxy for the testing purposes.
Expand Down Expand Up @@ -44,7 +45,8 @@ var BlocklistProxy = {
observe(aSubject, aTopic, aData) {
},

getAddonBlocklistState(aAddon, aAppVersion, aToolkitVersion) {
async getAddonBlocklistState(aAddon, aAppVersion, aToolkitVersion) {
await new Promise(r => setTimeout(r, 0));
return 0; // STATE_NOT_BLOCKED
},

Expand Down
7 changes: 4 additions & 3 deletions toolkit/mozapps/extensions/internal/AddonTestUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ChromeUtils.import("resource://gre/modules/AsyncShutdown.jsm");
ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/Timer.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");

const {EventEmitter} = ChromeUtils.import("resource://gre/modules/EventEmitter.jsm", {});
Expand Down Expand Up @@ -136,13 +137,13 @@ class MockBlocklist {
this._reLazifyService();
}

getAddonBlocklistState(addon, appVersion, toolkitVersion) {
async getAddonBlocklistState(addon, appVersion, toolkitVersion) {
await new Promise(r => setTimeout(r, 0));
return this.addons.get(addon.id) || Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
}

async getAddonBlocklistEntry(addon, appVersion, toolkitVersion) {
await Promise.resolve();
let state = this.getAddonBlocklistState(addon, appVersion, toolkitVersion);
let state = await this.getAddonBlocklistState(addon, appVersion, toolkitVersion);
if (state != Ci.nsIBlocklistService.STATE_NOT_BLOCKED) {
return {
state,
Expand Down
10 changes: 5 additions & 5 deletions toolkit/mozapps/extensions/internal/AddonUpdateChecker.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ var AddonUpdateChecker = {
},

/**
* Returns the newest available update from a list of update objects.
* Asynchronously returns the newest available update from a list of update objects.
*
* @param aUpdates
* An array of update objects
Expand All @@ -496,9 +496,9 @@ var AddonUpdateChecker = {
* Array of AddonCompatibilityOverride to take into account. Optional.
* @return an update object if one matches or null if not
*/
getNewestCompatibleUpdate(aUpdates, aAppVersion, aPlatformVersion,
aIgnoreMaxVersion, aIgnoreStrictCompat,
aCompatOverrides) {
async getNewestCompatibleUpdate(aUpdates, aAppVersion, aPlatformVersion,
aIgnoreMaxVersion, aIgnoreStrictCompat,
aCompatOverrides) {
if (!aAppVersion)
aAppVersion = Services.appinfo.version;
if (!aPlatformVersion)
Expand All @@ -508,7 +508,7 @@ var AddonUpdateChecker = {
for (let update of aUpdates) {
if (!update.updateURL)
continue;
let state = Services.blocklist.getAddonBlocklistState(update, aAppVersion, aPlatformVersion);
let state = await Services.blocklist.getAddonBlocklistState(update, aAppVersion, aPlatformVersion);
if (state != Ci.nsIBlocklistService.STATE_NOT_BLOCKED)
continue;
if ((newest == null || (Services.vc.compare(newest.version, update.version) < 0)) &&
Expand Down
9 changes: 3 additions & 6 deletions toolkit/mozapps/extensions/internal/XPIInstall.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -2595,12 +2595,9 @@ UpdateChecker.prototype = {
null :
await AddonRepository.getCompatibilityOverrides(this.addon.id);

let update = AUC.getNewestCompatibleUpdate(aUpdates,
this.appVersion,
this.platformVersion,
ignoreMaxVersion,
ignoreStrictCompat,
compatOverrides);
let update = await AUC.getNewestCompatibleUpdate(
aUpdates, this.appVersion, this.platformVersion,
ignoreMaxVersion, ignoreStrictCompat, compatOverrides);

if (update && Services.vc.compare(this.addon.version, update.version) < 0
&& !this.addon._installLocation.locked) {
Expand Down
5 changes: 2 additions & 3 deletions toolkit/mozapps/extensions/nsBlocklistService.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,8 @@ Blocklist.prototype = {
* is used.
* @returns {integer} The STATE constant.
*/
getAddonBlocklistState(addon, appVersion, toolkitVersion) {
if (!this.isLoaded)
this._loadBlocklist();
async getAddonBlocklistState(addon, appVersion, toolkitVersion) {
await this.loadBlocklistAsync();
return this._getAddonBlocklistState(addon, this._addonEntries,
appVersion, toolkitVersion);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,17 @@ add_task(async function test_1() {
await loadBlocklist("test_bug393285.xml");

let addons = await getAddons(ADDON_IDS);
function isBlocklisted(addon, appVer, toolkitVer) {
return Services.blocklist.getAddonBlocklistState(addon, appVer, toolkitVer) != Services.blocklist.STATE_NOT_BLOCKED;
async function isBlocklisted(addon, appVer, toolkitVer) {
let state = await Services.blocklist.getAddonBlocklistState(addon, appVer, toolkitVer);
return state != Services.blocklist.STATE_NOT_BLOCKED;
}
for (let [id, options] of Object.entries(ADDONS)) {
for (let blocklisted of options.blocklisted || []) {
ok(isBlocklisted(addons.get(id), ...blocklisted),
ok(await isBlocklisted(addons.get(id), ...blocklisted),
`Add-on ${id} should be blocklisted in app/platform version ${blocklisted}`);
}
for (let notBlocklisted of options.notBlocklisted || []) {
ok(!isBlocklisted(addons.get(id), ...notBlocklisted),
ok(!(await isBlocklisted(addons.get(id), ...notBlocklisted)),
`Add-on ${id} should not be blocklisted in app/platform version ${notBlocklisted}`);
}
}
Expand Down
55 changes: 28 additions & 27 deletions toolkit/mozapps/extensions/test/xpcshell/test_overrideblocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,32 @@ function run_test() {
run_next_test();
}

function isBlocklisted(addon, appVer, toolkitVer) {
return Services.blocklist.getAddonBlocklistState(addon, appVer, toolkitVer) != Services.blocklist.STATE_NOT_BLOCKED;
async function isBlocklisted(addon, appVer, toolkitVer) {
let state = await Services.blocklist.getAddonBlocklistState(addon, appVer, toolkitVer);
return state != Services.blocklist.STATE_NOT_BLOCKED;
}

// On first run whataver is in the app dir should get copied to the profile
add_test(function test_copy() {
add_test(async function test_copy() {
clearBlocklists();
copyToApp(OLD);

incrementAppVersion();
startupManager();

reloadBlocklist();
Assert.ok(!isBlocklisted(invalidAddon));
Assert.ok(!isBlocklisted(ancientAddon));
Assert.ok(isBlocklisted(oldAddon));
Assert.ok(!isBlocklisted(newAddon));
Assert.ok(!(await isBlocklisted(invalidAddon)));
Assert.ok(!(await isBlocklisted(ancientAddon)));
Assert.ok(await isBlocklisted(oldAddon));
Assert.ok(!(await isBlocklisted(newAddon)));

shutdownManager();

run_next_test();
});

// An ancient blocklist should be ignored
add_test(function test_ancient() {
add_test(async function test_ancient() {
clearBlocklists();
copyToApp(ANCIENT);
copyToProfile(OLD, OLD_TSTAMP);
Expand All @@ -118,18 +119,18 @@ add_test(function test_ancient() {

reloadBlocklist();

Assert.ok(!isBlocklisted(invalidAddon));
Assert.ok(!isBlocklisted(ancientAddon));
Assert.ok(isBlocklisted(oldAddon));
Assert.ok(!isBlocklisted(newAddon));
Assert.ok(!(await isBlocklisted(invalidAddon)));
Assert.ok(!(await isBlocklisted(ancientAddon)));
Assert.ok(await isBlocklisted(oldAddon));
Assert.ok(!(await isBlocklisted(newAddon)));

shutdownManager();

run_next_test();
});

// A new blocklist should override an old blocklist
add_test(function test_override() {
add_test(async function test_override() {
clearBlocklists();
copyToApp(NEW);
copyToProfile(OLD, OLD_TSTAMP);
Expand All @@ -139,18 +140,18 @@ add_test(function test_override() {

reloadBlocklist();

Assert.ok(!isBlocklisted(invalidAddon));
Assert.ok(!isBlocklisted(ancientAddon));
Assert.ok(!isBlocklisted(oldAddon));
Assert.ok(isBlocklisted(newAddon));
Assert.ok(!(await isBlocklisted(invalidAddon)));
Assert.ok(!(await isBlocklisted(ancientAddon)));
Assert.ok(!(await isBlocklisted(oldAddon)));
Assert.ok(await isBlocklisted(newAddon));

shutdownManager();

run_next_test();
});

// An old blocklist shouldn't override a new blocklist
add_test(function test_retain() {
add_test(async function test_retain() {
clearBlocklists();
copyToApp(OLD);
copyToProfile(NEW, NEW_TSTAMP);
Expand All @@ -160,18 +161,18 @@ add_test(function test_retain() {

reloadBlocklist();

Assert.ok(!isBlocklisted(invalidAddon));
Assert.ok(!isBlocklisted(ancientAddon));
Assert.ok(!isBlocklisted(oldAddon));
Assert.ok(isBlocklisted(newAddon));
Assert.ok(!(await isBlocklisted(invalidAddon)));
Assert.ok(!(await isBlocklisted(ancientAddon)));
Assert.ok(!(await isBlocklisted(oldAddon)));
Assert.ok(await isBlocklisted(newAddon));

shutdownManager();

run_next_test();
});

// A missing blocklist in the profile should still load an app-shipped blocklist
add_test(function test_missing() {
add_test(async function test_missing() {
clearBlocklists();
copyToApp(OLD);
copyToProfile(NEW, NEW_TSTAMP);
Expand All @@ -186,10 +187,10 @@ add_test(function test_missing() {

reloadBlocklist();

Assert.ok(!isBlocklisted(invalidAddon));
Assert.ok(!isBlocklisted(ancientAddon));
Assert.ok(isBlocklisted(oldAddon));
Assert.ok(!isBlocklisted(newAddon));
Assert.ok(!(await isBlocklisted(invalidAddon)));
Assert.ok(!(await isBlocklisted(ancientAddon)));
Assert.ok(await isBlocklisted(oldAddon));
Assert.ok(!(await isBlocklisted(newAddon)));

shutdownManager();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ add_task(async function test_update_check() {
let updates = await checkUpdates("[email protected]", file);

equal(updates.length, 5);
let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates);
let update = await AddonUpdateChecker.getNewestCompatibleUpdate(updates);
notEqual(update, null);
equal(update.version, "3.0");
update = AddonUpdateChecker.getCompatibilityUpdate(updates, "2");
Expand Down
8 changes: 4 additions & 4 deletions toolkit/mozapps/extensions/test/xpcshell/test_updatecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_task(async function() {
let updates = await checkUpdates("[email protected]", UPDATE_FILE);

equal(updates.length, 5);
let update = AddonUpdateChecker.getNewestCompatibleUpdate(updates);
let update = await AddonUpdateChecker.getNewestCompatibleUpdate(updates);
notEqual(update, null);
equal(update.version, "3.0");
update = AddonUpdateChecker.getCompatibilityUpdate(updates, "2");
Expand Down Expand Up @@ -159,7 +159,7 @@ add_task(async function() {
let updates = await checkUpdates("[email protected]",
UPDATE_FILE);
equal(updates.length, 3);
let update = AddonUpdateChecker.getNewestCompatibleUpdate(
let update = await AddonUpdateChecker.getNewestCompatibleUpdate(
updates, null, null, true);
notEqual(update, null);
equal(update.version, 2);
Expand All @@ -184,7 +184,7 @@ add_task(async function() {
appMinVersion: 1,
appMaxVersion: 2
}];
let update = AddonUpdateChecker.getNewestCompatibleUpdate(
let update = await AddonUpdateChecker.getNewestCompatibleUpdate(
updates, null, null, true, false, overrides);
notEqual(update, null);
equal(update.version, 1);
Expand All @@ -194,7 +194,7 @@ add_task(async function() {
let updates = await checkUpdates("[email protected]",
UPDATE_FILE);
equal(updates.length, 1);
let update = AddonUpdateChecker.getNewestCompatibleUpdate(
let update = await AddonUpdateChecker.getNewestCompatibleUpdate(
updates, null, null, true, false);
equal(update, null);
});

0 comments on commit df8eb8d

Please sign in to comment.