Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/common/extensions/docs/server2/api_models.py

Issue 375133002: Docserver: Display API features that are available to content scripts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import posixpath 5 import posixpath
6 6
7 from compiled_file_system import SingleFile, Unicode 7 from compiled_file_system import SingleFile, Unicode
8 from docs_server_utils import StringIdentity 8 from docs_server_utils import StringIdentity
9 from extensions_paths import API_PATHS 9 from extensions_paths import API_PATHS
10 from features_bundle import HasParentFeature 10 from features_bundle import HasParentFeature, GetParentFeature
11 from file_system import FileNotFoundError 11 from file_system import FileNotFoundError
12 from future import Collect, Future 12 from future import Collect, Future
13 from operator import itemgetter
13 from platform_util import PlatformToExtensionType 14 from platform_util import PlatformToExtensionType
14 from schema_util import ProcessSchema 15 from schema_util import ProcessSchema
15 from third_party.json_schema_compiler.json_schema import DeleteNodes 16 from third_party.json_schema_compiler.json_schema import DeleteNodes
16 from third_party.json_schema_compiler.model import Namespace, UnixName 17 from third_party.json_schema_compiler.model import Namespace, UnixName
17 18
18 19
19 class APIModels(object): 20 class APIModels(object):
20 '''Tracks APIs and their Models. 21 '''Tracks APIs and their Models.
21 ''' 22 '''
22 23
23 def __init__(self, 24 def __init__(self,
24 features_bundle, 25 features_bundle,
25 compiled_fs_factory, 26 compiled_fs_factory,
26 file_system, 27 file_system,
28 object_store_creator,
27 platform): 29 platform):
28 self._features_bundle = features_bundle 30 self._features_bundle = features_bundle
29 self._platform = PlatformToExtensionType(platform) 31 self._platform = PlatformToExtensionType(platform)
30 self._model_cache = compiled_fs_factory.Create( 32 self._model_cache = compiled_fs_factory.Create(
31 file_system, self._CreateAPIModel, APIModels, category=self._platform) 33 file_system, self._CreateAPIModel, APIModels, category=self._platform)
34 self._object_store = object_store_creator.Create(APIModels)
32 35
33 @SingleFile 36 @SingleFile
34 @Unicode 37 @Unicode
35 def _CreateAPIModel(self, path, data): 38 def _CreateAPIModel(self, path, data):
36 def does_not_include_platform(node): 39 def does_not_include_platform(node):
37 return ('extension_types' in node and 40 return ('extension_types' in node and
38 node['extension_types'] != 'all' and 41 node['extension_types'] != 'all' and
39 self._platform not in node['extension_types']) 42 self._platform not in node['extension_types'])
40 43
41 schema = ProcessSchema(path, data, inline=True)[0] 44 schema = ProcessSchema(path, data, inline=True)[0]
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 def resolve(): 93 def resolve():
91 for future in futures: 94 for future in futures:
92 try: 95 try:
93 return future.Get() 96 return future.Get()
94 # Either the file wasn't found or there was no schema for the file 97 # Either the file wasn't found or there was no schema for the file
95 except (FileNotFoundError, ValueError): pass 98 except (FileNotFoundError, ValueError): pass
96 # Propagate the first error if neither were found. 99 # Propagate the first error if neither were found.
97 futures[0].Get() 100 futures[0].Get()
98 return Future(callback=resolve) 101 return Future(callback=resolve)
99 102
103 def GetContentScriptsAPIs(self):
not at google - send to devlin 2014/07/09 02:55:55 s/GetContentScriptsAPIs/GetContentScriptAPIs/
104 '''Creates a dict of APIs and nodes supported by content scripts in
105 this format:
106
107 {
108 'extension': {
109 'name': 'extension',
not at google - send to devlin 2014/07/09 02:55:55 Can we return an actual type rather than a diction
110 'fullSupport': False,
not at google - send to devlin 2014/07/09 02:55:55 'allNodes' might be more consistent with 'supporte
111 'supportedNodes': [{
112 'parent': 'extension',
113 'node': sendMessage
114 },
115 ...
116 ]
117 },
118 ...
119 }
120 '''
121 content_script_apis_future = self._object_store.Get('content_script_apis')
122 api_features_future = self._features_bundle.GetAPIFeatures()
123 def resolve():
124 content_script_apis = content_script_apis_future.Get()
125 api_features = api_features_future.Get()
not at google - send to devlin 2014/07/09 02:55:55 you don't need to resolve this is if content_scrip
126 if content_script_apis is not None:
127 return content_script_apis
128
129 content_script_apis = {}
130 for feature in api_features.itervalues():
131 # Gets all the features that have 'content_script' in their context.
132 if 'content_script' in feature.get('contexts', ()):
not at google - send to devlin 2014/07/09 02:55:55 early-continue
133 parent = GetParentFeature(feature['name'], feature, api_features)
134 # If it has no parent, then the API is fully supported.
135 if parent is None:
136 content_script_apis[feature['name']] = {
137 'name': feature['name'],
138 'fullSupport': True,
139 'supportedNodes': []
140 }
141 else:
142 # Creates a dict for the individual node.
143 node = {'parent': parent, 'node': feature['name'][len(parent) + 1:]}
144 # Initializes the supportedNodes list if it doesn't exist yet.
145 if parent not in content_script_apis:
146 content_script_apis[parent] = {
147 'name': parent,
148 'fullSupport': False,
149 'supportedNodes': [node]
150 }
151 else:
152 content_script_apis[parent]['supportedNodes'].append(node)
153
154 # Sort all of the supportedNodes.
155 for content_script_api in content_script_apis.itervalues():
156 if content_script_api['supportedNodes']:
157 content_script_api['supportedNodes'].sort(key=itemgetter('node'))
158 # Annotate the first and last ones for use in the templates.
159 content_script_api['supportedNodes'][0]['first'] = True
160 content_script_api['supportedNodes'][-1]['last'] = True
161
162 self._object_store.Set('content_script_apis', content_script_apis)
163 return content_script_apis
164 return Future(callback=resolve)
165
100 def Cron(self): 166 def Cron(self):
101 futures = [self.GetModel(name) for name in self.GetNames()] 167 futures = [self.GetModel(name) for name in self.GetNames()]
102 return Collect(futures, except_pass=(FileNotFoundError, ValueError)) 168 return Collect(futures, except_pass=(FileNotFoundError, ValueError))
103 169
104 def IterModels(self): 170 def IterModels(self):
105 future_models = [(name, self.GetModel(name)) for name in self.GetNames()] 171 future_models = [(name, self.GetModel(name)) for name in self.GetNames()]
106 for name, future_model in future_models: 172 for name, future_model in future_models:
107 try: 173 try:
108 model = future_model.Get() 174 model = future_model.Get()
109 except FileNotFoundError: 175 except FileNotFoundError:
110 continue 176 continue
111 if model: 177 if model:
112 yield name, model 178 yield name, model
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698