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 features_utility
import Filtered
11 from future
import Future
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
or not permission
['platforms']:
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
._features
_bundle
= server_instance
.features_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
.GetTrunk())
54 def _CreatePermissionsData(self
):
55 api_features_future
= self
._features
_bundle
.GetAPIFeatures()
56 permission_features_future
= self
._features
_bundle
.GetPermissionFeatures()
58 permission_features
= permission_features_future
.Get()
59 _AddDependencyDescriptions(permission_features
, api_features_future
.Get())
61 # Turn partial templates into descriptions, ensure anchors are set.
62 for permission
in permission_features
.values():
63 if not 'anchor' in permission
:
64 permission
['anchor'] = permission
['name']
65 if 'partial' in permission
:
66 permission
['description'] = self
._template
_cache
.GetFromFile(
67 PRIVATE_TEMPLATES
+ permission
['partial']).Get()
68 del permission
['partial']
70 def filter_for_platform(permissions
, platform
):
71 return _ListifyPermissions(Filtered(permissions
, platform
))
73 'declare_apps': filter_for_platform(permission_features
, 'apps'),
74 'declare_extensions': filter_for_platform(
75 permission_features
, 'extensions')
77 return Future(callback
=resolve
)
79 def _GetCachedPermissionsData(self
):
80 data
= self
._object
_store
.Get('permissions_data').Get()
82 data
= self
._CreatePermissionsData
().Get()
83 self
._object
_store
.Set('permissions_data', data
)
87 return self
._CreatePermissionsData
()
90 return self
._GetCachedPermissionsData
().get(key
)