2 * (C) Copyright 2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2010 John J. Foerch
5 * Portions of this file were derived from Mozilla,
6 * (C) Copyright 1998-2008 Mozilla Foundation.
8 * Use, modification, and distribution are subject to the terms specified in the
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 let (favicon_set_internal) {
21 if (version_compare(get_mozilla_version(), "18.0") >= 0) {
22 favicon_set_internal = function (buffer, icon_url) {
23 favicon_service.setAndLoadFaviconForPage(
24 buffer.current_uri, icon_url, false,
25 favicon_service.FAVICON_LOAD_NON_PRIVATE);
28 favicon_set_internal = function (buffer, icon_url) {
29 favicon_service.setAndLoadFaviconForPage(
30 buffer.current_uri, icon_url, false);
33 function favicon_set (buffer, icon_url) {
34 favicon_set_internal(buffer, icon_url);
35 buffer.icon = icon_url.spec;
40 function favicon_content_buffer_started_loading (buffer) {
45 function favicon_content_buffer_finished_loading (buffer) {
46 if (buffer.icon != null)
49 if (buffer.document instanceof Ci.nsIImageDocument) {
50 var req = buffer.document.imageRequest;
51 if (req && req.image &&
52 req.image.width <= favicon_image_max_size &&
53 req.image.height <= favicon_image_max_size)
55 favicon_set(buffer, buffer.current_uri);
60 var uri = buffer.current_uri;
61 // Only load favicons for http and https
62 if (!uri.schemeIs("http") && !uri.schemeIs("https"))
65 var icon_url = make_uri(uri.prePath + "/favicon.ico");
66 if (!favicon_service.isFailedFavicon(icon_url))
67 favicon_set(buffer, icon_url);
71 function favicon_content_buffer_dom_link_added (buffer, event) {
72 var link = event.originalTarget;
73 if (!link || !link.ownerDocument || !link.rel || !link.href ||
74 link.ownerDocument != buffer.document)
78 var rel = link.rel.toLowerCase();
79 var rel_strings = rel.split(/\s+/);
80 if (rel_strings.indexOf("icon") == -1)
83 /* FIXME: perhaps worry about newURI throwing an exception */
84 var target_doc = link.ownerDocument;
85 var ios = Cc["@mozilla.org/network/io-service;1"]
86 .getService(Ci.nsIIOService);
87 var uri = ios.newURI(link.href, target_doc.characterSet, null);
89 if (favicon_service.isFailedFavicon(uri))
92 // Verify that the load of this icon is legal.
93 // error pages can load their favicon, to be on the safe side,
94 // only allow chrome:// favicons
95 const about_neterr = "about:neterror?";
96 if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr
97 || !uri.schemeIs("chrome"))
99 var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
100 .getService(Ci.nsIScriptSecurityManager);
102 ssm.checkLoadURIWithPrincipal(
103 target_doc.nodePrincipal, uri,
104 Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
112 return; // Refuse to load if we can't do a security check.
115 // Security says okay, now ask content policy
116 var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"]
117 .getService(Ci.nsIContentPolicy);
118 if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
119 uri, target_doc.documentURIObject,
120 link, link.type, null)
121 != Ci.nsIContentPolicy.ACCEPT)
126 favicon_set(buffer, uri);
130 add_hook("content_buffer_started_loading_hook",
131 favicon_content_buffer_started_loading);
133 add_hook("content_buffer_finished_loading_hook",
134 favicon_content_buffer_finished_loading);
136 add_hook("content_buffer_dom_link_added_hook",
137 favicon_content_buffer_dom_link_added);