2012-04-17 07:17:23 -07:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
const DEFAULT_ICON_URL = "chrome://global/skin/icons/webapps-64.png";
|
|
|
|
|
2012-04-17 07:17:23 -07:00
|
|
|
/**
|
|
|
|
* This function receives a list of icon sizes
|
|
|
|
* and URLs and returns the url string for the biggest icon.
|
|
|
|
*
|
|
|
|
* @param aIcons An object where the keys are the icon sizes
|
|
|
|
* and the values are URL strings. E.g.:
|
|
|
|
* aIcons = {
|
|
|
|
* "16": "http://www.example.org/icon16.png",
|
|
|
|
* "32": "http://www.example.org/icon32.png"
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* @returns the URL string for the largest specified icon
|
|
|
|
*/
|
|
|
|
function getBiggestIconURL(aIcons) {
|
2012-04-19 18:47:56 -07:00
|
|
|
if (!aIcons) {
|
2013-08-26 12:04:33 -07:00
|
|
|
return DEFAULT_ICON_URL;
|
2012-04-19 18:47:56 -07:00
|
|
|
}
|
|
|
|
|
2012-04-17 07:17:23 -07:00
|
|
|
let iconSizes = Object.keys(aIcons);
|
|
|
|
if (iconSizes.length == 0) {
|
2013-08-26 12:04:33 -07:00
|
|
|
return DEFAULT_ICON_URL;
|
2012-04-17 07:17:23 -07:00
|
|
|
}
|
|
|
|
iconSizes.sort(function(a, b) a - b);
|
|
|
|
return aIcons[iconSizes.pop()];
|
|
|
|
}
|
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
// Download an icon using either a temp file or a pipe.
|
|
|
|
function downloadIcon(aIconURI) {
|
|
|
|
let deferred = Promise.defer();
|
2012-04-17 07:17:23 -07:00
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
|
2012-04-17 07:17:23 -07:00
|
|
|
let mimeType;
|
|
|
|
try {
|
2013-08-26 12:04:33 -07:00
|
|
|
let tIndex = aIconURI.path.indexOf(";");
|
|
|
|
if("data" == aIconURI.scheme && tIndex != -1) {
|
|
|
|
mimeType = aIconURI.path.substring(0, tIndex);
|
2012-04-17 07:17:23 -07:00
|
|
|
} else {
|
2013-08-26 12:04:33 -07:00
|
|
|
mimeType = mimeService.getTypeFromURI(aIconURI);
|
|
|
|
}
|
2013-08-26 09:25:49 -07:00
|
|
|
} catch(e) {
|
2013-08-26 12:04:33 -07:00
|
|
|
deferred.reject("Failed to determine icon MIME type: " + e);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onIconDownloaded(aStatusCode, aIcon) {
|
|
|
|
if (Components.isSuccessCode(aStatusCode)) {
|
|
|
|
deferred.resolve([ mimeType, aIcon ]);
|
|
|
|
} else {
|
|
|
|
deferred.reject("Failure downloading icon: " + aStatusCode);
|
|
|
|
}
|
2012-04-17 07:17:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-08-26 12:04:33 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
let downloadObserver = {
|
|
|
|
onDownloadComplete: function(downloader, request, cx, aStatus, file) {
|
|
|
|
onIconDownloaded(aStatus, file);
|
|
|
|
}
|
|
|
|
};
|
2012-04-17 07:17:23 -07:00
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
let tmpIcon = Services.dirsvc.get("TmpD", Ci.nsIFile);
|
|
|
|
tmpIcon.append("tmpicon." + mimeService.getPrimaryExtension(mimeType, ""));
|
|
|
|
tmpIcon.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
|
2012-04-17 07:17:23 -07:00
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
let listener = Cc["@mozilla.org/network/downloader;1"]
|
|
|
|
.createInstance(Ci.nsIDownloader);
|
|
|
|
listener.init(downloadObserver, tmpIcon);
|
|
|
|
#else
|
|
|
|
let pipe = Cc["@mozilla.org/pipe;1"]
|
|
|
|
.createInstance(Ci.nsIPipe);
|
|
|
|
pipe.init(true, true, 0, 0xffffffff, null);
|
2012-04-17 07:17:23 -07:00
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
let listener = Cc["@mozilla.org/network/simple-stream-listener;1"]
|
|
|
|
.createInstance(Ci.nsISimpleStreamListener);
|
|
|
|
listener.init(pipe.outputStream, {
|
|
|
|
onStartRequest: function() {},
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
|
|
|
pipe.outputStream.close();
|
|
|
|
onIconDownloaded(aStatusCode, pipe.inputStream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#endif
|
2012-04-17 07:17:23 -07:00
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
let channel = NetUtil.newChannel(aIconURI);
|
|
|
|
let { BadCertHandler } = Cu.import("resource://gre/modules/CertUtils.jsm", {});
|
2012-04-17 07:17:23 -07:00
|
|
|
// Pass true to avoid optional redirect-cert-checking behavior.
|
2013-08-26 12:04:33 -07:00
|
|
|
channel.notificationCallbacks = new BadCertHandler(true);
|
2012-04-17 07:17:23 -07:00
|
|
|
|
|
|
|
channel.asyncOpen(listener, null);
|
|
|
|
} catch(e) {
|
2013-08-26 12:04:33 -07:00
|
|
|
deferred.reject("Failure initiating download of icon: " + e);
|
2012-04-17 07:17:23 -07:00
|
|
|
}
|
|
|
|
|
2013-08-26 12:04:33 -07:00
|
|
|
return deferred.promise;
|
2012-04-17 07:17:23 -07:00
|
|
|
}
|