gecko-dev/mobile/android/modules/LocaleRepository.jsm
Kyle Huey 0469a02b25 Bug 798491: Add an option to stick all chrome JSMs/JS components in the same compartment. r=mrbkap,philikon
--HG--
extra : rebase_source : 98337b6a8c07d05e8c961a452dd05a7d75c3c60b
2012-10-31 09:13:28 -07:00

319 lines
10 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
this.EXPORTED_SYMBOLS = ["LocaleRepository"];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
// A map between XML keys to LocaleSearchResult keys for string values
// that require no extra parsing from XML
const STRING_KEY_MAP = {
name: "name",
target_locale: "targetLocale",
version: "version",
icon: "iconURL",
homepage: "homepageURL",
support: "supportURL",
strings: "strings"
};
this.LocaleRepository = {
loggingEnabled: false,
log: function(aMessage) {
if (this.loggingEnabled)
dump(aMessage + "\n");
},
_getUniqueDescendant: function _getUniqueDescendant(aElement, aTagName) {
let elementsList = aElement.getElementsByTagName(aTagName);
return (elementsList.length == 1) ? elementsList[0] : null;
},
_getTextContent: function _getTextContent(aElement) {
let textContent = aElement.textContent.trim();
return (textContent.length > 0) ? textContent : null;
},
_getDescendantTextContent: function _getDescendantTextContent(aElement, aTagName) {
let descendant = this._getUniqueDescendant(aElement, aTagName);
return (descendant != null) ? this._getTextContent(descendant) : null;
},
getLocales: function getLocales(aCallback, aFilters) {
let url = Services.prefs.getCharPref("extensions.getLocales.get.url");
if (!url) {
aCallback([]);
return;
}
let buildID = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).QueryInterface(Ci.nsIXULRuntime).appBuildID;
if (aFilters) {
if (aFilters.buildID)
buildID = aFilters.buildID;
}
buildID = buildID.substring(0,4) + "-" + buildID.substring(4).replace(/\d{2}(?=\d)/g, "$&-");
url = url.replace(/%BUILDID_EXPANDED%/g, buildID);
url = Services.urlFormatter.formatURL(url);
let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
request.mozBackgroundRequest = true;
request.open("GET", url, true);
request.overrideMimeType("text/xml");
let self = this;
request.addEventListener("readystatechange", function () {
if (request.readyState == 4) {
if (request.status == 200) {
self.log("---- got response")
let documentElement = request.responseXML.documentElement;
let elements = documentElement.getElementsByTagName("addon");
let totalResults = elements.length;
let parsedTotalResults = parseInt(documentElement.getAttribute("total_results"));
if (parsedTotalResults >= totalResults)
totalResults = parsedTotalResults;
// TODO: Create a real Skip object from installed locales
self._parseLocales(elements, totalResults, { ids: [], sourceURIs: [] }, aCallback);
} else {
Cu.reportError("Locale Repository: Error getting locale from AMO [" + request.status + "]");
}
}
}, false);
request.send(null);
},
_parseLocale: function _parseLocale(aElement, aSkip) {
let skipIDs = (aSkip && aSkip.ids) ? aSkip.ids : [];
let skipSourceURIs = (aSkip && aSkip.sourceURIs) ? aSkip.sourceURIs : [];
let guid = this._getDescendantTextContent(aElement, "guid");
if (guid == null || skipIDs.indexOf(guid) != -1)
return null;
let addon = new LocaleSearchResult(guid);
let result = {
addon: addon,
xpiURL: null,
xpiHash: null
};
let self = this;
for (let node = aElement.firstChild; node; node = node.nextSibling) {
if (!(node instanceof Ci.nsIDOMElement))
continue;
let localName = node.localName;
// Handle case where the wanted string value is located in text content
// but only if the content is not empty
if (localName in STRING_KEY_MAP) {
addon[STRING_KEY_MAP[localName]] = this._getTextContent(node) || addon[STRING_KEY_MAP[localName]];
continue;
}
// 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 5:
addon.type = "language";
break;
default:
this.log("Unknown type id when parsing addon: " + id);
}
break;
case "authors":
let authorNodes = node.getElementsByTagName("author");
Array.forEach(authorNodes, function(aAuthorNode) {
let name = self._getDescendantTextContent(aAuthorNode, "name");
if (name == null)
name = self._getTextContent(aAuthorNode);
let link = self._getDescendantTextContent(aAuthorNode, "link");
if (name == null && link == null)
return;
let author = { name: name, link: link };
if (addon.creator == null) {
addon.creator = author;
} else {
if (addon.developers == null)
addon.developers = [];
addon.developers.push(author);
}
});
break;
case "status":
let repositoryStatus = parseInt(node.getAttribute("id"));
if (!isNaN(repositoryStatus))
addon.repositoryStatus = repositoryStatus;
break;
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;
case "install":
// No os attribute means the xpi is compatible with any os
if (node.hasAttribute("os") && node.getAttribute("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)
break;
if (skipSourceURIs.indexOf(xpiURL) != -1)
return null;
result.xpiURL = xpiURL;
try {
addon.sourceURI = NetUtil.newURI(xpiURL);
} catch(ex) {
this.log("Addon has invalid uri: " + addon.sourceURI);
addon.sourceURI = null;
}
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;
}
}
return result;
},
_parseLocales: function _parseLocales(aElements, aTotalResults, aSkip, aCallback) {
let self = this;
let results = [];
for (let i = 0; i < aElements.length; 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._parseLocale(element, aSkip);
if (result == null)
continue;
// Ignore add-on missing a required attribute
let requiredAttributes = ["id", "name", "version", "type", "targetLocale", "sourceURI"];
if (requiredAttributes.some(function(aAttribute) !result.addon[aAttribute]))
continue;
// 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
if (!result.xpiURL)
continue;
results.push(result);
// Ignore this add-on from now on by adding it to the skip array
aSkip.ids.push(result.addon.id);
}
// Immediately report success if no AddonInstall instances to create
let pendingResults = results.length;
if (pendingResults == 0) {
aCallback([]);
return;
}
// Create an AddonInstall for each result
let self = this;
results.forEach(function(aResult) {
let addon = aResult.addon;
let callback = function(aInstall) {
aResult.addon.install = aInstall;
pendingResults--;
if (pendingResults == 0)
aCallback(results);
}
if (aResult.xpiURL) {
AddonManager.getInstallForURL(aResult.xpiURL, callback,
"application/x-xpinstall", aResult.xpiHash,
addon.name, addon.iconURL, addon.version);
} else {
callback(null);
}
});
}
};
function LocaleSearchResult(aId) {
this.id = aId;
}
LocaleSearchResult.prototype = {
id: null,
type: null,
targetLocale: null,
name: null,
addon: null,
version: null,
iconURL: null,
install: null,
sourceURI: null,
repositoryStatus: null,
size: null,
strings: "",
updateDate: null,
isCompatible: true,
isPlatformCompatible: true,
providesUpdatesSecurely: true,
blocklistState: Ci.nsIBlocklistService.STATE_NOT_BLOCKED,
appDisabled: false,
userDisabled: false,
scope: AddonManager.SCOPE_PROFILE,
isActive: true,
pendingOperations: AddonManager.PENDING_NONE,
permissions: 0
};