2008-01-28 17:11:48 +00:00
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is the Extension Manager.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is mozilla.org
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Dave Townsend <dtownsend@oxymoronical.com>
|
2010-08-26 17:44:14 +00:00
|
|
|
# Ben Parr <bparr@bparr.com>
|
2008-01-28 17:11:48 +00:00
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-07-19 20:16:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2008-01-28 17:11:48 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2010-07-19 23:01:23 +00:00
|
|
|
const Cu = Components.utils;
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
Components.utils.import("resource://gre/modules/FileUtils.jsm");
|
2010-08-26 17:44:14 +00:00
|
|
|
Components.utils.import("resource://gre/modules/NetUtil.jsm");
|
2010-07-19 23:01:23 +00:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2010-04-29 17:41:24 +00:00
|
|
|
Components.utils.import("resource://gre/modules/AddonManager.jsm");
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
var EXPORTED_SYMBOLS = [ "AddonRepository" ];
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
|
2011-02-15 23:18:07 +00:00
|
|
|
const PREF_GETADDONS_CACHE_TYPES = "extensions.getAddons.cache.types";
|
|
|
|
const PREF_GETADDONS_CACHE_ID_ENABLED = "extensions.%ID%.getAddons.cache.enabled"
|
2008-01-28 17:11:48 +00:00
|
|
|
const PREF_GETADDONS_BROWSEADDONS = "extensions.getAddons.browseAddons";
|
2010-08-26 17:44:14 +00:00
|
|
|
const PREF_GETADDONS_BYIDS = "extensions.getAddons.get.url";
|
2008-01-28 17:11:48 +00:00
|
|
|
const PREF_GETADDONS_BROWSERECOMMENDED = "extensions.getAddons.recommended.browseURL";
|
|
|
|
const PREF_GETADDONS_GETRECOMMENDED = "extensions.getAddons.recommended.url";
|
|
|
|
const PREF_GETADDONS_BROWSESEARCHRESULTS = "extensions.getAddons.search.browseURL";
|
|
|
|
const PREF_GETADDONS_GETSEARCHRESULTS = "extensions.getAddons.search.url";
|
|
|
|
|
2008-03-07 02:05:22 +00:00
|
|
|
const XMLURI_PARSE_ERROR = "http://www.mozilla.org/newlayout/xml/parsererror.xml";
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
const API_VERSION = "1.5";
|
2011-02-15 23:18:07 +00:00
|
|
|
const DEFAULT_CACHE_TYPES = "extension,theme,locale";
|
2010-08-26 17:44:14 +00:00
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
const KEY_PROFILEDIR = "ProfD";
|
|
|
|
const FILE_DATABASE = "addons.sqlite";
|
2011-09-05 10:26:06 +00:00
|
|
|
const DB_SCHEMA = 2;
|
2010-08-24 01:04:06 +00:00
|
|
|
|
|
|
|
["LOG", "WARN", "ERROR"].forEach(function(aName) {
|
|
|
|
this.__defineGetter__(aName, function() {
|
|
|
|
Components.utils.import("resource://gre/modules/AddonLogging.jsm");
|
|
|
|
|
|
|
|
LogManager.getLogger("addons.repository", this);
|
|
|
|
return this[aName];
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
|
|
// Add-on properties parsed out of AMO results
|
|
|
|
// Note: the 'install' property is added for results from
|
|
|
|
// retrieveRecommendedAddons and searchAddons
|
|
|
|
const PROP_SINGLE = ["id", "type", "name", "version", "creator", "description",
|
|
|
|
"fullDescription", "developerComments", "eula", "iconURL",
|
|
|
|
"homepageURL", "supportURL", "contributionURL",
|
|
|
|
"contributionAmount", "averageRating", "reviewCount",
|
|
|
|
"reviewURL", "totalDownloads", "weeklyDownloads",
|
|
|
|
"dailyUsers", "sourceURI", "repositoryStatus", "size",
|
|
|
|
"updateDate"];
|
|
|
|
const PROP_MULTI = ["developers", "screenshots"]
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// A map between XML keys to AddonSearchResult keys for string values
|
|
|
|
// that require no extra parsing from XML
|
|
|
|
const STRING_KEY_MAP = {
|
|
|
|
name: "name",
|
|
|
|
version: "version",
|
2010-09-13 22:00:28 +00:00
|
|
|
icon: "iconURL",
|
|
|
|
homepage: "homepageURL",
|
|
|
|
support: "supportURL"
|
2010-09-13 17:35:37 +00:00
|
|
|
};
|
|
|
|
|
2010-09-14 23:56:38 +00:00
|
|
|
// A map between XML keys to AddonSearchResult keys for string values
|
|
|
|
// that require parsing from HTML
|
|
|
|
const HTML_KEY_MAP = {
|
|
|
|
summary: "description",
|
|
|
|
description: "fullDescription",
|
|
|
|
developer_comments: "developerComments",
|
|
|
|
eula: "eula"
|
|
|
|
};
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// A map between XML keys to AddonSearchResult keys for integer values
|
|
|
|
// that require no extra parsing from XML
|
|
|
|
const INTEGER_KEY_MAP = {
|
|
|
|
total_downloads: "totalDownloads",
|
|
|
|
weekly_downloads: "weeklyDownloads",
|
|
|
|
daily_users: "dailyUsers"
|
|
|
|
};
|
|
|
|
|
2008-03-07 11:16:48 +00:00
|
|
|
|
2010-09-14 23:56:38 +00:00
|
|
|
function convertHTMLToPlainText(html) {
|
|
|
|
if (!html)
|
|
|
|
return html;
|
|
|
|
var converter = Cc["@mozilla.org/widget/htmlformatconverter;1"].
|
|
|
|
createInstance(Ci.nsIFormatConverter);
|
|
|
|
|
|
|
|
var input = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
input.data = html.replace("\n", "<br>", "g");
|
|
|
|
|
|
|
|
var output = {};
|
|
|
|
converter.convert("text/html", input, input.data.length, "text/unicode",
|
|
|
|
output, {});
|
|
|
|
|
|
|
|
if (output.value instanceof Ci.nsISupportsString)
|
|
|
|
return output.value.data.replace("\r\n", "\n", "g");
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2011-02-15 23:18:07 +00:00
|
|
|
function getAddonsToCache(aIds, aCallback) {
|
|
|
|
try {
|
|
|
|
var types = Services.prefs.getCharPref(PREF_GETADDONS_CACHE_TYPES);
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
if (!types)
|
|
|
|
types = DEFAULT_CACHE_TYPES;
|
|
|
|
|
|
|
|
types = types.split(",");
|
|
|
|
|
|
|
|
AddonManager.getAddonsByIDs(aIds, function(aAddons) {
|
|
|
|
let enabledIds = [];
|
|
|
|
for (var i = 0; i < aIds.length; i++) {
|
|
|
|
var preference = PREF_GETADDONS_CACHE_ID_ENABLED.replace("%ID%", aIds[i]);
|
|
|
|
try {
|
|
|
|
if (!Services.prefs.getBoolPref(preference))
|
|
|
|
continue;
|
|
|
|
} catch(e) {
|
|
|
|
// If the preference doesn't exist caching is enabled by default
|
|
|
|
}
|
|
|
|
|
|
|
|
// The add-ons manager may not know about this ID yet if it is a pending
|
|
|
|
// install. In that case we'll just cache it regardless
|
|
|
|
if (aAddons[i] && (types.indexOf(aAddons[i].type) == -1))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
enabledIds.push(aIds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(enabledIds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
function AddonSearchResult(aId) {
|
|
|
|
this.id = aId;
|
2008-01-28 17:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddonSearchResult.prototype = {
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* The ID of the add-on
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
id: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
/**
|
|
|
|
* The add-on type (e.g. "extension" or "theme")
|
|
|
|
*/
|
|
|
|
type: null,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* The name of the add-on
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
name: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The version of the add-on
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
version: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
/**
|
|
|
|
* The creator of the add-on
|
|
|
|
*/
|
|
|
|
creator: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The developers of the add-on
|
|
|
|
*/
|
|
|
|
developers: null,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* A short description of the add-on
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
description: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The full description of the add-on
|
|
|
|
*/
|
|
|
|
fullDescription: null,
|
|
|
|
|
|
|
|
/**
|
2010-08-26 17:44:14 +00:00
|
|
|
* The developer comments for the add-on. This includes any information
|
|
|
|
* that may be helpful to end users that isn't necessarily applicable to
|
|
|
|
* the add-on description (e.g. known major bugs)
|
|
|
|
*/
|
|
|
|
developerComments: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The end-user licensing agreement (EULA) of the add-on
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2010-08-26 17:44:14 +00:00
|
|
|
eula: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
|
|
|
/**
|
2010-08-26 17:44:14 +00:00
|
|
|
* The url of the add-on's icon
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
iconURL: null,
|
2010-06-17 22:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of screenshot urls for the add-on
|
|
|
|
*/
|
|
|
|
screenshots: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The homepage for the add-on
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
homepageURL: null,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
2010-08-26 17:44:14 +00:00
|
|
|
* The support URL for the add-on
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2010-08-26 17:44:14 +00:00
|
|
|
supportURL: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The contribution url of the add-on
|
|
|
|
*/
|
|
|
|
contributionURL: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The suggested contribution amount
|
|
|
|
*/
|
|
|
|
contributionAmount: null,
|
|
|
|
|
2011-01-07 17:09:09 +00:00
|
|
|
/**
|
|
|
|
* The URL to visit in order to purchase the add-on
|
|
|
|
*/
|
|
|
|
purchaseURL: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The numerical cost of the add-on in some currency, for sorting purposes
|
|
|
|
* only
|
|
|
|
*/
|
|
|
|
purchaseAmount: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The display cost of the add-on, for display purposes only
|
|
|
|
*/
|
|
|
|
purchaseDisplayAmount: null,
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
/**
|
|
|
|
* The rating of the add-on, 0-5
|
|
|
|
*/
|
|
|
|
averageRating: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of reviews for this add-on
|
|
|
|
*/
|
|
|
|
reviewCount: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL to the list of reviews for this add-on
|
|
|
|
*/
|
|
|
|
reviewURL: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The total number of times the add-on was downloaded
|
|
|
|
*/
|
|
|
|
totalDownloads: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of times the add-on was downloaded the current week
|
|
|
|
*/
|
|
|
|
weeklyDownloads: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of daily users for the add-on
|
|
|
|
*/
|
|
|
|
dailyUsers: null,
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* AddonInstall object generated from the add-on XPI url
|
|
|
|
*/
|
|
|
|
install: null,
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
/**
|
|
|
|
* nsIURI storing where this add-on was installed from
|
|
|
|
*/
|
|
|
|
sourceURI: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The status of the add-on in the repository (e.g. 4 = "Public")
|
|
|
|
*/
|
|
|
|
repositoryStatus: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of the add-on's files in bytes. For an add-on that have not yet
|
|
|
|
* been downloaded this may be an estimated value.
|
|
|
|
*/
|
|
|
|
size: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Date that the add-on was most recently updated
|
|
|
|
*/
|
|
|
|
updateDate: null,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* True or false depending on whether the add-on is compatible with the
|
2011-01-07 17:09:09 +00:00
|
|
|
* current version of the application
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
|
|
|
isCompatible: true,
|
|
|
|
|
2011-01-07 17:09:09 +00:00
|
|
|
/**
|
|
|
|
* True or false depending on whether the add-on is compatible with the
|
|
|
|
* current platform
|
|
|
|
*/
|
|
|
|
isPlatformCompatible: true,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* True if the add-on has a secure means of updating
|
|
|
|
*/
|
|
|
|
providesUpdatesSecurely: true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current blocklist state of the add-on
|
|
|
|
*/
|
|
|
|
blocklistState: Ci.nsIBlocklistService.STATE_NOT_BLOCKED,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this add-on cannot be used in the application based on version
|
|
|
|
* compatibility, dependencies and blocklisting
|
|
|
|
*/
|
|
|
|
appDisabled: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the user wants this add-on to be disabled
|
|
|
|
*/
|
|
|
|
userDisabled: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates what scope the add-on is installed in, per profile, user,
|
|
|
|
* system or application
|
|
|
|
*/
|
|
|
|
scope: AddonManager.SCOPE_PROFILE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the add-on is currently functional
|
|
|
|
*/
|
|
|
|
isActive: true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A bitfield holding all of the current operations that are waiting to be
|
|
|
|
* performed for this add-on
|
|
|
|
*/
|
|
|
|
pendingOperations: AddonManager.PENDING_NONE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A bitfield holding all the the operations that can be performed on
|
|
|
|
* this add-on
|
|
|
|
*/
|
2010-08-26 17:44:14 +00:00
|
|
|
permissions: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether this add-on is known to be compatible with a
|
|
|
|
* particular application and platform version.
|
|
|
|
*
|
|
|
|
* @param appVersion
|
|
|
|
* An application version to test against
|
|
|
|
* @param platformVersion
|
|
|
|
* A platform version to test against
|
|
|
|
* @return Boolean representing if the add-on is compatible
|
|
|
|
*/
|
|
|
|
isCompatibleWith: function(aAppVerison, aPlatformVersion) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts an update check for this add-on. This will perform
|
|
|
|
* asynchronously and deliver results to the given listener.
|
|
|
|
*
|
|
|
|
* @param aListener
|
|
|
|
* An UpdateListener for the update process
|
|
|
|
* @param aReason
|
|
|
|
* A reason code for performing the update
|
|
|
|
* @param aAppVersion
|
|
|
|
* An application version to check for updates for
|
|
|
|
* @param aPlatformVersion
|
|
|
|
* A platform version to check for updates for
|
|
|
|
*/
|
|
|
|
findUpdates: function(aListener, aReason, aAppVersion, aPlatformVersion) {
|
|
|
|
if ("onNoCompatibilityUpdateAvailable" in aListener)
|
|
|
|
aListener.onNoCompatibilityUpdateAvailable(this);
|
|
|
|
if ("onNoUpdateAvailable" in aListener)
|
|
|
|
aListener.onNoUpdateAvailable(this);
|
|
|
|
if ("onUpdateFinished" in aListener)
|
|
|
|
aListener.onUpdateFinished(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* The add-on repository is a source of add-ons that can be installed. It can
|
2010-08-26 17:44:14 +00:00
|
|
|
* be searched in three ways. The first takes a list of IDs and returns a
|
|
|
|
* list of the corresponding add-ons. The second returns a list of add-ons that
|
|
|
|
* come highly recommended. This list should change frequently. The third is to
|
2010-06-17 22:47:51 +00:00
|
|
|
* search for specific search terms entered by the user. Searches are
|
|
|
|
* asynchronous and results should be passed to the provided callback object
|
|
|
|
* when complete. The results passed to the callback should only include add-ons
|
|
|
|
* that are compatible with the current application and are not already
|
2010-08-26 17:44:14 +00:00
|
|
|
* installed.
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
|
|
|
var AddonRepository = {
|
2010-08-24 01:04:06 +00:00
|
|
|
/**
|
|
|
|
* Whether caching is currently enabled
|
|
|
|
*/
|
|
|
|
get cacheEnabled() {
|
|
|
|
// Act as though caching is disabled if there was an unrecoverable error
|
|
|
|
// openning the database.
|
|
|
|
if (!AddonDatabase.databaseOk)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let preference = PREF_GETADDONS_CACHE_ENABLED;
|
|
|
|
let enabled = false;
|
|
|
|
try {
|
|
|
|
enabled = Services.prefs.getBoolPref(preference);
|
|
|
|
} catch(e) {
|
|
|
|
WARN("cacheEnabled: Couldn't get pref: " + preference);
|
|
|
|
}
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
// A cache of the add-ons stored in the database
|
|
|
|
_addons: null,
|
|
|
|
|
|
|
|
// An array of callbacks pending the retrieval of add-ons from AddonDatabase
|
|
|
|
_pendingCallbacks: null,
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Whether a search is currently in progress
|
2008-01-28 17:11:48 +00:00
|
|
|
_searching: false,
|
|
|
|
|
|
|
|
// XHR associated with the current request
|
|
|
|
_request: null,
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/*
|
|
|
|
* Addon search results callback object that contains two functions
|
|
|
|
*
|
|
|
|
* searchSucceeded - Called when a search has suceeded.
|
|
|
|
*
|
2010-08-26 17:44:14 +00:00
|
|
|
* @param aAddons
|
|
|
|
* An array of the add-on results. In the case of searching for
|
|
|
|
* specific terms the ordering of results may be determined by
|
|
|
|
* the search provider.
|
|
|
|
* @param aAddonCount
|
|
|
|
* The length of aAddons
|
|
|
|
* @param aTotalResults
|
|
|
|
* The total results actually available in the repository
|
2010-06-17 22:47:51 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* searchFailed - Called when an error occurred when performing a search.
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
_callback: null,
|
|
|
|
|
|
|
|
// Maximum number of results to return
|
|
|
|
_maxResults: null,
|
2010-08-24 01:04:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize AddonRepository.
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Observe xpcom-shutdown notification, so we can shutdown cleanly.
|
|
|
|
*/
|
|
|
|
observe: function (aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
this.shutdown();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shut down AddonRepository
|
|
|
|
*/
|
|
|
|
shutdown: function() {
|
|
|
|
this.cancelSearch();
|
|
|
|
|
|
|
|
this._addons = null;
|
|
|
|
this._pendingCallbacks = null;
|
|
|
|
AddonDatabase.shutdown(function() {
|
|
|
|
Services.obs.notifyObservers(null, "addon-repository-shutdown", null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously get a cached add-on by id. The add-on (or null if the
|
|
|
|
* add-on is not found) is passed to the specified callback. If caching is
|
|
|
|
* disabled, null is passed to the specified callback.
|
|
|
|
*
|
|
|
|
* @param aId
|
|
|
|
* The id of the add-on to get
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to pass the result back to
|
|
|
|
*/
|
|
|
|
getCachedAddonByID: function(aId, aCallback) {
|
|
|
|
if (!aId || !this.cacheEnabled) {
|
|
|
|
aCallback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
function getAddon(aAddons) {
|
|
|
|
aCallback((aId in aAddons) ? aAddons[aId] : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._addons == null) {
|
|
|
|
if (this._pendingCallbacks == null) {
|
|
|
|
// Data has not been retrieved from the database, so retrieve it
|
|
|
|
this._pendingCallbacks = [];
|
|
|
|
this._pendingCallbacks.push(getAddon);
|
|
|
|
AddonDatabase.retrieveStoredData(function(aAddons) {
|
|
|
|
let pendingCallbacks = self._pendingCallbacks;
|
|
|
|
|
|
|
|
// Check if cache was shutdown or deleted before callback was called
|
|
|
|
if (pendingCallbacks == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Callbacks may want to trigger a other caching operations that may
|
|
|
|
// affect _addons and _pendingCallbacks, so set to final values early
|
|
|
|
self._pendingCallbacks = null;
|
|
|
|
self._addons = aAddons;
|
|
|
|
|
|
|
|
pendingCallbacks.forEach(function(aCallback) aCallback(aAddons));
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data is being retrieved from the database, so wait
|
|
|
|
this._pendingCallbacks.push(getAddon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data has been retrieved, so immediately return result
|
|
|
|
getAddon(this._addons);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously repopulate cache so it only contains the add-ons
|
|
|
|
* corresponding to the specified ids. If caching is disabled,
|
|
|
|
* the cache is completely removed.
|
|
|
|
*
|
|
|
|
* @param aIds
|
|
|
|
* The array of add-on ids to repopulate the cache with
|
|
|
|
* @param aCallback
|
|
|
|
* The optional callback to call once complete
|
|
|
|
*/
|
|
|
|
repopulateCache: function(aIds, aCallback) {
|
|
|
|
// Completely remove cache if caching is not enabled
|
|
|
|
if (!this.cacheEnabled) {
|
|
|
|
this._addons = null;
|
|
|
|
this._pendingCallbacks = null;
|
|
|
|
AddonDatabase.delete(aCallback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-15 23:18:07 +00:00
|
|
|
let self = this;
|
|
|
|
getAddonsToCache(aIds, function(aAddons) {
|
|
|
|
// Completely remove cache if there are no add-ons to cache
|
|
|
|
if (aAddons.length == 0) {
|
|
|
|
this._addons = null;
|
|
|
|
this._pendingCallbacks = null;
|
|
|
|
AddonDatabase.delete(aCallback);
|
|
|
|
return;
|
2010-12-12 20:44:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 23:18:07 +00:00
|
|
|
self.getAddonsByIDs(aAddons, {
|
|
|
|
searchSucceeded: function(aAddons) {
|
|
|
|
self._addons = {};
|
|
|
|
aAddons.forEach(function(aAddon) { self._addons[aAddon.id] = aAddon; });
|
|
|
|
AddonDatabase.repopulate(aAddons, aCallback);
|
|
|
|
},
|
|
|
|
searchFailed: function() {
|
|
|
|
WARN("Search failed when repopulating cache");
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
});
|
2010-08-24 01:04:06 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously add add-ons to the cache corresponding to the specified
|
|
|
|
* ids. If caching is disabled, the cache is unchanged and the callback is
|
|
|
|
* immediatly called if it is defined.
|
|
|
|
*
|
|
|
|
* @param aIds
|
|
|
|
* The array of add-on ids to add to the cache
|
|
|
|
* @param aCallback
|
|
|
|
* The optional callback to call once complete
|
|
|
|
*/
|
|
|
|
cacheAddons: function(aIds, aCallback) {
|
|
|
|
if (!this.cacheEnabled) {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
2011-02-15 23:18:07 +00:00
|
|
|
getAddonsToCache(aIds, function(aAddons) {
|
|
|
|
// If there are no add-ons to cache, act as if caching is disabled
|
|
|
|
if (aAddons.length == 0) {
|
2010-08-24 01:04:06 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
2011-02-15 23:18:07 +00:00
|
|
|
return;
|
2010-08-24 01:04:06 +00:00
|
|
|
}
|
2011-02-15 23:18:07 +00:00
|
|
|
|
|
|
|
self.getAddonsByIDs(aAddons, {
|
|
|
|
searchSucceeded: function(aAddons) {
|
|
|
|
aAddons.forEach(function(aAddon) { self._addons[aAddon.id] = aAddon; });
|
|
|
|
AddonDatabase.insertAddons(aAddons, aCallback);
|
|
|
|
},
|
|
|
|
searchFailed: function() {
|
|
|
|
WARN("Search failed when adding add-ons to cache");
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
});
|
2010-08-24 01:04:06 +00:00
|
|
|
});
|
|
|
|
},
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
2010-08-26 17:44:14 +00:00
|
|
|
* The homepage for visiting this repository. If the corresponding preference
|
|
|
|
* is not defined, defaults to about:blank.
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
get homepageURL() {
|
2010-08-26 17:44:14 +00:00
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_BROWSEADDONS, {});
|
2010-07-19 23:01:23 +00:00
|
|
|
return (url != null) ? url : "about:blank";
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this instance is currently performing a search. New
|
|
|
|
* searches will not be performed while this is the case.
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
get isSearching() {
|
|
|
|
return this._searching;
|
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* The url that can be visited to see recommended add-ons in this repository.
|
2010-08-26 17:44:14 +00:00
|
|
|
* If the corresponding preference is not defined, defaults to about:blank.
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
getRecommendedURL: function() {
|
2010-08-26 17:44:14 +00:00
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_BROWSERECOMMENDED, {});
|
2010-07-19 23:01:23 +00:00
|
|
|
return (url != null) ? url : "about:blank";
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the url that can be visited to see search results for the given
|
2010-08-26 17:44:14 +00:00
|
|
|
* terms. If the corresponding preference is not defined, defaults to
|
|
|
|
* about:blank.
|
2010-06-17 22:47:51 +00:00
|
|
|
*
|
2010-08-26 17:44:14 +00:00
|
|
|
* @param aSearchTerms
|
|
|
|
* Search terms used to search the repository
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
getSearchURL: function(aSearchTerms) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_BROWSESEARCHRESULTS, {
|
2010-07-19 23:01:23 +00:00
|
|
|
TERMS : encodeURIComponent(aSearchTerms)
|
|
|
|
});
|
|
|
|
return (url != null) ? url : "about:blank";
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* Cancels the search in progress. If there is no search in progress this
|
|
|
|
* does nothing.
|
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
cancelSearch: function() {
|
|
|
|
this._searching = false;
|
|
|
|
if (this._request) {
|
|
|
|
this._request.abort();
|
|
|
|
this._request = null;
|
|
|
|
}
|
2008-04-17 08:27:18 +00:00
|
|
|
this._callback = null;
|
2010-08-26 17:44:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a search for add-ons in this repository by ID. Results will be
|
|
|
|
* passed to the given callback.
|
|
|
|
*
|
|
|
|
* @param aIDs
|
|
|
|
* The array of ids to search for
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to pass results to
|
|
|
|
*/
|
|
|
|
getAddonsByIDs: function(aIDs, aCallback) {
|
2011-01-20 21:45:10 +00:00
|
|
|
let startupInfo = Cc["@mozilla.org/toolkit/app-startup;1"].
|
2011-03-25 15:03:35 +00:00
|
|
|
getService(Ci.nsIAppStartup).
|
2011-01-20 21:45:10 +00:00
|
|
|
getStartupInfo();
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
let ids = aIDs.slice(0);
|
2011-01-20 21:45:10 +00:00
|
|
|
|
|
|
|
let params = {
|
2010-08-26 17:44:14 +00:00
|
|
|
API_VERSION : API_VERSION,
|
2010-08-24 01:04:06 +00:00
|
|
|
IDS : ids.map(encodeURIComponent).join(',')
|
2011-01-20 21:45:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (startupInfo.process) {
|
|
|
|
if (startupInfo.main)
|
|
|
|
params.TIME_MAIN = startupInfo.main - startupInfo.process;
|
|
|
|
if (startupInfo.firstPaint)
|
|
|
|
params.TIME_FIRST_PAINT = startupInfo.firstPaint - startupInfo.process;
|
|
|
|
if (startupInfo.sessionRestored)
|
|
|
|
params.TIME_SESSION_RESTORED = startupInfo.sessionRestored -
|
|
|
|
startupInfo.process;
|
|
|
|
};
|
|
|
|
|
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_BYIDS, params);
|
2010-08-26 17:44:14 +00:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
function handleResults(aElements, aTotalResults) {
|
|
|
|
// Don't use this._parseAddons() so that, for example,
|
|
|
|
// incompatible add-ons are not filtered out
|
|
|
|
let results = [];
|
|
|
|
for (let i = 0; i < aElements.length && results.length < self._maxResults; i++) {
|
|
|
|
let result = self._parseAddon(aElements[i]);
|
|
|
|
if (result == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore add-on if it wasn't actually requested
|
2010-08-24 01:04:06 +00:00
|
|
|
let idIndex = ids.indexOf(result.addon.id);
|
2010-08-26 17:44:14 +00:00
|
|
|
if (idIndex == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
results.push(result);
|
|
|
|
// Ignore this add-on from now on
|
2010-08-24 01:04:06 +00:00
|
|
|
ids.splice(idIndex, 1);
|
2010-08-26 17:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// aTotalResults irrelevant
|
|
|
|
self._reportSuccess(results, -1);
|
|
|
|
}
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
this._beginSearch(url, ids.length, aCallback, handleResults);
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* Begins a search for recommended add-ons in this repository. Results will
|
|
|
|
* be passed to the given callback.
|
|
|
|
*
|
2010-08-26 17:44:14 +00:00
|
|
|
* @param aMaxResults
|
|
|
|
* The maximum number of results to return
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to pass results to
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
retrieveRecommendedAddons: function(aMaxResults, aCallback) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_GETRECOMMENDED, {
|
2010-07-19 23:01:23 +00:00
|
|
|
API_VERSION : API_VERSION,
|
|
|
|
|
|
|
|
// Get twice as many results to account for potential filtering
|
|
|
|
MAX_RESULTS : 2 * aMaxResults
|
|
|
|
});
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let self = this;
|
|
|
|
function handleResults(aElements, aTotalResults) {
|
|
|
|
self._getLocalAddonIds(function(aLocalAddonIds) {
|
|
|
|
// aTotalResults irrelevant
|
|
|
|
self._parseAddons(aElements, -1, aLocalAddonIds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._beginSearch(url, aMaxResults, aCallback, handleResults);
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
/**
|
|
|
|
* Begins a search for add-ons in this repository. Results will be passed to
|
|
|
|
* the given callback.
|
|
|
|
*
|
2010-08-26 17:44:14 +00:00
|
|
|
* @param aSearchTerms
|
|
|
|
* The terms to search for
|
|
|
|
* @param aMaxResults
|
|
|
|
* The maximum number of results to return
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to pass results to
|
2010-06-17 22:47:51 +00:00
|
|
|
*/
|
2008-01-28 17:11:48 +00:00
|
|
|
searchAddons: function(aSearchTerms, aMaxResults, aCallback) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let url = this._formatURLPref(PREF_GETADDONS_GETSEARCHRESULTS, {
|
2010-07-19 23:01:23 +00:00
|
|
|
API_VERSION : API_VERSION,
|
2010-08-26 17:44:14 +00:00
|
|
|
TERMS : encodeURIComponent(aSearchTerms),
|
2010-07-19 23:01:23 +00:00
|
|
|
|
|
|
|
// Get twice as many results to account for potential filtering
|
2010-08-26 17:44:14 +00:00
|
|
|
MAX_RESULTS : 2 * aMaxResults
|
2010-07-19 23:01:23 +00:00
|
|
|
});
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let self = this;
|
|
|
|
function handleResults(aElements, aTotalResults) {
|
|
|
|
self._getLocalAddonIds(function(aLocalAddonIds) {
|
|
|
|
self._parseAddons(aElements, aTotalResults, aLocalAddonIds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._beginSearch(url, aMaxResults, aCallback, handleResults);
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Posts results to the callback
|
2010-08-26 17:44:14 +00:00
|
|
|
_reportSuccess: function(aResults, aTotalResults) {
|
2008-01-28 17:11:48 +00:00
|
|
|
this._searching = false;
|
|
|
|
this._request = null;
|
2008-04-17 13:18:46 +00:00
|
|
|
// The callback may want to trigger a new search so clear references early
|
2010-08-26 17:44:14 +00:00
|
|
|
let addons = [result.addon for each(result in aResults)];
|
|
|
|
let callback = this._callback;
|
2008-04-17 08:27:18 +00:00
|
|
|
this._callback = null;
|
2010-08-26 17:44:14 +00:00
|
|
|
callback.searchSucceeded(addons, addons.length, aTotalResults);
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Notifies the callback of a failure
|
2010-06-17 22:47:51 +00:00
|
|
|
_reportFailure: function() {
|
2008-01-28 17:11:48 +00:00
|
|
|
this._searching = false;
|
|
|
|
this._request = null;
|
2008-04-17 13:18:46 +00:00
|
|
|
// The callback may want to trigger a new search so clear references early
|
2010-08-26 17:44:14 +00:00
|
|
|
let callback = this._callback;
|
2008-04-17 08:27:18 +00:00
|
|
|
this._callback = null;
|
2008-04-17 13:18:46 +00:00
|
|
|
callback.searchFailed();
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Get descendant by unique tag name. Returns null if not unique tag name.
|
|
|
|
_getUniqueDescendant: function(aElement, aTagName) {
|
|
|
|
let elementsList = aElement.getElementsByTagName(aTagName);
|
|
|
|
return (elementsList.length == 1) ? elementsList[0] : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Parse out trimmed text content. Returns null if text content empty.
|
|
|
|
_getTextContent: function(aElement) {
|
|
|
|
let textContent = aElement.textContent.trim();
|
|
|
|
return (textContent.length > 0) ? textContent : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Parse out trimmed text content of a descendant with the specified tag name
|
|
|
|
// Returns null if the parsing unsuccessful.
|
|
|
|
_getDescendantTextContent: function(aElement, aTagName) {
|
|
|
|
let descendant = this._getUniqueDescendant(aElement, aTagName);
|
|
|
|
return (descendant != null) ? this._getTextContent(descendant) : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates an AddonSearchResult by parsing an <addon> element
|
|
|
|
*
|
|
|
|
* @param aElement
|
|
|
|
* The <addon> element to parse
|
|
|
|
* @param aSkip
|
|
|
|
* Object containing ids and sourceURIs of add-ons to skip.
|
|
|
|
* @return Result object containing the parsed AddonSearchResult, xpiURL and
|
|
|
|
* xpiHash if the parsing was successful. Otherwise returns null.
|
|
|
|
*/
|
2010-06-17 22:47:51 +00:00
|
|
|
_parseAddon: function(aElement, aSkip) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let skipIDs = (aSkip && aSkip.ids) ? aSkip.ids : [];
|
|
|
|
let skipSourceURIs = (aSkip && aSkip.sourceURIs) ? aSkip.sourceURIs : [];
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let guid = this._getDescendantTextContent(aElement, "guid");
|
|
|
|
if (guid == null || skipIDs.indexOf(guid) != -1)
|
|
|
|
return null;
|
2010-05-19 04:05:44 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let addon = new AddonSearchResult(guid);
|
|
|
|
let result = {
|
|
|
|
addon: addon,
|
|
|
|
xpiURL: null,
|
|
|
|
xpiHash: null
|
|
|
|
};
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let self = this;
|
|
|
|
for (let node = aElement.firstChild; node; node = node.nextSibling) {
|
|
|
|
if (!(node instanceof Ci.nsIDOMElement))
|
|
|
|
continue;
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
let localName = node.localName;
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Handle case where the wanted string value is located in text content
|
2011-02-25 19:23:04 +00:00
|
|
|
// but only if the content is not empty
|
2010-08-26 17:44:14 +00:00
|
|
|
if (localName in STRING_KEY_MAP) {
|
2011-02-25 19:23:04 +00:00
|
|
|
addon[STRING_KEY_MAP[localName]] = this._getTextContent(node) || addon[STRING_KEY_MAP[localName]];
|
2010-08-26 17:44:14 +00:00
|
|
|
continue;
|
2008-01-28 17:11:48 +00:00
|
|
|
}
|
|
|
|
|
2010-09-14 23:56:38 +00:00
|
|
|
// Handle case where the wanted string value is html located in text content
|
|
|
|
if (localName in HTML_KEY_MAP) {
|
|
|
|
addon[HTML_KEY_MAP[localName]] = convertHTMLToPlainText(this._getTextContent(node));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Handle case where the wanted integer value is located in text content
|
|
|
|
if (localName in INTEGER_KEY_MAP) {
|
|
|
|
let value = parseInt(this._getTextContent(node));
|
|
|
|
if (value >= 0)
|
|
|
|
addon[INTEGER_KEY_MAP[localName]] = value;
|
|
|
|
continue;
|
2008-01-28 17:11:48 +00:00
|
|
|
}
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Handle cases that aren't as simple as grabbing the text content
|
|
|
|
switch (localName) {
|
|
|
|
case "type":
|
|
|
|
// Map AMO's type id to corresponding string
|
|
|
|
let id = parseInt(node.getAttribute("id"));
|
|
|
|
switch (id) {
|
|
|
|
case 1:
|
|
|
|
addon.type = "extension";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
addon.type = "theme";
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-24 01:04:06 +00:00
|
|
|
WARN("Unknown type id when parsing addon: " + id);
|
2010-08-26 17:44:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "authors":
|
|
|
|
let authorNodes = node.getElementsByTagName("author");
|
|
|
|
Array.forEach(authorNodes, function(aAuthorNode) {
|
|
|
|
let name = self._getDescendantTextContent(aAuthorNode, "name");
|
|
|
|
let link = self._getDescendantTextContent(aAuthorNode, "link");
|
|
|
|
if (name == null || link == null)
|
2010-06-17 22:47:51 +00:00
|
|
|
return;
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
let author = new AddonManagerPrivate.AddonAuthor(name, link);
|
2010-08-26 17:44:14 +00:00
|
|
|
if (addon.creator == null)
|
|
|
|
addon.creator = author;
|
2010-08-24 01:04:06 +00:00
|
|
|
else {
|
|
|
|
if (addon.developers == null)
|
|
|
|
addon.developers = [];
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
addon.developers.push(author);
|
2010-08-24 01:04:06 +00:00
|
|
|
}
|
2010-08-26 17:44:14 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "previews":
|
|
|
|
let previewNodes = node.getElementsByTagName("preview");
|
|
|
|
Array.forEach(previewNodes, function(aPreviewNode) {
|
2011-09-05 10:26:06 +00:00
|
|
|
let full = self._getUniqueDescendant(aPreviewNode, "full");
|
2010-08-26 17:44:14 +00:00
|
|
|
if (full == null)
|
|
|
|
return;
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
let fullURL = self._getTextContent(full);
|
|
|
|
let fullWidth = full.getAttribute("width");
|
|
|
|
let fullHeight = full.getAttribute("height");
|
|
|
|
|
|
|
|
let thumbnailURL, thumbnailWidth, thumbnailHeight;
|
|
|
|
let thumbnail = self._getUniqueDescendant(aPreviewNode, "thumbnail");
|
|
|
|
if (thumbnail) {
|
|
|
|
thumbnailURL = self._getTextContent(thumbnail);
|
|
|
|
thumbnailWidth = thumbnail.getAttribute("width");
|
|
|
|
thumbnailHeight = thumbnail.getAttribute("height");
|
|
|
|
}
|
2010-08-26 17:44:14 +00:00
|
|
|
let caption = self._getDescendantTextContent(aPreviewNode, "caption");
|
2011-09-05 10:26:06 +00:00
|
|
|
let screenshot = new AddonManagerPrivate.AddonScreenshot(fullURL, fullWidth, fullHeight,
|
|
|
|
thumbnailURL, thumbnailWidth,
|
|
|
|
thumbnailHeight, caption);
|
2010-08-24 01:04:06 +00:00
|
|
|
|
|
|
|
if (addon.screenshots == null)
|
|
|
|
addon.screenshots = [];
|
2010-08-26 17:44:14 +00:00
|
|
|
|
|
|
|
if (aPreviewNode.getAttribute("primary") == 1)
|
|
|
|
addon.screenshots.unshift(screenshot);
|
|
|
|
else
|
|
|
|
addon.screenshots.push(screenshot);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "learnmore":
|
|
|
|
addon.homepageURL = addon.homepageURL || this._getTextContent(node);
|
|
|
|
break;
|
|
|
|
case "contribution_data":
|
|
|
|
let meetDevelopers = this._getDescendantTextContent(node, "meet_developers");
|
|
|
|
let suggestedAmount = this._getDescendantTextContent(node, "suggested_amount");
|
2011-01-07 17:17:03 +00:00
|
|
|
if (meetDevelopers != null) {
|
2010-08-26 17:44:14 +00:00
|
|
|
addon.contributionURL = meetDevelopers;
|
|
|
|
addon.contributionAmount = suggestedAmount;
|
|
|
|
}
|
|
|
|
break
|
2011-01-07 17:09:09 +00:00
|
|
|
case "payment_data":
|
|
|
|
let link = this._getDescendantTextContent(node, "link");
|
|
|
|
let amountTag = this._getUniqueDescendant(node, "amount");
|
|
|
|
let amount = parseFloat(amountTag.getAttribute("amount"));
|
|
|
|
let displayAmount = this._getTextContent(amountTag);
|
|
|
|
if (link != null && amount != null && displayAmount != null) {
|
|
|
|
addon.purchaseURL = link;
|
|
|
|
addon.purchaseAmount = amount;
|
|
|
|
addon.purchaseDisplayAmount = displayAmount;
|
|
|
|
}
|
|
|
|
break
|
2010-08-26 17:44:14 +00:00
|
|
|
case "rating":
|
|
|
|
let averageRating = parseInt(this._getTextContent(node));
|
|
|
|
if (averageRating >= 0)
|
|
|
|
addon.averageRating = Math.min(5, averageRating);
|
|
|
|
break;
|
|
|
|
case "reviews":
|
|
|
|
let url = this._getTextContent(node);
|
|
|
|
let num = parseInt(node.getAttribute("num"));
|
|
|
|
if (url != null && num >= 0) {
|
|
|
|
addon.reviewURL = url;
|
|
|
|
addon.reviewCount = num;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "status":
|
|
|
|
let repositoryStatus = parseInt(node.getAttribute("id"));
|
|
|
|
if (!isNaN(repositoryStatus))
|
|
|
|
addon.repositoryStatus = repositoryStatus;
|
|
|
|
break;
|
2011-01-07 17:09:09 +00:00
|
|
|
case "all_compatible_os":
|
|
|
|
let nodes = node.getElementsByTagName("os");
|
|
|
|
addon.isPlatformCompatible = Array.some(nodes, function(aNode) {
|
|
|
|
let text = aNode.textContent.toLowerCase().trim();
|
|
|
|
return text == "all" || text == Services.appinfo.OS.toLowerCase();
|
|
|
|
});
|
|
|
|
break;
|
2010-08-26 17:44:14 +00:00
|
|
|
case "install":
|
|
|
|
// No os attribute means the xpi is compatible with any os
|
|
|
|
if (node.hasAttribute("os")) {
|
|
|
|
let os = node.getAttribute("os").trim().toLowerCase();
|
|
|
|
// If the os is not ALL and not the current OS then ignore this xpi
|
|
|
|
if (os != "all" && os != Services.appinfo.OS.toLowerCase())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let xpiURL = this._getTextContent(node);
|
|
|
|
if (xpiURL == null)
|
2008-01-28 17:11:48 +00:00
|
|
|
break;
|
2010-08-26 17:44:14 +00:00
|
|
|
|
|
|
|
if (skipSourceURIs.indexOf(xpiURL) != -1)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
result.xpiURL = xpiURL;
|
|
|
|
addon.sourceURI = NetUtil.newURI(xpiURL);
|
|
|
|
|
|
|
|
let size = parseInt(node.getAttribute("size"));
|
|
|
|
addon.size = (size >= 0) ? size : null;
|
|
|
|
|
|
|
|
let xpiHash = node.getAttribute("hash");
|
|
|
|
if (xpiHash != null)
|
|
|
|
xpiHash = xpiHash.trim();
|
|
|
|
result.xpiHash = xpiHash ? xpiHash : null;
|
|
|
|
break;
|
|
|
|
case "last_updated":
|
|
|
|
let epoch = parseInt(node.getAttribute("epoch"));
|
|
|
|
if (!isNaN(epoch))
|
|
|
|
addon.updateDate = new Date(1000 * epoch);
|
|
|
|
break;
|
2008-01-28 17:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
return result;
|
2010-06-17 22:47:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_parseAddons: function(aElements, aTotalResults, aSkip) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let self = this;
|
|
|
|
let results = [];
|
|
|
|
for (let i = 0; i < aElements.length && results.length < this._maxResults; i++) {
|
|
|
|
let element = aElements[i];
|
|
|
|
|
|
|
|
// Ignore add-ons not compatible with this Application
|
|
|
|
let tags = this._getUniqueDescendant(element, "compatible_applications");
|
|
|
|
if (tags == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let applications = tags.getElementsByTagName("appID");
|
|
|
|
let compatible = Array.some(applications, function(aAppNode) {
|
|
|
|
if (self._getTextContent(aAppNode) != Services.appinfo.ID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let parent = aAppNode.parentNode;
|
|
|
|
let minVersion = self._getDescendantTextContent(parent, "min_version");
|
|
|
|
let maxVersion = self._getDescendantTextContent(parent, "max_version");
|
|
|
|
if (minVersion == null || maxVersion == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let currentVersion = Services.appinfo.version;
|
|
|
|
return (Services.vc.compare(minVersion, currentVersion) <= 0 &&
|
|
|
|
Services.vc.compare(currentVersion, maxVersion) <= 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!compatible)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add-on meets all requirements, so parse out data
|
|
|
|
let result = this._parseAddon(element, aSkip);
|
|
|
|
if (result == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore add-on missing a required attribute
|
|
|
|
let requiredAttributes = ["id", "name", "version", "type", "creator"];
|
|
|
|
if (requiredAttributes.some(function(aAttribute) !result.addon[aAttribute]))
|
|
|
|
continue;
|
|
|
|
|
2011-01-07 17:09:09 +00:00
|
|
|
// Add only if the add-on is compatible with the platform
|
|
|
|
if (!result.addon.isPlatformCompatible)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add only if there was an xpi compatible with this OS or there was a
|
|
|
|
// way to purchase the add-on
|
|
|
|
if (!result.xpiURL && !result.addon.purchaseURL)
|
2010-08-26 17:44:14 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
results.push(result);
|
|
|
|
// Ignore this add-on from now on by adding it to the skip array
|
|
|
|
aSkip.ids.push(result.addon.id);
|
|
|
|
}
|
2010-06-17 22:47:51 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Immediately report success if no AddonInstall instances to create
|
|
|
|
let pendingResults = results.length;
|
2010-06-17 22:47:51 +00:00
|
|
|
if (pendingResults == 0) {
|
2010-08-26 17:44:14 +00:00
|
|
|
this._reportSuccess(results, aTotalResults);
|
2010-06-17 22:47:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Create an AddonInstall for each result
|
|
|
|
let self = this;
|
|
|
|
results.forEach(function(aResult) {
|
|
|
|
let addon = aResult.addon;
|
|
|
|
let callback = function(aInstall) {
|
2010-06-17 22:47:51 +00:00
|
|
|
addon.install = aInstall;
|
|
|
|
pendingResults--;
|
|
|
|
if (pendingResults == 0)
|
2010-08-26 17:44:14 +00:00
|
|
|
self._reportSuccess(results, aTotalResults);
|
2010-06-17 22:47:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 17:09:09 +00:00
|
|
|
if (aResult.xpiURL) {
|
|
|
|
AddonManager.getInstallForURL(aResult.xpiURL, callback,
|
|
|
|
"application/x-xpinstall", aResult.xpiHash,
|
|
|
|
addon.name, addon.iconURL, addon.version);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2010-06-17 22:47:51 +00:00
|
|
|
});
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Begins a new search if one isn't currently executing
|
|
|
|
_beginSearch: function(aURI, aMaxResults, aCallback, aHandleResults) {
|
|
|
|
if (this._searching || aURI == null || aMaxResults <= 0) {
|
|
|
|
aCallback.searchFailed();
|
2008-01-28 17:11:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-29 17:41:24 +00:00
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
this._searching = true;
|
|
|
|
this._callback = aCallback;
|
|
|
|
this._maxResults = aMaxResults;
|
2010-06-17 22:47:51 +00:00
|
|
|
|
2011-01-20 21:45:10 +00:00
|
|
|
LOG("Requesting " + aURI);
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
|
|
|
createInstance(Ci.nsIXMLHttpRequest);
|
2011-02-25 04:16:25 +00:00
|
|
|
this._request.mozBackgroundRequest = true;
|
2010-08-26 17:44:14 +00:00
|
|
|
this._request.open("GET", aURI, true);
|
|
|
|
this._request.overrideMimeType("text/xml");
|
|
|
|
|
|
|
|
let self = this;
|
2011-09-29 16:06:36 +00:00
|
|
|
this._request.addEventListener("error", function(aEvent) {
|
|
|
|
self._reportFailure();
|
|
|
|
}, false);
|
|
|
|
this._request.addEventListener("load", function(aEvent) {
|
2010-08-26 17:44:14 +00:00
|
|
|
let request = aEvent.target;
|
|
|
|
let responseXML = request.responseXML;
|
|
|
|
|
|
|
|
if (!responseXML || responseXML.documentElement.namespaceURI == XMLURI_PARSE_ERROR ||
|
|
|
|
(request.status != 200 && request.status != 0)) {
|
|
|
|
self._reportFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let documentElement = responseXML.documentElement;
|
|
|
|
let elements = documentElement.getElementsByTagName("addon");
|
|
|
|
let totalResults = elements.length;
|
|
|
|
let parsedTotalResults = parseInt(documentElement.getAttribute("total_results"));
|
|
|
|
// Parsed value of total results only makes sense if >= elements.length
|
|
|
|
if (parsedTotalResults >= totalResults)
|
|
|
|
totalResults = parsedTotalResults;
|
|
|
|
|
|
|
|
aHandleResults(elements, totalResults);
|
2011-09-29 16:06:36 +00:00
|
|
|
}, false);
|
2010-08-26 17:44:14 +00:00
|
|
|
this._request.send(null);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Gets the id's of local add-ons, and the sourceURI's of local installs,
|
|
|
|
// passing the results to aCallback
|
|
|
|
_getLocalAddonIds: function(aCallback) {
|
|
|
|
let self = this;
|
|
|
|
let localAddonIds = {ids: null, sourceURIs: null};
|
2008-03-07 11:16:48 +00:00
|
|
|
|
2010-06-17 22:47:51 +00:00
|
|
|
AddonManager.getAllAddons(function(aAddons) {
|
2010-08-26 17:44:14 +00:00
|
|
|
localAddonIds.ids = [a.id for each (a in aAddons)];
|
|
|
|
if (localAddonIds.sourceURIs)
|
|
|
|
aCallback(localAddonIds);
|
2010-06-17 22:47:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
AddonManager.getAllInstalls(function(aInstalls) {
|
2010-08-26 17:44:14 +00:00
|
|
|
localAddonIds.sourceURIs = [];
|
2010-06-17 22:47:51 +00:00
|
|
|
aInstalls.forEach(function(aInstall) {
|
|
|
|
if (aInstall.state != AddonManager.STATE_AVAILABLE)
|
2010-08-26 17:44:14 +00:00
|
|
|
localAddonIds.sourceURIs.push(aInstall.sourceURI.spec);
|
2010-06-17 22:47:51 +00:00
|
|
|
});
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
if (localAddonIds.ids)
|
|
|
|
aCallback(localAddonIds);
|
2010-04-29 17:41:24 +00:00
|
|
|
});
|
2008-01-28 17:11:48 +00:00
|
|
|
},
|
|
|
|
|
2010-08-26 17:44:14 +00:00
|
|
|
// Create url from preference, returning null if preference does not exist
|
|
|
|
_formatURLPref: function(aPreference, aSubstitutions) {
|
|
|
|
let url = null;
|
2010-07-19 23:01:23 +00:00
|
|
|
try {
|
2010-08-26 17:44:14 +00:00
|
|
|
url = Services.prefs.getCharPref(aPreference);
|
2010-07-19 23:01:23 +00:00
|
|
|
} catch(e) {
|
2010-08-24 01:04:06 +00:00
|
|
|
WARN("_formatURLPref: Couldn't get pref: " + aPreference);
|
2010-07-19 23:01:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = url.replace(/%([A-Z_]+)%/g, function(aMatch, aKey) {
|
|
|
|
return (aKey in aSubstitutions) ? aSubstitutions[aKey] : aMatch;
|
|
|
|
});
|
2010-08-26 17:44:14 +00:00
|
|
|
|
2010-07-19 23:01:23 +00:00
|
|
|
return Services.urlFormatter.formatURL(url);
|
2010-06-17 22:47:51 +00:00
|
|
|
}
|
2010-08-24 01:04:06 +00:00
|
|
|
};
|
|
|
|
AddonRepository.initialize();
|
|
|
|
|
|
|
|
var AddonDatabase = {
|
|
|
|
// true if the database connection has been opened
|
|
|
|
initialized: false,
|
|
|
|
// false if there was an unrecoverable error openning the database
|
|
|
|
databaseOk: true,
|
|
|
|
// A cache of statements that are used and need to be finalized on shutdown
|
|
|
|
statementCache: {},
|
|
|
|
|
|
|
|
// The statements used by the database
|
|
|
|
statements: {
|
|
|
|
getAllAddons: "SELECT internal_id, id, type, name, version, " +
|
|
|
|
"creator, creatorURL, description, fullDescription, " +
|
|
|
|
"developerComments, eula, iconURL, homepageURL, supportURL, " +
|
|
|
|
"contributionURL, contributionAmount, averageRating, " +
|
|
|
|
"reviewCount, reviewURL, totalDownloads, weeklyDownloads, " +
|
|
|
|
"dailyUsers, sourceURI, repositoryStatus, size, updateDate " +
|
|
|
|
"FROM addon",
|
|
|
|
|
|
|
|
getAllDevelopers: "SELECT addon_internal_id, name, url FROM developer " +
|
|
|
|
"ORDER BY addon_internal_id, num",
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
getAllScreenshots: "SELECT addon_internal_id, url, width, height, " +
|
|
|
|
"thumbnailURL, thumbnailWidth, thumbnailHeight, caption " +
|
2010-08-24 01:04:06 +00:00
|
|
|
"FROM screenshot ORDER BY addon_internal_id, num",
|
|
|
|
|
|
|
|
insertAddon: "INSERT INTO addon VALUES (NULL, :id, :type, :name, :version, " +
|
|
|
|
":creator, :creatorURL, :description, :fullDescription, " +
|
|
|
|
":developerComments, :eula, :iconURL, :homepageURL, :supportURL, " +
|
|
|
|
":contributionURL, :contributionAmount, :averageRating, " +
|
|
|
|
":reviewCount, :reviewURL, :totalDownloads, :weeklyDownloads, " +
|
|
|
|
":dailyUsers, :sourceURI, :repositoryStatus, :size, :updateDate)",
|
|
|
|
|
|
|
|
insertDeveloper: "INSERT INTO developer VALUES (:addon_internal_id, " +
|
|
|
|
":num, :name, :url)",
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
// We specify column names here because the columns
|
|
|
|
// could be out of order due to schema changes.
|
|
|
|
insertScreenshot: "INSERT INTO screenshot (addon_internal_id, " +
|
|
|
|
"num, url, width, height, thumbnailURL, " +
|
|
|
|
"thumbnailWidth, thumbnailHeight, caption) " +
|
|
|
|
"VALUES (:addon_internal_id, " +
|
|
|
|
":num, :url, :width, :height, :thumbnailURL, " +
|
|
|
|
":thumbnailWidth, :thumbnailHeight, :caption)",
|
2010-08-24 01:04:06 +00:00
|
|
|
|
|
|
|
emptyAddon: "DELETE FROM addon"
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to log an SQL error.
|
|
|
|
*
|
|
|
|
* @param aError
|
|
|
|
* The storage error code associated with the error
|
|
|
|
* @param aErrorString
|
|
|
|
* An error message
|
|
|
|
*/
|
|
|
|
logSQLError: function AD_logSQLError(aError, aErrorString) {
|
|
|
|
ERROR("SQL error " + aError + ": " + aErrorString);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to log any errors that occur during async statements.
|
|
|
|
*
|
|
|
|
* @param aError
|
|
|
|
* A mozIStorageError to log
|
|
|
|
*/
|
|
|
|
asyncErrorLogger: function AD_asyncErrorLogger(aError) {
|
|
|
|
ERROR("Async SQL error " + aError.result + ": " + aError.message);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously opens a new connection to the database file.
|
|
|
|
*
|
|
|
|
* @param aSecondAttempt
|
|
|
|
* Whether this is a second attempt to open the database
|
|
|
|
* @return the mozIStorageConnection for the database
|
|
|
|
*/
|
|
|
|
openConnection: function AD_openConnection(aSecondAttempt) {
|
|
|
|
this.initialized = true;
|
|
|
|
delete this.connection;
|
|
|
|
|
|
|
|
let dbfile = FileUtils.getFile(KEY_PROFILEDIR, [FILE_DATABASE], true);
|
|
|
|
let dbMissing = !dbfile.exists();
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
function tryAgain() {
|
|
|
|
LOG("Deleting database, and attempting openConnection again");
|
|
|
|
this.initialized = false;
|
|
|
|
if (this.connection.connectionReady)
|
|
|
|
this.connection.close();
|
|
|
|
if (dbfile.exists())
|
|
|
|
dbfile.remove(false);
|
|
|
|
return this.openConnection(true);
|
|
|
|
}
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
try {
|
|
|
|
this.connection = Services.storage.openUnsharedDatabase(dbfile);
|
|
|
|
} catch (e) {
|
|
|
|
this.initialized = false;
|
2010-11-11 18:48:06 +00:00
|
|
|
ERROR("Failed to open database", e);
|
2010-08-24 01:04:06 +00:00
|
|
|
if (aSecondAttempt || dbMissing) {
|
|
|
|
this.databaseOk = false;
|
|
|
|
throw e;
|
|
|
|
}
|
2011-09-05 10:26:06 +00:00
|
|
|
return tryAgain();
|
2010-08-24 01:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.connection.executeSimpleSQL("PRAGMA locking_mode = EXCLUSIVE");
|
2011-09-05 10:26:06 +00:00
|
|
|
if (dbMissing)
|
2010-08-24 01:04:06 +00:00
|
|
|
this._createSchema();
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
switch (this.connection.schemaVersion) {
|
|
|
|
case 0:
|
|
|
|
this._createSchema();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
try {
|
|
|
|
this.connection.executeSimpleSQL("ALTER TABLE screenshot ADD COLUMN width INTEGER");
|
|
|
|
this.connection.executeSimpleSQL("ALTER TABLE screenshot ADD COLUMN height INTEGER");
|
|
|
|
this.connection.executeSimpleSQL("ALTER TABLE screenshot ADD COLUMN thumbnailWidth INTEGER");
|
|
|
|
this.connection.executeSimpleSQL("ALTER TABLE screenshot ADD COLUMN thumbnailHeight INTEGER");
|
|
|
|
this._createIndices();
|
|
|
|
this.connection.schemaVersion = DB_SCHEMA;
|
|
|
|
} catch (e) {
|
|
|
|
ERROR("Failed to create database schema", e);
|
|
|
|
this.logSQLError(this.connection.lastError, this.connection.lastErrorString);
|
|
|
|
this.connection.rollbackTransaction();
|
|
|
|
return tryAgain();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return tryAgain();
|
|
|
|
}
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
return this.connection;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lazy getter for the database connection.
|
|
|
|
*/
|
|
|
|
get connection() {
|
|
|
|
return this.openConnection();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously shuts down the database connection and releases all
|
|
|
|
* cached objects
|
|
|
|
*
|
|
|
|
* @param aCallback
|
|
|
|
* An optional callback to call once complete
|
|
|
|
*/
|
|
|
|
shutdown: function AD_shutdown(aCallback) {
|
|
|
|
this.databaseOk = true;
|
|
|
|
if (!this.initialized) {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initialized = false;
|
|
|
|
|
|
|
|
for each (let stmt in this.statementCache)
|
|
|
|
stmt.finalize();
|
|
|
|
this.statementCache = {};
|
|
|
|
|
|
|
|
if (this.connection.transactionInProgress) {
|
|
|
|
ERROR("Outstanding transaction, rolling back.");
|
|
|
|
this.connection.rollbackTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
let connection = this.connection;
|
|
|
|
delete this.connection;
|
2008-01-28 17:11:48 +00:00
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
// Re-create the connection smart getter to allow the database to be
|
|
|
|
// re-loaded during testing.
|
|
|
|
this.__defineGetter__("connection", function() {
|
|
|
|
return this.openConnection();
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.asyncClose(aCallback);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously deletes the database, shutting down the connection
|
|
|
|
* first if initialized
|
|
|
|
*
|
|
|
|
* @param aCallback
|
|
|
|
* An optional callback to call once complete
|
|
|
|
*/
|
|
|
|
delete: function AD_delete(aCallback) {
|
|
|
|
this.shutdown(function() {
|
|
|
|
let dbfile = FileUtils.getFile(KEY_PROFILEDIR, [FILE_DATABASE], true);
|
|
|
|
if (dbfile.exists())
|
|
|
|
dbfile.remove(false);
|
|
|
|
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a cached statement or creates a new statement if it doesn't already
|
|
|
|
* exist.
|
|
|
|
*
|
|
|
|
* @param aKey
|
|
|
|
* A unique key to reference the statement
|
|
|
|
* @return a mozIStorageStatement for the SQL corresponding to the unique key
|
|
|
|
*/
|
|
|
|
getStatement: function AD_getStatement(aKey) {
|
|
|
|
if (aKey in this.statementCache)
|
|
|
|
return this.statementCache[aKey];
|
|
|
|
|
|
|
|
let sql = this.statements[aKey];
|
|
|
|
try {
|
|
|
|
return this.statementCache[aKey] = this.connection.createStatement(sql);
|
|
|
|
} catch (e) {
|
2011-09-07 04:25:23 +00:00
|
|
|
ERROR("Error creating statement " + aKey + " (" + sql + ")");
|
2010-08-24 01:04:06 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously retrieve all add-ons from the database, and pass it
|
|
|
|
* to the specified callback
|
|
|
|
*
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to pass the add-ons back to
|
|
|
|
*/
|
|
|
|
retrieveStoredData: function AD_retrieveStoredData(aCallback) {
|
|
|
|
let self = this;
|
|
|
|
let addons = {};
|
|
|
|
|
|
|
|
// Retrieve all data from the addon table
|
|
|
|
function getAllAddons() {
|
|
|
|
self.getStatement("getAllAddons").executeAsync({
|
|
|
|
handleResult: function(aResults) {
|
|
|
|
let row = null;
|
|
|
|
while (row = aResults.getNextRow()) {
|
|
|
|
let internal_id = row.getResultByName("internal_id");
|
|
|
|
addons[internal_id] = self._makeAddonFromAsyncRow(row);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
ERROR("Error retrieving add-ons from database. Returning empty results");
|
|
|
|
aCallback({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllDevelopers();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all data from the developer table
|
|
|
|
function getAllDevelopers() {
|
|
|
|
self.getStatement("getAllDevelopers").executeAsync({
|
|
|
|
handleResult: function(aResults) {
|
|
|
|
let row = null;
|
|
|
|
while (row = aResults.getNextRow()) {
|
|
|
|
let addon_internal_id = row.getResultByName("addon_internal_id");
|
|
|
|
if (!(addon_internal_id in addons)) {
|
|
|
|
WARN("Found a developer not linked to an add-on in database");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let addon = addons[addon_internal_id];
|
|
|
|
if (!addon.developers)
|
|
|
|
addon.developers = [];
|
|
|
|
|
|
|
|
addon.developers.push(self._makeDeveloperFromAsyncRow(row));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
ERROR("Error retrieving developers from database. Returning empty results");
|
|
|
|
aCallback({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllScreenshots();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all data from the screenshot table
|
|
|
|
function getAllScreenshots() {
|
|
|
|
self.getStatement("getAllScreenshots").executeAsync({
|
|
|
|
handleResult: function(aResults) {
|
|
|
|
let row = null;
|
|
|
|
while (row = aResults.getNextRow()) {
|
|
|
|
let addon_internal_id = row.getResultByName("addon_internal_id");
|
|
|
|
if (!(addon_internal_id in addons)) {
|
|
|
|
WARN("Found a screenshot not linked to an add-on in database");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let addon = addons[addon_internal_id];
|
|
|
|
if (!addon.screenshots)
|
|
|
|
addon.screenshots = [];
|
|
|
|
addon.screenshots.push(self._makeScreenshotFromAsyncRow(row));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
ERROR("Error retrieving screenshots from database. Returning empty results");
|
|
|
|
aCallback({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let returnedAddons = {};
|
2011-07-19 20:16:35 +00:00
|
|
|
for each (let addon in addons)
|
2010-08-24 01:04:06 +00:00
|
|
|
returnedAddons[addon.id] = addon;
|
|
|
|
aCallback(returnedAddons);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin asynchronous process
|
|
|
|
getAllAddons();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously repopulates the database so it only contains the
|
|
|
|
* specified add-ons
|
|
|
|
*
|
|
|
|
* @param aAddons
|
|
|
|
* The array of add-ons to repopulate the database with
|
|
|
|
* @param aCallback
|
|
|
|
* An optional callback to call once complete
|
|
|
|
*/
|
|
|
|
repopulate: function AD_repopulate(aAddons, aCallback) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Completely empty the database
|
|
|
|
let stmts = [this.getStatement("emptyAddon")];
|
|
|
|
|
|
|
|
this.connection.executeAsync(stmts, stmts.length, {
|
|
|
|
handleResult: function() {},
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED)
|
|
|
|
ERROR("Error emptying database. Attempting to continue repopulating database");
|
|
|
|
|
|
|
|
// Insert the specified add-ons
|
|
|
|
self.insertAddons(aAddons, aCallback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously inserts an array of add-ons into the database
|
|
|
|
*
|
|
|
|
* @param aAddons
|
|
|
|
* The array of add-ons to insert
|
|
|
|
* @param aCallback
|
|
|
|
* An optional callback to call once complete
|
|
|
|
*/
|
|
|
|
insertAddons: function AD_insertAddons(aAddons, aCallback) {
|
|
|
|
let self = this;
|
|
|
|
let currentAddon = -1;
|
|
|
|
|
|
|
|
// Chain insertions
|
|
|
|
function insertNextAddon() {
|
|
|
|
if (++currentAddon == aAddons.length) {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._insertAddon(aAddons[currentAddon], insertNextAddon);
|
|
|
|
}
|
|
|
|
|
|
|
|
insertNextAddon();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts an individual add-on into the database. If the add-on already
|
|
|
|
* exists in the database (by id), then the specified add-on will not be
|
|
|
|
* inserted.
|
|
|
|
*
|
|
|
|
* @param aAddon
|
|
|
|
* The add-on to insert into the database
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to call once complete
|
|
|
|
*/
|
|
|
|
_insertAddon: function AD__insertAddon(aAddon, aCallback) {
|
|
|
|
let self = this;
|
|
|
|
let internal_id = null;
|
|
|
|
this.connection.beginTransaction();
|
|
|
|
|
|
|
|
// Simultaneously insert the developers and screenshots of the add-on
|
|
|
|
function insertDevelopersAndScreenshots() {
|
|
|
|
let stmts = [];
|
|
|
|
|
|
|
|
// Initialize statement and parameters for inserting an array
|
|
|
|
function initializeArrayInsert(aStatementKey, aArray, aAddParams) {
|
|
|
|
if (!aArray || aArray.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let stmt = self.getStatement(aStatementKey);
|
|
|
|
let params = stmt.newBindingParamsArray();
|
|
|
|
aArray.forEach(function(aElement, aIndex) {
|
|
|
|
aAddParams(params, internal_id, aElement, aIndex);
|
|
|
|
});
|
|
|
|
|
|
|
|
stmt.bindParameters(params);
|
|
|
|
stmts.push(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize statements to insert developers and screenshots
|
|
|
|
initializeArrayInsert("insertDeveloper", aAddon.developers,
|
|
|
|
self._addDeveloperParams);
|
|
|
|
initializeArrayInsert("insertScreenshot", aAddon.screenshots,
|
|
|
|
self._addScreenshotParams);
|
|
|
|
|
|
|
|
// Immediately call callback if nothing to insert
|
|
|
|
if (stmts.length == 0) {
|
|
|
|
self.connection.commitTransaction();
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.connection.executeAsync(stmts, stmts.length, {
|
|
|
|
handleResult: function() {},
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
ERROR("Error inserting developers and screenshots into database. Attempting to continue");
|
|
|
|
self.connection.rollbackTransaction();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.connection.commitTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert add-on into database
|
|
|
|
this._makeAddonStatement(aAddon).executeAsync({
|
|
|
|
handleResult: function() {},
|
|
|
|
handleError: self.asyncErrorLogger,
|
|
|
|
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
if (aReason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
ERROR("Error inserting add-ons into database. Attempting to continue.");
|
|
|
|
self.connection.rollbackTransaction();
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_id = self.connection.lastInsertRowID;
|
|
|
|
insertDevelopersAndScreenshots();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an asynchronous statement that will insert the specified add-on
|
|
|
|
*
|
|
|
|
* @param aAddon
|
|
|
|
* The add-on to make the statement for
|
|
|
|
* @return The asynchronous mozIStorageStatement
|
|
|
|
*/
|
|
|
|
_makeAddonStatement: function AD__makeAddonStatement(aAddon) {
|
|
|
|
let stmt = this.getStatement("insertAddon");
|
|
|
|
let params = stmt.params;
|
|
|
|
|
|
|
|
PROP_SINGLE.forEach(function(aProperty) {
|
|
|
|
switch (aProperty) {
|
|
|
|
case "sourceURI":
|
|
|
|
params.sourceURI = aAddon.sourceURI ? aAddon.sourceURI.spec : null;
|
|
|
|
break;
|
|
|
|
case "creator":
|
|
|
|
params.creator = aAddon.creator ? aAddon.creator.name : null;
|
|
|
|
params.creatorURL = aAddon.creator ? aAddon.creator.url : null;
|
|
|
|
break;
|
|
|
|
case "updateDate":
|
|
|
|
params.updateDate = aAddon.updateDate ? aAddon.updateDate.getTime() : null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
params[aProperty] = aAddon[aProperty];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return stmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add developer parameters to the specified mozIStorageBindingParamsArray
|
|
|
|
*
|
|
|
|
* @param aParams
|
|
|
|
* The mozIStorageBindingParamsArray to add the parameters to
|
|
|
|
* @param aInternalID
|
|
|
|
* The internal_id of the add-on that this developer is for
|
|
|
|
* @param aDeveloper
|
|
|
|
* The developer to make the parameters from
|
|
|
|
* @param aIndex
|
|
|
|
* The index of this developer
|
|
|
|
* @return The asynchronous mozIStorageStatement
|
|
|
|
*/
|
|
|
|
_addDeveloperParams: function AD__addDeveloperParams(aParams, aInternalID,
|
|
|
|
aDeveloper, aIndex) {
|
|
|
|
let bp = aParams.newBindingParams();
|
|
|
|
bp.bindByName("addon_internal_id", aInternalID);
|
|
|
|
bp.bindByName("num", aIndex);
|
|
|
|
bp.bindByName("name", aDeveloper.name);
|
|
|
|
bp.bindByName("url", aDeveloper.url);
|
|
|
|
aParams.addParams(bp);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add screenshot parameters to the specified mozIStorageBindingParamsArray
|
|
|
|
*
|
|
|
|
* @param aParams
|
|
|
|
* The mozIStorageBindingParamsArray to add the parameters to
|
|
|
|
* @param aInternalID
|
|
|
|
* The internal_id of the add-on that this screenshot is for
|
|
|
|
* @param aScreenshot
|
|
|
|
* The screenshot to make the parameters from
|
|
|
|
* @param aIndex
|
|
|
|
* The index of this screenshot
|
|
|
|
*/
|
|
|
|
_addScreenshotParams: function AD__addScreenshotParams(aParams, aInternalID,
|
|
|
|
aScreenshot, aIndex) {
|
|
|
|
let bp = aParams.newBindingParams();
|
|
|
|
bp.bindByName("addon_internal_id", aInternalID);
|
|
|
|
bp.bindByName("num", aIndex);
|
|
|
|
bp.bindByName("url", aScreenshot.url);
|
2011-09-05 10:26:06 +00:00
|
|
|
bp.bindByName("width", aScreenshot.width);
|
|
|
|
bp.bindByName("height", aScreenshot.height);
|
2010-08-24 01:04:06 +00:00
|
|
|
bp.bindByName("thumbnailURL", aScreenshot.thumbnailURL);
|
2011-09-05 10:26:06 +00:00
|
|
|
bp.bindByName("thumbnailWidth", aScreenshot.thumbnailWidth);
|
|
|
|
bp.bindByName("thumbnailHeight", aScreenshot.thumbnailHeight);
|
2010-08-24 01:04:06 +00:00
|
|
|
bp.bindByName("caption", aScreenshot.caption);
|
|
|
|
aParams.addParams(bp);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make add-on from an asynchronous row
|
|
|
|
* Note: This add-on will be lacking both developers and screenshots
|
|
|
|
*
|
|
|
|
* @param aRow
|
|
|
|
* The asynchronous row to use
|
|
|
|
* @return The created add-on
|
|
|
|
*/
|
|
|
|
_makeAddonFromAsyncRow: function AD__makeAddonFromAsyncRow(aRow) {
|
|
|
|
let addon = {};
|
|
|
|
|
|
|
|
PROP_SINGLE.forEach(function(aProperty) {
|
|
|
|
let value = aRow.getResultByName(aProperty);
|
|
|
|
|
|
|
|
switch (aProperty) {
|
|
|
|
case "sourceURI":
|
|
|
|
addon.sourceURI = value ? NetUtil.newURI(value) : null;
|
|
|
|
break;
|
|
|
|
case "creator":
|
|
|
|
let creatorURL = aRow.getResultByName("creatorURL");
|
|
|
|
if (value || creatorURL)
|
|
|
|
addon.creator = new AddonManagerPrivate.AddonAuthor(value, creatorURL);
|
|
|
|
else
|
|
|
|
addon.creator = null;
|
|
|
|
break;
|
|
|
|
case "updateDate":
|
|
|
|
addon.updateDate = value ? new Date(value) : null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
addon[aProperty] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return addon;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a developer from an asynchronous row
|
|
|
|
*
|
|
|
|
* @param aRow
|
|
|
|
* The asynchronous row to use
|
|
|
|
* @return The created developer
|
|
|
|
*/
|
|
|
|
_makeDeveloperFromAsyncRow: function AD__makeDeveloperFromAsyncRow(aRow) {
|
|
|
|
let name = aRow.getResultByName("name");
|
|
|
|
let url = aRow.getResultByName("url")
|
|
|
|
return new AddonManagerPrivate.AddonAuthor(name, url);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a screenshot from an asynchronous row
|
|
|
|
*
|
|
|
|
* @param aRow
|
|
|
|
* The asynchronous row to use
|
|
|
|
* @return The created screenshot
|
|
|
|
*/
|
|
|
|
_makeScreenshotFromAsyncRow: function AD__makeScreenshotFromAsyncRow(aRow) {
|
|
|
|
let url = aRow.getResultByName("url");
|
2011-09-05 10:26:06 +00:00
|
|
|
let width = aRow.getResultByName("width");
|
|
|
|
let height = aRow.getResultByName("height");
|
2010-08-24 01:04:06 +00:00
|
|
|
let thumbnailURL = aRow.getResultByName("thumbnailURL");
|
2011-09-05 10:26:06 +00:00
|
|
|
let thumbnailWidth = aRow.getResultByName("thumbnailWidth");
|
|
|
|
let thumbnailHeight = aRow.getResultByName("thumbnailHeight");
|
|
|
|
let caption = aRow.getResultByName("caption");
|
|
|
|
return new AddonManagerPrivate.AddonScreenshot(url, width, height, thumbnailURL,
|
|
|
|
thumbnailWidth, thumbnailHeight, caption);
|
2010-08-24 01:04:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously creates the schema in the database.
|
|
|
|
*/
|
|
|
|
_createSchema: function AD__createSchema() {
|
|
|
|
LOG("Creating database schema");
|
|
|
|
this.connection.beginTransaction();
|
|
|
|
|
|
|
|
// Any errors in here should rollback
|
|
|
|
try {
|
|
|
|
this.connection.createTable("addon",
|
|
|
|
"internal_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
|
|
|
"id TEXT UNIQUE, " +
|
|
|
|
"type TEXT, " +
|
|
|
|
"name TEXT, " +
|
|
|
|
"version TEXT, " +
|
|
|
|
"creator TEXT, " +
|
|
|
|
"creatorURL TEXT, " +
|
|
|
|
"description TEXT, " +
|
|
|
|
"fullDescription TEXT, " +
|
|
|
|
"developerComments TEXT, " +
|
|
|
|
"eula TEXT, " +
|
|
|
|
"iconURL TEXT, " +
|
|
|
|
"homepageURL TEXT, " +
|
|
|
|
"supportURL TEXT, " +
|
|
|
|
"contributionURL TEXT, " +
|
|
|
|
"contributionAmount TEXT, " +
|
|
|
|
"averageRating INTEGER, " +
|
|
|
|
"reviewCount INTEGER, " +
|
|
|
|
"reviewURL TEXT, " +
|
|
|
|
"totalDownloads INTEGER, " +
|
|
|
|
"weeklyDownloads INTEGER, " +
|
|
|
|
"dailyUsers INTEGER, " +
|
|
|
|
"sourceURI TEXT, " +
|
|
|
|
"repositoryStatus INTEGER, " +
|
|
|
|
"size INTEGER, " +
|
|
|
|
"updateDate INTEGER");
|
|
|
|
|
|
|
|
this.connection.createTable("developer",
|
|
|
|
"addon_internal_id INTEGER, " +
|
|
|
|
"num INTEGER, " +
|
|
|
|
"name TEXT, " +
|
|
|
|
"url TEXT, " +
|
|
|
|
"PRIMARY KEY (addon_internal_id, num)");
|
|
|
|
|
|
|
|
this.connection.createTable("screenshot",
|
|
|
|
"addon_internal_id INTEGER, " +
|
|
|
|
"num INTEGER, " +
|
|
|
|
"url TEXT, " +
|
2011-09-05 10:26:06 +00:00
|
|
|
"width INTEGER, " +
|
|
|
|
"height INTEGER, " +
|
2010-08-24 01:04:06 +00:00
|
|
|
"thumbnailURL TEXT, " +
|
2011-09-05 10:26:06 +00:00
|
|
|
"thumbnailWidth INTEGER, " +
|
|
|
|
"thumbnailHeight INTEGER, " +
|
2010-08-24 01:04:06 +00:00
|
|
|
"caption TEXT, " +
|
|
|
|
"PRIMARY KEY (addon_internal_id, num)");
|
|
|
|
|
2011-09-05 10:26:06 +00:00
|
|
|
this._createIndices();
|
|
|
|
|
2010-08-24 01:04:06 +00:00
|
|
|
this.connection.executeSimpleSQL("CREATE TRIGGER delete_addon AFTER DELETE " +
|
|
|
|
"ON addon BEGIN " +
|
|
|
|
"DELETE FROM developer WHERE addon_internal_id=old.internal_id; " +
|
|
|
|
"DELETE FROM screenshot WHERE addon_internal_id=old.internal_id; " +
|
|
|
|
"END");
|
|
|
|
|
|
|
|
this.connection.schemaVersion = DB_SCHEMA;
|
|
|
|
this.connection.commitTransaction();
|
|
|
|
} catch (e) {
|
2010-11-11 18:48:06 +00:00
|
|
|
ERROR("Failed to create database schema", e);
|
2010-08-24 01:04:06 +00:00
|
|
|
this.logSQLError(this.connection.lastError, this.connection.lastErrorString);
|
|
|
|
this.connection.rollbackTransaction();
|
|
|
|
throw e;
|
|
|
|
}
|
2011-09-05 10:26:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously creates the indices in the database.
|
|
|
|
*/
|
|
|
|
_createIndices: function AD__createIndices() {
|
|
|
|
this.connection.executeSimpleSQL("CREATE INDEX IF NOT EXISTS developer_idx " +
|
|
|
|
"ON developer (addon_internal_id)");
|
|
|
|
this.connection.executeSimpleSQL("CREATE INDEX IF NOT EXISTS screenshot_idx " +
|
|
|
|
"ON screenshot (addon_internal_id)");
|
2010-08-24 01:04:06 +00:00
|
|
|
}
|
|
|
|
};
|