keywords.js: make write_keywords public and more flexible
[conkeror.git] / modules / favicon.js
blob87cda8809c93b562d20c94617ce3eb28dca1e544
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Portions of this file were derived from Mozilla,
5  * (C) Copyright 1998-2008 Mozilla Foundation.
6  *
7  * Use, modification, and distribution are subject to the terms specified in the
8  * COPYING file.
9 **/
11 const favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
13 define_buffer_local_hook("buffer_favicon_change_hook");
15 function favicon_content_buffer_started_loading(buffer) {
16     var old = buffer.favicon;
17     buffer.favicon = null;
18     if (old != null)
19         buffer_favicon_change_hook.run(buffer);
21 add_hook("content_buffer_started_loading_hook", favicon_content_buffer_started_loading);
23 define_variable("favicon_image_max_size", 1024, "Maximum (pixel) width and height of an image document that is considered for use as a favicon.");
24 var favicon_image_max_size = 1024;
26 function favicon_content_buffer_finished_loading(buffer) {
27     if (buffer.favicon != null)
28         return;
30     if (buffer.document instanceof Ci.nsIImageDocument) {
31         var req = buffer.document.imageRequest;
32         if (req && req.image &&
33             req.image.width <= favicon_image_max_size  &&
34             req.image.height <= favicon_image_max_size) {
35             favicon_set(buffer, buffer.current_URI);
36             return;
37         }
38     }
40     var uri = buffer.current_URI;
41     // Only load favicons for http and https
42     if (!uri.schemeIs("http") && !uri.schemeIs("https"))
43         return;
45     var icon_url = makeURL(uri.prePath + "/favicon.ico");
46     if (!favicon_service.isFailedFavicon(icon_url))
47         favicon_set(buffer, icon_url);
49 add_hook("content_buffer_finished_loading_hook", favicon_content_buffer_finished_loading);
51 var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"].getService(Ci.nsIContentPolicy);
53 function favicon_content_buffer_dom_link_added(buffer, event) {
54     var link = event.originalTarget;
56     if (!link || !link.ownerDocument || !link.rel || !link.href)
57         return;
59     var rel = link.rel.toLowerCase();
60     var rel_strings = rel.split(/\s+/);
61     if (rel_strings.indexOf("icon") == -1)
62         return;
64     /* FIXME: perhaps worry about newURI throwing an exception */
65     var target_doc = link.ownerDocument;
66     var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
67     var uri = ios.newURI(link.href, target_doc.characterSet, null);
69     if (favicon_service.isFailedFavicon(uri))
70         return;
72     // Verify that the load of this icon is legal.
73     // error pages can load their favicon, to be on the safe side,
74     // only allow chrome:// favicons
75     const about_neterr = "about:neterror?";
76     if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr || !uri.schemeIs("chrome")) {
77         var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
78         try {
79             ssm.checkLoadURIWithPrincipal(target_doc.nodePrincipal, uri,
80                                           Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
81         } catch(e) {
82             return;
83         }
84     }
86     try {
87     } catch(e) {
88         return; // Refuse to load if we can't do a security check.
89     }
91     // Security says okay, now ask content policy
92     if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
93                                           uri, target_doc.documentURIObject,
94                                           link, link.type, null)
95         != Ci.nsIContentPolicy.ACCEPT)
96         return;
98     favicon_set(buffer, uri);
100 add_hook("content_buffer_dom_link_added_hook", favicon_content_buffer_dom_link_added);
102 function favicon_set(buffer, icon_url) {
103     buffer.favicon = icon_url.spec;
105     favicon_service.setAndLoadFaviconForPage(buffer.current_URI, icon_url, false);
106     buffer_favicon_change_hook.run(buffer);