2012-01-19 19:01:42 +04: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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PageThumbsProtocol.js
|
|
|
|
*
|
|
|
|
* This file implements the moz-page-thumb:// protocol and the corresponding
|
|
|
|
* channel delivering cached thumbnails.
|
|
|
|
*
|
|
|
|
* URL structure:
|
|
|
|
*
|
2015-10-29 18:03:00 +03:00
|
|
|
* moz-page-thumb://thumbnail/?url=http%3A%2F%2Fwww.mozilla.org%2F&revision=XX
|
2012-01-19 19:01:42 +04:00
|
|
|
*
|
|
|
|
* This URL requests an image for 'http://www.mozilla.org/'.
|
2015-10-29 18:03:00 +03:00
|
|
|
* The value of the revision key may change when the stored thumbnail changes.
|
2012-01-19 19:01:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2013-02-17 08:15:41 +04:00
|
|
|
Cu.import("resource://gre/modules/PageThumbs.jsm");
|
2012-01-19 19:01:42 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2015-10-20 08:15:32 +03:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm", this);
|
2012-01-19 19:01:42 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
|
|
|
"resource://gre/modules/Services.jsm");
|
2013-03-27 19:35:56 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
|
|
|
"resource://gre/modules/FileUtils.jsm");
|
2012-01-19 19:01:42 +04:00
|
|
|
|
2015-10-20 08:15:32 +03:00
|
|
|
const SUBSTITUTING_URL_CID = "{dea9657c-18cf-4984-bde9-ccef5d8ab473}";
|
|
|
|
|
2012-01-19 19:01:42 +04:00
|
|
|
/**
|
2015-10-20 08:15:32 +03:00
|
|
|
* Implements the thumbnail protocol handler responsible for moz-page-thumb: URLs.
|
2012-01-19 19:01:42 +04:00
|
|
|
*/
|
|
|
|
function Protocol() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Protocol.prototype = {
|
|
|
|
/**
|
|
|
|
* The scheme used by this protocol.
|
|
|
|
*/
|
2015-09-24 14:32:23 +03:00
|
|
|
get scheme() {
|
|
|
|
return PageThumbs.scheme;
|
|
|
|
},
|
2012-01-19 19:01:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default port for this protocol (we don't support ports).
|
|
|
|
*/
|
2015-09-24 14:32:23 +03:00
|
|
|
get defaultPort() {
|
|
|
|
return -1;
|
|
|
|
},
|
2012-01-19 19:01:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The flags specific to this protocol implementation.
|
|
|
|
*/
|
|
|
|
get protocolFlags() {
|
2012-01-26 21:28:41 +04:00
|
|
|
return Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD |
|
2014-08-28 13:26:35 +04:00
|
|
|
Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
|
2012-01-19 19:01:42 +04:00
|
|
|
Ci.nsIProtocolHandler.URI_NORELATIVE |
|
|
|
|
Ci.nsIProtocolHandler.URI_NOAUTH;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new URI object that is suitable for loading by this protocol.
|
|
|
|
* @param aSpec The URI string in UTF8 encoding.
|
|
|
|
* @param aOriginCharset The charset of the document from which the URI originated.
|
|
|
|
* @return The newly created URI.
|
|
|
|
*/
|
|
|
|
newURI: function Proto_newURI(aSpec, aOriginCharset) {
|
2015-10-20 08:15:32 +03:00
|
|
|
let uri = Components.classesByID[SUBSTITUTING_URL_CID].createInstance(Ci.nsIURL);
|
2012-01-19 19:01:42 +04:00
|
|
|
uri.spec = aSpec;
|
|
|
|
return uri;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new channel from the given URI for this protocol handler.
|
|
|
|
* @param aURI The URI for which to construct a channel.
|
2015-04-09 20:10:45 +03:00
|
|
|
* @param aLoadInfo The Loadinfo which to use on the channel.
|
2012-01-19 19:01:42 +04:00
|
|
|
* @return The newly created channel.
|
|
|
|
*/
|
2015-04-09 20:10:45 +03:00
|
|
|
newChannel2: function Proto_newChannel2(aURI, aLoadInfo) {
|
2015-10-20 08:15:46 +03:00
|
|
|
let {file} = aURI.QueryInterface(Ci.nsIFileURL);
|
|
|
|
let fileuri = Services.io.newFileURI(file);
|
2015-05-13 04:45:49 +03:00
|
|
|
let channel = Services.io.newChannelFromURIWithLoadInfo(fileuri, aLoadInfo);
|
|
|
|
channel.originalURI = aURI;
|
|
|
|
return channel;
|
2015-04-09 20:10:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
newChannel: function Proto_newChannel(aURI) {
|
2015-08-06 21:11:32 +03:00
|
|
|
return this.newChannel2(aURI, null);
|
2012-01-19 19:01:42 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decides whether to allow a blacklisted port.
|
|
|
|
* @return Always false, we'll never allow ports.
|
|
|
|
*/
|
2015-09-24 14:32:23 +03:00
|
|
|
allowPort: () => false,
|
2012-01-19 19:01:42 +04:00
|
|
|
|
2015-10-20 08:15:32 +03:00
|
|
|
// nsISubstitutingProtocolHandler methods
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Substituting the scheme and host isn't enough, we also transform the path.
|
|
|
|
* So declare no-op implementations for (get|set|has)Substitution methods and
|
|
|
|
* do all the work in resolveURI.
|
|
|
|
*/
|
|
|
|
|
|
|
|
setSubstitution(root, baseURI) {},
|
|
|
|
|
|
|
|
getSubstitution(root) {
|
|
|
|
throw Cr.NS_ERROR_NOT_AVAILABLE;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasSubstitution(root) {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveURI(resURI) {
|
|
|
|
let {url} = parseURI(resURI);
|
|
|
|
let path = PageThumbsStorage.getFilePathForURL(url);
|
|
|
|
return OS.Path.toFileURI(path);
|
|
|
|
},
|
|
|
|
|
|
|
|
// xpcom machinery
|
2012-01-19 19:01:42 +04:00
|
|
|
classID: Components.ID("{5a4ae9b5-f475-48ae-9dce-0b4c1d347884}"),
|
2015-10-20 08:15:32 +03:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler,
|
|
|
|
Ci.nsISubstitutingProtocolHandler])
|
2012-01-19 19:01:42 +04:00
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Protocol]);
|
2012-01-19 19:01:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a given URI and extracts all parameters relevant to this protocol.
|
|
|
|
* @param aURI The URI to parse.
|
|
|
|
* @return The parsed parameters.
|
|
|
|
*/
|
|
|
|
function parseURI(aURI) {
|
2015-10-20 08:15:40 +03:00
|
|
|
if (aURI.host != PageThumbs.staticHost)
|
|
|
|
throw Cr.NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
let {query} = aURI.QueryInterface(Ci.nsIURL);
|
2012-01-19 19:01:42 +04:00
|
|
|
let params = {};
|
|
|
|
|
2016-11-12 02:22:34 +03:00
|
|
|
query.split("&").forEach(function(aParam) {
|
2012-01-19 19:01:42 +04:00
|
|
|
let [key, value] = aParam.split("=").map(decodeURIComponent);
|
|
|
|
params[key.toLowerCase()] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|