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