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
14 const favicon_service = Cc["@mozilla.org/browser/favicon-service;1"]
15 .getService(Ci.nsIFaviconService);
17 define_variable("favicon_image_max_size", 1024,
18 "Maximum (pixel) width and height of an image document that "+
19 "is considered for use as a favicon.");
22 function favicon_set (buffer, icon_url) {
23 favicon_service.setAndLoadFaviconForPage(buffer.current_uri,
25 buffer.icon = icon_url.spec;
29 function favicon_content_buffer_started_loading (buffer) {
34 function favicon_content_buffer_finished_loading (buffer) {
35 if (buffer.icon != null)
38 if (buffer.document instanceof Ci.nsIImageDocument) {
39 var req = buffer.document.imageRequest;
40 if (req && req.image &&
41 req.image.width <= favicon_image_max_size &&
42 req.image.height <= favicon_image_max_size)
44 favicon_set(buffer, buffer.current_uri);
49 var uri = buffer.current_uri;
50 // Only load favicons for http and https
51 if (!uri.schemeIs("http") && !uri.schemeIs("https"))
54 var icon_url = make_uri(uri.prePath + "/favicon.ico");
55 if (!favicon_service.isFailedFavicon(icon_url))
56 favicon_set(buffer, icon_url);
60 function favicon_content_buffer_dom_link_added (buffer, event) {
61 var link = event.originalTarget;
62 if (!link || !link.ownerDocument || !link.rel || !link.href ||
63 link.ownerDocument != buffer.document)
67 var rel = link.rel.toLowerCase();
68 var rel_strings = rel.split(/\s+/);
69 if (rel_strings.indexOf("icon") == -1)
72 /* FIXME: perhaps worry about newURI throwing an exception */
73 var target_doc = link.ownerDocument;
74 var ios = Cc["@mozilla.org/network/io-service;1"]
75 .getService(Ci.nsIIOService);
76 var uri = ios.newURI(link.href, target_doc.characterSet, null);
78 if (favicon_service.isFailedFavicon(uri))
81 // Verify that the load of this icon is legal.
82 // error pages can load their favicon, to be on the safe side,
83 // only allow chrome:// favicons
84 const about_neterr = "about:neterror?";
85 if (target_doc.documentURI.substr(0, about_neterr.length) != about_neterr
86 || !uri.schemeIs("chrome"))
88 var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
89 .getService(Ci.nsIScriptSecurityManager);
91 ssm.checkLoadURIWithPrincipal(
92 target_doc.nodePrincipal, uri,
93 Ci.nsIScriptSecurityManager.DISALLOW_SCRIPT);
101 return; // Refuse to load if we can't do a security check.
104 // Security says okay, now ask content policy
105 var content_policy_service = Cc["@mozilla.org/layout/content-policy;1"]
106 .getService(Ci.nsIContentPolicy);
107 if (content_policy_service.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
108 uri, target_doc.documentURIObject,
109 link, link.type, null)
110 != Ci.nsIContentPolicy.ACCEPT)
115 favicon_set(buffer, uri);
119 add_hook("content_buffer_started_loading_hook",
120 favicon_content_buffer_started_loading);
122 add_hook("content_buffer_finished_loading_hook",
123 favicon_content_buffer_finished_loading);
125 add_hook("content_buffer_dom_link_added_hook",
126 favicon_content_buffer_dom_link_added);