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 SingleFile, Unicode
8 from extensions_paths import (
9 API_FEATURES, JSON_TEMPLATES, MANIFEST_FEATURES, PERMISSION_FEATURES)
10 import features_utility
11 from file_system import FileNotFoundError
12 from future import Future
13 from third_party.json_schema_compiler.json_parse import Parse
16 def _AddPlatformsFromDependencies(feature,
22 'manifest': manifest_features,
23 'permission': permission_features,
25 dependencies = feature.get('dependencies')
26 if dependencies is None:
27 return ['apps', 'extensions']
29 for dependency in dependencies:
30 dep_type, dep_name = dependency.split(':')
31 dependency_features = features_map[dep_type]
32 dependency_feature = dependency_features.get(dep_name)
33 # If the dependency can't be resolved, it is inaccessible and therefore
35 if dependency_feature is None:
37 platforms = platforms.union(dependency_feature['platforms'])
38 feature['platforms'] = list(platforms)
41 class _FeaturesCache(object):
42 def __init__(self, file_system, compiled_fs_factory, *json_paths):
43 populate = self._CreateCache
44 if len(json_paths) == 1:
45 populate = SingleFile(populate)
47 self._cache = compiled_fs_factory.Create(file_system, populate, type(self))
48 self._text_cache = compiled_fs_factory.ForUnicode(file_system)
49 self._json_path = json_paths[0]
50 self._extra_paths = json_paths[1:]
53 def _CreateCache(self, _, features_json):
54 extra_path_futures = [self._text_cache.GetFromFile(path)
55 for path in self._extra_paths]
56 features = features_utility.Parse(Parse(features_json))
57 for path_future in extra_path_futures:
59 extra_json = path_future.Get()
60 except FileNotFoundError:
61 # Not all file system configurations have the extra files.
63 features = features_utility.MergedWith(
64 features_utility.Parse(Parse(extra_json)), features)
67 def GetFeatures(self):
68 if self._json_path is None:
69 return Future(value={})
70 return self._cache.GetFromFile(self._json_path)
73 class FeaturesBundle(object):
74 '''Provides access to properties of API, Manifest, and Permission features.
76 def __init__(self, file_system, compiled_fs_factory, object_store_creator):
77 self._api_cache = _FeaturesCache(
81 self._manifest_cache = _FeaturesCache(
85 posixpath.join(JSON_TEMPLATES, 'manifest.json'))
86 self._permission_cache = _FeaturesCache(
90 posixpath.join(JSON_TEMPLATES, 'permissions.json'))
91 # Namespace the object store by the file system ID because this class is
92 # used by the availability finder cross-channel.
93 # TODO(kalman): Configure this at the ObjectStore level.
94 self._object_store = object_store_creator.Create(
95 _FeaturesCache, category=file_system.GetIdentity())
97 def GetPermissionFeatures(self):
98 return self._permission_cache.GetFeatures()
100 def GetManifestFeatures(self):
101 return self._manifest_cache.GetFeatures()
103 def GetAPIFeatures(self):
104 api_features = self._object_store.Get('api_features').Get()
105 if api_features is not None:
106 return Future(value=api_features)
108 api_features_future = self._api_cache.GetFeatures()
109 manifest_features_future = self._manifest_cache.GetFeatures()
110 permission_features_future = self._permission_cache.GetFeatures()
112 api_features = api_features_future.Get()
113 manifest_features = manifest_features_future.Get()
114 permission_features = permission_features_future.Get()
115 # TODO(rockot): Handle inter-API dependencies more gracefully.
116 # Not yet a problem because there is only one such case (windows -> tabs).
117 # If we don't store this value before annotating platforms, inter-API
118 # dependencies will lead to infinite recursion.
119 for feature in api_features.itervalues():
120 _AddPlatformsFromDependencies(
121 feature, api_features, manifest_features, permission_features)
122 self._object_store.Set('api_features', api_features)
124 return Future(callback=resolve)