toolkit/components/thumbnails/PageThumbsProtocol.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/thumbnails/PageThumbsProtocol.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     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 +/**
     1.9 + * PageThumbsProtocol.js
    1.10 + *
    1.11 + * This file implements the moz-page-thumb:// protocol and the corresponding
    1.12 + * channel delivering cached thumbnails.
    1.13 + *
    1.14 + * URL structure:
    1.15 + *
    1.16 + * moz-page-thumb://thumbnail?url=http%3A%2F%2Fwww.mozilla.org%2F
    1.17 + *
    1.18 + * This URL requests an image for 'http://www.mozilla.org/'.
    1.19 + */
    1.20 +
    1.21 +"use strict";
    1.22 +
    1.23 +const Cu = Components.utils;
    1.24 +const Cc = Components.classes;
    1.25 +const Cr = Components.results;
    1.26 +const Ci = Components.interfaces;
    1.27 +
    1.28 +Cu.import("resource://gre/modules/PageThumbs.jsm");
    1.29 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.30 +
    1.31 +XPCOMUtils.defineLazyModuleGetter(this, "Services",
    1.32 +  "resource://gre/modules/Services.jsm");
    1.33 +XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
    1.34 +  "resource://gre/modules/FileUtils.jsm");
    1.35 +
    1.36 +/**
    1.37 + * Implements the thumbnail protocol handler responsible for moz-page-thumb: URIs.
    1.38 + */
    1.39 +function Protocol() {
    1.40 +}
    1.41 +
    1.42 +Protocol.prototype = {
    1.43 +  /**
    1.44 +   * The scheme used by this protocol.
    1.45 +   */
    1.46 +  get scheme() PageThumbs.scheme,
    1.47 +
    1.48 +  /**
    1.49 +   * The default port for this protocol (we don't support ports).
    1.50 +   */
    1.51 +  get defaultPort() -1,
    1.52 +
    1.53 +  /**
    1.54 +   * The flags specific to this protocol implementation.
    1.55 +   */
    1.56 +  get protocolFlags() {
    1.57 +    return Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD |
    1.58 +           Ci.nsIProtocolHandler.URI_NORELATIVE |
    1.59 +           Ci.nsIProtocolHandler.URI_NOAUTH;
    1.60 +  },
    1.61 +
    1.62 +  /**
    1.63 +   * Creates a new URI object that is suitable for loading by this protocol.
    1.64 +   * @param aSpec The URI string in UTF8 encoding.
    1.65 +   * @param aOriginCharset The charset of the document from which the URI originated.
    1.66 +   * @return The newly created URI.
    1.67 +   */
    1.68 +  newURI: function Proto_newURI(aSpec, aOriginCharset) {
    1.69 +    let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
    1.70 +    uri.spec = aSpec;
    1.71 +    return uri;
    1.72 +  },
    1.73 +
    1.74 +  /**
    1.75 +   * Constructs a new channel from the given URI for this protocol handler.
    1.76 +   * @param aURI The URI for which to construct a channel.
    1.77 +   * @return The newly created channel.
    1.78 +   */
    1.79 +  newChannel: function Proto_newChannel(aURI) {
    1.80 +    let {url} = parseURI(aURI);
    1.81 +    let file = PageThumbsStorage.getFilePathForURL(url);
    1.82 +    let fileuri = Services.io.newFileURI(new FileUtils.File(file));
    1.83 +    return Services.io.newChannelFromURI(fileuri);
    1.84 +  },
    1.85 +
    1.86 +  /**
    1.87 +   * Decides whether to allow a blacklisted port.
    1.88 +   * @return Always false, we'll never allow ports.
    1.89 +   */
    1.90 +  allowPort: function () false,
    1.91 +
    1.92 +  classID: Components.ID("{5a4ae9b5-f475-48ae-9dce-0b4c1d347884}"),
    1.93 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
    1.94 +};
    1.95 +
    1.96 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Protocol]);
    1.97 +
    1.98 +/**
    1.99 + * Parses a given URI and extracts all parameters relevant to this protocol.
   1.100 + * @param aURI The URI to parse.
   1.101 + * @return The parsed parameters.
   1.102 + */
   1.103 +function parseURI(aURI) {
   1.104 +  let {scheme, staticHost} = PageThumbs;
   1.105 +  let re = new RegExp("^" + scheme + "://" + staticHost + ".*?\\?");
   1.106 +  let query = aURI.spec.replace(re, "");
   1.107 +  let params = {};
   1.108 +
   1.109 +  query.split("&").forEach(function (aParam) {
   1.110 +    let [key, value] = aParam.split("=").map(decodeURIComponent);
   1.111 +    params[key.toLowerCase()] = value;
   1.112 +  });
   1.113 +
   1.114 +  return params;
   1.115 +}

mercurial