Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 | |
| 6 /** @suppress {duplicate} */ | |
| 7 var remoting = remoting || {}; | |
| 8 | |
| 9 (function() { | |
| 10 'use strict'; | |
|
garykac
2015/03/18 17:28:36
All of our files should have 'use strict' at globa
kelvinp
2015/03/18 17:52:19
The convention (as least imposed by jshint and jsl
John Williams
2015/03/18 20:20:19
Since there's disagreement, I'm going to leave it
| |
| 11 | |
| 12 /** | |
| 13 * @param {*} value | |
| 14 * @return {boolean} | |
| 15 */ | |
| 16 var isArray = function(value) { | |
| 17 return Array.isArray(value); | |
| 18 }; | |
| 19 | |
| 20 /** | |
| 21 * @param {*} value | |
| 22 * @return {boolean} | |
| 23 */ | |
| 24 var isBoolean = function(value) { | |
| 25 return typeof value == 'boolean'; | |
| 26 }; | |
| 27 | |
| 28 /** | |
| 29 * @param {*} value | |
| 30 * @return {boolean} | |
| 31 */ | |
| 32 var isNumber = function(value) { | |
| 33 return typeof value == 'number'; | |
| 34 }; | |
| 35 | |
| 36 /** | |
| 37 * @param {*} value | |
| 38 * @return {boolean} | |
| 39 */ | |
| 40 var isObject = function(value) { | |
| 41 return value != null && typeof value == 'object' && !Array.isArray(value); | |
| 42 }; | |
| 43 | |
| 44 /** | |
| 45 * @param {*} value | |
| 46 * @return {boolean} | |
| 47 */ | |
| 48 var isString = function(value) { | |
| 49 return typeof value == 'string'; | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 53 * @param {*} value | |
| 54 * @return {string} | |
| 55 */ | |
| 56 var jsonTypeOf = function(value) { | |
| 57 if (typeof value == 'object') { | |
| 58 if (value === null) { | |
| 59 return 'null'; | |
| 60 } else if (Array.isArray(value)) { | |
| 61 return 'array'; | |
| 62 } else { | |
| 63 return 'object'; | |
| 64 } | |
| 65 } else { | |
| 66 return typeof value; | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 /** | |
| 71 * @param {*} value the value to check; must be an object | |
| 72 * @param {function(*):boolean} pred | |
| 73 * @param {string} typeDesc | |
| 74 * @return {*} the argument | |
| 75 */ | |
| 76 var assertType = function(value, pred, typeDesc) { | |
| 77 if (pred(value)) { | |
| 78 return value; | |
| 79 } else { | |
| 80 throw new Error('Invalid data type' + | |
| 81 ' (expected: ' + typeDesc + | |
| 82 ', actual: ' + jsonTypeOf(value) + ')'); | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 /** | |
| 87 * @param {*} value the value to check; must be an object | |
| 88 * @return {!Array} the argument | |
| 89 */ | |
| 90 base.assertArray = function(value) { | |
| 91 return /** @type {!Array} */ (assertType(value, isArray, 'array')); | |
| 92 }; | |
| 93 | |
| 94 /** | |
| 95 * @param {*} value the value to check; must be a boolean | |
| 96 * @return {boolean} the argument | |
| 97 */ | |
| 98 base.assertBoolean = function(value) { | |
| 99 return /** @type {boolean} */ (assertType(value, isBoolean, 'boolean')); | |
| 100 }; | |
| 101 | |
| 102 /** | |
| 103 * @param {*} value the value to check; must be a number | |
| 104 * @return {number} the argument | |
| 105 */ | |
| 106 base.assertNumber = function(value) { | |
| 107 return /** @type {number} */ (assertType(value, isNumber, 'number')); | |
| 108 }; | |
| 109 | |
| 110 /** | |
| 111 * @param {*} value the value to check; must be an object | |
| 112 * @return {!Object} the argument | |
| 113 */ | |
| 114 base.assertObject = function(value) { | |
| 115 return /** @type {!Object} */ (assertType(value, isObject, 'object')); | |
| 116 }; | |
| 117 | |
| 118 /** | |
| 119 * @param {*} value the value to check; must be a string | |
| 120 * @return {string} the argument | |
| 121 */ | |
| 122 base.assertString = function(value) { | |
| 123 return /** @type {string} */ (assertType(value, isString, 'string')); | |
| 124 }; | |
| 125 | |
| 126 /** | |
| 127 * @param {Object<string,*>} 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. | |
| 133 */ | |
| 134 var getTypedAttr = function(dict, key, pred, typeDesc, opt_default) { | |
| 135 var value = /** @type {*} */ (dict[key]); | |
| 136 if (pred(value)) { | |
| 137 return value; | |
| 138 } else if (opt_default !== undefined) { | |
| 139 return opt_default; | |
| 140 } else { | |
| 141 throw new Error('Invalid data type for ' + key + | |
| 142 ' (expected: ' + typeDesc + ', actual: ' + | |
| 143 jsonTypeOf(value) + ')'); | |
| 144 } | |
| 145 }; | |
| 146 | |
| 5 /** | 147 /** |
| 6 * Get the |key| attribute in the given |dict| and verify that it is an | 148 * Get the |key| attribute in the given |dict| and verify that it is an |
| 7 * array value. | 149 * array value. |
| 8 * | 150 * |
| 9 * If the attribute is not an array, then an exception will be thrown unless | 151 * If the attribute is not an array, then an exception will be thrown unless |
| 10 * a default value is specified in |opt_default|. | 152 * a default value is specified in |opt_default|. |
| 11 * | 153 * |
| 12 * @param {Object<string,*>} dict The dictionary containing the |key| | 154 * @param {Object<string,*>} dict The dictionary containing the |key| |
| 13 * @param {string} key The key to typecheck in the |dict|. | 155 * @param {string} key The key to typecheck in the |dict|. |
| 14 * @param {Array=} opt_default The value to return if the key is not a bool. | 156 * @param {Array=} opt_default The value to return if the key is not a bool. |
| 15 * @return {Array} The |key| attribute value as an object. | 157 * @return {Array} The |key| attribute value as an object. |
| 16 */ | 158 */ |
| 17 function getArrayAttr(dict, key, opt_default) { | 159 base.getArrayAttr = function(dict, key, opt_default) { |
| 18 var value = /** @type {Array} */ (dict[key]); | 160 return /** @type {Array} */ ( |
| 19 if (!(value instanceof Array)) { | 161 getTypedAttr(dict, key, isArray, 'array', opt_default)); |
| 20 if (opt_default === undefined) { | 162 }; |
| 21 throw 'Invalid data type for ' + key + | |
| 22 ' (expected: array, actual: ' + typeof value + ')'; | |
| 23 } else { | |
| 24 return opt_default; | |
| 25 } | |
| 26 } | |
| 27 return value; | |
| 28 } | |
| 29 | 163 |
| 30 /** | 164 /** |
| 31 * Get the |key| attribute in the given |dict| and verify that it is a | 165 * Get the |key| attribute in the given |dict| and verify that it is a |
| 32 * boolean value. | 166 * boolean value. |
| 33 * | 167 * |
| 34 * If the attribute is not a boolean, then an exception will be thrown unless | 168 * If the attribute is not a boolean, then an exception will be thrown unless |
| 35 * a default value is specified in |opt_default|. | 169 * a default value is specified in |opt_default|. |
| 36 * | 170 * |
| 37 * @param {Object<string,*>} dict The dictionary containing the |key| | 171 * @param {Object<string,*>} dict The dictionary containing the |key| |
| 38 * @param {string} key The key to typecheck in the |dict|. | 172 * @param {string} key The key to typecheck in the |dict|. |
| 39 * @param {boolean=} opt_default The value to return if the key is not a bool. | 173 * @param {boolean=} opt_default The value to return if the key is not a bool. |
| 40 * @return {boolean} The |key| attribute value as a boolean. | 174 * @return {boolean} The |key| attribute value as a boolean. |
| 41 */ | 175 */ |
| 42 function getBooleanAttr(dict, key, opt_default) { | 176 base.getBooleanAttr = function(dict, key, opt_default) { |
| 43 var value = /** @type {boolean} */ (dict[key]); | 177 var value = /** @type {*} */ (dict[key]); |
| 44 if (value == 'true' || value == 'false') { | 178 if (value == 'true' || value == 'false') { |
| 45 return (value == 'true'); | 179 return value == 'true'; |
| 46 } | 180 } |
| 47 if (typeof value !== 'boolean') { | 181 return /** @type {boolean} */ ( |
| 48 if (opt_default === undefined) { | 182 getTypedAttr(dict, key, isBoolean, 'boolean', opt_default)); |
| 49 throw 'Invalid data type for ' + key + | 183 }; |
| 50 ' (expected: boolean, actual: ' + typeof value + ')'; | |
| 51 } else { | |
| 52 return opt_default; | |
| 53 } | |
| 54 } | |
| 55 return value; | |
| 56 } | |
| 57 | 184 |
| 58 /** | 185 /** |
| 59 * Get the |key| attribute in the given |dict| and verify that it is a | 186 * Get the |key| attribute in the given |dict| and verify that it is a |
| 60 * number value. | 187 * number value. |
| 61 * | 188 * |
| 62 * If the attribute is not a number, then an exception will be thrown unless | 189 * If the attribute is not a number, then an exception will be thrown unless |
| 63 * a default value is specified in |opt_default|. | 190 * a default value is specified in |opt_default|. |
| 64 * | 191 * |
| 65 * @param {Object<string,*>} dict The dictionary containing the |key| | 192 * @param {Object<string,*>} dict The dictionary containing the |key| |
| 66 * @param {string} key The key to typecheck in the |dict|. | 193 * @param {string} key The key to typecheck in the |dict|. |
| 67 * @param {number=} opt_default The value to return if the key is not a number. | 194 * @param {number=} opt_default The value to return if the key is not a number. |
| 68 * @return {number} The |key| attribute value as a number. | 195 * @return {number} The |key| attribute value as a number. |
| 69 */ | 196 */ |
| 70 function getNumberAttr(dict, key, opt_default) { | 197 base.getNumberAttr = function(dict, key, opt_default) { |
| 71 var value = /** @type {number} */(dict[key]); | 198 return /** @type {number} */ ( |
| 72 if (typeof value != 'number') { | 199 getTypedAttr(dict, key, isNumber, 'number', opt_default)); |
| 73 if (opt_default === undefined) { | 200 }; |
| 74 throw 'Invalid data type for ' + key + | |
| 75 ' (expected: number, actual: ' + typeof value + ')'; | |
| 76 } else { | |
| 77 return opt_default; | |
| 78 } | |
| 79 } | |
| 80 return value; | |
| 81 } | |
| 82 | 201 |
| 83 /** | 202 /** |
| 84 * Get the |key| attribute in the given |dict| and verify that it is an | 203 * Get the |key| attribute in the given |dict| and verify that it is an |
| 85 * object value. | 204 * object value. |
| 86 * | 205 * |
| 87 * If the attribute is not an object, then an exception will be thrown unless | 206 * If the attribute is not an object, then an exception will be thrown unless |
| 88 * a default value is specified in |opt_default|. | 207 * a default value is specified in |opt_default|. |
| 89 * | 208 * |
| 90 * @param {Object<string,*>} dict The dictionary containing the |key| | 209 * @param {Object<string,*>} dict The dictionary containing the |key| |
| 91 * @param {string} key The key to typecheck in the |dict|. | 210 * @param {string} key The key to typecheck in the |dict|. |
| 92 * @param {Object=} opt_default The value to return if the key is not a bool. | 211 * @param {Object=} opt_default The value to return if the key is not a bool. |
| 93 * @return {Object} The |key| attribute value as an object. | 212 * @return {!Object} The |key| attribute value as an object. |
| 94 */ | 213 */ |
| 95 function getObjectAttr(dict, key, opt_default) { | 214 base.getObjectAttr = function(dict, key, opt_default) { |
| 96 var value = /** @type {Object} */ (dict[key]); | 215 return /** @type {!Object} */ ( |
| 97 if (typeof value != 'object') { | 216 getTypedAttr(dict, key, isObject, 'object', opt_default)); |
| 98 if (opt_default === undefined) { | 217 }; |
| 99 throw 'Invalid data type for ' + key + | |
| 100 ' (expected: object, actual: ' + typeof value + ')'; | |
| 101 } else { | |
| 102 return opt_default; | |
| 103 } | |
| 104 } | |
| 105 return value; | |
| 106 } | |
| 107 | 218 |
| 108 /** | 219 /** |
| 109 * Get the |key| attribute in the given |dict| and verify that it is a | 220 * Get the |key| attribute in the given |dict| and verify that it is a |
| 110 * string value. | 221 * string value. |
| 111 * | 222 * |
| 112 * If the attribute is not a string, then an exception will be thrown unless | 223 * If the attribute is not a string, then an exception will be thrown unless |
| 113 * a default value is specified in |opt_default|. | 224 * a default value is specified in |opt_default|. |
| 114 * | 225 * |
| 115 * @param {Object<string,*>} dict The dictionary containing the |key| | 226 * @param {Object<string,*>} dict The dictionary containing the |key| |
| 116 * @param {string} key The key to typecheck in the |dict|. | 227 * @param {string} key The key to typecheck in the |dict|. |
| 117 * @param {string=} opt_default The value to return if the key is not a string. | 228 * @param {string=} opt_default The value to return if the key is not a string. |
| 118 * @return {string} The |key| attribute value as a string. | 229 * @return {string} The |key| attribute value as a string. |
| 119 */ | 230 */ |
| 120 function getStringAttr(dict, key, opt_default) { | 231 base.getStringAttr = function(dict, key, opt_default) { |
| 121 var value = /** @type {string} */ (dict[key]); | 232 return /** @type {string} */ ( |
| 122 if (typeof value != 'string') { | 233 getTypedAttr(dict, key, isString, 'string', opt_default)); |
| 123 if (opt_default === undefined) { | 234 }; |
| 124 throw 'Invalid data type for ' + key + | |
| 125 ' (expected: string, actual: ' + typeof value + ')'; | |
| 126 } else { | |
| 127 return opt_default; | |
| 128 } | |
| 129 } | |
| 130 return value; | |
| 131 } | |
| 132 | 235 |
| 133 /** | 236 /** |
| 134 * Return a JSON object parsed from a string. | 237 * Return a JSON object parsed from a string. |
| 135 * | 238 * |
| 136 * If the string cannot be parsed, or does not result in an object, then an | 239 * If the string cannot be parsed, or does not result in an object, then an |
| 137 * exception will be thrown. | 240 * exception will be thrown. |
| 138 * | 241 * |
| 139 * @param {string} jsonString The JSON string to parse. | 242 * @param {string} jsonString The JSON string to parse. |
| 140 * @return {Object} The JSON object created from the |jsonString|. | 243 * @return {Object} The JSON object created from the |jsonString|. |
| 141 */ | 244 */ |
| 142 function getJsonObjectFromString(jsonString) { | 245 base.getJsonObjectFromString = function(jsonString) { |
| 143 var value = base.jsonParseSafe(jsonString); | 246 return base.assertObject(base.jsonParseSafe(jsonString)); |
| 144 if (typeof value != 'object') { | 247 }; |
| 145 throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')'; | 248 |
| 146 } | 249 })(); |
| 147 return value; | |
| 148 } | |
| OLD | NEW |