| 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 * @fileoverview This file defines a singleton which provides access to all data | 6 * @fileoverview This file defines a singleton which provides access to all data |
| 7 * that is available as soon as the page's resources are loaded (before DOM | 7 * that is available as soon as the page's resources are loaded (before DOM |
| 8 * content has finished loading). This data includes both localized strings and | 8 * content has finished loading). This data includes both localized strings and |
| 9 * any data that is important to have ready from a very early stage (e.g. things | 9 * any data that is important to have ready from a very early stage (e.g. things |
| 10 * that must be displayed right away). | 10 * that must be displayed right away). |
| 11 */ | 11 */ |
| 12 | 12 |
| 13 /** @type {!LoadTimeData} */ var loadTimeData; | 13 /** @type {!LoadTimeData} */ var loadTimeData; |
| 14 | 14 |
| 15 // Expose this type globally as a temporary work around until | 15 // Expose this type globally as a temporary work around until |
| 16 // https://github.com/google/closure-compiler/issues/544 is fixed. | 16 // https://github.com/google/closure-compiler/issues/544 is fixed. |
| 17 /** @constructor */ | 17 /** @constructor */ |
| 18 function LoadTimeData() {} | 18 function LoadTimeData(){} |
| 19 | 19 |
| 20 (function() { | 20 (function() { |
| 21 'use strict'; | 21 'use strict'; |
| 22 | 22 |
| 23 LoadTimeData.prototype = { | 23 LoadTimeData.prototype = { |
| 24 /** | 24 /** |
| 25 * Sets the backing object. | 25 * Sets the backing object. |
| 26 * | 26 * |
| 27 * Note that there is no getter for |data_| to discourage abuse of the form: | 27 * Note that there is no getter for |data_| to discourage abuse of the form: |
| 28 * | 28 * |
| 29 * var value = loadTimeData.data()['key']; | 29 * var value = loadTimeData.data()['key']; |
| 30 * | 30 * |
| 31 * @param {Object} value The de-serialized page data. | 31 * @param {Object} value The de-serialized page data. |
| 32 */ | 32 */ |
| 33 set data(value) { | 33 set data(value) { |
| 34 expect(!this.data_, 'Re-setting data.'); | 34 expect(!this.data_, 'Re-setting data.'); |
| 35 this.data_ = value; | 35 this.data_ = value; |
| 36 }, | 36 }, |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Returns a JsEvalContext for |data_|. | 39 * Returns a JsEvalContext for |data_|. |
| 40 * @returns {JsEvalContext} | 40 * @returns {JsEvalContext} |
| 41 */ | 41 */ |
| 42 createJsEvalContext: function() { | 42 createJsEvalContext: function() { return new JsEvalContext(this.data_); }, |
| 43 return new JsEvalContext(this.data_); | |
| 44 }, | |
| 45 | 43 |
| 46 /** | 44 /** |
| 47 * @param {string} id An ID of a value that might exist. | 45 * @param {string} id An ID of a value that might exist. |
| 48 * @return {boolean} True if |id| is a key in the dictionary. | 46 * @return {boolean} True if |id| is a key in the dictionary. |
| 49 */ | 47 */ |
| 50 valueExists: function(id) { | 48 valueExists: function(id) { return id in this.data_; }, |
| 51 return id in this.data_; | |
| 52 }, | |
| 53 | 49 |
| 54 /** | 50 /** |
| 55 * Fetches a value, expecting that it exists. | 51 * Fetches a value, expecting that it exists. |
| 56 * @param {string} id The key that identifies the desired value. | 52 * @param {string} id The key that identifies the desired value. |
| 57 * @return {*} The corresponding value. | 53 * @return {*} The corresponding value. |
| 58 */ | 54 */ |
| 59 getValue: function(id) { | 55 getValue: function(id) { |
| 60 expect(this.data_, 'No data. Did you remember to include strings.js?'); | 56 expect(this.data_, 'No data. Did you remember to include strings.js?'); |
| 61 var value = this.data_[id]; | 57 var value = this.data_[id]; |
| 62 expect(typeof value != 'undefined', 'Could not find value for ' + id); | 58 expect(typeof value != 'undefined', 'Could not find value for ' + id); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 81 * @param {...(string|number)} var_args The extra values to include in the | 77 * @param {...(string|number)} var_args The extra values to include in the |
| 82 * formatted output. | 78 * formatted output. |
| 83 * @return {string} The formatted string. | 79 * @return {string} The formatted string. |
| 84 */ | 80 */ |
| 85 getStringF: function(id, var_args) { | 81 getStringF: function(id, var_args) { |
| 86 var value = this.getString(id); | 82 var value = this.getString(id); |
| 87 if (!value) | 83 if (!value) |
| 88 return ''; | 84 return ''; |
| 89 | 85 |
| 90 var varArgs = arguments; | 86 var varArgs = arguments; |
| 91 return value.replace(/\$[$1-9]/g, function(m) { | 87 return value.replace( |
| 92 return m == '$$' ? '$' : varArgs[m[1]]; | 88 /\$[$1-9]/g, function(m) { return m == '$$' ? '$' : varArgs[m[1]]; }); |
| 93 }); | |
| 94 }, | 89 }, |
| 95 | 90 |
| 96 /** | 91 /** |
| 97 * As above, but also makes sure that the value is a boolean. | 92 * As above, but also makes sure that the value is a boolean. |
| 98 * @param {string} id The key that identifies the desired boolean. | 93 * @param {string} id The key that identifies the desired boolean. |
| 99 * @return {boolean} The corresponding boolean value. | 94 * @return {boolean} The corresponding boolean value. |
| 100 */ | 95 */ |
| 101 getBoolean: function(id) { | 96 getBoolean: function(id) { |
| 102 var value = this.getValue(id); | 97 var value = this.getValue(id); |
| 103 expectIsType(id, value, 'boolean'); | 98 expectIsType(id, value, 'boolean'); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 expectIsType(id, value, 'number'); | 109 expectIsType(id, value, 'number'); |
| 115 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); | 110 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); |
| 116 return /** @type {number} */ (value); | 111 return /** @type {number} */ (value); |
| 117 }, | 112 }, |
| 118 | 113 |
| 119 /** | 114 /** |
| 120 * Override values in loadTimeData with the values found in |replacements|. | 115 * Override values in loadTimeData with the values found in |replacements|. |
| 121 * @param {Object} replacements The dictionary object of keys to replace. | 116 * @param {Object} replacements The dictionary object of keys to replace. |
| 122 */ | 117 */ |
| 123 overrideValues: function(replacements) { | 118 overrideValues: function(replacements) { |
| 124 expect(typeof replacements == 'object', | 119 expect( |
| 125 'Replacements must be a dictionary object.'); | 120 typeof replacements == 'object', |
| 121 'Replacements must be a dictionary object.'); |
| 126 for (var key in replacements) { | 122 for (var key in replacements) { |
| 127 this.data_[key] = replacements[key]; | 123 this.data_[key] = replacements[key]; |
| 128 } | 124 } |
| 129 } | 125 } |
| 130 }; | 126 }; |
| 131 | 127 |
| 132 /** | 128 /** |
| 133 * Checks condition, displays error message if expectation fails. | 129 * Checks condition, displays error message if expectation fails. |
| 134 * @param {*} condition The condition to check for truthiness. | 130 * @param {*} condition The condition to check for truthiness. |
| 135 * @param {string} message The message to display if the check fails. | 131 * @param {string} message The message to display if the check fails. |
| 136 */ | 132 */ |
| 137 function expect(condition, message) { | 133 function expect(condition, message) { |
| 138 if (!condition) { | 134 if (!condition) { |
| 139 console.error('Unexpected condition on ' + document.location.href + ': ' + | 135 console.error( |
| 140 message); | 136 'Unexpected condition on ' + document.location.href + ': ' + message); |
| 141 } | 137 } |
| 142 } | 138 } |
| 143 | 139 |
| 144 /** | 140 /** |
| 145 * Checks that the given value has the given type. | 141 * Checks that the given value has the given type. |
| 146 * @param {string} id The id of the value (only used for error message). | 142 * @param {string} id The id of the value (only used for error message). |
| 147 * @param {*} value The value to check the type on. | 143 * @param {*} value The value to check the type on. |
| 148 * @param {string} type The type we expect |value| to be. | 144 * @param {string} type The type we expect |value| to be. |
| 149 */ | 145 */ |
| 150 function expectIsType(id, value, type) { | 146 function expectIsType(id, value, type) { |
| 151 expect(typeof value == type, '[' + value + '] (' + id + | 147 expect( |
| 152 ') is not a ' + type); | 148 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); |
| 153 } | 149 } |
| 154 | 150 |
| 155 expect(!loadTimeData, 'should only include this file once'); | 151 expect(!loadTimeData, 'should only include this file once'); |
| 156 loadTimeData = new LoadTimeData; | 152 loadTimeData = new LoadTimeData; |
| 157 })(); | 153 })(); |
| OLD | NEW |