toolkit/webapps/WebappsIconHelpers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/webapps/WebappsIconHelpers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const DEFAULT_ICON_URL = "chrome://global/skin/icons/webapps-64.png";
     1.9 +
    1.10 +/**
    1.11 + * This function receives a list of icon sizes
    1.12 + * and URLs and returns the url string for the biggest icon.
    1.13 + *
    1.14 + * @param aIcons An object where the keys are the icon sizes
    1.15 + *               and the values are URL strings. E.g.:
    1.16 + *               aIcons = {
    1.17 + *                 "16": "http://www.example.org/icon16.png",
    1.18 + *                 "32": "http://www.example.org/icon32.png"
    1.19 + *               };
    1.20 + *
    1.21 + * @returns the URL string for the largest specified icon
    1.22 + */
    1.23 +function getBiggestIconURL(aIcons) {
    1.24 +  if (!aIcons) {
    1.25 +    return DEFAULT_ICON_URL;
    1.26 +  }
    1.27 +
    1.28 +  let iconSizes = Object.keys(aIcons);
    1.29 +  if (iconSizes.length == 0) {
    1.30 +    return DEFAULT_ICON_URL;
    1.31 +  }
    1.32 +  iconSizes.sort(function(a, b) a - b);
    1.33 +  return aIcons[iconSizes.pop()];
    1.34 +}
    1.35 +
    1.36 +// Download an icon using either a temp file or a pipe.
    1.37 +function downloadIcon(aIconURI) {
    1.38 +  let deferred = Promise.defer();
    1.39 +
    1.40 +  let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
    1.41 +  let mimeType;
    1.42 +  try {
    1.43 +    let tIndex = aIconURI.path.indexOf(";");
    1.44 +    if("data" == aIconURI.scheme && tIndex != -1) {
    1.45 +      mimeType = aIconURI.path.substring(0, tIndex);
    1.46 +    } else {
    1.47 +      mimeType = mimeService.getTypeFromURI(aIconURI);
    1.48 +     }
    1.49 +  } catch(e) {
    1.50 +    deferred.reject("Failed to determine icon MIME type: " + e);
    1.51 +    return deferred.promise;
    1.52 +  }
    1.53 +
    1.54 +  function onIconDownloaded(aStatusCode, aIcon) {
    1.55 +    if (Components.isSuccessCode(aStatusCode)) {
    1.56 +      deferred.resolve([ mimeType, aIcon ]);
    1.57 +    } else {
    1.58 +      deferred.reject("Failure downloading icon: " + aStatusCode);
    1.59 +    }
    1.60 +  }
    1.61 +
    1.62 +  try {
    1.63 +#ifdef XP_MACOSX
    1.64 +    let downloadObserver = {
    1.65 +      onDownloadComplete: function(downloader, request, cx, aStatus, file) {
    1.66 +        onIconDownloaded(aStatus, file);
    1.67 +      }
    1.68 +    };
    1.69 +
    1.70 +    let tmpIcon = Services.dirsvc.get("TmpD", Ci.nsIFile);
    1.71 +    tmpIcon.append("tmpicon." + mimeService.getPrimaryExtension(mimeType, ""));
    1.72 +    tmpIcon.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
    1.73 +
    1.74 +    let listener = Cc["@mozilla.org/network/downloader;1"]
    1.75 +                     .createInstance(Ci.nsIDownloader);
    1.76 +    listener.init(downloadObserver, tmpIcon);
    1.77 +#else
    1.78 +    let pipe = Cc["@mozilla.org/pipe;1"]
    1.79 +                 .createInstance(Ci.nsIPipe);
    1.80 +    pipe.init(true, true, 0, 0xffffffff, null);
    1.81 +
    1.82 +    let listener = Cc["@mozilla.org/network/simple-stream-listener;1"]
    1.83 +                     .createInstance(Ci.nsISimpleStreamListener);
    1.84 +    listener.init(pipe.outputStream, {
    1.85 +        onStartRequest: function() {},
    1.86 +        onStopRequest: function(aRequest, aContext, aStatusCode) {
    1.87 +          pipe.outputStream.close();
    1.88 +          onIconDownloaded(aStatusCode, pipe.inputStream);
    1.89 +       }
    1.90 +    });
    1.91 +#endif
    1.92 +
    1.93 +    let channel = NetUtil.newChannel(aIconURI);
    1.94 +    let { BadCertHandler } = Cu.import("resource://gre/modules/CertUtils.jsm", {});
    1.95 +    // Pass true to avoid optional redirect-cert-checking behavior.
    1.96 +    channel.notificationCallbacks = new BadCertHandler(true);
    1.97 +
    1.98 +    channel.asyncOpen(listener, null);
    1.99 +  } catch(e) {
   1.100 +    deferred.reject("Failure initiating download of icon: " + e);
   1.101 +  }
   1.102 +
   1.103 +  return deferred.promise;
   1.104 +}

mercurial