Adding the orphaned options pages to the navigation
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / permissions_data_source.py
blob39bbb869e151260653a3e97fc96f5367677c346c
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.
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:
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._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()
59 def resolve():
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())
81 def resolve():
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()
88 if data is None:
89 data = self._CreatePermissionsData().Get()
90 self._object_store.Set('permissions_data', data)
91 return data
93 def get(self, key):
94 return self._GetCachedPermissionsData().get(key)
96 def Refresh(self, path):
97 return self._CreatePermissionsData()