|
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/. */ |
|
4 |
|
5 /** |
|
6 * PageThumbsProtocol.js |
|
7 * |
|
8 * This file implements the moz-page-thumb:// protocol and the corresponding |
|
9 * channel delivering cached thumbnails. |
|
10 * |
|
11 * URL structure: |
|
12 * |
|
13 * moz-page-thumb://thumbnail?url=http%3A%2F%2Fwww.mozilla.org%2F |
|
14 * |
|
15 * This URL requests an image for 'http://www.mozilla.org/'. |
|
16 */ |
|
17 |
|
18 "use strict"; |
|
19 |
|
20 const Cu = Components.utils; |
|
21 const Cc = Components.classes; |
|
22 const Cr = Components.results; |
|
23 const Ci = Components.interfaces; |
|
24 |
|
25 Cu.import("resource://gre/modules/PageThumbs.jsm"); |
|
26 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
27 |
|
28 XPCOMUtils.defineLazyModuleGetter(this, "Services", |
|
29 "resource://gre/modules/Services.jsm"); |
|
30 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", |
|
31 "resource://gre/modules/FileUtils.jsm"); |
|
32 |
|
33 /** |
|
34 * Implements the thumbnail protocol handler responsible for moz-page-thumb: URIs. |
|
35 */ |
|
36 function Protocol() { |
|
37 } |
|
38 |
|
39 Protocol.prototype = { |
|
40 /** |
|
41 * The scheme used by this protocol. |
|
42 */ |
|
43 get scheme() PageThumbs.scheme, |
|
44 |
|
45 /** |
|
46 * The default port for this protocol (we don't support ports). |
|
47 */ |
|
48 get defaultPort() -1, |
|
49 |
|
50 /** |
|
51 * The flags specific to this protocol implementation. |
|
52 */ |
|
53 get protocolFlags() { |
|
54 return Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD | |
|
55 Ci.nsIProtocolHandler.URI_NORELATIVE | |
|
56 Ci.nsIProtocolHandler.URI_NOAUTH; |
|
57 }, |
|
58 |
|
59 /** |
|
60 * Creates a new URI object that is suitable for loading by this protocol. |
|
61 * @param aSpec The URI string in UTF8 encoding. |
|
62 * @param aOriginCharset The charset of the document from which the URI originated. |
|
63 * @return The newly created URI. |
|
64 */ |
|
65 newURI: function Proto_newURI(aSpec, aOriginCharset) { |
|
66 let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI); |
|
67 uri.spec = aSpec; |
|
68 return uri; |
|
69 }, |
|
70 |
|
71 /** |
|
72 * Constructs a new channel from the given URI for this protocol handler. |
|
73 * @param aURI The URI for which to construct a channel. |
|
74 * @return The newly created channel. |
|
75 */ |
|
76 newChannel: function Proto_newChannel(aURI) { |
|
77 let {url} = parseURI(aURI); |
|
78 let file = PageThumbsStorage.getFilePathForURL(url); |
|
79 let fileuri = Services.io.newFileURI(new FileUtils.File(file)); |
|
80 return Services.io.newChannelFromURI(fileuri); |
|
81 }, |
|
82 |
|
83 /** |
|
84 * Decides whether to allow a blacklisted port. |
|
85 * @return Always false, we'll never allow ports. |
|
86 */ |
|
87 allowPort: function () false, |
|
88 |
|
89 classID: Components.ID("{5a4ae9b5-f475-48ae-9dce-0b4c1d347884}"), |
|
90 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]) |
|
91 }; |
|
92 |
|
93 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Protocol]); |
|
94 |
|
95 /** |
|
96 * Parses a given URI and extracts all parameters relevant to this protocol. |
|
97 * @param aURI The URI to parse. |
|
98 * @return The parsed parameters. |
|
99 */ |
|
100 function parseURI(aURI) { |
|
101 let {scheme, staticHost} = PageThumbs; |
|
102 let re = new RegExp("^" + scheme + "://" + staticHost + ".*?\\?"); |
|
103 let query = aURI.spec.replace(re, ""); |
|
104 let params = {}; |
|
105 |
|
106 query.split("&").forEach(function (aParam) { |
|
107 let [key, value] = aParam.split("=").map(decodeURIComponent); |
|
108 params[key.toLowerCase()] = value; |
|
109 }); |
|
110 |
|
111 return params; |
|
112 } |