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;
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)
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);
31 var uri = buffer.current_URI;
32 // Only load favicons for http and https
33 if (!uri.schemeIs("http") && !uri.schemeIs("https"))
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)
50 var rel = link.rel.toLowerCase();
51 var rel_strings = rel.split(/\s+/);
52 if (rel_strings.indexOf("icon") == -1)
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))
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);
70 ssm.checkLoadURIWithPrincipal(target_doc.nodePrincipal, uri,
71 Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
79 return; // Refuse to load if we can't do a security check.
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)
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);