| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * A set of utilities for use with the Chrome Extension APIs. | 6 * A set of utilities for use with the Chrome Extension APIs. |
| 7 * | 7 * |
| 8 * Allows for easy access to required JS objects. | 8 * Allows for easy access to required JS objects. |
| 9 */ | 9 */ |
| 10 part of chrome; | 10 part of chrome; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 /** | 39 /** |
| 40 * Useful functions for converting arguments. | 40 * Useful functions for converting arguments. |
| 41 */ | 41 */ |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Converts the given map-type argument to js-friendly format, recursively. | 44 * Converts the given map-type argument to js-friendly format, recursively. |
| 45 * Returns the new Map object. | 45 * Returns the new Map object. |
| 46 */ | 46 */ |
| 47 Object _convertMapArgument(Map argument) { | 47 Object _convertMapArgument(Map argument) { |
| 48 Map m = new Map(); | 48 Map m = new Map(); |
| 49 for (Object key in argument.keys) | 49 for (Object key in argument.keys) m[key] = convertArgument(argument[key]); |
| 50 m[key] = convertArgument(argument[key]); | |
| 51 return convertDartToNative_Dictionary(m); | 50 return convertDartToNative_Dictionary(m); |
| 52 } | 51 } |
| 53 | 52 |
| 54 /** | 53 /** |
| 55 * Converts the given list-type argument to js-friendly format, recursively. | 54 * Converts the given list-type argument to js-friendly format, recursively. |
| 56 * Returns the new List object. | 55 * Returns the new List object. |
| 57 */ | 56 */ |
| 58 List _convertListArgument(List argument) { | 57 List _convertListArgument(List argument) { |
| 59 List l = new List(); | 58 List l = new List(); |
| 60 for (var i = 0; i < argument.length; i ++) | 59 for (var i = 0; i < argument.length; i++) l.add(convertArgument(argument[i])); |
| 61 l.add(convertArgument(argument[i])); | |
| 62 return l; | 60 return l; |
| 63 } | 61 } |
| 64 | 62 |
| 65 /** | 63 /** |
| 66 * Converts the given argument Object to js-friendly format, recursively. | 64 * Converts the given argument Object to js-friendly format, recursively. |
| 67 * | 65 * |
| 68 * Flattens out all Chrome objects into their corresponding ._toMap() | 66 * Flattens out all Chrome objects into their corresponding ._toMap() |
| 69 * definitions, then converts them to JS objects. | 67 * definitions, then converts them to JS objects. |
| 70 * | 68 * |
| 71 * Returns the new argument. | 69 * Returns the new argument. |
| 72 * | 70 * |
| 73 * Cannot be used for functions. | 71 * Cannot be used for functions. |
| 74 */ | 72 */ |
| 75 Object convertArgument(var argument) { | 73 Object convertArgument(var argument) { |
| 76 if (argument == null) | 74 if (argument == null) return argument; |
| 77 return argument; | |
| 78 | 75 |
| 79 if (argument is num || argument is String || argument is bool) | 76 if (argument is num || argument is String || argument is bool) |
| 80 return argument; | 77 return argument; |
| 81 | 78 |
| 82 if (argument is ChromeObject) | 79 if (argument is ChromeObject) return argument._jsObject; |
| 83 return argument._jsObject; | |
| 84 | 80 |
| 85 if (argument is List) | 81 if (argument is List) return _convertListArgument(argument); |
| 86 return _convertListArgument(argument); | |
| 87 | 82 |
| 88 if (argument is Map) | 83 if (argument is Map) return _convertMapArgument(argument); |
| 89 return _convertMapArgument(argument); | |
| 90 | 84 |
| 91 if (argument is Function) | 85 if (argument is Function) |
| 92 throw new Exception("Cannot serialize Function argument ${argument}."); | 86 throw new Exception("Cannot serialize Function argument ${argument}."); |
| 93 | 87 |
| 94 // TODO(sashab): Try and detect whether the argument is already serialized. | 88 // TODO(sashab): Try and detect whether the argument is already serialized. |
| 95 return argument; | 89 return argument; |
| 96 } | 90 } |
| 97 | 91 |
| 98 /// Description of a declarative rule for handling events. | 92 /// Description of a declarative rule for handling events. |
| 99 class Rule extends ChromeObject { | 93 class Rule extends ChromeObject { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 127 |
| 134 set actions(List actions) { | 128 set actions(List actions) { |
| 135 JS('void', '#.actions = #', this._jsObject, convertArgument(actions)); | 129 JS('void', '#.actions = #', this._jsObject, convertArgument(actions)); |
| 136 } | 130 } |
| 137 | 131 |
| 138 int get priority => JS('int', '#.priority', this._jsObject); | 132 int get priority => JS('int', '#.priority', this._jsObject); |
| 139 | 133 |
| 140 set priority(int priority) { | 134 set priority(int priority) { |
| 141 JS('void', '#.priority = #', this._jsObject, priority); | 135 JS('void', '#.priority = #', this._jsObject, priority); |
| 142 } | 136 } |
| 143 | |
| 144 } | 137 } |
| 145 | 138 |
| 146 /** | 139 /** |
| 147 * The Event class. | 140 * The Event class. |
| 148 * | 141 * |
| 149 * Chrome Event classes extend this interface. | 142 * Chrome Event classes extend this interface. |
| 150 * | 143 * |
| 151 * e.g. | 144 * e.g. |
| 152 * | 145 * |
| 153 * // chrome.app.runtime.onLaunched | 146 * // chrome.app.runtime.onLaunched |
| (...skipping 26 matching lines...) Expand all Loading... |
| 180 /* | 173 /* |
| 181 * Private constructor | 174 * Private constructor |
| 182 */ | 175 */ |
| 183 Event._(this._jsObject, this._callbackArity); | 176 Event._(this._jsObject, this._callbackArity); |
| 184 | 177 |
| 185 /* | 178 /* |
| 186 * Methods | 179 * Methods |
| 187 */ | 180 */ |
| 188 | 181 |
| 189 /// Registers an event listener <em>callback</em> to an event. | 182 /// Registers an event listener <em>callback</em> to an event. |
| 190 void addListener(Function callback) => | 183 void addListener(Function callback) => JS('void', '#.addListener(#)', |
| 191 JS('void', | 184 this._jsObject, convertDartClosureToJS(callback, this._callbackArity)); |
| 192 '#.addListener(#)', | |
| 193 this._jsObject, | |
| 194 convertDartClosureToJS(callback, this._callbackArity) | |
| 195 ); | |
| 196 | 185 |
| 197 /// Deregisters an event listener <em>callback</em> from an event. | 186 /// Deregisters an event listener <em>callback</em> from an event. |
| 198 void removeListener(Function callback) => | 187 void removeListener(Function callback) => JS('void', '#.removeListener(#)', |
| 199 JS('void', | 188 this._jsObject, convertDartClosureToJS(callback, this._callbackArity)); |
| 200 '#.removeListener(#)', | |
| 201 this._jsObject, | |
| 202 convertDartClosureToJS(callback, this._callbackArity) | |
| 203 ); | |
| 204 | 189 |
| 205 /// Returns True if <em>callback</em> is registered to the event. | 190 /// Returns True if <em>callback</em> is registered to the event. |
| 206 bool hasListener(Function callback) => | 191 bool hasListener(Function callback) => JS('bool', '#.hasListener(#)', |
| 207 JS('bool', | 192 this._jsObject, convertDartClosureToJS(callback, this._callbackArity)); |
| 208 '#.hasListener(#)', | |
| 209 this._jsObject, | |
| 210 convertDartClosureToJS(callback, this._callbackArity) | |
| 211 ); | |
| 212 | 193 |
| 213 /// Returns true if any event listeners are registered to the event. | 194 /// Returns true if any event listeners are registered to the event. |
| 214 bool hasListeners() => | 195 bool hasListeners() => JS('bool', '#.hasListeners()', this._jsObject); |
| 215 JS('bool', | |
| 216 '#.hasListeners()', | |
| 217 this._jsObject | |
| 218 ); | |
| 219 | 196 |
| 220 /// Registers rules to handle events. | 197 /// Registers rules to handle events. |
| 221 /// | 198 /// |
| 222 /// [eventName] is the name of the event this function affects and [rules] are | 199 /// [eventName] is the name of the event this function affects and [rules] are |
| 223 /// the rules to be registered. These do not replace previously registered | 200 /// the rules to be registered. These do not replace previously registered |
| 224 /// rules. [callback] is called with registered rules. | 201 /// rules. [callback] is called with registered rules. |
| 225 /// | 202 /// |
| 226 void addRules(String eventName, List<Rule> rules, | 203 void addRules(String eventName, List<Rule> rules, |
| 227 [void callback(List<Rule> rules)]) { | 204 [void callback(List<Rule> rules)]) { |
| 228 // proxy the callback | 205 // proxy the callback |
| 229 void __proxy_callback(List rules) { | 206 void __proxy_callback(List rules) { |
| 230 if (callback != null) { | 207 if (callback != null) { |
| 231 List<Rule> __proxy_rules = new List<Rule>(); | 208 List<Rule> __proxy_rules = new List<Rule>(); |
| 232 | 209 |
| 233 for (Object o in rules) | 210 for (Object o in rules) __proxy_rules.add(new Rule._proxy(o)); |
| 234 __proxy_rules.add(new Rule._proxy(o)); | |
| 235 | 211 |
| 236 callback(__proxy_rules); | 212 callback(__proxy_rules); |
| 237 } | 213 } |
| 238 } | 214 } |
| 239 | 215 |
| 240 JS('void', | 216 JS( |
| 241 '#.addRules(#, #, #)', | 217 'void', |
| 242 this._jsObject, | 218 '#.addRules(#, #, #)', |
| 243 convertArgument(eventName), | 219 this._jsObject, |
| 244 convertArgument(rules), | 220 convertArgument(eventName), |
| 245 convertDartClosureToJS(__proxy_callback, 1) | 221 convertArgument(rules), |
| 246 ); | 222 convertDartClosureToJS(__proxy_callback, 1)); |
| 247 } | 223 } |
| 248 | 224 |
| 249 /// Returns currently registered rules. | 225 /// Returns currently registered rules. |
| 250 /// | 226 /// |
| 251 /// [eventName] is the name of the event this function affects and, if an arra
y | 227 /// [eventName] is the name of the event this function affects and, if an arra
y |
| 252 /// is passed as [ruleIdentifiers], only rules with identifiers contained in | 228 /// is passed as [ruleIdentifiers], only rules with identifiers contained in |
| 253 /// this array are returned. [callback] is called with registered rules. | 229 /// this array are returned. [callback] is called with registered rules. |
| 254 /// | 230 /// |
| 255 void getRules(String eventName, [List<String> ruleIdentifiers, | 231 void getRules(String eventName, |
| 256 void callback(List<Rule> rules)]) { | 232 [List<String> ruleIdentifiers, void callback(List<Rule> rules)]) { |
| 257 // proxy the callback | 233 // proxy the callback |
| 258 void __proxy_callback(List rules) { | 234 void __proxy_callback(List rules) { |
| 259 if (callback != null) { | 235 if (callback != null) { |
| 260 List<Rule> __proxy_rules = new List<Rule>(); | 236 List<Rule> __proxy_rules = new List<Rule>(); |
| 261 | 237 |
| 262 for (Object o in rules) | 238 for (Object o in rules) __proxy_rules.add(new Rule._proxy(o)); |
| 263 __proxy_rules.add(new Rule._proxy(o)); | |
| 264 | 239 |
| 265 callback(__proxy_rules); | 240 callback(__proxy_rules); |
| 266 } | 241 } |
| 267 } | 242 } |
| 268 | 243 |
| 269 JS('void', | 244 JS( |
| 270 '#.getRules(#, #, #)', | 245 'void', |
| 271 this._jsObject, | 246 '#.getRules(#, #, #)', |
| 272 convertArgument(eventName), | 247 this._jsObject, |
| 273 convertArgument(ruleIdentifiers), | 248 convertArgument(eventName), |
| 274 convertDartClosureToJS(__proxy_callback, 1) | 249 convertArgument(ruleIdentifiers), |
| 275 ); | 250 convertDartClosureToJS(__proxy_callback, 1)); |
| 276 } | 251 } |
| 277 | 252 |
| 278 /// Unregisters currently registered rules. | 253 /// Unregisters currently registered rules. |
| 279 /// | 254 /// |
| 280 /// [eventName] is the name of the event this function affects and, if an arra
y | 255 /// [eventName] is the name of the event this function affects and, if an arra
y |
| 281 /// is passed as [ruleIdentifiers], only rules with identifiers contained in | 256 /// is passed as [ruleIdentifiers], only rules with identifiers contained in |
| 282 /// this array are unregistered. [callback] is called when the rules are | 257 /// this array are unregistered. [callback] is called when the rules are |
| 283 /// unregistered. | 258 /// unregistered. |
| 284 /// | 259 /// |
| 285 void removeRules(String eventName, [List<String> ruleIdentifiers, | 260 void removeRules(String eventName, |
| 286 void callback()]) => | 261 [List<String> ruleIdentifiers, void callback()]) => |
| 287 JS('void', | 262 JS( |
| 288 '#.removeRules(#, #, #)', | 263 'void', |
| 289 this._jsObject, | 264 '#.removeRules(#, #, #)', |
| 290 convertArgument(eventName), | 265 this._jsObject, |
| 291 convertArgument(ruleIdentifiers), | 266 convertArgument(eventName), |
| 292 convertDartClosureToJS(callback, 0) | 267 convertArgument(ruleIdentifiers), |
| 293 ); | 268 convertDartClosureToJS(callback, 0)); |
| 294 } | 269 } |
| 295 | |
| OLD | NEW |