| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 // <include src="cr.js"> |
| 6 // <include src="util.js"> |
| 7 |
| 8 cr.define('cr.icon', function() { |
| 9 /** |
| 10 * @return {!Array<number>} The scale factors supported by this platform for |
| 11 * webui resources. |
| 12 */ |
| 13 function getSupportedScaleFactors() { |
| 14 var supportedScaleFactors = []; |
| 15 if (cr.isMac || cr.isChromeOS || cr.isWindows || cr.isLinux) { |
| 16 // All desktop platforms support zooming which also updates the |
| 17 // renderer's device scale factors (a.k.a devicePixelRatio), and |
| 18 // these platforms has high DPI assets for 2.0x. Use 1x and 2x in |
| 19 // image-set on these platforms so that the renderer can pick the |
| 20 // closest image for the current device scale factor. |
| 21 supportedScaleFactors.push(1); |
| 22 supportedScaleFactors.push(2); |
| 23 } else { |
| 24 // For other platforms that use fixed device scale factor, use |
| 25 // the window's device pixel ratio. |
| 26 // TODO(oshima): Investigate if Android/iOS need to use image-set. |
| 27 supportedScaleFactors.push(window.devicePixelRatio); |
| 28 } |
| 29 return supportedScaleFactors; |
| 30 } |
| 31 |
| 32 /** |
| 33 * Returns the URL of the image, or an image set of URLs for the profile |
| 34 * avatar. Default avatars have resources available for multiple scalefactors, |
| 35 * whereas the GAIA profile image only comes in one size. |
| 36 * |
| 37 * @param {string} path The path of the image. |
| 38 * @return {string} The url, or an image set of URLs of the avatar image. |
| 39 */ |
| 40 function getProfileAvatarIcon(path) { |
| 41 var chromeThemePath = 'chrome://theme'; |
| 42 var isDefaultAvatar = |
| 43 (path.slice(0, chromeThemePath.length) == chromeThemePath); |
| 44 return isDefaultAvatar ? imageset(path + '@scalefactorx'): url(path); |
| 45 } |
| 46 |
| 47 /** |
| 48 * Generates a CSS -webkit-image-set for a chrome:// url. |
| 49 * An entry in the image set is added for each of getSupportedScaleFactors(). |
| 50 * The scale-factor-specific url is generated by replacing the first instance |
| 51 * of 'scalefactor' in |path| with the numeric scale factor. |
| 52 * @param {string} path The URL to generate an image set for. |
| 53 * 'scalefactor' should be a substring of |path|. |
| 54 * @return {string} The CSS -webkit-image-set. |
| 55 */ |
| 56 function imageset(path) { |
| 57 var supportedScaleFactors = getSupportedScaleFactors(); |
| 58 |
| 59 var replaceStartIndex = path.indexOf('scalefactor'); |
| 60 if (replaceStartIndex < 0) |
| 61 return url(path); |
| 62 |
| 63 var s = ''; |
| 64 for (var i = 0; i < supportedScaleFactors.length; ++i) { |
| 65 var scaleFactor = supportedScaleFactors[i]; |
| 66 var pathWithScaleFactor = path.substr(0, replaceStartIndex) + |
| 67 scaleFactor + path.substr(replaceStartIndex + 'scalefactor'.length); |
| 68 |
| 69 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x'; |
| 70 |
| 71 if (i != supportedScaleFactors.length - 1) |
| 72 s += ', '; |
| 73 } |
| 74 return '-webkit-image-set(' + s + ')'; |
| 75 } |
| 76 |
| 77 /** |
| 78 * A regular expression for identifying favicon URLs. |
| 79 * @const {!RegExp} |
| 80 */ |
| 81 var FAVICON_URL_REGEX = /\.ico$/i; |
| 82 |
| 83 /** |
| 84 * Creates a CSS -webkit-image-set for a favicon request. |
| 85 * @param {string} url Either the URL of the original page or of the favicon |
| 86 * itself. |
| 87 * @param {number=} opt_size Optional preferred size of the favicon. |
| 88 * @param {string=} opt_type Optional type of favicon to request. Valid values |
| 89 * are 'favicon' and 'touch-icon'. Default is 'favicon'. |
| 90 * @return {string} -webkit-image-set for the favicon. |
| 91 */ |
| 92 function getFaviconImageSet(url, opt_size, opt_type) { |
| 93 var size = opt_size || 16; |
| 94 var type = opt_type || 'favicon'; |
| 95 |
| 96 return imageset( |
| 97 'chrome://' + type + '/size/' + size + '@scalefactorx/' + |
| 98 // Note: Literal 'iconurl' must match |kIconURLParameter| in |
| 99 // components/favicon_base/favicon_url_parser.cc. |
| 100 (FAVICON_URL_REGEX.test(url) ? 'iconurl/' : '') + url); |
| 101 } |
| 102 |
| 103 return { |
| 104 getProfileAvatarIcon: getProfileAvatarIcon, |
| 105 getFaviconImageSet: getFaviconImageSet, |
| 106 }; |
| 107 }); |
| OLD | NEW |