isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / favicon.js
blob83b281b55cc33f4dd354334c9c54f9f268ec6dbe
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2010 John J. Foerch
4  *
5  * Portions of this file were derived from Mozilla,
6  * (C) Copyright 1998-2008 Mozilla Foundation.
7  *
8  * Use, modification, and distribution are subject to the terms specified in the
9  * COPYING file.
10 **/
12 const favicon_service = Cc["@mozilla.org/browser/favicon-service;1"]
13     .getService(Ci.nsIFaviconService);
15 define_variable("favicon_image_max_size", 1024,
16     "Maximum (pixel) width and height of an image document that "+
17     "is considered for use as a favicon.");
20 function favicon_set (buffer, icon_url) {
21     favicon_service.setAndLoadFaviconForPage(buffer.current_uri,
22                                              icon_url, false);
23     buffer.icon = icon_url.spec;
27 function favicon_content_buffer_started_loading (buffer) {
28     buffer.icon = null;
32 function favicon_content_buffer_finished_loading (buffer) {
33     if (buffer.icon != null)
34         return;
36     if (buffer.document instanceof Ci.nsIImageDocument) {
37         var req = buffer.document.imageRequest;
38         if (req && req.image &&
39             req.image.width <= favicon_image_max_size  &&
40             req.image.height <= favicon_image_max_size)
41         {
42             favicon_set(buffer, buffer.current_uri);
43             return;
44         }
45     }
47     var uri = buffer.current_uri;
48     // Only load favicons for http and https
49     if (!uri.schemeIs("http") && !uri.schemeIs("https"))
50         return;
52     var icon_url = make_uri(uri.prePath + "/favicon.ico");
53     if (!favicon_service.isFailedFavicon(icon_url))
54         favicon_set(buffer, icon_url);
58 function favicon_content_buffer_dom_link_added (buffer, event) {
59     var link = event.originalTarget;
60     if (!link || !link.ownerDocument || !link.rel || !link.href ||
61         link.ownerDocument != buffer.document)
62     {
63         return;
64     }
65     var rel = link.rel.toLowerCase();
66     var rel_strings = rel.split(/\s+/);
67     if (rel_strings.indexOf("icon") == -1)
68         return;
70     /* FIXME: perhaps worry about newURI throwing an exception */
71     var target_doc = link.ownerDocument;
72     var ios = Cc["@mozilla.org/network/io-service;1"]
73         .getService(Ci.nsIIOService);
74     var uri = ios.newURI(link.href, target_doc.characterSet, null);
76     if (favicon_service.isFailedFavicon(uri))
77         return;
79     // Verify that the load of this icon is legal.
80     // error pages can load their favicon, to be on the safe side,
81     // only allow chrome:// favicons
82     const about_neterr = "about:neterror?";
83     if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr
84         || !uri.schemeIs("chrome"))
85     {
86         var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
87             .getService(Ci.nsIScriptSecurityManager);
88         try {
89             ssm.checkLoadURIWithPrincipal(
90                 target_doc.nodePrincipal, uri,
91                 Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
92         } catch(e) {
93             return;
94         }
95     }
97     try {
98     } catch(e) {
99         return; // Refuse to load if we can't do a security check.
100     }
102     // Security says okay, now ask content policy
103     var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"]
104         .getService(Ci.nsIContentPolicy);
105     if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
106                                           uri, target_doc.documentURIObject,
107                                           link, link.type, null)
108         != Ci.nsIContentPolicy.ACCEPT)
109     {
110         return;
111     }
113     favicon_set(buffer, uri);
117 add_hook("content_buffer_started_loading_hook",
118          favicon_content_buffer_started_loading);
120 add_hook("content_buffer_finished_loading_hook",
121          favicon_content_buffer_finished_loading);
123 add_hook("content_buffer_dom_link_added_hook",
124          favicon_content_buffer_dom_link_added);
126 provide("favicon");