Suppress tabs permission warning if there is already a browsingHistory warning.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / permissions_data_source.py
blobfb8c239ebad2dcb65e61d0af57721f12e47447f0
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.
17 '''
18 def filter_permissions(perm):
19 return 'description' in perm and not perm['name'].endswith('Private')
21 return sorted(
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.
29 '''
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']:
33 continue
35 has_deps = False
36 if name in api_features:
37 for dependency in api_features[name].get('dependencies', ()):
38 if dependency.startswith('permission:'):
39 has_deps = True
41 if has_deps:
42 permission['partial'] = 'permissions/generic_description.html'
44 class PermissionsDataSource(DataSource):
45 '''Load and format permissions features to be used by templates.
46 '''
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()
57 def resolve():
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))
72 return {
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()
81 if data is None:
82 data = self._CreatePermissionsData().Get()
83 self._object_store.Set('permissions_data', data)
84 return data
86 def Cron(self):
87 return self._CreatePermissionsData()
89 def get(self, key):
90 return self._GetCachedPermissionsData().get(key)