1 // Copyright 2014 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 /** @suppress {duplicate} */
7 var remoting = remoting || {};
16 var isArray = function(value) {
17 return Array.isArray(value);
24 var isBoolean = function(value) {
25 return typeof value == 'boolean';
32 var isNumber = function(value) {
33 return typeof value == 'number';
40 var isObject = function(value) {
41 return value != null && typeof value == 'object' && !Array.isArray(value);
48 var isString = function(value) {
49 return typeof value == 'string';
56 var jsonTypeOf = function(value) {
57 if (typeof value == 'object') {
60 } else if (Array.isArray(value)) {
71 * @param {*} value the value to check; must be an object
72 * @param {function(*):boolean} pred
73 * @param {string} typeDesc
74 * @return {*} the argument
76 var assertType = function(value, pred, typeDesc) {
80 throw new Error('Invalid data type' +
81 ' (expected: ' + typeDesc +
82 ', actual: ' + jsonTypeOf(value) + ')');
87 * @param {*} value the value to check; must be an object
88 * @return {!Array} the argument
90 base.assertArray = function(value) {
91 return /** @type {!Array} */ (assertType(value, isArray, 'array'));
95 * @param {*} value the value to check; must be a boolean
96 * @return {boolean} the argument
98 base.assertBoolean = function(value) {
99 return /** @type {boolean} */ (assertType(value, isBoolean, 'boolean'));
103 * @param {*} value the value to check; must be a number
104 * @return {number} the argument
106 base.assertNumber = function(value) {
107 return /** @type {number} */ (assertType(value, isNumber, 'number'));
111 * @param {*} value the value to check; must be an object
112 * @return {!Object} the argument
114 base.assertObject = function(value) {
115 return /** @type {!Object} */ (assertType(value, isObject, 'object'));
119 * @param {*} value the value to check; must be a string
120 * @return {string} the argument
122 base.assertString = function(value) {
123 return /** @type {string} */ (assertType(value, isString, 'string'));
127 * @param {Object<*>} dict The dictionary containing the |key|
128 * @param {string} key The key to typecheck in the |dict|.
129 * @param {function(*):boolean} pred
130 * @param {string} typeDesc
131 * @param {*=} opt_default The value to return if pred returns false.
132 * @return {*} The |key| attribute value.
134 var getTypedAttr = function(dict, key, pred, typeDesc, opt_default) {
135 var value = /** @type {*} */ (dict[key]);
138 } else if (opt_default !== undefined) {
141 throw new Error('Invalid data type for ' + key +
142 ' (expected: ' + typeDesc + ', actual: ' +
143 jsonTypeOf(value) + ')');
148 * Get the |key| attribute in the given |dict| and verify that it is an
151 * If the attribute is not an array, then an exception will be thrown unless
152 * a default value is specified in |opt_default|.
154 * @param {Object<*>} dict The dictionary containing the |key|
155 * @param {string} key The key to typecheck in the |dict|.
156 * @param {Array=} opt_default The value to return if the key is not a bool.
157 * @return {Array} The |key| attribute value as an object.
159 base.getArrayAttr = function(dict, key, opt_default) {
160 return /** @type {Array} */ (
161 getTypedAttr(dict, key, isArray, 'array', opt_default));
165 * Get the |key| attribute in the given |dict| and verify that it is a
168 * If the attribute is not a boolean, then an exception will be thrown unless
169 * a default value is specified in |opt_default|.
171 * @param {Object<*>} dict The dictionary containing the |key|
172 * @param {string} key The key to typecheck in the |dict|.
173 * @param {boolean=} opt_default The value to return if the key is not a bool.
174 * @return {boolean} The |key| attribute value as a boolean.
176 base.getBooleanAttr = function(dict, key, opt_default) {
177 var value = /** @type {*} */ (dict[key]);
178 if (value == 'true' || value == 'false') {
179 return value == 'true';
181 return /** @type {boolean} */ (
182 getTypedAttr(dict, key, isBoolean, 'boolean', opt_default));
186 * Get the |key| attribute in the given |dict| and verify that it is a
189 * If the attribute is not a number, then an exception will be thrown unless
190 * a default value is specified in |opt_default|.
192 * @param {Object<*>} dict The dictionary containing the |key|
193 * @param {string} key The key to typecheck in the |dict|.
194 * @param {number=} opt_default The value to return if the key is not a number.
195 * @return {number} The |key| attribute value as a number.
197 base.getNumberAttr = function(dict, key, opt_default) {
198 return /** @type {number} */ (
199 getTypedAttr(dict, key, isNumber, 'number', opt_default));
203 * Get the |key| attribute in the given |dict| and verify that it is an
206 * If the attribute is not an object, then an exception will be thrown unless
207 * a default value is specified in |opt_default|.
209 * @param {Object<*>} dict The dictionary containing the |key|
210 * @param {string} key The key to typecheck in the |dict|.
211 * @param {Object=} opt_default The value to return if the key is not a bool.
212 * @return {!Object} The |key| attribute value as an object.
214 base.getObjectAttr = function(dict, key, opt_default) {
215 return /** @type {!Object} */ (
216 getTypedAttr(dict, key, isObject, 'object', opt_default));
220 * Get the |key| attribute in the given |dict| and verify that it is a
223 * If the attribute is not a string, then an exception will be thrown unless
224 * a default value is specified in |opt_default|.
226 * @param {Object<*>} dict The dictionary containing the |key|
227 * @param {string} key The key to typecheck in the |dict|.
228 * @param {string=} opt_default The value to return if the key is not a string.
229 * @return {string} The |key| attribute value as a string.
231 base.getStringAttr = function(dict, key, opt_default) {
232 return /** @type {string} */ (
233 getTypedAttr(dict, key, isString, 'string', opt_default));
237 * Return a JSON object parsed from a string.
239 * If the string cannot be parsed, or does not result in an object, then an
240 * exception will be thrown.
242 * @param {string} jsonString The JSON string to parse.
243 * @return {Object} The JSON object created from the |jsonString|.
245 base.getJsonObjectFromString = function(jsonString) {
246 return base.assertObject(base.jsonParseSafe(jsonString));