1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file adheres to closure-compiler conventions in order to enable
6 // compilation with ADVANCED_OPTIMIZATIONS. In particular, members that are to
7 // be accessed externally should be specified in this['style'] as opposed to
8 // this.style because member identifiers are minified by default.
9 // See http://goo.gl/FwOgy
11 window['chrome'] = window ['chrome'] || {};
13 window['chrome']['send'] = function(message, args) {
14 __gCrWeb.message.invokeOnHost({'command': 'chrome.send',
15 'message': '' + message,
16 'arguments': args || []});
19 // Chrome defines bind on all functions, so this is expected to exist by
21 Function.prototype.bind = function(context) {
22 // Reference to the Function instance.
24 // Reference to the current arguments.
25 var curriedArguments = [];
26 for (var i = 1; i < arguments.length; i++)
27 curriedArguments.push(arguments[i]);
29 var finalArguments = [];
30 for (var i = 0; i < curriedArguments.length; i++)
31 finalArguments.push(curriedArguments[i]);
32 for (var i = 0; i < arguments.length; i++)
33 finalArguments.push(arguments[i]);
34 return self.apply(context, finalArguments);
39 * Sends message requesting favicon at the URL from imageSet for el. Sets
40 * favicon-url attribute on el to the favicon URL.
41 * @param {Element} el The DOM element to request the favicon for.
42 * @param {string} imageSet The CSS -webkit-image-set.
44 window['chrome']['requestFavicon'] = function(el, imageSet) {
45 var cssUrls = imageSet.match(/url\([^\)]+\) \dx/g);
46 // TODO(jyquinn): Review match above (crbug.com/528080).
50 // Extract url from CSS -webkit-image-set.
52 for (var i = 0; i < cssUrls.length; ++i) {
53 var scaleFactorExp = /(\d)x$/;
54 var scaleFactor = getSupportedScaleFactors()[0];
55 if (parseInt(scaleFactorExp.exec(cssUrls[i])[1], 10) === scaleFactor) {
56 var urlExp = /url\(\"(.+)\"\)/;
57 faviconUrl = urlExp.exec(cssUrls[i])[1];
61 el.setAttribute('favicon-url', url(faviconUrl));
62 chrome.send('webui.requestFavicon', [faviconUrl]);
66 * Called to set elements with favicon-url attribute equal to faviconUrl to
68 * @param {string} faviconUrl Favicon URL used to locate favicon element
69 * via the favicon-url attribute.
70 * @param {string} dataUrl The data URL to assign to the favicon element's
73 window['chrome']['setFaviconBackground'] = function(faviconUrl, dataUrl) {
74 var selector = "[favicon-url='" + url(faviconUrl) + "']";
75 var elements = document.querySelectorAll(selector);
76 for (var i = 0; i < elements.length; ++i) {
77 elements[i].style.backgroundImage = url(dataUrl);