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