Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / api_data_source.py
blob67004e9ca7c5af0a849d0c9a8fb2d3edfd624371
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 import logging
7 from data_source import DataSource
8 from docs_server_utils import StringIdentity
9 from environment import IsPreviewServer
10 from file_system import FileNotFoundError
11 from future import All, Future
12 from jsc_view import CreateJSCView, GetEventByNameFromEvents
13 from platform_util import GetPlatforms
14 from third_party.json_schema_compiler.model import UnixName
17 class APIDataSource(DataSource):
18 '''This class fetches and loads JSON APIs from the FileSystem passed in with
19 |compiled_fs_factory|, so the APIs can be plugged into templates.
20 '''
21 def __init__(self, server_instance, request):
22 file_system = server_instance.host_file_system_provider.GetMaster()
23 self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system)
24 self._template_cache = server_instance.compiled_fs_factory.ForTemplates(
25 file_system)
26 self._platform_bundle = server_instance.platform_bundle
27 self._view_cache = server_instance.object_store_creator.Create(
28 APIDataSource,
29 # Update the models when any of templates, APIs, or Features change.
30 category=StringIdentity(self._json_cache.GetIdentity(),
31 self._template_cache.GetIdentity(),
32 self._platform_bundle.GetIdentity()))
34 # This caches the result of _LoadEventByName.
35 self._event_byname_futures = {}
36 self._request = request
38 def _LoadEventByName(self, platform):
39 '''All events have some members in common. We source their description
40 from Event in events.json.
41 '''
42 if platform not in self._event_byname_futures:
43 future = self._GetSchemaView(platform, 'events')
44 self._event_byname_futures[platform] = Future(
45 callback=lambda: GetEventByNameFromEvents(future.Get()))
46 return self._event_byname_futures[platform]
48 def _GetSchemaView(self, platform, api_name):
49 object_store_key = '/'.join((platform, api_name))
50 api_models = self._platform_bundle.GetAPIModels(platform)
51 jsc_view_future = self._view_cache.Get(object_store_key)
52 model_future = api_models.GetModel(api_name)
53 content_script_apis_future = api_models.GetContentScriptAPIs()
55 # Parsing samples on the preview server takes forever, so disable it.
56 if IsPreviewServer():
57 samples_future = Future(value=[])
58 else:
59 samples_future = (self._platform_bundle.GetSamplesModel(platform)
60 .FilterSamples(api_name))
62 def resolve():
63 jsc_view = jsc_view_future.Get()
64 if jsc_view is None:
65 jsc_view = CreateJSCView(
66 content_script_apis_future.Get(),
67 model_future.Get(),
68 self._platform_bundle.GetAvailabilityFinder(platform),
69 self._json_cache,
70 self._template_cache,
71 self._platform_bundle.GetFeaturesBundle(platform),
72 self._LoadEventByName(platform),
73 platform,
74 samples_future.Get(),
75 self._request)
76 self._view_cache.Set(object_store_key, jsc_view)
77 return jsc_view
78 return Future(callback=resolve)
80 def get(self, platform):
81 '''Return a getter object so that templates can perform lookups such
82 as apis.extensions.runtime.
83 '''
84 getter = lambda: 0
85 getter.get = lambda api_name: self._GetSchemaView(platform, api_name).Get()
86 return getter
88 def Refresh(self):
89 def get_api_schema(platform, api):
90 return self._GetSchemaView(platform, api)
92 def get_platform_schemas(platform):
93 return All([get_api_schema(platform, api)
94 for api in self._platform_bundle.GetAPIModels(platform)
95 .GetNames()],
96 except_pass=FileNotFoundError)
98 return All([get_platform_schemas(platform) for platform in GetPlatforms()])