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 // Custom binding for the Permissions API. | 5 // Custom binding for the Permissions API. |
| 6 | 6 |
| 7 var binding = require('binding').Binding.create('permissions'); | 7 var binding = apiBridge || require('binding').Binding.create('permissions'); |
| 8 | 8 |
| 9 var Event = require('event_bindings').Event; | 9 var registerArgumentMassager = bindingUtil ? |
| 10 $Function.bind(bindingUtil.registerEventArgumentMassager, bindingUtil) : | |
| 11 require('event_bindings').registerArgumentMassager; | |
| 12 | |
| 13 function maybeConvertToObject(str) { | |
| 14 var parts = $String.split(str, '|'); | |
| 15 if (parts.length != 2) | |
| 16 return str; | |
| 17 | |
| 18 var ret = {}; | |
| 19 ret[parts[0]] = $JSON.parse(parts[1]); | |
| 20 return ret; | |
| 21 } | |
| 22 | |
| 23 function massager(args, dispatch) { | |
| 24 // Convert complex permissions back to objects for events. | |
| 25 for (var i = 0; i < args[0].permissions.length; i += 1) | |
|
lazyboy
2017/06/15 21:25:38
++i?
Devlin
2017/06/15 22:53:43
was copy-pasted from below.
Fixed this, and updat
| |
| 26 args[0].permissions[i] = maybeConvertToObject(args[0].permissions[i]); | |
| 27 dispatch(args); | |
| 28 } | |
| 29 | |
| 30 registerArgumentMassager('permissions.onAdded', massager); | |
| 31 registerArgumentMassager('permissions.onRemoved', massager); | |
| 10 | 32 |
| 11 // These custom binding are only necessary because it is not currently | 33 // These custom binding are only necessary because it is not currently |
| 12 // possible to have a union of types as the type of the items in an array. | 34 // possible to have a union of types as the type of the items in an array. |
| 13 // Once that is fixed, this entire file should go away. | 35 // Once that is fixed, this entire file should go away. |
| 14 // See, | 36 // See, |
| 15 // https://code.google.com/p/chromium/issues/detail?id=162044 | 37 // https://code.google.com/p/chromium/issues/detail?id=162044 |
| 16 // https://code.google.com/p/chromium/issues/detail?id=162042 | 38 // https://code.google.com/p/chromium/issues/detail?id=162042 |
| 17 // TODO(bryeung): delete this file. | 39 // TODO(bryeung): delete this file. |
| 18 binding.registerCustomHook(function(api) { | 40 binding.registerCustomHook(function(api) { |
| 19 var apiFunctions = api.apiFunctions; | 41 var apiFunctions = api.apiFunctions; |
| 20 var permissions = api.compiledApi; | 42 var permissions = api.compiledApi; |
| 21 | 43 |
| 22 function maybeConvertToObject(str) { | |
| 23 var parts = $String.split(str, '|'); | |
| 24 if (parts.length != 2) | |
| 25 return str; | |
| 26 | |
| 27 var ret = {}; | |
| 28 ret[parts[0]] = JSON.parse(parts[1]); | |
| 29 return ret; | |
| 30 } | |
| 31 | |
| 32 function convertObjectPermissionsToStrings() { | 44 function convertObjectPermissionsToStrings() { |
| 33 if (arguments.length < 1) | 45 if (arguments.length < 1) |
| 34 return arguments; | 46 return arguments; |
| 35 | 47 |
| 36 var args = arguments[0].permissions; | 48 var args = arguments[0].permissions; |
| 37 if (!args) | 49 if (!args) |
| 38 return arguments; | 50 return arguments; |
| 39 | 51 |
| 40 for (var i = 0; i < args.length; i += 1) { | 52 for (var i = 0; i < args.length; i += 1) { |
| 41 if (typeof(args[i]) == 'object') { | 53 if (typeof(args[i]) == 'object') { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 67 response.permissions[i] = | 79 response.permissions[i] = |
| 68 maybeConvertToObject(response.permissions[i]); | 80 maybeConvertToObject(response.permissions[i]); |
| 69 } | 81 } |
| 70 | 82 |
| 71 // Since the schema says Permissions.permissions contains strings and | 83 // Since the schema says Permissions.permissions contains strings and |
| 72 // not objects, validation will fail after the for-loop above. This | 84 // not objects, validation will fail after the for-loop above. This |
| 73 // skips validation and calls the callback directly. | 85 // skips validation and calls the callback directly. |
| 74 if (callback) | 86 if (callback) |
| 75 callback(response); | 87 callback(response); |
| 76 }); | 88 }); |
| 77 | |
| 78 // Also convert complex permissions back to objects for events. The | |
| 79 // dispatchToListener call happens after argument validation, which works | |
| 80 // around the problem that Permissions.permissions is supposed to be a list | |
| 81 // of strings. | |
| 82 permissions.onAdded.dispatchToListener = function(callback, args) { | |
| 83 for (var i = 0; i < args[0].permissions.length; i += 1) { | |
| 84 args[0].permissions[i] = maybeConvertToObject(args[0].permissions[i]); | |
| 85 } | |
| 86 $Function.call(Event.prototype.dispatchToListener, this, callback, args); | |
| 87 }; | |
| 88 permissions.onRemoved.dispatchToListener = | |
| 89 permissions.onAdded.dispatchToListener; | |
| 90 }); | 89 }); |
| 91 | 90 |
| 92 exports.$set('binding', binding.generate()); | 91 if (!apiBridge) |
| 92 exports.$set('binding', binding.generate()); | |
| OLD | NEW |