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. 4 5/** 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). 11 */ 12 13var loadTimeData; 14 15(function() { 16 'use strict'; 17 18 function LoadTimeData() { 19 } 20 21 LoadTimeData.prototype = { 22 /** 23 * Sets the backing object. 24 * @param {Object} value The de-serialized page data. 25 */ 26 set data(value) { 27 expect(!this.data_, 'Re-setting data.'); 28 this.data_ = value; 29 }, 30 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 /** 39 * Fetches a value, expecting that it exists. 40 * @param {string} id The key that identifies the desired value. 41 * @return {*} The corresponding value. 42 */ 43 getValue: function(id) { 44 expect(this.data_, 'No data. Did you remember to include strings.js?'); 45 var value = this.data_[id]; 46 expect(typeof value != 'undefined', 'Could not find value for ' + id); 47 return value; 48 }, 49 50 /** 51 * As above, but also makes sure that the value is a string. 52 * @param {string} id The key that identifies the desired string. 53 * @return {string} The corresponding string value. 54 */ 55 getString: function(id) { 56 var value = this.getValue(id); 57 expectIsType(id, value, 'string'); 58 return value; 59 }, 60 61 /** 62 * Returns a formatted localized string where $1 to $9 are replaced by the 63 * second to the tenth argument. 64 * @param {string} id The ID of the string we want. 65 * @param {...string} The extra values to include in the formatted output. 66 * @return {string} The formatted string. 67 */ 68 getStringF: function(id) { 69 var value = this.getString(id); 70 if (!value) 71 return; 72 73 var varArgs = arguments; 74 return value.replace(/\$[$1-9]/g, function(m) { 75 return m == '$$' ? '$' : varArgs[m[1]]; 76 }); 77 }, 78 79 /** 80 * As above, but also makes sure that the value is a boolean. 81 * @param {string} id The key that identifies the desired boolean. 82 * @return {boolean} The corresponding boolean value. 83 */ 84 getBoolean: function(id) { 85 var value = this.getValue(id); 86 expectIsType(id, value, 'boolean'); 87 return value; 88 }, 89 90 /** 91 * As above, but also makes sure that the value is an integer. 92 * @param {string} id The key that identifies the desired number. 93 * @return {number} The corresponding number value. 94 */ 95 getInteger: function(id) { 96 var value = this.getValue(id); 97 expectIsType(id, value, 'number'); 98 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); 99 return value; 100 }, 101 }; 102 103 /** 104 * Checks condition, displays error message if expectation fails. 105 * @param {*} condition The condition to check for truthiness. 106 * @param {string} message The message to display if the check fails. 107 */ 108 function expect(condition, message) { 109 if (!condition) 110 console.error(message); 111 } 112 113 /** 114 * Checks that the given value has the given type. 115 * @param {string} id The id of the value (only used for error message). 116 * @param {*} value The value to check the type on. 117 * @param {string} type The type we expect |value| to be. 118 */ 119 function expectIsType(id, value, type) { 120 expect(typeof value == type, '[' + value + '] (' + id + 121 ') is not a ' + type); 122 } 123 124 expect(!loadTimeData, 'should only include this file once'); 125 loadTimeData = new LoadTimeData; 126})(); 127