api_list_data_source.py revision effb81e5f8246d0db0270817048dc992db66e9fb
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.
4
5from data_source import DataSource
6from future import Future
7from operator import itemgetter
8
9import docs_server_utils as utils
10
11class APIListDataSource(DataSource):
12  """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs
13  for extensions and apps that are used in the api_index.html,
14  experimental.html, and private_apis.html pages.
15
16  An API is considered listable if it is listed in _api_features.json,
17  it has a corresponding HTML file in the public template path, and one of
18  the following conditions is met:
19    - It has no "dependencies" or "extension_types" properties in _api_features
20    - It has an "extension_types" property in _api_features with either/both
21      "extension"/"platform_app" values present.
22    - It has a dependency in _{api,manifest,permission}_features with an
23      "extension_types" property where either/both "extension"/"platform_app"
24      values are present.
25  """
26  def __init__(self, server_instance, _):
27    self._features_bundle = server_instance.features_bundle
28    self._object_store = server_instance.object_store_creator.Create(
29        APIListDataSource)
30    self._api_models = server_instance.api_models
31    self._api_categorizer = server_instance.api_categorizer
32    self._availability_finder = server_instance.availability_finder
33
34  def _GenerateAPIDict(self):
35    def get_channel_info(api_name):
36      return self._availability_finder.GetApiAvailability(api_name)
37
38    def get_api_platform(api_name):
39      feature = self._features_bundle.GetAPIFeatures().Get()[api_name]
40      return feature['platforms']
41
42    def make_dict_for_platform(platform):
43      platform_dict = {
44        'chrome': {'stable': [], 'beta': [], 'dev': [], 'trunk': []},
45      }
46      private_apis = []
47      experimental_apis = []
48      all_apis = []
49      for api_name, api_model in self._api_models.IterModels():
50        if not self._api_categorizer.IsDocumented(platform, api_name):
51          continue
52        api = {
53          'name': api_name,
54          'description': api_model.description,
55          'platforms': get_api_platform(api_name),
56        }
57        category = self._api_categorizer.GetCategory(platform, api_name)
58        if category == 'chrome':
59          channel_info = get_channel_info(api_name)
60          channel = channel_info.channel
61          if channel == 'stable':
62            version = channel_info.version
63            api['version'] = version
64          platform_dict[category][channel].append(api)
65          all_apis.append(api)
66        elif category == 'experimental':
67          experimental_apis.append(api)
68          all_apis.append(api)
69        elif category == 'private':
70          private_apis.append(api)
71
72      for channel, apis_by_channel in platform_dict['chrome'].iteritems():
73        apis_by_channel.sort(key=itemgetter('name'))
74        utils.MarkLast(apis_by_channel)
75        platform_dict['chrome'][channel] = apis_by_channel
76
77      for key, apis in (('all', all_apis),
78                        ('private', private_apis),
79                        ('experimental', experimental_apis)):
80        apis.sort(key=itemgetter('name'))
81        utils.MarkLast(apis)
82        platform_dict[key] = apis
83
84      return platform_dict
85    return {
86      'apps': make_dict_for_platform('apps'),
87      'extensions': make_dict_for_platform('extensions'),
88    }
89
90  def _GetCachedAPIData(self):
91    data_future = self._object_store.Get('api_data')
92    def resolve():
93      data = data_future.Get()
94      if data is None:
95        data = self._GenerateAPIDict()
96        self._object_store.Set('api_data', data)
97      return data
98    return Future(callback=resolve)
99
100  def get(self, key):
101    return self._GetCachedAPIData().Get().get(key)
102
103  def Cron(self):
104    return self._GetCachedAPIData()
105