| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import features_utility | 5 import features_utility |
| 6 from future import Gettable, Future |
| 6 import svn_constants | 7 import svn_constants |
| 7 from third_party.json_schema_compiler.json_parse import Parse | 8 from third_party.json_schema_compiler.json_parse import Parse |
| 8 | 9 |
| 9 | 10 |
| 10 def _AddPlatformsFromDependencies(feature, features_bundle): | 11 def _AddPlatformsFromDependencies(feature, |
| 12 api_features, |
| 13 manifest_features, |
| 14 permission_features): |
| 11 features_map = { | 15 features_map = { |
| 12 'api': features_bundle.GetAPIFeatures(), | 16 'api': api_features, |
| 13 'manifest': features_bundle.GetManifestFeatures(), | 17 'manifest': manifest_features, |
| 14 'permission': features_bundle.GetPermissionFeatures() | 18 'permission': permission_features, |
| 15 } | 19 } |
| 16 dependencies = feature.get('dependencies') | 20 dependencies = feature.get('dependencies') |
| 17 if dependencies is None: | 21 if dependencies is None: |
| 18 return ['apps', 'extensions'] | 22 return ['apps', 'extensions'] |
| 19 platforms = set() | 23 platforms = set() |
| 20 for dependency in dependencies: | 24 for dependency in dependencies: |
| 21 dep_type, dep_name = dependency.split(':') | 25 dep_type, dep_name = dependency.split(':') |
| 22 dependency_features = features_map[dep_type] | 26 dependency_features = features_map[dep_type] |
| 23 dependency_feature = dependency_features.get(dep_name) | 27 dependency_feature = dependency_features.get(dep_name) |
| 24 # If the dependency can't be resolved, it is inaccessible and therefore | 28 # If the dependency can't be resolved, it is inaccessible and therefore |
| 25 # so is this feature. | 29 # so is this feature. |
| 26 if dependency_feature is None: | 30 if dependency_feature is None: |
| 27 return [] | 31 return [] |
| 28 platforms = platforms.union(dependency_feature['platforms']) | 32 platforms = platforms.union(dependency_feature['platforms']) |
| 29 feature['platforms'] = list(platforms) | 33 feature['platforms'] = list(platforms) |
| 30 | 34 |
| 31 | 35 |
| 32 class _FeaturesCache(object): | 36 class _FeaturesCache(object): |
| 33 def __init__(self, file_system, compiled_fs_factory, *json_paths): | 37 def __init__(self, file_system, compiled_fs_factory, *json_paths): |
| 34 self._file_system = file_system | 38 self._file_system = file_system |
| 35 self._cache = compiled_fs_factory.Create( | 39 self._cache = compiled_fs_factory.Create( |
| 36 file_system, self._CreateCache, type(self)) | 40 file_system, self._CreateCache, type(self)) |
| 37 self._json_path = json_paths[0] | 41 self._json_path = json_paths[0] |
| 38 self._extra_paths = json_paths[1:] | 42 self._extra_paths = json_paths[1:] |
| 39 | 43 |
| 40 def _CreateCache(self, _, features_json): | 44 def _CreateCache(self, _, features_json): |
| 45 extra_path_futures = [self._file_system.ReadSingle(path) |
| 46 for path in self._extra_paths] |
| 41 features = features_utility.Parse(Parse(features_json)) | 47 features = features_utility.Parse(Parse(features_json)) |
| 42 for path in self._extra_paths: | 48 for path_future in extra_path_futures: |
| 43 extra_json = self._file_system.ReadSingle(path).Get() | 49 extra_json = path_future.Get() |
| 44 features = features_utility.MergedWith( | 50 features = features_utility.MergedWith( |
| 45 features_utility.Parse(Parse(extra_json)), features) | 51 features_utility.Parse(Parse(extra_json)), features) |
| 46 return features | 52 return features |
| 47 | 53 |
| 48 def GetFeatures(self): | 54 def GetFeatures(self): |
| 49 if self._json_path is None: | 55 if self._json_path is None: |
| 50 return {} | 56 return Future(value={}) |
| 51 return self._cache.GetFromFile(self._json_path).Get() | 57 return self._cache.GetFromFile(self._json_path) |
| 52 | 58 |
| 53 | 59 |
| 54 class FeaturesBundle(object): | 60 class FeaturesBundle(object): |
| 55 '''Provides access to properties of API, Manifest, and Permission features. | 61 '''Provides access to properties of API, Manifest, and Permission features. |
| 56 ''' | 62 ''' |
| 57 def __init__(self, file_system, compiled_fs_factory, object_store_creator): | 63 def __init__(self, file_system, compiled_fs_factory, object_store_creator): |
| 58 self._api_cache = _FeaturesCache( | 64 self._api_cache = _FeaturesCache( |
| 59 file_system, | 65 file_system, |
| 60 compiled_fs_factory, | 66 compiled_fs_factory, |
| 61 svn_constants.API_FEATURES_PATH) | 67 svn_constants.API_FEATURES_PATH) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 self._object_store = object_store_creator.Create(_FeaturesCache, 'features') | 78 self._object_store = object_store_creator.Create(_FeaturesCache, 'features') |
| 73 | 79 |
| 74 def GetPermissionFeatures(self): | 80 def GetPermissionFeatures(self): |
| 75 return self._permission_cache.GetFeatures() | 81 return self._permission_cache.GetFeatures() |
| 76 | 82 |
| 77 def GetManifestFeatures(self): | 83 def GetManifestFeatures(self): |
| 78 return self._manifest_cache.GetFeatures() | 84 return self._manifest_cache.GetFeatures() |
| 79 | 85 |
| 80 def GetAPIFeatures(self): | 86 def GetAPIFeatures(self): |
| 81 api_features = self._object_store.Get('api_features').Get() | 87 api_features = self._object_store.Get('api_features').Get() |
| 82 if api_features is None: | 88 if api_features is not None: |
| 83 api_features = self._api_cache.GetFeatures() | 89 return Future(value=api_features) |
| 90 |
| 91 api_features_future = self._api_cache.GetFeatures() |
| 92 manifest_features_future = self._manifest_cache.GetFeatures() |
| 93 permission_features_future = self._permission_cache.GetFeatures() |
| 94 def resolve(): |
| 95 api_features = api_features_future.Get() |
| 96 manifest_features = manifest_features_future.Get() |
| 97 permission_features = permission_features_future.Get() |
| 84 # TODO(rockot): Handle inter-API dependencies more gracefully. | 98 # TODO(rockot): Handle inter-API dependencies more gracefully. |
| 85 # Not yet a problem because there is only one such case (windows -> tabs). | 99 # Not yet a problem because there is only one such case (windows -> tabs). |
| 86 # If we don't store this value before annotating platforms, inter-API | 100 # If we don't store this value before annotating platforms, inter-API |
| 87 # dependencies will lead to infinite recursion. | 101 # dependencies will lead to infinite recursion. |
| 102 for feature in api_features.itervalues(): |
| 103 _AddPlatformsFromDependencies( |
| 104 feature, api_features, manifest_features, permission_features) |
| 88 self._object_store.Set('api_features', api_features) | 105 self._object_store.Set('api_features', api_features) |
| 89 for feature in api_features.itervalues(): | 106 return api_features |
| 90 _AddPlatformsFromDependencies(feature, self) | 107 return Future(delegate=Gettable(resolve)) |
| 91 self._object_store.Set('api_features', api_features) | |
| 92 return api_features | |
| OLD | NEW |