Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * @return {string} The callstack of the current method. | 44 * @return {string} The callstack of the current method. |
| 45 */ | 45 */ |
| 46 base.debug.callstack = function() { | 46 base.debug.callstack = function() { |
| 47 try { | 47 try { |
| 48 throw new Error(); | 48 throw new Error(); |
| 49 } catch (e) { | 49 } catch (/** @type {*} */ e) { |
| 50 var error = /** @type {Error} */ e; | 50 var error = /** @type {Error} */ (e); |
| 51 var callstack = error.stack | 51 var callstack = error.stack |
| 52 .replace(/^\s+(at eval )?at\s+/gm, '') // Remove 'at' and indentation. | 52 .replace(/^\s+(at eval )?at\s+/gm, '') // Remove 'at' and indentation. |
| 53 .split('\n'); | 53 .split('\n'); |
| 54 callstack.splice(0,2); // Remove the stack of the current function. | 54 callstack.splice(0,2); // Remove the stack of the current function. |
| 55 } | 55 } |
| 56 return callstack.join('\n'); | 56 return callstack.join('\n'); |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * @interface | 60 * @interface |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 84 base.debug.assert(!dest.hasOwnProperty(prop),"Don't override properties"); | 84 base.debug.assert(!dest.hasOwnProperty(prop),"Don't override properties"); |
| 85 dest[prop] = src[prop]; | 85 dest[prop] = src[prop]; |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Adds a mixin to a class. | 91 * Adds a mixin to a class. |
| 92 * @param {Object} dest | 92 * @param {Object} dest |
| 93 * @param {Object} src | 93 * @param {Object} src |
| 94 * @suppress {checkTypes} | 94 * @suppress {checkTypes|reportUnknownTypes} |
| 95 */ | 95 */ |
| 96 base.extend = function(dest, src) { | 96 base.extend = function(dest, src) { |
| 97 base.mix(dest.prototype, src.prototype || src); | 97 base.mix(dest.prototype, src.prototype || src); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 base.doNothing = function() {}; | 100 base.doNothing = function() {}; |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Returns an array containing the values of |dict|. | 103 * Returns an array containing the values of |dict|. |
| 104 * @param {!Object} dict | 104 * @param {!Object} dict |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 * window.setTimeout(function() { | 188 * window.setTimeout(function() { |
| 189 * deferred.resolve(); | 189 * deferred.resolve(); |
| 190 * }, 100); | 190 * }, 100); |
| 191 * return deferred.promise(); | 191 * return deferred.promise(); |
| 192 * }; | 192 * }; |
| 193 * | 193 * |
| 194 * @constructor | 194 * @constructor |
| 195 */ | 195 */ |
| 196 base.Deferred = function() { | 196 base.Deferred = function() { |
| 197 /** | 197 /** |
| 198 * @type {?function(?=)} | 198 * @type {?function(?):void} |
| 199 * @private | 199 * @private |
| 200 */ | 200 */ |
| 201 this.resolve_ = null; | 201 this.resolve_ = null; |
| 202 | 202 |
| 203 /** | 203 /** |
| 204 * @type {?function(?)} | 204 * @type {?function(?):void} |
| 205 * @private | 205 * @private |
| 206 */ | 206 */ |
| 207 this.reject_ = null; | 207 this.reject_ = null; |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * @this {base.Deferred} | |
| 211 * @param {function(?):void} resolve | |
| 212 * @param {function(*):void} reject | |
| 213 */ | |
| 214 var recordPromise = function(resolve, reject) { | |
|
garykac
2015/01/12 20:22:37
Hoisted here because @this doesn't seem to work fo
| |
| 215 this.resolve_ = resolve; | |
| 216 this.reject_ = reject; | |
| 217 }; | |
| 218 | |
| 219 /** | |
| 210 * @type {Promise} | 220 * @type {Promise} |
| 211 * @private | 221 * @private |
| 212 */ | 222 */ |
| 213 this.promise_ = new Promise( | 223 this.promise_ = new Promise(recordPromise.bind(this)); |
| 214 /** | |
| 215 * @param {function(?=):void} resolve | |
| 216 * @param {function(?):void} reject | |
| 217 * @this {base.Deferred} | |
| 218 */ | |
| 219 function(resolve, reject) { | |
| 220 this.resolve_ = resolve; | |
| 221 this.reject_ = reject; | |
| 222 }.bind(this) | |
| 223 ); | |
| 224 }; | 224 }; |
| 225 | 225 |
| 226 /** @param {*} reason */ | 226 /** @param {*} reason */ |
| 227 base.Deferred.prototype.reject = function(reason) { | 227 base.Deferred.prototype.reject = function(reason) { |
| 228 this.reject_(reason); | 228 this.reject_(reason); |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 /** @param {*=} opt_value */ | 231 /** @param {*=} opt_value */ |
| 232 base.Deferred.prototype.resolve = function(opt_value) { | 232 base.Deferred.prototype.resolve = function(opt_value) { |
| 233 this.resolve_(opt_value); | 233 this.resolve_(opt_value); |
| 234 }; | 234 }; |
| 235 | 235 |
| 236 /** @return {Promise} */ | 236 /** @return {Promise} */ |
| 237 base.Deferred.prototype.promise = function() { | 237 base.Deferred.prototype.promise = function() { |
| 238 return this.promise_; | 238 return this.promise_; |
| 239 }; | 239 }; |
| 240 | 240 |
| 241 base.Promise = function() {}; | 241 base.Promise = function() {}; |
| 242 | 242 |
| 243 /** | 243 /** |
| 244 * @param {number} delay | 244 * @param {number} delay |
| 245 * @return {Promise} a Promise that will be fulfilled after |delay| ms. | 245 * @return {Promise} a Promise that will be fulfilled after |delay| ms. |
| 246 */ | 246 */ |
| 247 base.Promise.sleep = function(delay) { | 247 base.Promise.sleep = function(delay) { |
| 248 return new Promise( | 248 return new Promise( |
| 249 /** @param {function():void} fulfill */ | 249 /** @param {function(*):void} fulfill */ |
| 250 function(fulfill) { | 250 function(fulfill) { |
| 251 window.setTimeout(fulfill, delay); | 251 window.setTimeout(fulfill, delay); |
| 252 }); | 252 }); |
| 253 }; | 253 }; |
| 254 | 254 |
| 255 | 255 |
| 256 /** | 256 /** |
| 257 * @param {Promise} promise | 257 * @param {Promise} promise |
| 258 * @return {Promise} a Promise that will be fulfilled iff the specified Promise | 258 * @return {Promise} a Promise that will be fulfilled iff the specified Promise |
| 259 * is rejected. | 259 * is rejected. |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ | 429 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ |
| 430 base.generateXsrfToken = function() { | 430 base.generateXsrfToken = function() { |
| 431 var random = new Uint8Array(16); | 431 var random = new Uint8Array(16); |
| 432 window.crypto.getRandomValues(random); | 432 window.crypto.getRandomValues(random); |
| 433 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); | 433 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); |
| 434 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | 434 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
| 435 }; | 435 }; |
| 436 | 436 |
| 437 /** | 437 /** |
| 438 * @param {string} jsonString A JSON-encoded string. | 438 * @param {string} jsonString A JSON-encoded string. |
| 439 * @return {*} The decoded object, or undefined if the string cannot be parsed. | 439 * @return {Object|undefined} The decoded object, or undefined if the string |
| 440 * cannot be parsed. | |
| 440 */ | 441 */ |
| 441 base.jsonParseSafe = function(jsonString) { | 442 base.jsonParseSafe = function(jsonString) { |
| 442 try { | 443 try { |
| 443 return JSON.parse(jsonString); | 444 return /** @type {Object} */ (JSON.parse(jsonString)); |
| 444 } catch (err) { | 445 } catch (err) { |
| 445 return undefined; | 446 return undefined; |
| 446 } | 447 } |
| 447 } | 448 } |
| OLD | NEW |