| 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). |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 /** | 22 /** |
| 23 * Sets the backing object. | 23 * Sets the backing object. |
| 24 * @param {Object} value The de-serialized page data. | 24 * @param {Object} value The de-serialized page data. |
| 25 */ | 25 */ |
| 26 set data(value) { | 26 set data(value) { |
| 27 assert(!this.data_, 'Re-setting data.'); | 27 assert(!this.data_, 'Re-setting data.'); |
| 28 this.data_ = value; | 28 this.data_ = value; |
| 29 }, | 29 }, |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @return {boolean} True if |id| is a key in the dictionary. |
| 33 */ |
| 34 valueExists: function(id) { |
| 35 return id in this.data_; |
| 36 }, |
| 37 |
| 38 /** |
| 32 * Fetches a value, asserting that it exists. | 39 * Fetches a value, asserting that it exists. |
| 33 * @param {string} id The key that identifies the desired value. | 40 * @param {string} id The key that identifies the desired value. |
| 34 * @return {*} The corresponding value. | 41 * @return {*} The corresponding value. |
| 35 */ | 42 */ |
| 36 getValue: function(id) { | 43 getValue: function(id) { |
| 37 assert(this.data_, 'No data. Did you remember to include strings.js?'); | 44 assert(this.data_, 'No data. Did you remember to include strings.js?'); |
| 38 var value = this.data_[id]; | 45 var value = this.data_[id]; |
| 39 assert(typeof value != 'undefined', 'Could not find value for ' + id); | 46 assert(typeof value != 'undefined', 'Could not find value for ' + id); |
| 40 return value; | 47 return value; |
| 41 }, | 48 }, |
| 42 | 49 |
| 43 /** | 50 /** |
| 44 * As above, but also makes sure that the value is a string. | 51 * As above, but also makes sure that the value is a string. |
| 45 * @param {string} id The key that identifies the desired string. | 52 * @param {string} id The key that identifies the desired string. |
| 46 * @return {string} The corresponding string value. | 53 * @return {string} The corresponding string value. |
| 47 */ | 54 */ |
| 48 getString: function(id) { | 55 getString: function(id) { |
| 49 var value = this.getValue(id); | 56 var value = this.getValue(id); |
| 50 assert(typeof value == 'string', '[' + value + '] (' + id + | 57 assertIsType(id, value, 'string'); |
| 51 ') is not a string'); | 58 return value; |
| 59 }, |
| 60 |
| 61 /** |
| 62 * As above, but also makes sure that the value is a boolean. |
| 63 * @param {string} id The key that identifies the desired boolean. |
| 64 * @return {boolean} The corresponding boolean value. |
| 65 */ |
| 66 getBoolean: function(id) { |
| 67 var value = this.getValue(id); |
| 68 assertIsType(id, value, 'boolean'); |
| 69 return value; |
| 70 }, |
| 71 |
| 72 /** |
| 73 * As above, but also makes sure that the value is an integer. |
| 74 * @param {string} id The key that identifies the desired number. |
| 75 * @return {number} The corresponding number value. |
| 76 */ |
| 77 getInteger: function(id) { |
| 78 var value = this.getValue(id); |
| 79 assertIsType(id, value, 'number'); |
| 80 assert(value == Math.floor(value), 'Number isn\'t integer: ' + value); |
| 52 return value; | 81 return value; |
| 53 }, | 82 }, |
| 54 }; | 83 }; |
| 55 | 84 |
| 56 /** | 85 /** |
| 57 * Asserts, displays error message if assert fails. | 86 * Asserts, displays error message if assert fails. |
| 58 * @param {*} condition The condition to check for truthiness. | 87 * @param {*} condition The condition to check for truthiness. |
| 59 * @param {string} message The message to display if the check fails. | 88 * @param {string} message The message to display if the check fails. |
| 60 */ | 89 */ |
| 61 function assert(condition, message) { | 90 function assert(condition, message) { |
| 62 if (!condition) { | 91 if (!condition) |
| 63 console.error(message); | 92 console.error(message); |
| 64 } | 93 } |
| 94 |
| 95 /** |
| 96 * Asserts that the given value has the given type. |
| 97 * @param {string} id The id of the value (only used for error message). |
| 98 * @param {*} value The value to check the type on. |
| 99 * @param {string} type The type we expect |value| to be. |
| 100 */ |
| 101 function assertIsType(id, value, type) { |
| 102 assert(typeof value == type, '[' + value + '] (' + id + |
| 103 ') is not a ' + type); |
| 65 } | 104 } |
| 66 | 105 |
| 67 assert(!loadTimeData, 'should only include this file once'); | 106 assert(!loadTimeData, 'should only include this file once'); |
| 68 loadTimeData = new LoadTimeData; | 107 loadTimeData = new LoadTimeData; |
| 69 })(); | 108 })(); |
| OLD | NEW |