toolkit/webapps/WebappsIconHelpers.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial