1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 from data_source import DataSource
6 from docs_server_utils import StringIdentity
7 from environment import IsPreviewServer
8 from file_system import FileNotFoundError
9 from future import Future, All
10 from jsc_view import JSCView, GetEventByNameFromEvents
11 from platform_util import GetPlatforms
12 from samples_data_source import CreateSamplesView
15 class APIDataSource(DataSource):
16 '''This class fetches and loads JSON APIs from the FileSystem passed in with
17 |compiled_fs_factory|, so the APIs can be plugged into templates.
19 def __init__(self, server_instance, request):
20 file_system = server_instance.host_file_system_provider.GetTrunk()
21 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system)
22 self._template_cache = server_instance.compiled_fs_factory.ForTemplates(
24 self._platform_bundle = server_instance.platform_bundle
25 self._view_cache = server_instance.object_store_creator.Create(
27 # Update the models when any of templates, APIs, or Features change.
28 category=StringIdentity(self._json_cache.GetIdentity(),
29 self._template_cache.GetIdentity(),
30 self._platform_bundle.GetIdentity()))
32 # This caches the result of _LoadEventByName.
33 self._event_byname_futures = {}
34 self._request = request
36 def _LoadEventByName(self, platform):
37 '''All events have some members in common. We source their description
38 from Event in events.json.
40 if platform not in self._event_byname_futures:
41 future = self._GetSchemaView(platform, 'events')
42 self._event_byname_futures[platform] = Future(
43 callback=lambda: GetEventByNameFromEvents(future.Get()))
44 return self._event_byname_futures[platform]
46 def _GetSchemaView(self, platform, api_name):
47 object_store_key = '/'.join((platform, api_name))
48 api_models = self._platform_bundle.GetAPIModels(platform)
49 jsc_view_future = self._view_cache.Get(object_store_key)
50 model_future = api_models.GetModel(api_name)
51 content_script_apis_future = api_models.GetContentScriptAPIs()
53 jsc_view = jsc_view_future.Get()
56 content_script_apis_future.Get(),
58 self._platform_bundle.GetAvailabilityFinder(platform),
61 self._platform_bundle.GetFeaturesBundle(platform),
62 self._LoadEventByName(platform),
64 self._view_cache.Set(object_store_key, jsc_view)
66 return Future(callback=resolve)
68 def _GetImpl(self, platform, api_name):
69 jsc_view_future = self._GetSchemaView(platform, api_name)
71 jsc_view = jsc_view_future.Get()
72 # Parsing samples on the preview server takes seconds and doesn't add
73 # anything. Don't do it.
74 if not IsPreviewServer():
75 samples_model = self._platform_bundle.GetSamplesModel(platform)
76 # Creates an object that lazily gets samples.
77 jsc_view['samples'] = type('getter', (object,), {
78 'get': lambda _, platform: CreateSamplesView(
79 samples_model.FilterSamples(jsc_view['name']), self._request)
82 return Future(callback=resolve)
84 def get(self, platform):
85 '''Return a getter object so that templates can perform lookups such
86 as apis.extensions.runtime.
89 getter.get = lambda api_name: self._GetImpl(platform, api_name).Get()
94 for platform in GetPlatforms():
95 futures += [self._GetImpl(platform, name)
96 for name in self._platform_bundle.GetAPIModels(platform).GetNames()]
97 return All(futures, except_pass=FileNotFoundError)