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 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 /** | 415 /** |
416 * Decodes UTF-8 string from ArrayBuffer. | 416 * Decodes UTF-8 string from ArrayBuffer. |
417 * | 417 * |
418 * @param {ArrayBuffer} buffer | 418 * @param {ArrayBuffer} buffer |
419 * @return {string} | 419 * @return {string} |
420 */ | 420 */ |
421 base.decodeUtf8 = function(buffer) { | 421 base.decodeUtf8 = function(buffer) { |
422 return decodeURIComponent( | 422 return decodeURIComponent( |
423 escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); | 423 escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); |
424 } | 424 } |
| 425 |
| 426 /** |
| 427 * Generate a nonce, to be used as an xsrf protection token. |
| 428 * |
| 429 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ |
| 430 base.generateXsrfToken = function() { |
| 431 var random = new Uint8Array(16); |
| 432 window.crypto.getRandomValues(random); |
| 433 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); |
| 434 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
| 435 }; |
| 436 |
| 437 /** |
| 438 * @param {string} jsonString A JSON-encoded string. |
| 439 * @return {*} The decoded object, or undefined if the string cannot be parsed. |
| 440 */ |
| 441 base.jsonParseSafe = function(jsonString) { |
| 442 try { |
| 443 return JSON.parse(jsonString); |
| 444 } catch (err) { |
| 445 return undefined; |
| 446 } |
| 447 } |
OLD | NEW |