Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / features_bundle.py
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 posixpath
6
7 from compiled_file_system import Unicode
8 from extensions_paths import (
9     API_FEATURES, JSON_TEMPLATES, MANIFEST_FEATURES, PERMISSION_FEATURES)
10 import features_utility
11 from future import Gettable, Future
12 from third_party.json_schema_compiler.json_parse import Parse
13
14
15 def _AddPlatformsFromDependencies(feature,
16                                   api_features,
17                                   manifest_features,
18                                   permission_features):
19   features_map = {
20     'api': api_features,
21     'manifest': manifest_features,
22     'permission': permission_features,
23   }
24   dependencies = feature.get('dependencies')
25   if dependencies is None:
26     return ['apps', 'extensions']
27   platforms = set()
28   for dependency in dependencies:
29     dep_type, dep_name = dependency.split(':')
30     dependency_features = features_map[dep_type]
31     dependency_feature = dependency_features.get(dep_name)
32     # If the dependency can't be resolved, it is inaccessible and therefore
33     # so is this feature.
34     if dependency_feature is None:
35       return []
36     platforms = platforms.union(dependency_feature['platforms'])
37   feature['platforms'] = list(platforms)
38
39
40 class _FeaturesCache(object):
41   def __init__(self, file_system, compiled_fs_factory, *json_paths):
42     self._cache = compiled_fs_factory.Create(
43         file_system, self._CreateCache, type(self))
44     self._text_cache = compiled_fs_factory.ForUnicode(file_system)
45     self._json_path = json_paths[0]
46     self._extra_paths = json_paths[1:]
47
48   @Unicode
49   def _CreateCache(self, _, features_json):
50     extra_path_futures = [self._text_cache.GetFromFile(path)
51                           for path in self._extra_paths]
52     features = features_utility.Parse(Parse(features_json))
53     for path_future in extra_path_futures:
54       extra_json = path_future.Get()
55       features = features_utility.MergedWith(
56           features_utility.Parse(Parse(extra_json)), features)
57     return features
58
59   def GetFeatures(self):
60     if self._json_path is None:
61       return Future(value={})
62     return self._cache.GetFromFile(self._json_path)
63
64
65 class FeaturesBundle(object):
66   '''Provides access to properties of API, Manifest, and Permission features.
67   '''
68   def __init__(self, file_system, compiled_fs_factory, object_store_creator):
69     self._api_cache = _FeaturesCache(
70         file_system,
71         compiled_fs_factory,
72         API_FEATURES)
73     self._manifest_cache = _FeaturesCache(
74         file_system,
75         compiled_fs_factory,
76         MANIFEST_FEATURES,
77         posixpath.join(JSON_TEMPLATES, 'manifest.json'))
78     self._permission_cache = _FeaturesCache(
79         file_system,
80         compiled_fs_factory,
81         PERMISSION_FEATURES,
82         posixpath.join(JSON_TEMPLATES, 'permissions.json'))
83     self._object_store = object_store_creator.Create(_FeaturesCache, 'features')
84
85   def GetPermissionFeatures(self):
86     return self._permission_cache.GetFeatures()
87
88   def GetManifestFeatures(self):
89     return self._manifest_cache.GetFeatures()
90
91   def GetAPIFeatures(self):
92     api_features = self._object_store.Get('api_features').Get()
93     if api_features is not None:
94       return Future(value=api_features)
95
96     api_features_future = self._api_cache.GetFeatures()
97     manifest_features_future = self._manifest_cache.GetFeatures()
98     permission_features_future = self._permission_cache.GetFeatures()
99     def resolve():
100       api_features = api_features_future.Get()
101       manifest_features = manifest_features_future.Get()
102       permission_features = permission_features_future.Get()
103       # TODO(rockot): Handle inter-API dependencies more gracefully.
104       # Not yet a problem because there is only one such case (windows -> tabs).
105       # If we don't store this value before annotating platforms, inter-API
106       # dependencies will lead to infinite recursion.
107       for feature in api_features.itervalues():
108         _AddPlatformsFromDependencies(
109             feature, api_features, manifest_features, permission_features)
110       self._object_store.Set('api_features', api_features)
111       return api_features
112     return Future(delegate=Gettable(resolve))