Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/resources/shared/js/util.js

Issue 11762004: Make favicons for about:history and about:bookmarks switch from lodpi to hidpi when dragging browser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * The global object. 6 * The global object.
7 * @type {!Object} 7 * @type {!Object}
8 * @const 8 * @const
9 */ 9 */
10 var global = this; 10 var global = this;
(...skipping 20 matching lines...) Expand all
31 // restore 31 // restore
32 global[callbackName] = old; 32 global[callbackName] = old;
33 33
34 var args = Array.prototype.slice.call(arguments); 34 var args = Array.prototype.slice.call(arguments);
35 return callback.apply(global, args); 35 return callback.apply(global, args);
36 }; 36 };
37 chrome.send(name, params); 37 chrome.send(name, params);
38 } 38 }
39 39
40 /** 40 /**
41 * Returns the scale factors supported by this platform.
42 * @return {array} The supported scale factors.
43 */
44 function getSupportedScaleFactors() {
45 var supportedScaleFactors = [];
46 if (cr.isMac || cr.isChromeOS) {
47 supportedScaleFactors.push(1);
48 supportedScaleFactors.push(2);
49 } else {
50 // Windows must be restarted to display at a different scale factor.
51 supportedScaleFactors.push(window.devicePixelRatio);
52 }
53 return supportedScaleFactors;
54 }
55
56 /**
41 * Generates a CSS url string. 57 * Generates a CSS url string.
42 * @param {string} s The URL to generate the CSS url for. 58 * @param {string} s The URL to generate the CSS url for.
43 * @return {string} The CSS url string. 59 * @return {string} The CSS url string.
44 */ 60 */
45 function url(s) { 61 function url(s) {
46 // http://www.w3.org/TR/css3-values/#uris 62 // http://www.w3.org/TR/css3-values/#uris
47 // Parentheses, commas, whitespace characters, single quotes (') and double 63 // Parentheses, commas, whitespace characters, single quotes (') and double
48 // quotes (") appearing in a URI must be escaped with a backslash 64 // quotes (") appearing in a URI must be escaped with a backslash
49 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1'); 65 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
50 // WebKit has a bug when it comes to URLs that end with \ 66 // WebKit has a bug when it comes to URLs that end with \
51 // https://bugs.webkit.org/show_bug.cgi?id=28885 67 // https://bugs.webkit.org/show_bug.cgi?id=28885
52 if (/\\\\$/.test(s2)) { 68 if (/\\\\$/.test(s2)) {
53 // Add a space to work around the WebKit bug. 69 // Add a space to work around the WebKit bug.
54 s2 += ' '; 70 s2 += ' ';
55 } 71 }
56 return 'url("' + s2 + '")'; 72 return 'url("' + s2 + '")';
57 } 73 }
58 74
59 /** 75 /**
76 * Generates a CSS -webkit-image-set for a chrome:// url.
77 * An entry in the image set is added for each of getSupportedScaleFactors().
78 * The scale-factor-specific url is generated by replacing the first instance of
79 * 'scalefactor' in |path| with the numeric scale factor.
80 * @param {string} path The URL to generate an image set for.
81 * 'scalefactor' should be a substring of |path|.
82 * @return {string} The CSS -webkit-image-set.
83 */
84 function imageset(path) {
85 var supportedScaleFactors = getSupportedScaleFactors();
86
87 var replaceStartIndex = path.indexOf('scalefactor');
88 if (replaceStartIndex < 0)
89 return url(path);
90
91 var s = '';
92 for (var i = 0; i < supportedScaleFactors.length; ++i) {
93 var scaleFactor = supportedScaleFactors[i];
94 var pathWithScaleFactor = path.substr(0, replaceStartIndex) + scaleFactor +
95 path.substr(replaceStartIndex + 'scalefactor'.length);
96
97 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x';
98
99 if (i != supportedScaleFactors.length - 1)
100 s += ', ';
101 }
102 return '-webkit-image-set(' + s + ')';
103 }
104
105 /**
60 * Parses query parameters from Location. 106 * Parses query parameters from Location.
61 * @param {string} location The URL to generate the CSS url for. 107 * @param {string} location The URL to generate the CSS url for.
62 * @return {object} Dictionary containing name value pairs for URL 108 * @return {object} Dictionary containing name value pairs for URL
63 */ 109 */
64 function parseQueryParams(location) { 110 function parseQueryParams(location) {
65 var params = {}; 111 var params = {};
66 var query = unescape(location.search.substring(1)); 112 var query = unescape(location.search.substring(1));
67 var vars = query.split('&'); 113 var vars = query.split('&');
68 for (var i = 0; i < vars.length; i++) { 114 for (var i = 0; i < vars.length; i++) {
69 var pair = vars[i].split('='); 115 var pair = vars[i].split('=');
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 */ 267 */
222 function appendParam(url, key, value) { 268 function appendParam(url, key, value) {
223 var param = encodeURIComponent(key) + '=' + encodeURIComponent(value); 269 var param = encodeURIComponent(key) + '=' + encodeURIComponent(value);
224 270
225 if (url.indexOf('?') == -1) 271 if (url.indexOf('?') == -1)
226 return url + '?' + param; 272 return url + '?' + param;
227 return url + '&' + param; 273 return url + '&' + param;
228 } 274 }
229 275
230 /** 276 /**
231 * Creates a new URL for a favicon request. 277 * Creates a CSS -webkit-image-set for a favicon request.
278 * @param {string} url The url for the favicon.
279 * @param {number=} opt_size Optional preferred size of the favicon.
280 * @param {boolean=} opt_sessionFavicon Optional flag to indicate if
281 * requesting a session favicon.
282 * @return {string} -webkit-image-set for the favicon.
283 */
284 function getFaviconImageSet(url, opt_size, opt_sessionFavicon) {
285 var size = opt_size || 16;
286 var type = opt_sessionFavicon ? 'session-favicon' : 'favicon';
287 return imageset(
288 'chrome://' + type + '/size/' + size + '@scalefactorx/' + url);
289 }
290
291 /**
292 * Creates a new URL for a favicon request for the current device pixel ratio.
293 * The URL must be updated when the user moves the browser to a screen with a
294 * different device pixel ratio. Use getFaviconImageSet() for the updating to
295 * occur automatically.
232 * @param {string} url The url for the favicon. 296 * @param {string} url The url for the favicon.
233 * @param {number=} opt_size Optional preferred size of the favicon. 297 * @param {number=} opt_size Optional preferred size of the favicon.
234 * @param {boolean=} opt_sessionFavicon Optional flag to indicate if 298 * @param {boolean=} opt_sessionFavicon Optional flag to indicate if
235 * requesting a session favicon. 299 * requesting a session favicon.
236 * @return {string} Updated URL for the favicon. 300 * @return {string} Updated URL for the favicon.
237 */ 301 */
238 function getFaviconUrl(url, opt_size, opt_sessionFavicon) { 302 function getFaviconUrlForCurrentDevicePixelRatio(url,
303 opt_size,
304 opt_sessionFavicon) {
239 var size = opt_size || 16; 305 var size = opt_size || 16;
240 var type = opt_sessionFavicon ? 'session-favicon' : 'favicon'; 306 var type = opt_sessionFavicon ? 'session-favicon' : 'favicon';
241 return 'chrome://' + type + '/size/' + size + '@' + 307 return 'chrome://' + type + '/size/' + size + '@' +
242 window.devicePixelRatio + 'x/' + url; 308 window.devicePixelRatio + 'x/' + url;
243 } 309 }
244 310
245 /** 311 /**
246 * Creates an element of a specified type with a specified class name. 312 * Creates an element of a specified type with a specified class name.
247 * @param {string} type The node type. 313 * @param {string} type The node type.
248 * @param {string} className The class name to use. 314 * @param {string} className The class name to use.
249 * @return {Element} The created element. 315 * @return {Element} The created element.
250 */ 316 */
251 function createElementWithClassName(type, className) { 317 function createElementWithClassName(type, className) {
252 var elm = document.createElement(type); 318 var elm = document.createElement(type);
253 elm.className = className; 319 elm.className = className;
254 return elm; 320 return elm;
255 } 321 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698