1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 from itertools
import ifilter
6 from operator
import itemgetter
8 from data_source
import DataSource
9 from extensions_paths
import PRIVATE_TEMPLATES
10 from future
import Future
11 from platform_util
import GetPlatforms
14 def _ListifyPermissions(permissions
):
15 '''Filter out any permissions that do not have a description or with a name
16 that ends with Private then sort permissions features by name into a list.
18 def filter_permissions(perm
):
19 return 'description' in perm
and not perm
['name'].endswith('Private')
22 ifilter(filter_permissions
, permissions
.itervalues()),
23 key
=itemgetter('name'))
26 def _AddDependencyDescriptions(permissions
, api_features
):
27 '''Use |api_features| to determine the dependencies APIs have on permissions.
28 Add descriptions to |permissions| based on those dependencies.
30 for name
, permission
in permissions
.iteritems():
31 # Don't overwrite the description created by expanding a partial template.
32 if 'partial' in permission
:
36 if name
in api_features
:
37 for dependency
in api_features
[name
].get('dependencies', ()):
38 if dependency
.startswith('permission:'):
42 permission
['partial'] = 'permissions/generic_description.html'
44 class PermissionsDataSource(DataSource
):
45 '''Load and format permissions features to be used by templates.
47 def __init__(self
, server_instance
, request
):
48 self
._platform
_bundle
= server_instance
.platform_bundle
49 self
._object
_store
= server_instance
.object_store_creator
.Create(
50 PermissionsDataSource
)
51 self
._template
_cache
= server_instance
.compiled_fs_factory
.ForTemplates(
52 server_instance
.host_file_system_provider
.GetMaster())
54 def _CreatePermissionsDataForPlatform(self
, platform
):
55 features_bundle
= self
._platform
_bundle
.GetFeaturesBundle(platform
)
56 api_features_future
= features_bundle
.GetAPIFeatures()
57 permission_features_future
= features_bundle
.GetPermissionFeatures()
60 api_features
= api_features_future
.Get()
61 permission_features
= permission_features_future
.Get()
62 _AddDependencyDescriptions(permission_features
, api_features
)
64 # Turn partial templates into descriptions, ensure anchors are set.
65 for permission
in permission_features
.values():
66 if not 'anchor' in permission
:
67 permission
['anchor'] = permission
['name']
68 if 'partial' in permission
:
69 permission
['description'] = self
._template
_cache
.GetFromFile(
70 PRIVATE_TEMPLATES
+ permission
['partial']).Get()
71 del permission
['partial']
73 return _ListifyPermissions(permission_features
)
74 return Future(callback
=resolve
)
76 def _CreatePermissionsData(self
):
77 permissions_data_futures
= dict(
78 (platform
, self
._CreatePermissionsDataForPlatform
(platform
))
79 for platform
in GetPlatforms())
82 return dict(('declare_' + platform
, future
.Get())
83 for platform
, future
in permissions_data_futures
.iteritems())
84 return Future(callback
=resolve
)
86 def _GetCachedPermissionsData(self
):
87 data
= self
._object
_store
.Get('permissions_data').Get()
89 data
= self
._CreatePermissionsData
().Get()
90 self
._object
_store
.Set('permissions_data', data
)
94 return self
._GetCachedPermissionsData
().get(key
)
96 def Refresh(self
, path
):
97 return self
._CreatePermissionsData
()