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 | 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 * |
| 27 * Note that there is no getter for |data_| to discourage abuse of the form: |
| 28 * |
| 29 * var value = loadTimeData.data()['key']; |
| 30 * |
26 * @param {Object} value The de-serialized page data. | 31 * @param {Object} value The de-serialized page data. |
27 */ | 32 */ |
28 set data(value) { | 33 set data(value) { |
29 expect(!this.data_, 'Re-setting data.'); | 34 expect(!this.data_, 'Re-setting data.'); |
30 this.data_ = value; | 35 this.data_ = value; |
31 }, | 36 }, |
32 | 37 |
33 /** | 38 /** |
| 39 * Returns a JsEvalContext for |data_|. |
| 40 * @returns {JsEvalContext} |
| 41 */ |
| 42 createJsEvalContext: function() { |
| 43 return new JsEvalContext(this.data_); |
| 44 }, |
| 45 |
| 46 /** |
34 * @param {string} id An ID of a value that might exist. | 47 * @param {string} id An ID of a value that might exist. |
35 * @return {boolean} True if |id| is a key in the dictionary. | 48 * @return {boolean} True if |id| is a key in the dictionary. |
36 */ | 49 */ |
37 valueExists: function(id) { | 50 valueExists: function(id) { |
38 return id in this.data_; | 51 return id in this.data_; |
39 }, | 52 }, |
40 | 53 |
41 /** | 54 /** |
42 * Fetches a value, expecting that it exists. | 55 * Fetches a value, expecting that it exists. |
43 * @param {string} id The key that identifies the desired value. | 56 * @param {string} id The key that identifies the desired value. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 * @param {string} type The type we expect |value| to be. | 148 * @param {string} type The type we expect |value| to be. |
136 */ | 149 */ |
137 function expectIsType(id, value, type) { | 150 function expectIsType(id, value, type) { |
138 expect(typeof value == type, '[' + value + '] (' + id + | 151 expect(typeof value == type, '[' + value + '] (' + id + |
139 ') is not a ' + type); | 152 ') is not a ' + type); |
140 } | 153 } |
141 | 154 |
142 expect(!loadTimeData, 'should only include this file once'); | 155 expect(!loadTimeData, 'should only include this file once'); |
143 loadTimeData = new LoadTimeData; | 156 loadTimeData = new LoadTimeData; |
144 })(); | 157 })(); |
OLD | NEW |