OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 import features_utility |
| 6 import svn_constants |
| 7 from third_party.json_schema_compiler.json_parse import Parse |
| 8 |
| 9 |
| 10 def _AddPlatformsFromDependencies(feature, features_bundle): |
| 11 features_map = { |
| 12 'api': features_bundle.GetAPIFeatures(), |
| 13 'manifest': features_bundle.GetManifestFeatures(), |
| 14 'permission': features_bundle.GetPermissionFeatures() |
| 15 } |
| 16 dependencies = feature.get('dependencies') |
| 17 if dependencies is None: |
| 18 return ['apps', 'extensions'] |
| 19 platforms = set() |
| 20 for dependency in dependencies: |
| 21 dep_type, dep_name = dependency.split(':') |
| 22 dependency_features = features_map[dep_type] |
| 23 dependency_feature = dependency_features.get(dep_name) |
| 24 # If the dependency can't be resolved, it is inaccessible and therefore |
| 25 # so is this feature. |
| 26 if dependency_feature is None: |
| 27 return [] |
| 28 platforms = platforms.union(dependency_feature['platforms']) |
| 29 feature['platforms'] = list(platforms) |
| 30 |
| 31 |
| 32 class _FeaturesCache(object): |
| 33 def __init__(self, file_system, compiled_fs_factory, *json_paths): |
| 34 self._file_system = file_system |
| 35 self._cache = compiled_fs_factory.Create(self._CreateCache, type(self)) |
| 36 self._json_path = json_paths[0] |
| 37 self._extra_paths = json_paths[1:] |
| 38 |
| 39 def _CreateCache(self, _, features_json): |
| 40 features = features_utility.Parse(Parse(features_json)) |
| 41 for path in self._extra_paths: |
| 42 extra_json = self._file_system.ReadSingle(path) |
| 43 features = features_utility.MergedWith( |
| 44 features_utility.Parse(Parse(extra_json)), features) |
| 45 return features |
| 46 |
| 47 def GetFeatures(self): |
| 48 if self._json_path is None: |
| 49 return {} |
| 50 return self._cache.GetFromFile(self._json_path) |
| 51 |
| 52 |
| 53 class FeaturesBundle(object): |
| 54 '''Provides access to properties of API, Manifest, and Permission features. |
| 55 ''' |
| 56 def __init__(self, file_system, compiled_fs_factory, object_store_creator): |
| 57 self._api_cache = _FeaturesCache( |
| 58 file_system, |
| 59 compiled_fs_factory, |
| 60 svn_constants.API_FEATURES_PATH) |
| 61 self._manifest_cache = _FeaturesCache( |
| 62 file_system, |
| 63 compiled_fs_factory, |
| 64 svn_constants.MANIFEST_FEATURES_PATH, |
| 65 svn_constants.MANIFEST_JSON_PATH) |
| 66 self._permission_cache = _FeaturesCache( |
| 67 file_system, |
| 68 compiled_fs_factory, |
| 69 svn_constants.PERMISSION_FEATURES_PATH, |
| 70 svn_constants.PERMISSIONS_JSON_PATH) |
| 71 self._object_store = object_store_creator.Create(_FeaturesCache, 'features') |
| 72 |
| 73 def GetPermissionFeatures(self): |
| 74 return self._permission_cache.GetFeatures() |
| 75 |
| 76 def GetManifestFeatures(self): |
| 77 return self._manifest_cache.GetFeatures() |
| 78 |
| 79 def GetAPIFeatures(self): |
| 80 api_features = self._object_store.Get('api_features').Get() |
| 81 if api_features is None: |
| 82 api_features = self._api_cache.GetFeatures() |
| 83 # TODO(rockot): Handle inter-API dependencies more gracefully. |
| 84 # Not yet a problem because there is only one such case (windows -> tabs). |
| 85 # If we don't store this value before annotating platforms, inter-API |
| 86 # dependencies will lead to infinite recursion. |
| 87 self._object_store.Set('api_features', api_features) |
| 88 for feature in api_features.itervalues(): |
| 89 _AddPlatformsFromDependencies(feature, self) |
| 90 self._object_store.Set('api_features', api_features) |
| 91 return api_features |
OLD | NEW |