• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 _AddPlatformsAndChannelsFromDependencies(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
38  platforms = set()
39  channel = None
40  for dependency in dependencies:
41    dep_type, dep_name = dependency.split(':')
42    dependency_features = features_map[dep_type]
43    dependency_feature = dependency_features.get(dep_name)
44    # If the dependency can't be resolved, it is inaccessible and therefore
45    # so is this feature.
46    if dependency_feature is None:
47      return
48    # Import the platforms from the dependency. The logic is a bit odd; if
49    # |feature| specifies platforms the it's considered an override. If not,
50    # we form the union of all dependency's platforms.
51    # TODO(kalman): Fix this (see http://crbug.com/322094).
52    platforms.update(dependency_feature.get('platforms', set()))
53    # Import the channel from the dependency.
54    channel = dependency_feature.get('channel', channel)
55  if platforms and not feature.get('platforms'):
56    feature['platforms'] = list(platforms)
57  if channel and not feature.get('channel'):
58    feature['channel'] = channel
59
60
61class _FeaturesCache(object):
62  def __init__(self, file_system, compiled_fs_factory, json_paths):
63    populate = self._CreateCache
64    if len(json_paths) == 1:
65      populate = SingleFile(populate)
66
67    self._cache = compiled_fs_factory.Create(file_system, populate, type(self))
68    self._text_cache = compiled_fs_factory.ForUnicode(file_system)
69    self._json_path = json_paths[0]
70    self._extra_paths = json_paths[1:]
71
72  @Unicode
73  def _CreateCache(self, _, features_json):
74    extra_path_futures = [self._text_cache.GetFromFile(path)
75                          for path in self._extra_paths]
76    features = features_utility.Parse(Parse(features_json))
77    for path_future in extra_path_futures:
78      try:
79        extra_json = path_future.Get()
80      except FileNotFoundError:
81        # Not all file system configurations have the extra files.
82        continue
83      features = features_utility.MergedWith(
84          features_utility.Parse(Parse(extra_json)), features)
85    return features
86
87  def GetFeatures(self):
88    if self._json_path is None:
89      return Future(value={})
90    return self._cache.GetFromFile(self._json_path)
91
92
93class FeaturesBundle(object):
94  '''Provides access to properties of API, Manifest, and Permission features.
95  '''
96  def __init__(self, file_system, compiled_fs_factory, object_store_creator):
97    self._api_cache = _FeaturesCache(
98        file_system,
99        compiled_fs_factory,
100        _GetFeaturePaths(_API_FEATURES))
101    self._manifest_cache = _FeaturesCache(
102        file_system,
103        compiled_fs_factory,
104        _GetFeaturePaths(_MANIFEST_FEATURES,
105                         posixpath.join(JSON_TEMPLATES, 'manifest.json')))
106    self._permission_cache = _FeaturesCache(
107        file_system,
108        compiled_fs_factory,
109        _GetFeaturePaths(_PERMISSION_FEATURES,
110                         posixpath.join(JSON_TEMPLATES, 'permissions.json')))
111    self._identity = file_system.GetIdentity()
112    self._object_store = object_store_creator.Create(
113        _FeaturesCache,
114        category=self._identity)
115
116  def GetPermissionFeatures(self):
117    return self._permission_cache.GetFeatures()
118
119  def GetManifestFeatures(self):
120    return self._manifest_cache.GetFeatures()
121
122  def GetAPIFeatures(self):
123    api_features = self._object_store.Get('api_features').Get()
124    if api_features is not None:
125      return Future(value=api_features)
126
127    api_features_future = self._api_cache.GetFeatures()
128    manifest_features_future = self._manifest_cache.GetFeatures()
129    permission_features_future = self._permission_cache.GetFeatures()
130    def resolve():
131      api_features = api_features_future.Get()
132      manifest_features = manifest_features_future.Get()
133      permission_features = permission_features_future.Get()
134      # TODO(rockot): Handle inter-API dependencies more gracefully.
135      # Not yet a problem because there is only one such case (windows -> tabs).
136      # If we don't store this value before annotating platforms, inter-API
137      # dependencies will lead to infinite recursion.
138      for feature in api_features.itervalues():
139        _AddPlatformsAndChannelsFromDependencies(
140            feature, api_features, manifest_features, permission_features)
141      self._object_store.Set('api_features', api_features)
142      return api_features
143    return Future(callback=resolve)
144
145  def GetIdentity(self):
146    return self._identity
147