features_bundle.py revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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 5import posixpath 6 7from compiled_file_system import SingleFile, Unicode 8from extensions_paths import API_PATHS, JSON_TEMPLATES 9import features_utility 10from file_system import FileNotFoundError 11from future import Future 12from third_party.json_schema_compiler.json_parse import Parse 13 14 15_API_FEATURES = '_api_features.json' 16_MANIFEST_FEATURES = '_manifest_features.json' 17_PERMISSION_FEATURES = '_permission_features.json' 18 19 20def _GetFeaturePaths(feature_file, *extra_paths): 21 paths = [posixpath.join(api_path, feature_file) for api_path in API_PATHS] 22 paths.extend(extra_paths) 23 return paths 24 25 26def _AddPlatformsFromDependencies(feature, 27 api_features, 28 manifest_features, 29 permission_features): 30 features_map = { 31 'api': api_features, 32 'manifest': manifest_features, 33 'permission': permission_features, 34 } 35 dependencies = feature.get('dependencies') 36 if dependencies is None: 37 return ['apps', 'extensions'] 38 platforms = set() 39 for dependency in dependencies: 40 dep_type, dep_name = dependency.split(':') 41 dependency_features = features_map[dep_type] 42 dependency_feature = dependency_features.get(dep_name) 43 # If the dependency can't be resolved, it is inaccessible and therefore 44 # so is this feature. 45 if dependency_feature is None: 46 return [] 47 platforms = platforms.union(dependency_feature['platforms']) 48 feature['platforms'] = list(platforms) 49 50 51class _FeaturesCache(object): 52 def __init__(self, file_system, compiled_fs_factory, json_paths): 53 populate = self._CreateCache 54 if len(json_paths) == 1: 55 populate = SingleFile(populate) 56 57 self._cache = compiled_fs_factory.Create(file_system, populate, type(self)) 58 self._text_cache = compiled_fs_factory.ForUnicode(file_system) 59 self._json_path = json_paths[0] 60 self._extra_paths = json_paths[1:] 61 62 @Unicode 63 def _CreateCache(self, _, features_json): 64 extra_path_futures = [self._text_cache.GetFromFile(path) 65 for path in self._extra_paths] 66 features = features_utility.Parse(Parse(features_json)) 67 for path_future in extra_path_futures: 68 try: 69 extra_json = path_future.Get() 70 except FileNotFoundError: 71 # Not all file system configurations have the extra files. 72 continue 73 features = features_utility.MergedWith( 74 features_utility.Parse(Parse(extra_json)), features) 75 return features 76 77 def GetFeatures(self): 78 if self._json_path is None: 79 return Future(value={}) 80 return self._cache.GetFromFile(self._json_path) 81 82 83class FeaturesBundle(object): 84 '''Provides access to properties of API, Manifest, and Permission features. 85 ''' 86 def __init__(self, file_system, compiled_fs_factory, object_store_creator): 87 self._api_cache = _FeaturesCache( 88 file_system, 89 compiled_fs_factory, 90 _GetFeaturePaths(_API_FEATURES)) 91 self._manifest_cache = _FeaturesCache( 92 file_system, 93 compiled_fs_factory, 94 _GetFeaturePaths(_MANIFEST_FEATURES, 95 posixpath.join(JSON_TEMPLATES, 'manifest.json'))) 96 self._permission_cache = _FeaturesCache( 97 file_system, 98 compiled_fs_factory, 99 _GetFeaturePaths(_PERMISSION_FEATURES, 100 posixpath.join(JSON_TEMPLATES, 'permissions.json'))) 101 # Namespace the object store by the file system ID because this class is 102 # used by the availability finder cross-channel. 103 # TODO(kalman): Configure this at the ObjectStore level. 104 self._object_store = object_store_creator.Create( 105 _FeaturesCache, category=file_system.GetIdentity()) 106 107 def GetPermissionFeatures(self): 108 return self._permission_cache.GetFeatures() 109 110 def GetManifestFeatures(self): 111 return self._manifest_cache.GetFeatures() 112 113 def GetAPIFeatures(self): 114 api_features = self._object_store.Get('api_features').Get() 115 if api_features is not None: 116 return Future(value=api_features) 117 118 api_features_future = self._api_cache.GetFeatures() 119 manifest_features_future = self._manifest_cache.GetFeatures() 120 permission_features_future = self._permission_cache.GetFeatures() 121 def resolve(): 122 api_features = api_features_future.Get() 123 manifest_features = manifest_features_future.Get() 124 permission_features = permission_features_future.Get() 125 # TODO(rockot): Handle inter-API dependencies more gracefully. 126 # Not yet a problem because there is only one such case (windows -> tabs). 127 # If we don't store this value before annotating platforms, inter-API 128 # dependencies will lead to infinite recursion. 129 for feature in api_features.itervalues(): 130 _AddPlatformsFromDependencies( 131 feature, api_features, manifest_features, permission_features) 132 self._object_store.Set('api_features', api_features) 133 return api_features 134 return Future(callback=resolve) 135