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

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
9 from extensions_paths import API_PATHS 8 from extensions_paths import API_PATHS
10 from features_bundle import HasParentFeature 9 from features_bundle import HasParentFeature, GetParentFeature
11 from file_system import FileNotFoundError 10 from file_system import FileNotFoundError
12 from future import Collect, Future 11 from future import Collect, Future
12 from operator import itemgetter
13 from platform_util import PlatformToExtensionType 13 from platform_util import PlatformToExtensionType
14 from schema_util import ProcessSchema 14 from schema_util import ProcessSchema
15 from third_party.json_schema_compiler.json_schema import DeleteNodes 15 from third_party.json_schema_compiler.json_schema import DeleteNodes
16 from third_party.json_schema_compiler.model import Namespace, UnixName 16 from third_party.json_schema_compiler.model import Namespace, UnixName
17 17
18 18
19 class ContentScriptAPI(object):
20 '''Represents an API available to content scripts.
21
22 |name| is the name of the API or API node this object represents.
23 |restrictedTo| is a list of dictionaries representing the nodes
24 of this API that are available to content scripts, or None if the
25 entire API is available to content scripts.
26 '''
27 def __init__(self, name):
28 self.name = name
29 self.restrictedTo = None
30
31 def __eq__(self, o):
32 return self.name == o.name and self.restrictedTo == o.restrictedTo
33
34 def __ne__(self, o):
35 return not (self == o)
36
37 def __repr__(self):
38 return '<ContentScriptAPI name=%s, restrictedTo=%s>' % (name, restrictedTo)
39
40 def __str__(self):
41 return repr(self)
42
43
19 class APIModels(object): 44 class APIModels(object):
20 '''Tracks APIs and their Models. 45 '''Tracks APIs and their Models.
21 ''' 46 '''
22 47
23 def __init__(self, 48 def __init__(self,
24 features_bundle, 49 features_bundle,
25 compiled_fs_factory, 50 compiled_fs_factory,
26 file_system, 51 file_system,
52 object_store_creator,
27 platform): 53 platform):
28 self._features_bundle = features_bundle 54 self._features_bundle = features_bundle
29 self._platform = PlatformToExtensionType(platform) 55 self._platform = PlatformToExtensionType(platform)
30 self._model_cache = compiled_fs_factory.Create( 56 self._model_cache = compiled_fs_factory.Create(
31 file_system, self._CreateAPIModel, APIModels, category=self._platform) 57 file_system, self._CreateAPIModel, APIModels, category=self._platform)
58 self._object_store = object_store_creator.Create(APIModels)
32 59
33 @SingleFile 60 @SingleFile
34 @Unicode 61 @Unicode
35 def _CreateAPIModel(self, path, data): 62 def _CreateAPIModel(self, path, data):
36 def does_not_include_platform(node): 63 def does_not_include_platform(node):
37 return ('extension_types' in node and 64 return ('extension_types' in node and
38 node['extension_types'] != 'all' and 65 node['extension_types'] != 'all' and
39 self._platform not in node['extension_types']) 66 self._platform not in node['extension_types'])
40 67
41 schema = ProcessSchema(path, data, inline=True)[0] 68 schema = ProcessSchema(path, data, inline=True)[0]
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 def resolve(): 117 def resolve():
91 for future in futures: 118 for future in futures:
92 try: 119 try:
93 return future.Get() 120 return future.Get()
94 # Either the file wasn't found or there was no schema for the file 121 # Either the file wasn't found or there was no schema for the file
95 except (FileNotFoundError, ValueError): pass 122 except (FileNotFoundError, ValueError): pass
96 # Propagate the first error if neither were found. 123 # Propagate the first error if neither were found.
97 futures[0].Get() 124 futures[0].Get()
98 return Future(callback=resolve) 125 return Future(callback=resolve)
99 126
127 def GetContentScriptAPIs(self):
128 '''Creates a dict of APIs and nodes supported by content scripts in
129 this format:
130
131 {
132 'extension': '<ContentScriptAPI name='extension',
133 restrictedTo=[{'node': 'onRequest'}]>',
134 ...
135 }
136 '''
137 content_script_apis_future = self._object_store.Get('content_script_apis')
138 api_features_future = self._features_bundle.GetAPIFeatures()
139 def resolve():
140 content_script_apis = content_script_apis_future.Get()
141 if content_script_apis is not None:
142 return content_script_apis
143
144 api_features = api_features_future.Get()
145 content_script_apis = {}
146 for feature in api_features.itervalues():
not at google - send to devlin 2014/07/21 17:51:20 maybe you should do "name, feature in api_features
147 if 'content_script' not in feature.get('contexts', ()):
148 continue
149 parent = GetParentFeature(feature['name'], feature, api_features)
not at google - send to devlin 2014/07/21 17:51:19 can you rename GetParentFeature to GetParentFeatur
150 if parent is None:
151 content_script_apis[feature['name']] = ContentScriptAPI(
152 feature['name'])
153 else:
154 # Creates a dict for the individual node.
155 node = {'node': feature['name'][len(parent) + 1:]}
156 if parent not in content_script_apis:
157 content_script_apis[parent] = ContentScriptAPI(parent)
158 if content_script_apis[parent].restrictedTo:
159 content_script_apis[parent].restrictedTo.append(node)
160 else:
161 content_script_apis[parent].restrictedTo = [node]
162
163 self._object_store.Set('content_script_apis', content_script_apis)
164 return content_script_apis
165 return Future(callback=resolve)
166
100 def Cron(self): 167 def Cron(self):
101 futures = [self.GetModel(name) for name in self.GetNames()] 168 futures = [self.GetModel(name) for name in self.GetNames()]
102 return Collect(futures, except_pass=(FileNotFoundError, ValueError)) 169 return Collect(futures, except_pass=(FileNotFoundError, ValueError))
103 170
104 def IterModels(self): 171 def IterModels(self):
105 future_models = [(name, self.GetModel(name)) for name in self.GetNames()] 172 future_models = [(name, self.GetModel(name)) for name in self.GetNames()]
106 for name, future_model in future_models: 173 for name, future_model in future_models:
107 try: 174 try:
108 model = future_model.Get() 175 model = future_model.Get()
109 except FileNotFoundError: 176 except FileNotFoundError:
110 continue 177 continue
111 if model: 178 if model:
112 yield name, model 179 yield name, model
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698