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
3 // found in the LICENSE file.
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
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
10 * that must be displayed right away).
15 // Expose this type globally as a temporary work around until
16 // https://github.com/google/closure-compiler/issues/544 is fixed.
18 function LoadTimeData() {}
23 LoadTimeData.prototype = {
25 * Sets the backing object.
27 * Note that there is no getter for |data_| to discourage abuse of the form:
29 * var value = loadTimeData.data()['key'];
31 * @param {Object} value The de-serialized page data.
34 expect(!this.data_, 'Re-setting data.');
39 * Returns a JsEvalContext for |data_|.
40 * @returns {JsEvalContext}
42 createJsEvalContext: function() {
43 return new JsEvalContext(this.data_);
47 * @param {string} id An ID of a value that might exist.
48 * @return {boolean} True if |id| is a key in the dictionary.
50 valueExists: function(id) {
51 return id in this.data_;
55 * Fetches a value, expecting that it exists.
56 * @param {string} id The key that identifies the desired value.
57 * @return {*} The corresponding value.
59 getValue: function(id) {
60 expect(this.data_, 'No data. Did you remember to include strings.js?');
61 var value = this.data_[id];
62 expect(typeof value != 'undefined', 'Could not find value for ' + id);
67 * As above, but also makes sure that the value is a string.
68 * @param {string} id The key that identifies the desired string.
69 * @return {string} The corresponding string value.
71 getString: function(id) {
72 var value = this.getValue(id);
73 expectIsType(id, value, 'string');
74 return /** @type {string} */ (value);
78 * Returns a formatted localized string where $1 to $9 are replaced by the
79 * second to the tenth argument.
80 * @param {string} id The ID of the string we want.
81 * @param {...string} var_args The extra values to include in the formatted
83 * @return {string} The formatted string.
85 getStringF: function(id, var_args) {
86 var value = this.getString(id);
90 var varArgs = arguments;
91 return value.replace(/\$[$1-9]/g, function(m) {
92 return m == '$$' ? '$' : varArgs[m[1]];
97 * As above, but also makes sure that the value is a boolean.
98 * @param {string} id The key that identifies the desired boolean.
99 * @return {boolean} The corresponding boolean value.
101 getBoolean: function(id) {
102 var value = this.getValue(id);
103 expectIsType(id, value, 'boolean');
104 return /** @type {boolean} */ (value);
108 * As above, but also makes sure that the value is an integer.
109 * @param {string} id The key that identifies the desired number.
110 * @return {number} The corresponding number value.
112 getInteger: function(id) {
113 var value = this.getValue(id);
114 expectIsType(id, value, 'number');
115 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value);
116 return /** @type {number} */ (value);
120 * Override values in loadTimeData with the values found in |replacements|.
121 * @param {Object} replacements The dictionary object of keys to replace.
123 overrideValues: function(replacements) {
124 expect(typeof replacements == 'object',
125 'Replacements must be a dictionary object.');
126 for (var key in replacements) {
127 this.data_[key] = replacements[key];
133 * Checks condition, displays error message if expectation fails.
134 * @param {*} condition The condition to check for truthiness.
135 * @param {string} message The message to display if the check fails.
137 function expect(condition, message) {
139 console.error('Unexpected condition on ' + document.location.href + ': ' +
145 * Checks that the given value has the given type.
146 * @param {string} id The id of the value (only used for error message).
147 * @param {*} value The value to check the type on.
148 * @param {string} type The type we expect |value| to be.
150 function expectIsType(id, value, type) {
151 expect(typeof value == type, '[' + value + '] (' + id +
152 ') is not a ' + type);
155 expect(!loadTimeData, 'should only include this file once');
156 loadTimeData = new LoadTimeData;