Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 * The global object. | 6 * The global object. |
| 7 * @type {!Object} | 7 * @type {!Object} |
| 8 * @const | 8 * @const |
| 9 */ | 9 */ |
| 10 var global = this; | 10 var global = this; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 * Helper function for defineProperty that returns the getter to use for the | 102 * Helper function for defineProperty that returns the getter to use for the |
| 103 * property. | 103 * property. |
| 104 * @param {string} name The name of the property. | 104 * @param {string} name The name of the property. |
| 105 * @param {PropertyKind} kind The kind of the property. | 105 * @param {PropertyKind} kind The kind of the property. |
| 106 * @return {function():*} The getter for the property. | 106 * @return {function():*} The getter for the property. |
| 107 */ | 107 */ |
| 108 function getGetter(name, kind) { | 108 function getGetter(name, kind) { |
| 109 switch (kind) { | 109 switch (kind) { |
| 110 case PropertyKind.JS: | 110 case PropertyKind.JS: |
| 111 var privateName = name + '_'; | 111 var privateName = name + '_'; |
| 112 return function() { | 112 return function() { return this[privateName]; }; |
| 113 return this[privateName]; | |
| 114 }; | |
| 115 case PropertyKind.ATTR: | 113 case PropertyKind.ATTR: |
| 116 var attributeName = getAttributeName(name); | 114 var attributeName = getAttributeName(name); |
| 117 return function() { | 115 return function() { return this.getAttribute(attributeName); }; |
| 118 return this.getAttribute(attributeName); | |
| 119 }; | |
| 120 case PropertyKind.BOOL_ATTR: | 116 case PropertyKind.BOOL_ATTR: |
| 121 var attributeName = getAttributeName(name); | 117 var attributeName = getAttributeName(name); |
| 122 return function() { | 118 return function() { return this.hasAttribute(attributeName); }; |
| 123 return this.hasAttribute(attributeName); | |
| 124 }; | |
| 125 } | 119 } |
| 126 | 120 |
| 127 // TODO(dbeam): replace with assertNotReached() in assert.js when I can coax | 121 // TODO(dbeam): replace with assertNotReached() in assert.js when I can coax |
| 128 // the browser/unit tests to preprocess this file through grit. | 122 // the browser/unit tests to preprocess this file through grit. |
| 129 throw 'not reached'; | 123 throw 'not reached'; |
| 130 } | 124 } |
| 131 | 125 |
| 132 /** | 126 /** |
| 133 * Helper function for defineProperty that returns the setter of the right | 127 * Helper function for defineProperty that returns the setter of the right |
| 134 * kind. | 128 * kind. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 } | 207 } |
| 214 | 208 |
| 215 /** | 209 /** |
| 216 * Counter for use with createUid | 210 * Counter for use with createUid |
| 217 */ | 211 */ |
| 218 var uidCounter = 1; | 212 var uidCounter = 1; |
| 219 | 213 |
| 220 /** | 214 /** |
| 221 * @return {number} A new unique ID. | 215 * @return {number} A new unique ID. |
| 222 */ | 216 */ |
| 223 function createUid() { | 217 function createUid() { return uidCounter++; } |
| 224 return uidCounter++; | |
| 225 } | |
| 226 | 218 |
| 227 /** | 219 /** |
| 228 * Returns a unique ID for the item. This mutates the item so it needs to be | 220 * Returns a unique ID for the item. This mutates the item so it needs to be |
| 229 * an object | 221 * an object |
| 230 * @param {!Object} item The item to get the unique ID for. | 222 * @param {!Object} item The item to get the unique ID for. |
| 231 * @return {number} The unique ID for the item. | 223 * @return {number} The unique ID for the item. |
| 232 */ | 224 */ |
| 233 function getUid(item) { | 225 function getUid(item) { |
| 234 if (item.hasOwnProperty('uid')) | 226 if (item.hasOwnProperty('uid')) |
| 235 return item.uid; | 227 return item.uid; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 * @param {!Function} fun The function that will return an object containing | 265 * @param {!Function} fun The function that will return an object containing |
| 274 * the names and values of the new fields. | 266 * the names and values of the new fields. |
| 275 */ | 267 */ |
| 276 function define(name, fun) { | 268 function define(name, fun) { |
| 277 var obj = exportPath(name); | 269 var obj = exportPath(name); |
| 278 var exports = fun(); | 270 var exports = fun(); |
| 279 for (var propertyName in exports) { | 271 for (var propertyName in exports) { |
| 280 // Maybe we should check the prototype chain here? The current usage | 272 // Maybe we should check the prototype chain here? The current usage |
| 281 // pattern is always using an object literal so we only care about own | 273 // pattern is always using an object literal so we only care about own |
| 282 // properties. | 274 // properties. |
| 283 var propertyDescriptor = Object.getOwnPropertyDescriptor(exports, | 275 var propertyDescriptor = |
| 284 propertyName); | 276 Object.getOwnPropertyDescriptor(exports, propertyName); |
| 285 if (propertyDescriptor) | 277 if (propertyDescriptor) |
| 286 Object.defineProperty(obj, propertyName, propertyDescriptor); | 278 Object.defineProperty(obj, propertyName, propertyDescriptor); |
| 287 } | 279 } |
| 288 } | 280 } |
| 289 | 281 |
| 290 /** | 282 /** |
| 291 * Adds a {@code getInstance} static method that always return the same | 283 * Adds a {@code getInstance} static method that always return the same |
| 292 * instance object. | 284 * instance object. |
| 293 * @param {!Function} ctor The constructor for the class to add the static | 285 * @param {!Function} ctor The constructor for the class to add the static |
| 294 * method to. | 286 * method to. |
| 295 */ | 287 */ |
| 296 function addSingletonGetter(ctor) { | 288 function addSingletonGetter(ctor) { |
| 297 ctor.getInstance = function() { | 289 ctor.getInstance = function() { |
| 298 return ctor.instance_ || (ctor.instance_ = new ctor()); | 290 return ctor.instance_ || (ctor.instance_ = new ctor()); |
| 299 }; | 291 }; |
| 300 } | 292 } |
| 301 | 293 |
| 302 /** | 294 /** |
| 303 * Forwards public APIs to private implementations. | 295 * Forwards public APIs to private implementations. |
| 304 * @param {Function} ctor Constructor that have private implementations in its | 296 * @param {Function} ctor Constructor that have private implementations in its |
| 305 * prototype. | 297 * prototype. |
| 306 * @param {Array<string>} methods List of public method names that have their | 298 * @param {Array<string>} methods List of public method names that have their |
| 307 * underscored counterparts in constructor's prototype. | 299 * underscored counterparts in constructor's prototype. |
| 308 * @param {string=} opt_target Selector for target node. | 300 * @param {string=} opt_target Selector for target node. |
| 309 */ | 301 */ |
| 310 function makePublic(ctor, methods, opt_target) { | 302 function makePublic(ctor, methods, opt_target) { |
| 311 methods.forEach(function(method) { | 303 methods.forEach(function(method) { |
| 312 ctor[method] = function() { | 304 ctor[method] = function() { |
| 313 var target = opt_target ? document.getElementById(opt_target) : | 305 var target = opt_target ? document.getElementById(opt_target) : |
| 314 ctor.getInstance(); | 306 ctor.getInstance(); |
|
dschuyler
2016/12/22 19:41:40
In some cases the format seems to favor doing a si
Dan Beam
2016/12/22 19:48:11
this is configurable to some extent
| |
| 315 return target[method + '_'].apply(target, arguments); | 307 return target[method + '_'].apply(target, arguments); |
| 316 }; | 308 }; |
| 317 }); | 309 }); |
| 318 } | 310 } |
| 319 | 311 |
| 320 /** | 312 /** |
| 321 * The mapping used by the sendWithPromise mechanism to tie the Promise | 313 * The mapping used by the sendWithPromise mechanism to tie the Promise |
| 322 * returned to callers with the corresponding WebUI response. The mapping is | 314 * returned to callers with the corresponding WebUI response. The mapping is |
| 323 * from ID to the PromiseResolver helper; the ID is generated by | 315 * from ID to the PromiseResolver helper; the ID is generated by |
| 324 * sendWithPromise and is unique across all invocations of said method. | 316 * sendWithPromise and is unique across all invocations of said method. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 441 makePublic: makePublic, | 433 makePublic: makePublic, |
| 442 PropertyKind: PropertyKind, | 434 PropertyKind: PropertyKind, |
| 443 | 435 |
| 444 // C++ <-> JS communication related methods. | 436 // C++ <-> JS communication related methods. |
| 445 addWebUIListener: addWebUIListener, | 437 addWebUIListener: addWebUIListener, |
| 446 removeWebUIListener: removeWebUIListener, | 438 removeWebUIListener: removeWebUIListener, |
| 447 sendWithPromise: sendWithPromise, | 439 sendWithPromise: sendWithPromise, |
| 448 webUIListenerCallback: webUIListenerCallback, | 440 webUIListenerCallback: webUIListenerCallback, |
| 449 webUIResponse: webUIResponse, | 441 webUIResponse: webUIResponse, |
| 450 | 442 |
| 451 get doc() { | 443 get doc() { return document; }, |
| 452 return document; | |
| 453 }, | |
| 454 | 444 |
| 455 /** Whether we are using a Mac or not. */ | 445 /** Whether we are using a Mac or not. */ |
| 456 get isMac() { | 446 get isMac() { return /Mac/.test(navigator.platform); }, |
| 457 return /Mac/.test(navigator.platform); | |
| 458 }, | |
| 459 | 447 |
| 460 /** Whether this is on the Windows platform or not. */ | 448 /** Whether this is on the Windows platform or not. */ |
| 461 get isWindows() { | 449 get isWindows() { return /Win/.test(navigator.platform); }, |
| 462 return /Win/.test(navigator.platform); | |
| 463 }, | |
| 464 | 450 |
| 465 /** Whether this is on chromeOS or not. */ | 451 /** Whether this is on chromeOS or not. */ |
| 466 get isChromeOS() { | 452 get isChromeOS() { return /CrOS/.test(navigator.userAgent); }, |
| 467 return /CrOS/.test(navigator.userAgent); | |
| 468 }, | |
| 469 | 453 |
| 470 /** Whether this is on vanilla Linux (not chromeOS). */ | 454 /** Whether this is on vanilla Linux (not chromeOS). */ |
| 471 get isLinux() { | 455 get isLinux() { return /Linux/.test(navigator.userAgent); }, |
| 472 return /Linux/.test(navigator.userAgent); | |
| 473 }, | |
| 474 | 456 |
| 475 /** Whether this is on Android. */ | 457 /** Whether this is on Android. */ |
| 476 get isAndroid() { | 458 get isAndroid() { return /Android/.test(navigator.userAgent); }, |
| 477 return /Android/.test(navigator.userAgent); | |
| 478 }, | |
| 479 | 459 |
| 480 /** Whether this is on iOS. */ | 460 /** Whether this is on iOS. */ |
| 481 get isIOS() { | 461 get isIOS() { return /iPad|iPhone|iPod/.test(navigator.platform); } |
| 482 return /iPad|iPhone|iPod/.test(navigator.platform); | |
| 483 } | |
| 484 }; | 462 }; |
| 485 }(); | 463 }(); |
| OLD | NEW |