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

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: Rebase/Address comments 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 def __init__(self, name):
23 self.name = name
24 self.restrictedTo = None
not at google - send to devlin 2014/07/16 00:32:20 document these
25
26 def __eq__(self, o):
27 return self.name == o.name and self.restrictedTo == o.restrictedTo
28
29 def __ne__(self, o):
30 return not (self == o)
31
32 def __repr__(self):
33 return '<ContentScriptAPI name=%s, restrictedTo=%s>' % (name, restrictedTo)
34
35 def __str__(self):
36 return repr(self)
37
38
19 class APIModels(object): 39 class APIModels(object):
20 '''Tracks APIs and their Models. 40 '''Tracks APIs and their Models.
21 ''' 41 '''
22 42
23 def __init__(self, 43 def __init__(self,
24 features_bundle, 44 features_bundle,
25 compiled_fs_factory, 45 compiled_fs_factory,
26 file_system, 46 file_system,
47 object_store_creator,
27 platform): 48 platform):
28 self._features_bundle = features_bundle 49 self._features_bundle = features_bundle
29 self._platform = PlatformToExtensionType(platform) 50 self._platform = PlatformToExtensionType(platform)
30 self._model_cache = compiled_fs_factory.Create( 51 self._model_cache = compiled_fs_factory.Create(
31 file_system, self._CreateAPIModel, APIModels, category=self._platform) 52 file_system, self._CreateAPIModel, APIModels, category=self._platform)
53 self._object_store = object_store_creator.Create(APIModels)
32 54
33 @SingleFile 55 @SingleFile
34 @Unicode 56 @Unicode
35 def _CreateAPIModel(self, path, data): 57 def _CreateAPIModel(self, path, data):
36 def does_not_include_platform(node): 58 def does_not_include_platform(node):
37 return ('extension_types' in node and 59 return ('extension_types' in node and
38 node['extension_types'] != 'all' and 60 node['extension_types'] != 'all' and
39 self._platform not in node['extension_types']) 61 self._platform not in node['extension_types'])
40 62
41 schema = ProcessSchema(path, data, inline=True)[0] 63 schema = ProcessSchema(path, data, inline=True)[0]
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 def resolve(): 112 def resolve():
91 for future in futures: 113 for future in futures:
92 try: 114 try:
93 return future.Get() 115 return future.Get()
94 # Either the file wasn't found or there was no schema for the file 116 # Either the file wasn't found or there was no schema for the file
95 except (FileNotFoundError, ValueError): pass 117 except (FileNotFoundError, ValueError): pass
96 # Propagate the first error if neither were found. 118 # Propagate the first error if neither were found.
97 futures[0].Get() 119 futures[0].Get()
98 return Future(callback=resolve) 120 return Future(callback=resolve)
99 121
122 def GetContentScriptAPIs(self):
123 '''Creates a dict of APIs and nodes supported by content scripts in
124 this format:
125
126 {
127 'extension': '<ContentScriptAPI name='extension', restrictedTo=None>',
128 ...
129 }
130
131 where a ContentScriptAPI object's restrictedTo attribute items look like:
132
133 {
134 'parent': 'extension',
135 'node': 'sendMessage'
136 }
137 '''
138 content_script_apis_future = self._object_store.Get('content_script_apis')
139 api_features_future = self._features_bundle.GetAPIFeatures()
140 def resolve():
141 content_script_apis = content_script_apis_future.Get()
142 if content_script_apis is not None:
143 return content_script_apis
144
145 api_features = api_features_future.Get()
146 content_script_apis = {}
147 for feature in api_features.itervalues():
148 if 'content_script' not in feature.get('contexts', ()):
149 continue
150 parent = GetParentFeature(feature['name'], feature, api_features)
151 # If it has no parent, then the API is fully supported.
152 if parent is None:
153 content_script_apis[feature['name']] = ContentScriptAPI(
154 feature['name'])
155 else:
156 # Creates a dict for the individual node.
157 node = {'parent': parent, 'node': feature['name'][len(parent) + 1:]}
not at google - send to devlin 2014/07/16 00:32:20 this 'parent' property is still a concern of the t
158 if parent not in content_script_apis:
159 content_script_apis[parent] = ContentScriptAPI(parent)
160 if content_script_apis[parent].restrictedTo:
161 content_script_apis[parent].restrictedTo.append(node)
162 else:
163 content_script_apis[parent].restrictedTo = [node]
164
165 self._object_store.Set('content_script_apis', content_script_apis)
166 return content_script_apis
167 return Future(callback=resolve)
168
100 def Cron(self): 169 def Cron(self):
101 futures = [self.GetModel(name) for name in self.GetNames()] 170 futures = [self.GetModel(name) for name in self.GetNames()]
102 return Collect(futures, except_pass=(FileNotFoundError, ValueError)) 171 return Collect(futures, except_pass=(FileNotFoundError, ValueError))
103 172
104 def IterModels(self): 173 def IterModels(self):
105 future_models = [(name, self.GetModel(name)) for name in self.GetNames()] 174 future_models = [(name, self.GetModel(name)) for name in self.GetNames()]
106 for name, future_model in future_models: 175 for name, future_model in future_models:
107 try: 176 try:
108 model = future_model.Get() 177 model = future_model.Get()
109 except FileNotFoundError: 178 except FileNotFoundError:
110 continue 179 continue
111 if model: 180 if model:
112 yield name, model 181 yield name, model
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698