blob: e973fe9e1ec6adbe1b942fa4dc1aa1c3100b4b61 [file] [log] [blame]
[email protected]8f857ef82014-06-04 23:46:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]288ce6c2012-12-03 21:05:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4f1633f2013-03-09 14:26:245// Custom binding for the Permissions API.
[email protected]288ce6c2012-12-03 21:05:246
rdevlin.cronin6ebcb95f82017-06-15 22:58:367var binding = apiBridge || require('binding').Binding.create('permissions');
[email protected]4f1633f2013-03-09 14:26:248
rdevlin.cronin6ebcb95f82017-06-15 22:58:369var registerArgumentMassager = bindingUtil ?
10 $Function.bind(bindingUtil.registerEventArgumentMassager, bindingUtil) :
11 require('event_bindings').registerArgumentMassager;
12
13function 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
23function 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
30registerArgumentMassager('permissions.onAdded', massager);
31registerArgumentMassager('permissions.onRemoved', massager);
[email protected]288ce6c2012-12-03 21:05:2432
[email protected]4f1633f2013-03-09 14:26:2433// These custom binding are only necessary because it is not currently
[email protected]7bbdb8a22012-12-13 21:30:4134// 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]4f1633f2013-03-09 14:26:2440binding.registerCustomHook(function(api) {
[email protected]288ce6c2012-12-03 21:05:2441 var apiFunctions = api.apiFunctions;
[email protected]4f1633f2013-03-09 14:26:2442 var permissions = api.compiledApi;
[email protected]288ce6c2012-12-03 21:05:2443
[email protected]7bbdb8a22012-12-13 21:30:4144 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.cronin6ebcb95f82017-06-15 22:58:3652 for (var i = 0; i < args.length; ++i) {
53 if (typeof args[i] == 'object') {
[email protected]7bbdb8a22012-12-13 21:30:4154 var a = args[i];
[email protected]31bbfd72013-06-22 02:35:5455 var keys = $Object.keys(a);
[email protected]7bbdb8a22012-12-13 21:30:4156 if (keys.length != 1) {
rdevlin.cronin6ebcb95f82017-06-15 22:58:3657 throw new Error('Too many keys in object-style permission.');
[email protected]7bbdb8a22012-12-13 21:30:4158 }
rdevlin.cronin6ebcb95f82017-06-15 22:58:3659 arguments[0].permissions[i] =
60 keys[0] + '|' + $JSON.stringify(a[keys[0]]);
[email protected]7bbdb8a22012-12-13 21:30:4161 }
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',
rob6f4459702015-02-24 10:44:1477 function(name, request, callback, response) {
[email protected]7bbdb8a22012-12-13 21:30:4178 for (var i = 0; i < response.permissions.length; i += 1) {
79 response.permissions[i] =
80 maybeConvertToObject(response.permissions[i]);
[email protected]288ce6c2012-12-03 21:05:2481 }
82
[email protected]7bbdb8a22012-12-13 21:30:4183 // Since the schema says Permissions.permissions contains strings and
84 // not objects, validation will fail after the for-loop above. This
rob6f4459702015-02-24 10:44:1485 // skips validation and calls the callback directly.
86 if (callback)
87 callback(response);
[email protected]288ce6c2012-12-03 21:05:2488 });
[email protected]288ce6c2012-12-03 21:05:2489});
[email protected]4f1633f2013-03-09 14:26:2490
rdevlin.cronin6ebcb95f82017-06-15 22:58:3691if (!apiBridge)
92 exports.$set('binding', binding.generate());