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 var loadTimeData; | 13 var loadTimeData; |
14 | 14 |
| 15 // Expose this type globally as a temporary work around until |
| 16 // https://github.com/google/closure-compiler/issues/544 is fixed. |
| 17 /** @constructor */ |
| 18 function LoadTimeData() {} |
| 19 |
15 (function() { | 20 (function() { |
16 'use strict'; | 21 'use strict'; |
17 | 22 |
18 function LoadTimeData() { | |
19 } | |
20 | |
21 LoadTimeData.prototype = { | 23 LoadTimeData.prototype = { |
22 /** | 24 /** |
23 * Sets the backing object. | 25 * Sets the backing object. |
24 * @param {Object} value The de-serialized page data. | 26 * @param {Object} value The de-serialized page data. |
25 */ | 27 */ |
26 set data(value) { | 28 set data(value) { |
27 expect(!this.data_, 'Re-setting data.'); | 29 expect(!this.data_, 'Re-setting data.'); |
28 this.data_ = value; | 30 this.data_ = value; |
29 }, | 31 }, |
30 | 32 |
31 /** | 33 /** |
| 34 * @param {string} id An ID of a value that might exist. |
32 * @return {boolean} True if |id| is a key in the dictionary. | 35 * @return {boolean} True if |id| is a key in the dictionary. |
33 */ | 36 */ |
34 valueExists: function(id) { | 37 valueExists: function(id) { |
35 return id in this.data_; | 38 return id in this.data_; |
36 }, | 39 }, |
37 | 40 |
38 /** | 41 /** |
39 * Fetches a value, expecting that it exists. | 42 * Fetches a value, expecting that it exists. |
40 * @param {string} id The key that identifies the desired value. | 43 * @param {string} id The key that identifies the desired value. |
41 * @return {*} The corresponding value. | 44 * @return {*} The corresponding value. |
42 */ | 45 */ |
43 getValue: function(id) { | 46 getValue: function(id) { |
44 expect(this.data_, 'No data. Did you remember to include strings.js?'); | 47 expect(this.data_, 'No data. Did you remember to include strings.js?'); |
45 var value = this.data_[id]; | 48 var value = this.data_[id]; |
46 expect(typeof value != 'undefined', 'Could not find value for ' + id); | 49 expect(typeof value != 'undefined', 'Could not find value for ' + id); |
47 return value; | 50 return value; |
48 }, | 51 }, |
49 | 52 |
50 /** | 53 /** |
51 * As above, but also makes sure that the value is a string. | 54 * As above, but also makes sure that the value is a string. |
52 * @param {string} id The key that identifies the desired string. | 55 * @param {string} id The key that identifies the desired string. |
53 * @return {string} The corresponding string value. | 56 * @return {string} The corresponding string value. |
54 */ | 57 */ |
55 getString: function(id) { | 58 getString: function(id) { |
56 var value = this.getValue(id); | 59 var value = this.getValue(id); |
57 expectIsType(id, value, 'string'); | 60 expectIsType(id, value, 'string'); |
58 return value; | 61 return /** @type {string} */ (value); |
59 }, | 62 }, |
60 | 63 |
61 /** | 64 /** |
62 * Returns a formatted localized string where $1 to $9 are replaced by the | 65 * Returns a formatted localized string where $1 to $9 are replaced by the |
63 * second to the tenth argument. | 66 * second to the tenth argument. |
64 * @param {string} id The ID of the string we want. | 67 * @param {string} id The ID of the string we want. |
65 * @param {...string} The extra values to include in the formatted output. | 68 * @param {...string} var_args The extra values to include in the formatted |
| 69 * output. |
66 * @return {string} The formatted string. | 70 * @return {string} The formatted string. |
67 */ | 71 */ |
68 getStringF: function(id) { | 72 getStringF: function(id, var_args) { |
69 var value = this.getString(id); | 73 var value = this.getString(id); |
70 if (!value) | 74 if (!value) |
71 return; | 75 return ''; |
72 | 76 |
73 var varArgs = arguments; | 77 var varArgs = arguments; |
74 return value.replace(/\$[$1-9]/g, function(m) { | 78 return value.replace(/\$[$1-9]/g, function(m) { |
75 return m == '$$' ? '$' : varArgs[m[1]]; | 79 return m == '$$' ? '$' : varArgs[m[1]]; |
76 }); | 80 }); |
77 }, | 81 }, |
78 | 82 |
79 /** | 83 /** |
80 * As above, but also makes sure that the value is a boolean. | 84 * As above, but also makes sure that the value is a boolean. |
81 * @param {string} id The key that identifies the desired boolean. | 85 * @param {string} id The key that identifies the desired boolean. |
82 * @return {boolean} The corresponding boolean value. | 86 * @return {boolean} The corresponding boolean value. |
83 */ | 87 */ |
84 getBoolean: function(id) { | 88 getBoolean: function(id) { |
85 var value = this.getValue(id); | 89 var value = this.getValue(id); |
86 expectIsType(id, value, 'boolean'); | 90 expectIsType(id, value, 'boolean'); |
87 return value; | 91 return /** @type {boolean} */ (value); |
88 }, | 92 }, |
89 | 93 |
90 /** | 94 /** |
91 * As above, but also makes sure that the value is an integer. | 95 * As above, but also makes sure that the value is an integer. |
92 * @param {string} id The key that identifies the desired number. | 96 * @param {string} id The key that identifies the desired number. |
93 * @return {number} The corresponding number value. | 97 * @return {number} The corresponding number value. |
94 */ | 98 */ |
95 getInteger: function(id) { | 99 getInteger: function(id) { |
96 var value = this.getValue(id); | 100 var value = this.getValue(id); |
97 expectIsType(id, value, 'number'); | 101 expectIsType(id, value, 'number'); |
98 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); | 102 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); |
99 return value; | 103 return /** @type {number} */ (value); |
100 }, | 104 }, |
101 | 105 |
102 /** | 106 /** |
103 * Override values in loadTimeData with the values found in |replacements|. | 107 * Override values in loadTimeData with the values found in |replacements|. |
104 * @param {Object} replacements The dictionary object of keys to replace. | 108 * @param {Object} replacements The dictionary object of keys to replace. |
105 */ | 109 */ |
106 overrideValues: function(replacements) { | 110 overrideValues: function(replacements) { |
107 expect(typeof replacements == 'object', | 111 expect(typeof replacements == 'object', |
108 'Replacements must be a dictionary object.'); | 112 'Replacements must be a dictionary object.'); |
109 for (var key in replacements) { | 113 for (var key in replacements) { |
(...skipping 21 matching lines...) Expand all Loading... |
131 * @param {string} type The type we expect |value| to be. | 135 * @param {string} type The type we expect |value| to be. |
132 */ | 136 */ |
133 function expectIsType(id, value, type) { | 137 function expectIsType(id, value, type) { |
134 expect(typeof value == type, '[' + value + '] (' + id + | 138 expect(typeof value == type, '[' + value + '] (' + id + |
135 ') is not a ' + type); | 139 ') is not a ' + type); |
136 } | 140 } |
137 | 141 |
138 expect(!loadTimeData, 'should only include this file once'); | 142 expect(!loadTimeData, 'should only include this file once'); |
139 loadTimeData = new LoadTimeData; | 143 loadTimeData = new LoadTimeData; |
140 })(); | 144 })(); |
OLD | NEW |