2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Portions of this file were derived from Mozilla,
5 * (C) Copyright 1998-2008 Mozilla Foundation.
7 * Use, modification, and distribution are subject to the terms specified in the
13 const favicon_service = Cc["@mozilla.org/browser/favicon-service;1"]
14 .getService(Ci.nsIFaviconService);
16 define_variable("favicon_image_max_size", 1024,
17 "Maximum (pixel) width and height of an image document that "+
18 "is considered for use as a favicon.");
20 define_buffer_local_hook("buffer_favicon_change_hook");
21 define_current_buffer_hook("current_buffer_favicon_change_hook", "buffer_favicon_change_hook");
24 function favicon_set (buffer, icon_url) {
25 buffer.favicon = icon_url.spec;
26 favicon_service.setAndLoadFaviconForPage(buffer.current_uri,
28 buffer_favicon_change_hook.run(buffer);
32 function favicon_content_buffer_started_loading (buffer) {
33 var old = buffer.favicon;
34 buffer.favicon = null;
36 buffer_favicon_change_hook.run(buffer);
40 function favicon_content_buffer_finished_loading (buffer) {
41 if (buffer.favicon != null)
44 if (buffer.document instanceof Ci.nsIImageDocument) {
45 var req = buffer.document.imageRequest;
46 if (req && req.image &&
47 req.image.width <= favicon_image_max_size &&
48 req.image.height <= favicon_image_max_size)
50 favicon_set(buffer, buffer.current_uri);
55 var uri = buffer.current_uri;
56 // Only load favicons for http and https
57 if (!uri.schemeIs("http") && !uri.schemeIs("https"))
60 var icon_url = make_uri(uri.prePath + "/favicon.ico");
61 if (!favicon_service.isFailedFavicon(icon_url))
62 favicon_set(buffer, icon_url);
66 function favicon_content_buffer_dom_link_added (buffer, event) {
67 var link = event.originalTarget;
68 if (!link || !link.ownerDocument || !link.rel || !link.href ||
69 link.ownerDocument != buffer.document)
73 var rel = link.rel.toLowerCase();
74 var rel_strings = rel.split(/\s+/);
75 if (rel_strings.indexOf("icon") == -1)
78 /* FIXME: perhaps worry about newURI throwing an exception */
79 var target_doc = link.ownerDocument;
80 var ios = Cc["@mozilla.org/network/io-service;1"]
81 .getService(Ci.nsIIOService);
82 var uri = ios.newURI(link.href, target_doc.characterSet, null);
84 if (favicon_service.isFailedFavicon(uri))
87 // Verify that the load of this icon is legal.
88 // error pages can load their favicon, to be on the safe side,
89 // only allow chrome:// favicons
90 const about_neterr = "about:neterror?";
91 if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr
92 || !uri.schemeIs("chrome"))
94 var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
95 .getService(Ci.nsIScriptSecurityManager);
97 ssm.checkLoadURIWithPrincipal(
98 target_doc.nodePrincipal, uri,
99 Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
107 return; // Refuse to load if we can't do a security check.
110 // Security says okay, now ask content policy
111 var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"]
112 .getService(Ci.nsIContentPolicy);
113 if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
114 uri, target_doc.documentURIObject,
115 link, link.type, null)
116 != Ci.nsIContentPolicy.ACCEPT)
121 favicon_set(buffer, uri);
125 add_hook("content_buffer_started_loading_hook",
126 favicon_content_buffer_started_loading);
128 add_hook("content_buffer_finished_loading_hook",
129 favicon_content_buffer_finished_loading);
131 add_hook("content_buffer_dom_link_added_hook",
132 favicon_content_buffer_dom_link_added);