toolkit/webapps/WebappsIconHelpers.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const DEFAULT_ICON_URL = "chrome://global/skin/icons/webapps-64.png";
michael@0 6
michael@0 7 /**
michael@0 8 * This function receives a list of icon sizes
michael@0 9 * and URLs and returns the url string for the biggest icon.
michael@0 10 *
michael@0 11 * @param aIcons An object where the keys are the icon sizes
michael@0 12 * and the values are URL strings. E.g.:
michael@0 13 * aIcons = {
michael@0 14 * "16": "http://www.example.org/icon16.png",
michael@0 15 * "32": "http://www.example.org/icon32.png"
michael@0 16 * };
michael@0 17 *
michael@0 18 * @returns the URL string for the largest specified icon
michael@0 19 */
michael@0 20 function getBiggestIconURL(aIcons) {
michael@0 21 if (!aIcons) {
michael@0 22 return DEFAULT_ICON_URL;
michael@0 23 }
michael@0 24
michael@0 25 let iconSizes = Object.keys(aIcons);
michael@0 26 if (iconSizes.length == 0) {
michael@0 27 return DEFAULT_ICON_URL;
michael@0 28 }
michael@0 29 iconSizes.sort(function(a, b) a - b);
michael@0 30 return aIcons[iconSizes.pop()];
michael@0 31 }
michael@0 32
michael@0 33 // Download an icon using either a temp file or a pipe.
michael@0 34 function downloadIcon(aIconURI) {
michael@0 35 let deferred = Promise.defer();
michael@0 36
michael@0 37 let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
michael@0 38 let mimeType;
michael@0 39 try {
michael@0 40 let tIndex = aIconURI.path.indexOf(";");
michael@0 41 if("data" == aIconURI.scheme && tIndex != -1) {
michael@0 42 mimeType = aIconURI.path.substring(0, tIndex);
michael@0 43 } else {
michael@0 44 mimeType = mimeService.getTypeFromURI(aIconURI);
michael@0 45 }
michael@0 46 } catch(e) {
michael@0 47 deferred.reject("Failed to determine icon MIME type: " + e);
michael@0 48 return deferred.promise;
michael@0 49 }
michael@0 50
michael@0 51 function onIconDownloaded(aStatusCode, aIcon) {
michael@0 52 if (Components.isSuccessCode(aStatusCode)) {
michael@0 53 deferred.resolve([ mimeType, aIcon ]);
michael@0 54 } else {
michael@0 55 deferred.reject("Failure downloading icon: " + aStatusCode);
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 try {
michael@0 60 #ifdef XP_MACOSX
michael@0 61 let downloadObserver = {
michael@0 62 onDownloadComplete: function(downloader, request, cx, aStatus, file) {
michael@0 63 onIconDownloaded(aStatus, file);
michael@0 64 }
michael@0 65 };
michael@0 66
michael@0 67 let tmpIcon = Services.dirsvc.get("TmpD", Ci.nsIFile);
michael@0 68 tmpIcon.append("tmpicon." + mimeService.getPrimaryExtension(mimeType, ""));
michael@0 69 tmpIcon.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
michael@0 70
michael@0 71 let listener = Cc["@mozilla.org/network/downloader;1"]
michael@0 72 .createInstance(Ci.nsIDownloader);
michael@0 73 listener.init(downloadObserver, tmpIcon);
michael@0 74 #else
michael@0 75 let pipe = Cc["@mozilla.org/pipe;1"]
michael@0 76 .createInstance(Ci.nsIPipe);
michael@0 77 pipe.init(true, true, 0, 0xffffffff, null);
michael@0 78
michael@0 79 let listener = Cc["@mozilla.org/network/simple-stream-listener;1"]
michael@0 80 .createInstance(Ci.nsISimpleStreamListener);
michael@0 81 listener.init(pipe.outputStream, {
michael@0 82 onStartRequest: function() {},
michael@0 83 onStopRequest: function(aRequest, aContext, aStatusCode) {
michael@0 84 pipe.outputStream.close();
michael@0 85 onIconDownloaded(aStatusCode, pipe.inputStream);
michael@0 86 }
michael@0 87 });
michael@0 88 #endif
michael@0 89
michael@0 90 let channel = NetUtil.newChannel(aIconURI);
michael@0 91 let { BadCertHandler } = Cu.import("resource://gre/modules/CertUtils.jsm", {});
michael@0 92 // Pass true to avoid optional redirect-cert-checking behavior.
michael@0 93 channel.notificationCallbacks = new BadCertHandler(true);
michael@0 94
michael@0 95 channel.asyncOpen(listener, null);
michael@0 96 } catch(e) {
michael@0 97 deferred.reject("Failure initiating download of icon: " + e);
michael@0 98 }
michael@0 99
michael@0 100 return deferred.promise;
michael@0 101 }

mercurial