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.
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.
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(
26 self
._platform
_bundle
= server_instance
.platform_bundle
27 self
._view
_cache
= server_instance
.object_store_creator
.Create(
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.
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.
57 samples_future
= Future(value
=[])
59 samples_future
= (self
._platform
_bundle
.GetSamplesModel(platform
)
60 .FilterSamples(api_name
))
63 jsc_view
= jsc_view_future
.Get()
65 jsc_view
= CreateJSCView(
66 content_script_apis_future
.Get(),
68 self
._platform
_bundle
.GetAvailabilityFinder(platform
),
71 self
._platform
_bundle
.GetFeaturesBundle(platform
),
72 self
._LoadEventByName
(platform
),
76 self
._view
_cache
.Set(object_store_key
, 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.
85 getter
.get
= lambda api_name
: self
._GetSchemaView
(platform
, api_name
).Get()
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
)
96 except_pass
=FileNotFoundError
)
98 return All([get_platform_schemas(platform
) for platform
in GetPlatforms()])