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