summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/features_bundle.py
blob: b98bdfff576fc70e7a6a0eebfe28e5659688d56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import posixpath

from compiled_file_system import Unicode
from extensions_paths import (
    API_FEATURES, JSON_TEMPLATES, MANIFEST_FEATURES, PERMISSION_FEATURES)
import features_utility
from file_system import FileNotFoundError
from future import Future
from third_party.json_schema_compiler.json_parse import Parse


def _AddPlatformsFromDependencies(feature,
                                  api_features,
                                  manifest_features,
                                  permission_features):
  features_map = {
    'api': api_features,
    'manifest': manifest_features,
    'permission': permission_features,
  }
  dependencies = feature.get('dependencies')
  if dependencies is None:
    return ['apps', 'extensions']
  platforms = set()
  for dependency in dependencies:
    dep_type, dep_name = dependency.split(':')
    dependency_features = features_map[dep_type]
    dependency_feature = dependency_features.get(dep_name)
    # If the dependency can't be resolved, it is inaccessible and therefore
    # so is this feature.
    if dependency_feature is None:
      return []
    platforms = platforms.union(dependency_feature['platforms'])
  feature['platforms'] = list(platforms)


class _FeaturesCache(object):
  def __init__(self, file_system, compiled_fs_factory, *json_paths):
    self._cache = compiled_fs_factory.Create(
        file_system, self._CreateCache, type(self))
    self._text_cache = compiled_fs_factory.ForUnicode(file_system)
    self._json_path = json_paths[0]
    self._extra_paths = json_paths[1:]

  @Unicode
  def _CreateCache(self, _, features_json):
    extra_path_futures = [self._text_cache.GetFromFile(path)
                          for path in self._extra_paths]
    features = features_utility.Parse(Parse(features_json))
    for path_future in extra_path_futures:
      try:
        extra_json = path_future.Get()
      except FileNotFoundError:
        # Not all file system configurations have the extra files.
        continue
      features = features_utility.MergedWith(
          features_utility.Parse(Parse(extra_json)), features)
    return features

  def GetFeatures(self):
    if self._json_path is None:
      return Future(value={})
    return self._cache.GetFromFile(self._json_path)


class FeaturesBundle(object):
  '''Provides access to properties of API, Manifest, and Permission features.
  '''
  def __init__(self, file_system, compiled_fs_factory, object_store_creator):
    self._api_cache = _FeaturesCache(
        file_system,
        compiled_fs_factory,
        API_FEATURES)
    self._manifest_cache = _FeaturesCache(
        file_system,
        compiled_fs_factory,
        MANIFEST_FEATURES,
        posixpath.join(JSON_TEMPLATES, 'manifest.json'))
    self._permission_cache = _FeaturesCache(
        file_system,
        compiled_fs_factory,
        PERMISSION_FEATURES,
        posixpath.join(JSON_TEMPLATES, 'permissions.json'))
    # Namespace the object store by the file system ID because this class is
    # used by the availability finder cross-channel.
    # TODO(kalman): Configure this at the ObjectStore level.
    self._object_store = object_store_creator.Create(
        _FeaturesCache, category=file_system.GetIdentity())

  def GetPermissionFeatures(self):
    return self._permission_cache.GetFeatures()

  def GetManifestFeatures(self):
    return self._manifest_cache.GetFeatures()

  def GetAPIFeatures(self):
    api_features = self._object_store.Get('api_features').Get()
    if api_features is not None:
      return Future(value=api_features)

    api_features_future = self._api_cache.GetFeatures()
    manifest_features_future = self._manifest_cache.GetFeatures()
    permission_features_future = self._permission_cache.GetFeatures()
    def resolve():
      api_features = api_features_future.Get()
      manifest_features = manifest_features_future.Get()
      permission_features = permission_features_future.Get()
      # TODO(rockot): Handle inter-API dependencies more gracefully.
      # Not yet a problem because there is only one such case (windows -> tabs).
      # If we don't store this value before annotating platforms, inter-API
      # dependencies will lead to infinite recursion.
      for feature in api_features.itervalues():
        _AddPlatformsFromDependencies(
            feature, api_features, manifest_features, permission_features)
      self._object_store.Set('api_features', api_features)
      return api_features
    return Future(callback=resolve)