Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/api_models.py |
| diff --git a/chrome/common/extensions/docs/server2/api_models.py b/chrome/common/extensions/docs/server2/api_models.py |
| index 673dc434661fe18a297217db4f0bbf02d84716c2..c0ae0f4a61ca807c6a4888cf46809d6f75ba3ba8 100644 |
| --- a/chrome/common/extensions/docs/server2/api_models.py |
| +++ b/chrome/common/extensions/docs/server2/api_models.py |
| @@ -5,17 +5,36 @@ |
| import posixpath |
| from compiled_file_system import SingleFile, Unicode |
| -from docs_server_utils import StringIdentity |
| +from docs_server_utils import StringIdentity, MarkLast, MarkFirst |
| from extensions_paths import API_PATHS |
| -from features_bundle import HasParentFeature |
| +from features_bundle import HasParentFeature, GetParentFeature |
| from file_system import FileNotFoundError |
| from future import Collect, Future |
| +from operator import itemgetter |
| from platform_util import PlatformToExtensionType |
| from schema_util import ProcessSchema |
| from third_party.json_schema_compiler.json_schema import DeleteNodes |
| from third_party.json_schema_compiler.model import Namespace, UnixName |
| +class ContentScriptAPI(object): |
| + '''Represents an API available to content scripts. |
| + ''' |
| + def __init__(self, name): |
| + self.name = name |
| + self.restrictedTo = [] |
|
not at google - send to devlin
2014/07/10 17:31:27
I'd rather "nothing restricted" be modelled as Non
|
| + |
| + def get(self, key): |
|
not at google - send to devlin
2014/07/10 17:31:27
exposing a "get" method is an implementation detai
ahernandez
2014/07/14 18:11:18
If this doesn't have a get() method, how can handl
not at google - send to devlin
2014/07/15 16:26:38
__dict__ returns a dict all of the properties of t
|
| + if key == 'name': |
| + return self.name |
| + if key == 'restrictedTo': |
| + return self.restrictedTo |
| + raise AssertionError('%s: No such attribute' % key) |
| + |
| + def __eq__(self, o): |
| + return self.name == o.name and self.restrictedTo == o.restrictedTo |
| + |
| + |
| class APIModels(object): |
| '''Tracks APIs and their Models. |
| ''' |
| @@ -24,11 +43,13 @@ class APIModels(object): |
| features_bundle, |
| compiled_fs_factory, |
| file_system, |
| + object_store_creator, |
| platform): |
| self._features_bundle = features_bundle |
| self._platform = PlatformToExtensionType(platform) |
| self._model_cache = compiled_fs_factory.Create( |
| file_system, self._CreateAPIModel, APIModels, category=self._platform) |
| + self._object_store = object_store_creator.Create(APIModels) |
| @SingleFile |
| @Unicode |
| @@ -97,6 +118,59 @@ class APIModels(object): |
| futures[0].Get() |
| return Future(callback=resolve) |
| + def GetContentScriptAPIs(self): |
| + '''Creates a dict of APIs and nodes supported by content scripts in |
| + this format: |
| + |
| + { |
| + 'extension': <ContentScriptAPI object>, |
| + ... |
| + } |
| + |
| + where a ContentScriptAPI object's restrictedTo attribute items look like: |
| + |
| + { |
| + 'parent': 'extension', |
| + 'node': 'sendMessage' |
| + } |
| + ''' |
| + content_script_apis_future = self._object_store.Get('content_script_apis') |
| + api_features_future = self._features_bundle.GetAPIFeatures() |
| + def resolve(): |
| + content_script_apis = content_script_apis_future.Get() |
| + if content_script_apis is not None: |
| + return content_script_apis |
| + |
| + api_features = api_features_future.Get() |
| + content_script_apis = {} |
| + for feature in api_features.itervalues(): |
| + if 'content_script' not in feature.get('contexts', ()): |
| + continue |
| + parent = GetParentFeature(feature['name'], feature, api_features) |
| + # If it has no parent, then the API is fully supported. |
| + if parent is None: |
| + content_script_apis[feature['name']] = ContentScriptAPI( |
| + feature['name']) |
| + else: |
| + # Creates a dict for the individual node. |
| + node = {'parent': parent, 'node': feature['name'][len(parent) + 1:]} |
| + # Initializes the supportedNodes list if it doesn't exist yet. |
| + if parent not in content_script_apis: |
| + content_script_apis[parent] = ContentScriptAPI(parent) |
| + content_script_apis[parent].restrictedTo.append(node) |
| + |
| + # Sort all of the supportedNodes. |
|
not at google - send to devlin
2014/07/10 17:31:27
see comment at top of file.
|
| + for content_script_api in content_script_apis.itervalues(): |
| + restricted_nodes = content_script_api.restrictedTo |
| + if restricted_nodes: |
| + restricted_nodes.sort(key=itemgetter('node')) |
| + MarkFirst(restricted_nodes) |
| + MarkLast(restricted_nodes) |
| + |
| + self._object_store.Set('content_script_apis', content_script_apis) |
| + return content_script_apis |
| + return Future(callback=resolve) |
| + |
| def Cron(self): |
| futures = [self.GetModel(name) for name in self.GetNames()] |
| return Collect(futures, except_pass=(FileNotFoundError, ValueError)) |