minibuffer-completion.js: Avoid some minor errors
[conkeror.git] / modules / favicon.js
blobe3bfb48bbcefb7cdcabe0ea8d2be9a5962a3ca9c
2 const favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
4 define_buffer_local_hook("buffer_favicon_change_hook");
6 function favicon_content_buffer_started_loading(buffer) {
7     var old = buffer.favicon;
8     buffer.favicon = null;
9     if (old != null)
10         buffer_favicon_change_hook.run(buffer);
12 add_hook("content_buffer_started_loading_hook", favicon_content_buffer_started_loading);
14 define_variable("favicon_image_max_size", 1024, "Maximum (pixel) width and height of an image document that is considered for use as a favicon.");
15 var favicon_image_max_size = 1024;
17 function favicon_content_buffer_finished_loading(buffer) {
18     if (buffer.favicon != null)
19         return;
21     if (buffer.document instanceof Ci.nsIImageDocument) {
22         var req = buffer.document.imageRequest;
23         if (req && req.image &&
24             req.image.width <= favicon_image_max_size  &&
25             req.image.height <= favicon_image_max_size) {
26             favicon_set(buffer, buffer.current_URI);
27             return;
28         }
29     }
31     var uri = buffer.current_URI;
32     // Only load favicons for http and https
33     if (!uri.schemeIs("http") && !uri.schemeIs("https"))
34         return;
36     var icon_url = makeURL(uri.prePath + "/favicon.ico");
37     if (!favicon_service.isFailedFavicon(icon_url))
38         favicon_set(buffer, icon_url);
40 add_hook("content_buffer_finished_loading_hook", favicon_content_buffer_finished_loading);
42 var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"].getService(Ci.nsIContentPolicy);
44 function favicon_content_buffer_dom_link_added(buffer, event) {
45     var link = event.originalTarget;
47     if (!link || !link.ownerDocument || !link.rel || !link.href)
48         return;
50     var rel = link.rel.toLowerCase();
51     var rel_strings = rel.split(/\s+/);
52     if (rel_strings.indexOf("icon") == -1)
53         return;
55     /* FIXME: perhaps worry about newURI throwing an exception */
56     var target_doc = link.ownerDocument;
57     var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
58     var uri = ios.newURI(link.href, target_doc.characterSet, null);
60     if (favicon_service.isFailedFavicon(uri))
61         return;
63     // Verify that the load of this icon is legal.
64     // error pages can load their favicon, to be on the safe side,
65     // only allow chrome:// favicons
66     const about_neterr = "about:neterror?";
67     if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr || !uri.schemeIs("chrome")) {
68         var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
69         try {
70             ssm.checkLoadURIWithPrincipal(target_doc.nodePrincipal, uri,
71                                           Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
72         } catch(e) {
73             return;
74         }
75     }
77     try {
78     } catch(e) {
79         return; // Refuse to load if we can't do a security check.
80     }
82     // Security says okay, now ask content policy
83     if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
84                                           uri, target_doc.documentURIObject,
85                                           link, link.type, null)
86         != Ci.nsIContentPolicy.ACCEPT)
87         return;
89     favicon_set(buffer, uri);
91 add_hook("content_buffer_dom_link_added_hook", favicon_content_buffer_dom_link_added);
93 function favicon_set(buffer, icon_url) {
94     buffer.favicon = icon_url.spec;
96     favicon_service.setAndLoadFaviconForPage(buffer.current_URI, icon_url, false);
97     buffer_favicon_change_hook.run(buffer);