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.
7 from compiled_file_system import Unicode
8 from extensions_paths import (
9 API_FEATURES, JSON_TEMPLATES, MANIFEST_FEATURES, PERMISSION_FEATURES)
10 import features_utility
11 from future import Gettable, Future
12 from third_party.json_schema_compiler.json_parse import Parse
15 def _AddPlatformsFromDependencies(feature,
21 'manifest': manifest_features,
22 'permission': permission_features,
24 dependencies = feature.get('dependencies')
25 if dependencies is None:
26 return ['apps', 'extensions']
28 for dependency in dependencies:
29 dep_type, dep_name = dependency.split(':')
30 dependency_features = features_map[dep_type]
31 dependency_feature = dependency_features.get(dep_name)
32 # If the dependency can't be resolved, it is inaccessible and therefore
34 if dependency_feature is None:
36 platforms = platforms.union(dependency_feature['platforms'])
37 feature['platforms'] = list(platforms)
40 class _FeaturesCache(object):
41 def __init__(self, file_system, compiled_fs_factory, *json_paths):
42 self._cache = compiled_fs_factory.Create(
43 file_system, self._CreateCache, type(self))
44 self._text_cache = compiled_fs_factory.ForUnicode(file_system)
45 self._json_path = json_paths[0]
46 self._extra_paths = json_paths[1:]
49 def _CreateCache(self, _, features_json):
50 extra_path_futures = [self._text_cache.GetFromFile(path)
51 for path in self._extra_paths]
52 features = features_utility.Parse(Parse(features_json))
53 for path_future in extra_path_futures:
54 extra_json = path_future.Get()
55 features = features_utility.MergedWith(
56 features_utility.Parse(Parse(extra_json)), features)
59 def GetFeatures(self):
60 if self._json_path is None:
61 return Future(value={})
62 return self._cache.GetFromFile(self._json_path)
65 class FeaturesBundle(object):
66 '''Provides access to properties of API, Manifest, and Permission features.
68 def __init__(self, file_system, compiled_fs_factory, object_store_creator):
69 self._api_cache = _FeaturesCache(
73 self._manifest_cache = _FeaturesCache(
77 posixpath.join(JSON_TEMPLATES, 'manifest.json'))
78 self._permission_cache = _FeaturesCache(
82 posixpath.join(JSON_TEMPLATES, 'permissions.json'))
83 self._object_store = object_store_creator.Create(_FeaturesCache, 'features')
85 def GetPermissionFeatures(self):
86 return self._permission_cache.GetFeatures()
88 def GetManifestFeatures(self):
89 return self._manifest_cache.GetFeatures()
91 def GetAPIFeatures(self):
92 api_features = self._object_store.Get('api_features').Get()
93 if api_features is not None:
94 return Future(value=api_features)
96 api_features_future = self._api_cache.GetFeatures()
97 manifest_features_future = self._manifest_cache.GetFeatures()
98 permission_features_future = self._permission_cache.GetFeatures()
100 api_features = api_features_future.Get()
101 manifest_features = manifest_features_future.Get()
102 permission_features = permission_features_future.Get()
103 # TODO(rockot): Handle inter-API dependencies more gracefully.
104 # Not yet a problem because there is only one such case (windows -> tabs).
105 # If we don't store this value before annotating platforms, inter-API
106 # dependencies will lead to infinite recursion.
107 for feature in api_features.itervalues():
108 _AddPlatformsFromDependencies(
109 feature, api_features, manifest_features, permission_features)
110 self._object_store.Set('api_features', api_features)
112 return Future(delegate=Gettable(resolve))