From ea1981e4a02af388301fc8c91522e9bc59268f4e Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Thu, 29 Apr 2010 10:41:24 -0700 Subject: [PATCH] Bug 562518: Update nsAddonRepository to use the new API. r=robstrong --HG-- rename : toolkit/mozapps/extensions/test/unit/data/test_bug404024.xml => toolkit/mozapps/extensions/test/xpcshell/data/test_bug404024.xml rename : toolkit/mozapps/extensions/test/unit/data/test_bug417606.xml => toolkit/mozapps/extensions/test/xpcshell/data/test_bug417606.xml rename : toolkit/mozapps/extensions/test/unit/data/test_bug424262.xml => toolkit/mozapps/extensions/test/xpcshell/data/test_bug424262.xml rename : toolkit/mozapps/extensions/test/unit/test_bug404024.js => toolkit/mozapps/extensions/test/xpcshell/test_bug404024.js rename : toolkit/mozapps/extensions/test/unit/test_bug417606.js => toolkit/mozapps/extensions/test/xpcshell/test_bug417606.js rename : toolkit/mozapps/extensions/test/unit/test_bug424262.js => toolkit/mozapps/extensions/test/xpcshell/test_bug424262.js --- .../mozapps/extensions/nsAddonRepository.js | 43 ++++++----- .../mozapps/extensions/nsIAddonRepository.idl | 8 +- .../data/test_bug404024.xml | 0 .../data/test_bug417606.xml | 0 .../data/test_bug424262.xml | 0 .../test/{unit => xpcshell}/test_bug404024.js | 73 +++++++++--------- .../test/{unit => xpcshell}/test_bug417606.js | 75 ++++++++++--------- .../test/{unit => xpcshell}/test_bug424262.js | 4 +- 8 files changed, 108 insertions(+), 95 deletions(-) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/data/test_bug404024.xml (100%) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/data/test_bug417606.xml (100%) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/data/test_bug424262.xml (100%) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/test_bug404024.js (74%) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/test_bug417606.js (74%) rename toolkit/mozapps/extensions/test/{unit => xpcshell}/test_bug424262.js (96%) diff --git a/toolkit/mozapps/extensions/nsAddonRepository.js b/toolkit/mozapps/extensions/nsAddonRepository.js index fd1ad7d7ab5..33c959a9b29 100644 --- a/toolkit/mozapps/extensions/nsAddonRepository.js +++ b/toolkit/mozapps/extensions/nsAddonRepository.js @@ -42,6 +42,7 @@ const Ci = Components.interfaces; const Cr = Components.results; Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); +Components.utils.import("resource://gre/modules/AddonManager.jsm"); const PREF_GETADDONS_BROWSEADDONS = "extensions.getAddons.browseAddons"; const PREF_GETADDONS_BROWSERECOMMENDED = "extensions.getAddons.recommended.browseURL"; @@ -202,9 +203,7 @@ AddonRepository.prototype = { }, // Parses an add-on entry from an element - _parseAddon: function(element) { - var em = Cc["@mozilla.org/extensions/manager;1"]. - getService(Ci.nsIExtensionManager); + _parseAddon: function(element, known_ids) { var app = Cc["@mozilla.org/xre/app-info;1"]. getService(Ci.nsIXULAppInfo). QueryInterface(Ci.nsIXULRuntime); @@ -219,7 +218,7 @@ AddonRepository.prototype = { return; // Ignore installed add-ons - if (em.getItemForID(guid[0].textContent) != null) + if (known_ids.indexOf(guid[0].textContent) != -1) return; // Ignore sandboxed add-ons @@ -304,9 +303,9 @@ AddonRepository.prototype = { // The type element has an id attribute that is the id from AMO's // database. This doesn't match our type values to perform a mapping if (node.getAttribute("id") == 2) - addon.type = Ci.nsIUpdateItem.TYPE_THEME; + addon.type = Ci.nsIAddonSearchResult.TYPE_THEME; else - addon.type = Ci.nsIUpdateItem.TYPE_EXTENSION; + addon.type = Ci.nsIAddonSearchResult.TYPE_EXTENSION; break; case "install": // No os attribute means the xpi is compatible with any os @@ -341,22 +340,28 @@ AddonRepository.prototype = { this._reportFailure(); return; } - var elements = responseXML.documentElement.getElementsByTagName("addon"); - for (var i = 0; i < elements.length; i++) { - this._parseAddon(elements[i]); - var prefs = Components.classes["@mozilla.org/preferences-service;1"] - .getService(Components.interfaces.nsIPrefBranch); - if (this._addons.length == this._maxResults) { - this._reportSuccess(elements.length); - return; + var self = this; + AddonManager.getAllAddons(function(addons) { + var known_ids = [a.id for each(a in addons)]; + + var elements = responseXML.documentElement.getElementsByTagName("addon"); + for (var i = 0; i < elements.length; i++) { + self._parseAddon(elements[i], known_ids); + + var prefs = Components.classes["@mozilla.org/preferences-service;1"] + .getService(Components.interfaces.nsIPrefBranch); + if (self._addons.length == self._maxResults) { + self._reportSuccess(elements.length); + return; + } } - } - if (responseXML.documentElement.hasAttribute("total_results")) - this._reportSuccess(responseXML.documentElement.getAttribute("total_results")); - else - this._reportSuccess(elements.length); + if (responseXML.documentElement.hasAttribute("total_results")) + self._reportSuccess(responseXML.documentElement.getAttribute("total_results")); + else + self._reportSuccess(elements.length); + }); }, // Performs a new request for results diff --git a/toolkit/mozapps/extensions/nsIAddonRepository.idl b/toolkit/mozapps/extensions/nsIAddonRepository.idl index 7b2f6405b41..eb4a6967db3 100644 --- a/toolkit/mozapps/extensions/nsIAddonRepository.idl +++ b/toolkit/mozapps/extensions/nsIAddonRepository.idl @@ -38,9 +38,15 @@ #include "nsISupports.idl" -[scriptable, uuid(a549a714-2ada-4bb9-8a47-be26e73d49a5)] +[scriptable, uuid(f81ed0bc-ee98-4edd-bf2d-751b47bf665d)] interface nsIAddonSearchResult : nsISupports { + /** + * Values for the type attribute + */ + const unsigned long TYPE_EXTENSION = 0x02; + const unsigned long TYPE_THEME = 0x04; + /** * The ID of the add-on */ diff --git a/toolkit/mozapps/extensions/test/unit/data/test_bug404024.xml b/toolkit/mozapps/extensions/test/xpcshell/data/test_bug404024.xml similarity index 100% rename from toolkit/mozapps/extensions/test/unit/data/test_bug404024.xml rename to toolkit/mozapps/extensions/test/xpcshell/data/test_bug404024.xml diff --git a/toolkit/mozapps/extensions/test/unit/data/test_bug417606.xml b/toolkit/mozapps/extensions/test/xpcshell/data/test_bug417606.xml similarity index 100% rename from toolkit/mozapps/extensions/test/unit/data/test_bug417606.xml rename to toolkit/mozapps/extensions/test/xpcshell/data/test_bug417606.xml diff --git a/toolkit/mozapps/extensions/test/unit/data/test_bug424262.xml b/toolkit/mozapps/extensions/test/xpcshell/data/test_bug424262.xml similarity index 100% rename from toolkit/mozapps/extensions/test/unit/data/test_bug424262.xml rename to toolkit/mozapps/extensions/test/xpcshell/data/test_bug424262.xml diff --git a/toolkit/mozapps/extensions/test/unit/test_bug404024.js b/toolkit/mozapps/extensions/test/xpcshell/test_bug404024.js similarity index 74% rename from toolkit/mozapps/extensions/test/unit/test_bug404024.js rename to toolkit/mozapps/extensions/test/xpcshell/test_bug404024.js index 44466cafdaf..3d2686f032a 100644 --- a/toolkit/mozapps/extensions/test/unit/test_bug404024.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug404024.js @@ -69,7 +69,7 @@ var RESULTS = [ thumbnailURL: null, homepageURL: "https://addons.mozilla.org/addon/5992", eula: null, - type: Ci.nsIUpdateItem.TYPE_EXTENSION, + type: Ci.nsIAddonSearchResult.TYPE_EXTENSION, xpiURL: "http://localhost:4444/test.xpi", xpiHash: "sha1:c26f0b0d62e5dcddcda95074d3f3fedb9bbc26e3" }, @@ -84,7 +84,7 @@ var RESULTS = [ thumbnailURL: "http://localhost:4444/test_bug404024/thumbnail.png", homepageURL: null, eula: "EULA should be confirmed", - type: Ci.nsIUpdateItem.TYPE_THEME, + type: Ci.nsIAddonSearchResult.TYPE_THEME, xpiURL: "http://localhost:4444/test.xpi", xpiHash: null } @@ -158,47 +158,48 @@ var FailCallback = { function run_test() { // Setup for test + do_test_pending(); createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); // Install an add-on so we can check the same add-on isn't returns in the results - startupEM(); - gEM.installItemFromFile(do_get_addon("test_bug397778"), NS_INSTALL_LOCATION_APPPROFILE); - restartEM(); + startupManager(); + installAllFiles([do_get_addon("test_bug397778")], function() { + restartManager(); - server = new nsHttpServer(); - server.registerDirectory("/", do_get_file("data")); - server.start(4444); + server = new nsHttpServer(); + server.registerDirectory("/", do_get_file("data")); + server.start(4444); - // Point the addons repository to the test server - gPrefs.setCharPref(PREF_GETADDONS_BROWSEADDONS, BROWSE); - gPrefs.setCharPref(PREF_GETADDONS_BROWSERECOMMENDED, RECOMMENDED); - gPrefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug404024.xml"); - gPrefs.setCharPref(PREF_GETADDONS_BROWSESEARCHRESULTS, SEARCH + "%TERMS%"); - gPrefs.setCharPref(PREF_GETADDONS_GETSEARCHRESULTS, "http://localhost:4444/test_%TERMS%.xml"); - - addonRepo = Components.classes["@mozilla.org/extensions/addon-repository;1"] - .getService(Components.interfaces.nsIAddonRepository); + // Point the addons repository to the test server + Services.prefs.setCharPref(PREF_GETADDONS_BROWSEADDONS, BROWSE); + Services.prefs.setCharPref(PREF_GETADDONS_BROWSERECOMMENDED, RECOMMENDED); + Services.prefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug404024.xml"); + Services.prefs.setCharPref(PREF_GETADDONS_BROWSESEARCHRESULTS, SEARCH + "%TERMS%"); + Services.prefs.setCharPref(PREF_GETADDONS_GETSEARCHRESULTS, "http://localhost:4444/test_%TERMS%.xml"); - do_check_neq(addonRepo, null); - // Check the homepage and recommended urls - do_check_eq(addonRepo.homepageURL, BROWSE); - do_check_eq(addonRepo.getRecommendedURL(), RECOMMENDED); + addonRepo = Components.classes["@mozilla.org/extensions/addon-repository;1"] + .getService(Components.interfaces.nsIAddonRepository); - // Check that search urls are correct - for (var i = 0; i < BROWSE_SEARCH_URLS.length; i++) { - var url = addonRepo.getSearchURL(BROWSE_SEARCH_URLS[i][0]); - if (url != BROWSE_SEARCH_URLS[i][1]) - do_throw("BROWSE_SEARCH_URL[" + i + "] returned " + url); - } + do_check_neq(addonRepo, null); + // Check the homepage and recommended urls + do_check_eq(addonRepo.homepageURL, BROWSE); + do_check_eq(addonRepo.getRecommendedURL(), RECOMMENDED); - do_test_pending(); - // This should fail because we cancel it immediately. - addonRepo.retrieveRecommendedAddons(10, FailCallback); - addonRepo.cancelSearch(); - // Pull some results. - addonRepo.retrieveRecommendedAddons(10, RecommendedCallback); - // Should be searching now and any attempt to retrieve again should be ignored - do_check_true(addonRepo.isSearching); - addonRepo.retrieveRecommendedAddons(10, FailCallback); + // Check that search urls are correct + for (var i = 0; i < BROWSE_SEARCH_URLS.length; i++) { + var url = addonRepo.getSearchURL(BROWSE_SEARCH_URLS[i][0]); + if (url != BROWSE_SEARCH_URLS[i][1]) + do_throw("BROWSE_SEARCH_URL[" + i + "] returned " + url); + } + + // This should fail because we cancel it immediately. + addonRepo.retrieveRecommendedAddons(10, FailCallback); + addonRepo.cancelSearch(); + // Pull some results. + addonRepo.retrieveRecommendedAddons(10, RecommendedCallback); + // Should be searching now and any attempt to retrieve again should be ignored + do_check_true(addonRepo.isSearching); + addonRepo.retrieveRecommendedAddons(10, FailCallback); + }); } diff --git a/toolkit/mozapps/extensions/test/unit/test_bug417606.js b/toolkit/mozapps/extensions/test/xpcshell/test_bug417606.js similarity index 74% rename from toolkit/mozapps/extensions/test/unit/test_bug417606.js rename to toolkit/mozapps/extensions/test/xpcshell/test_bug417606.js index 1ed38476890..c69e54c79ed 100644 --- a/toolkit/mozapps/extensions/test/unit/test_bug417606.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug417606.js @@ -69,7 +69,7 @@ var RESULTS = [ thumbnailURL: null, homepageURL: "https://addons.mozilla.org/addon/5992", eula: null, - type: Ci.nsIUpdateItem.TYPE_EXTENSION, + type: Ci.nsIAddonSearchResult.TYPE_EXTENSION, xpiURL: "http://localhost:4444/test.xpi", xpiHash: "sha1:c26f0b0d62e5dcddcda95074d3f3fedb9bbc26e3" }, @@ -84,7 +84,7 @@ var RESULTS = [ thumbnailURL: "http://localhost:4444/test_bug404024/thumbnail.png", homepageURL: null, eula: "EULA should be confirmed", - type: Ci.nsIUpdateItem.TYPE_THEME, + type: Ci.nsIAddonSearchResult.TYPE_THEME, xpiURL: "http://localhost:4444/XPCShell.xpi", xpiHash: null } @@ -158,47 +158,48 @@ var FailCallback = { function run_test() { // Setup for test + do_test_pending(); createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); // Install an add-on so we can check the same add-on isn't returns in the results - startupEM(); - gEM.installItemFromFile(do_get_addon("test_bug397778"), NS_INSTALL_LOCATION_APPPROFILE); - restartEM(); - - server = new nsHttpServer(); - server.registerDirectory("/", do_get_file("data")); - server.start(4444); - - // Point the addons repository to the test server - gPrefs.setCharPref(PREF_GETADDONS_BROWSEADDONS, BROWSE); - gPrefs.setCharPref(PREF_GETADDONS_BROWSERECOMMENDED, RECOMMENDED); - gPrefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug417606.xml"); - gPrefs.setCharPref(PREF_GETADDONS_BROWSESEARCHRESULTS, SEARCH + "%TERMS%"); - gPrefs.setCharPref(PREF_GETADDONS_GETSEARCHRESULTS, "http://localhost:4444/test_%TERMS%.xml"); + startupManager(); + installAllFiles([do_get_addon("test_bug397778")], function() { + restartManager(); - addonRepo = Components.classes["@mozilla.org/extensions/addon-repository;1"] - .getService(Components.interfaces.nsIAddonRepository); + server = new nsHttpServer(); + server.registerDirectory("/", do_get_file("data")); + server.start(4444); - do_check_neq(addonRepo, null); - // Check the homepage and recommended urls - do_check_eq(addonRepo.homepageURL, BROWSE); - do_check_eq(addonRepo.getRecommendedURL(), RECOMMENDED); + // Point the addons repository to the test server + Services.prefs.setCharPref(PREF_GETADDONS_BROWSEADDONS, BROWSE); + Services.prefs.setCharPref(PREF_GETADDONS_BROWSERECOMMENDED, RECOMMENDED); + Services.prefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug417606.xml"); + Services.prefs.setCharPref(PREF_GETADDONS_BROWSESEARCHRESULTS, SEARCH + "%TERMS%"); + Services.prefs.setCharPref(PREF_GETADDONS_GETSEARCHRESULTS, "http://localhost:4444/test_%TERMS%.xml"); - // Check that search urls are correct - for (var i = 0; i < BROWSE_SEARCH_URLS.length; i++) { - var url = addonRepo.getSearchURL(BROWSE_SEARCH_URLS[i][0]); - if (url != BROWSE_SEARCH_URLS[i][1]) - do_throw("BROWSE_SEARCH_URL[" + i + "] returned " + url); - } + addonRepo = Components.classes["@mozilla.org/extensions/addon-repository;1"] + .getService(Components.interfaces.nsIAddonRepository); - do_test_pending(); - // This should fail because we cancel it immediately. - addonRepo.retrieveRecommendedAddons(10, FailCallback); - addonRepo.cancelSearch(); - // Pull some results. - addonRepo.retrieveRecommendedAddons(10, RecommendedCallback); - // Should be searching now and any attempt to retrieve again should be ignored - do_check_true(addonRepo.isSearching); - addonRepo.retrieveRecommendedAddons(10, FailCallback); + do_check_neq(addonRepo, null); + // Check the homepage and recommended urls + do_check_eq(addonRepo.homepageURL, BROWSE); + do_check_eq(addonRepo.getRecommendedURL(), RECOMMENDED); + + // Check that search urls are correct + for (var i = 0; i < BROWSE_SEARCH_URLS.length; i++) { + var url = addonRepo.getSearchURL(BROWSE_SEARCH_URLS[i][0]); + if (url != BROWSE_SEARCH_URLS[i][1]) + do_throw("BROWSE_SEARCH_URL[" + i + "] returned " + url); + } + + // This should fail because we cancel it immediately. + addonRepo.retrieveRecommendedAddons(10, FailCallback); + addonRepo.cancelSearch(); + // Pull some results. + addonRepo.retrieveRecommendedAddons(10, RecommendedCallback); + // Should be searching now and any attempt to retrieve again should be ignored + do_check_true(addonRepo.isSearching); + addonRepo.retrieveRecommendedAddons(10, FailCallback); + }); } diff --git a/toolkit/mozapps/extensions/test/unit/test_bug424262.js b/toolkit/mozapps/extensions/test/xpcshell/test_bug424262.js similarity index 96% rename from toolkit/mozapps/extensions/test/unit/test_bug424262.js rename to toolkit/mozapps/extensions/test/xpcshell/test_bug424262.js index 55f96deed43..42c45ca4003 100644 --- a/toolkit/mozapps/extensions/test/unit/test_bug424262.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug424262.js @@ -76,14 +76,14 @@ function run_test() { // EM needs to be running. createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); - startupEM(); + startupManager(); server = new nsHttpServer(); server.registerDirectory("/", do_get_file("data")); server.start(4444); // Point the addons repository to the test server - gPrefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug424262.xml"); + Services.prefs.setCharPref(PREF_GETADDONS_GETRECOMMENDED, "http://localhost:4444/test_bug424262.xml"); addonRepo = Components.classes["@mozilla.org/extensions/addon-repository;1"] .getService(Components.interfaces.nsIAddonRepository);