| 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 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 * | 703 * |
| 704 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ | 704 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ |
| 705 base.generateXsrfToken = function() { | 705 base.generateXsrfToken = function() { |
| 706 var random = new Uint8Array(16); | 706 var random = new Uint8Array(16); |
| 707 window.crypto.getRandomValues(random); | 707 window.crypto.getRandomValues(random); |
| 708 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); | 708 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); |
| 709 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | 709 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
| 710 }; | 710 }; |
| 711 | 711 |
| 712 /** | 712 /** |
| 713 * @return {string} A random UUID. |
| 714 */ |
| 715 base.generateUuid = function() { |
| 716 var random = new Uint16Array(8); |
| 717 window.crypto.getRandomValues(random); |
| 718 /** @type {Array<string>} */ |
| 719 var e = new Array(); |
| 720 for (var i = 0; i < 8; i++) { |
| 721 e[i] = (/** @type {number} */ (random[i]) + 0x10000). |
| 722 toString(16).substring(1); |
| 723 } |
| 724 return e[0] + e[1] + '-' + e[2] + '-' + e[3] + '-' + |
| 725 e[4] + '-' + e[5] + e[6] + e[7]; |
| 726 }; |
| 727 |
| 728 /** |
| 713 * @param {string} jsonString A JSON-encoded string. | 729 * @param {string} jsonString A JSON-encoded string. |
| 714 * @return {Object|undefined} The decoded object, or undefined if the string | 730 * @return {Object|undefined} The decoded object, or undefined if the string |
| 715 * cannot be parsed. | 731 * cannot be parsed. |
| 716 */ | 732 */ |
| 717 base.jsonParseSafe = function(jsonString) { | 733 base.jsonParseSafe = function(jsonString) { |
| 718 try { | 734 try { |
| 719 return /** @type {Object} */ (JSON.parse(jsonString)); | 735 return /** @type {Object} */ (JSON.parse(jsonString)); |
| 720 } catch (err) { | 736 } catch (err) { |
| 721 return undefined; | 737 return undefined; |
| 722 } | 738 } |
| 723 }; | 739 }; |
| 724 | 740 |
| 725 /** | 741 /** |
| 726 * Size the current window to fit its content vertically. | 742 * Size the current window to fit its content vertically. |
| 727 */ | 743 */ |
| 728 base.resizeWindowToContent = function() { | 744 base.resizeWindowToContent = function() { |
| 729 var appWindow = chrome.app.window.current(); | 745 var appWindow = chrome.app.window.current(); |
| 730 var outerBounds = appWindow.outerBounds; | 746 var outerBounds = appWindow.outerBounds; |
| 731 var borderY = outerBounds.height - appWindow.innerBounds.height; | 747 var borderY = outerBounds.height - appWindow.innerBounds.height; |
| 732 appWindow.resizeTo(outerBounds.width, document.body.clientHeight + borderY); | 748 appWindow.resizeTo(outerBounds.width, document.body.clientHeight + borderY); |
| 733 // Sometimes, resizing the window causes its position to be reset to (0, 0), | 749 // Sometimes, resizing the window causes its position to be reset to (0, 0), |
| 734 // so restore it explicitly. | 750 // so restore it explicitly. |
| 735 appWindow.moveTo(outerBounds.left, outerBounds.top); | 751 appWindow.moveTo(outerBounds.left, outerBounds.top); |
| 736 }; | 752 }; |
| OLD | NEW |