Commit 4dcc8e2b authored by Dan Beam's avatar Dan Beam Committed by Commit Bot

Brush up some [favicon] URL parsing/creation code

R=dpapad@chromium.org
BUG=953962

Change-Id: I1b74794585cb6a53ad5f5278a4b4262c9019f280
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1730295
Commit-Queue: Dan Beam <dbeam@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Auto-Submit: Dan Beam <dbeam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683456}
parent 10be2c7a
...@@ -517,9 +517,10 @@ const NetworkUI = (function() { ...@@ -517,9 +517,10 @@ const NetworkUI = (function() {
.getMojoServiceProxy(); .getMojoServiceProxy();
/** Set the refresh rate if the interval is found in the url. */ /** Set the refresh rate if the interval is found in the url. */
const interval = parseQueryParams(window.location)['refresh']; const interval = new URL(window.location.href).searchParams.get('refresh');
if (interval && interval != '') if (interval) {
setInterval(requestNetworks, parseInt(interval, 10) * 1000); setInterval(requestNetworks, parseInt(interval, 10) * 1000);
}
}; };
/** /**
......
...@@ -124,9 +124,9 @@ const DeviceLogUI = (function() { ...@@ -124,9 +124,9 @@ const DeviceLogUI = (function() {
* Sets refresh rate if the interval is found in the url. * Sets refresh rate if the interval is found in the url.
*/ */
const setRefresh = function() { const setRefresh = function() {
const interval = parseQueryParams(window.location)['refresh']; const interval = new URL(window.location).searchParams.get('refresh');
if (interval && interval != '') { if (interval) {
setInterval(requestLog, parseInt(interval) * 1000); setInterval(requestLog, parseInt(interval, 10) * 1000);
} }
}; };
......
...@@ -520,8 +520,7 @@ cr.define('local_discovery', function() { ...@@ -520,8 +520,7 @@ cr.define('local_discovery', function() {
function getOverlayIDFromPath() { function getOverlayIDFromPath() {
if (document.location.pathname == '/register') { if (document.location.pathname == '/register') {
const params = parseQueryParams(document.location); return new URL(document.location).searchParams.get('id');
return params['id'] || null;
} }
} }
......
...@@ -60,6 +60,12 @@ function convertArrayToRGBAColor(rgbaColor) { ...@@ -60,6 +60,12 @@ function convertArrayToRGBAColor(rgbaColor) {
* Parses query parameters from Location. * Parses query parameters from Location.
* @param {!Location} location The URL to generate the CSS url for. * @param {!Location} location The URL to generate the CSS url for.
* @return {Object} Dictionary containing name value pairs for URL. * @return {Object} Dictionary containing name value pairs for URL.
*
* TODO(dbeam): we should update callers of this method to use
* URLSearchParams#get() instead (which I have a higher confidence handles
* escaping and edge cases correctly). Note: that calling URLSearchParams#get()
* also has the behavior of only returning the first &param= in the URL (i.e.
* ?param=1&param=2 + .get('param') would return '1').
*/ */
function parseQueryParams(location) { function parseQueryParams(location) {
const params = Object.create(null); const params = Object.create(null);
......
...@@ -129,19 +129,26 @@ cr.define('cr.icon', function() { ...@@ -129,19 +129,26 @@ cr.define('cr.icon', function() {
* @return {string} -webkit-image-set for the favicon. * @return {string} -webkit-image-set for the favicon.
*/ */
function getFavicon(url, isSyncedUrlForHistoryUi, remoteIconUrlForUma = '') { function getFavicon(url, isSyncedUrlForHistoryUi, remoteIconUrlForUma = '') {
const isIconUrl = FAVICON_URL_REGEX.test(url); // Note: URL param keys used below must match those in the description of
// Note: Literal strings used below must match those in the description of
// chrome://favicon2 format in components/favicon_base/favicon_url_parser.h. // chrome://favicon2 format in components/favicon_base/favicon_url_parser.h.
let path = 'chrome://favicon2/?size=16&scale_factor=SCALEFACTORx' + const faviconUrl = new URL('chrome://favicon2/');
(isIconUrl ? '&icon_url=' : '&page_url=') + encodeURIComponent(url); faviconUrl.searchParams.set('size', '16');
if (!isIconUrl) { faviconUrl.searchParams.set('scale_factor', 'SCALEFACTORx');
path += '&allow_google_server_fallback=' +
(isSyncedUrlForHistoryUi ? '1' : '0'); if (FAVICON_URL_REGEX.test(url)) {
faviconUrl.searchParams.set('icon_url', url);
} else {
faviconUrl.searchParams.set('page_url', url);
// TODO(dbeam): use the presence of 'allow_google_server_fallback' to
// indicate true, otherwise false.
const fallback = isSyncedUrlForHistoryUi ? '1' : '0';
faviconUrl.searchParams.set('allow_google_server_fallback', fallback);
if (isSyncedUrlForHistoryUi) { if (isSyncedUrlForHistoryUi) {
path += '&icon_url=' + encodeURIComponent(remoteIconUrlForUma); faviconUrl.searchParams.set('icon_url', remoteIconUrlForUma);
} }
} }
return getImageSet(path);
return getImageSet(faviconUrl.toString());
} }
return { return {
......
...@@ -69,42 +69,6 @@ ...@@ -69,42 +69,6 @@
} }
// </if> // </if>
/**
* Parses query parameters from Location.
* @param {Location} location The URL to generate the CSS url for.
* @return {Object} Dictionary containing name value pairs for URL
*/
/* #export */ function parseQueryParams(location) {
const params = {};
const query = unescape(location.search.substring(1));
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
params[pair[0]] = pair[1];
}
return params;
}
/**
* Creates a new URL by appending or replacing the given query key and value.
* Not supporting URL with username and password.
* @param {Location} location The original URL.
* @param {string} key The query parameter name.
* @param {string} value The query parameter value.
* @return {string} The constructed new URL.
*/
/* #export */ function setQueryParam(location, key, value) {
const query = parseQueryParams(location);
query[encodeURIComponent(key)] = encodeURIComponent(value);
let newQuery = '';
for (const q in query) {
newQuery += (newQuery ? '&' : '?') + q + '=' + query[q];
}
return location.origin + location.pathname + newQuery + location.hash;
}
/** /**
* @param {Node} el A node to search for ancestors with |className|. * @param {Node} el A node to search for ancestors with |className|.
* @param {string} className A class to search for. * @param {string} className A class to search for.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment