[email protected] | 8f857ef8 | 2014-06-04 23:46:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 5 | // Custom binding for the Permissions API. |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 6 | |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 7 | var binding = apiBridge || require('binding').Binding.create('permissions'); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 8 | |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 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) |
| 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); |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 32 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 33 | // These custom binding are only necessary because it is not currently |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 34 | // possible to have a union of types as the type of the items in an array. |
| 35 | // Once that is fixed, this entire file should go away. |
| 36 | // See, |
| 37 | // https://code.google.com/p/chromium/issues/detail?id=162044 |
| 38 | // https://code.google.com/p/chromium/issues/detail?id=162042 |
| 39 | // TODO(bryeung): delete this file. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 40 | binding.registerCustomHook(function(api) { |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 41 | var apiFunctions = api.apiFunctions; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 42 | var permissions = api.compiledApi; |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 43 | |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 44 | function convertObjectPermissionsToStrings() { |
| 45 | if (arguments.length < 1) |
| 46 | return arguments; |
| 47 | |
| 48 | var args = arguments[0].permissions; |
| 49 | if (!args) |
| 50 | return arguments; |
| 51 | |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 52 | for (var i = 0; i < args.length; ++i) { |
| 53 | if (typeof args[i] == 'object') { |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 54 | var a = args[i]; |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 55 | var keys = $Object.keys(a); |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 56 | if (keys.length != 1) { |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 57 | throw new Error('Too many keys in object-style permission.'); |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 58 | } |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 59 | arguments[0].permissions[i] = |
| 60 | keys[0] + '|' + $JSON.stringify(a[keys[0]]); |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | return arguments; |
| 65 | } |
| 66 | |
| 67 | // Convert complex permissions to strings so they validate against the schema |
| 68 | apiFunctions.setUpdateArgumentsPreValidate( |
| 69 | 'contains', convertObjectPermissionsToStrings); |
| 70 | apiFunctions.setUpdateArgumentsPreValidate( |
| 71 | 'remove', convertObjectPermissionsToStrings); |
| 72 | apiFunctions.setUpdateArgumentsPreValidate( |
| 73 | 'request', convertObjectPermissionsToStrings); |
| 74 | |
| 75 | // Convert complex permissions back to objects |
| 76 | apiFunctions.setCustomCallback('getAll', |
rob | 6f445970 | 2015-02-24 10:44:14 | [diff] [blame] | 77 | function(name, request, callback, response) { |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 78 | for (var i = 0; i < response.permissions.length; i += 1) { |
| 79 | response.permissions[i] = |
| 80 | maybeConvertToObject(response.permissions[i]); |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 7bbdb8a2 | 2012-12-13 21:30:41 | [diff] [blame] | 83 | // Since the schema says Permissions.permissions contains strings and |
| 84 | // not objects, validation will fail after the for-loop above. This |
rob | 6f445970 | 2015-02-24 10:44:14 | [diff] [blame] | 85 | // skips validation and calls the callback directly. |
| 86 | if (callback) |
| 87 | callback(response); |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 88 | }); |
[email protected] | 288ce6c | 2012-12-03 21:05:24 | [diff] [blame] | 89 | }); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 90 | |
rdevlin.cronin | 6ebcb95f8 | 2017-06-15 22:58:36 | [diff] [blame] | 91 | if (!apiBridge) |
| 92 | exports.$set('binding', binding.generate()); |