OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 * @fileoverview | 6 * @fileoverview |
7 * A module that contains basic utility components and methods for the | 7 * A module that contains basic utility components and methods for the |
8 * chromoting project | 8 * chromoting project |
9 * | 9 * |
10 */ | 10 */ |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 return url; | 153 return url; |
154 } | 154 } |
155 var queryParameters = []; | 155 var queryParameters = []; |
156 for (var key in opt_params) { | 156 for (var key in opt_params) { |
157 queryParameters.push(encodeURIComponent(key) + "=" + | 157 queryParameters.push(encodeURIComponent(key) + "=" + |
158 encodeURIComponent(opt_params[key])); | 158 encodeURIComponent(opt_params[key])); |
159 } | 159 } |
160 return url + '?' + queryParameters.join('&'); | 160 return url + '?' + queryParameters.join('&'); |
161 }; | 161 }; |
162 | 162 |
163 | |
164 /** | |
165 * @return {Object.<string, string>} The URL parameters. | |
166 */ | |
167 base.getUrlParameters = function() { | |
kelvinp
2015/02/03 01:15:33
Grouping it closer to the other url utilities
| |
168 var result = {}; | |
169 var parts = window.location.search.substring(1).split('&'); | |
170 for (var i = 0; i < parts.length; i++) { | |
171 var pair = parts[i].split('='); | |
172 result[pair[0]] = decodeURIComponent(pair[1]); | |
173 } | |
174 return result; | |
175 }; | |
176 | |
163 /** | 177 /** |
164 * Convert special characters (e.g. &, < and >) to HTML entities. | 178 * Convert special characters (e.g. &, < and >) to HTML entities. |
165 * | 179 * |
166 * @param {string} str | 180 * @param {string} str |
167 * @return {string} | 181 * @return {string} |
168 */ | 182 */ |
169 base.escapeHTML = function(str) { | 183 base.escapeHTML = function(str) { |
170 var div = document.createElement('div'); | 184 var div = document.createElement('div'); |
171 div.appendChild(document.createTextNode(str)); | 185 div.appendChild(document.createTextNode(str)); |
172 return div.innerHTML; | 186 return div.innerHTML; |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 * | 451 * |
438 * @param {string} string | 452 * @param {string} string |
439 * @return {ArrayBuffer} | 453 * @return {ArrayBuffer} |
440 */ | 454 */ |
441 base.encodeUtf8 = function(string) { | 455 base.encodeUtf8 = function(string) { |
442 var utf8String = unescape(encodeURIComponent(string)); | 456 var utf8String = unescape(encodeURIComponent(string)); |
443 var result = new Uint8Array(utf8String.length); | 457 var result = new Uint8Array(utf8String.length); |
444 for (var i = 0; i < utf8String.length; i++) | 458 for (var i = 0; i < utf8String.length; i++) |
445 result[i] = utf8String.charCodeAt(i); | 459 result[i] = utf8String.charCodeAt(i); |
446 return result.buffer; | 460 return result.buffer; |
447 } | 461 }; |
448 | 462 |
449 /** | 463 /** |
450 * Decodes UTF-8 string from ArrayBuffer. | 464 * Decodes UTF-8 string from ArrayBuffer. |
451 * | 465 * |
452 * @param {ArrayBuffer} buffer | 466 * @param {ArrayBuffer} buffer |
453 * @return {string} | 467 * @return {string} |
454 */ | 468 */ |
455 base.decodeUtf8 = function(buffer) { | 469 base.decodeUtf8 = function(buffer) { |
456 return decodeURIComponent( | 470 return decodeURIComponent( |
457 escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); | 471 escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); |
458 } | 472 }; |
459 | 473 |
460 /** | 474 /** |
461 * Generate a nonce, to be used as an xsrf protection token. | 475 * Generate a nonce, to be used as an xsrf protection token. |
462 * | 476 * |
463 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ | 477 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ |
464 base.generateXsrfToken = function() { | 478 base.generateXsrfToken = function() { |
465 var random = new Uint8Array(16); | 479 var random = new Uint8Array(16); |
466 window.crypto.getRandomValues(random); | 480 window.crypto.getRandomValues(random); |
467 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); | 481 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); |
468 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | 482 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
469 }; | 483 }; |
470 | 484 |
471 /** | 485 /** |
472 * @param {string} jsonString A JSON-encoded string. | 486 * @param {string} jsonString A JSON-encoded string. |
473 * @return {Object|undefined} The decoded object, or undefined if the string | 487 * @return {Object|undefined} The decoded object, or undefined if the string |
474 * cannot be parsed. | 488 * cannot be parsed. |
475 */ | 489 */ |
476 base.jsonParseSafe = function(jsonString) { | 490 base.jsonParseSafe = function(jsonString) { |
477 try { | 491 try { |
478 return /** @type {Object} */ (JSON.parse(jsonString)); | 492 return /** @type {Object} */ (JSON.parse(jsonString)); |
479 } catch (err) { | 493 } catch (err) { |
480 return undefined; | 494 return undefined; |
481 } | 495 } |
482 } | 496 }; |
OLD | NEW |