Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / server_instance.py
blob7dd7db07d4d2bb7e37e7dd68114d0bb7473d9e54
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 api_categorizer import APICategorizer
6 from api_data_source import APIDataSource
7 from api_list_data_source import APIListDataSource
8 from api_models import APIModels
9 from availability_finder import AvailabilityFinder
10 from compiled_file_system import CompiledFileSystem
11 from content_providers import ContentProviders
12 from document_renderer import DocumentRenderer
13 from empty_dir_file_system import EmptyDirFileSystem
14 from environment import IsDevServer
15 from features_bundle import FeaturesBundle
16 from github_file_system_provider import GithubFileSystemProvider
17 from host_file_system_provider import HostFileSystemProvider
18 from host_file_system_iterator import HostFileSystemIterator
19 from object_store_creator import ObjectStoreCreator
20 from path_canonicalizer import PathCanonicalizer
21 from reference_resolver import ReferenceResolver
22 from samples_data_source import SamplesDataSource
23 from table_of_contents_renderer import TableOfContentsRenderer
24 from template_renderer import TemplateRenderer
25 from test_branch_utility import TestBranchUtility
26 from test_object_store import TestObjectStore
29 class ServerInstance(object):
31 def __init__(self,
32 object_store_creator,
33 compiled_fs_factory,
34 branch_utility,
35 host_file_system_provider,
36 github_file_system_provider,
37 base_path='/'):
38 '''
39 |object_store_creator|
40 The ObjectStoreCreator used to create almost all caches.
41 |compiled_fs_factory|
42 Factory used to create CompiledFileSystems, a higher-level cache type
43 than ObjectStores. This can usually be derived from just
44 |object_store_creator| but under special circumstances a different
45 implementation needs to be passed in.
46 |branch_utility|
47 Has knowledge of Chrome branches, channels, and versions.
48 |host_file_system_provider|
49 Creates FileSystem instances which host the server at alternative
50 revisions.
51 |github_file_system_provider|
52 Creates FileSystem instances backed by GitHub.
53 |base_path|
54 The path which all HTML is generated relative to. Usually this is /
55 but some servlets need to override this.
56 '''
57 self.object_store_creator = object_store_creator
59 self.compiled_fs_factory = compiled_fs_factory
61 self.host_file_system_provider = host_file_system_provider
62 host_fs_at_trunk = host_file_system_provider.GetTrunk()
64 self.github_file_system_provider = github_file_system_provider
66 assert base_path.startswith('/') and base_path.endswith('/')
67 self.base_path = base_path
69 self.host_file_system_iterator = HostFileSystemIterator(
70 host_file_system_provider,
71 branch_utility)
73 self.features_bundle = FeaturesBundle(
74 host_fs_at_trunk,
75 self.compiled_fs_factory,
76 self.object_store_creator)
78 self.api_models = APIModels(
79 self.features_bundle,
80 self.compiled_fs_factory,
81 host_fs_at_trunk)
83 self.availability_finder = AvailabilityFinder(
84 branch_utility,
85 compiled_fs_factory,
86 self.host_file_system_iterator,
87 host_fs_at_trunk,
88 object_store_creator)
90 self.api_categorizer = APICategorizer(
91 host_fs_at_trunk,
92 compiled_fs_factory)
94 self.api_data_source_factory = APIDataSource.Factory(
95 self.compiled_fs_factory,
96 host_fs_at_trunk,
97 self.availability_finder,
98 self.api_models,
99 self.object_store_creator)
101 self.api_list_data_source_factory = APIListDataSource.Factory(
102 self.compiled_fs_factory,
103 host_fs_at_trunk,
104 self.features_bundle,
105 self.object_store_creator,
106 self.api_models,
107 self.availability_finder,
108 self.api_categorizer)
110 self.ref_resolver_factory = ReferenceResolver.Factory(
111 self.api_data_source_factory,
112 self.api_models,
113 object_store_creator)
115 self.api_data_source_factory.SetReferenceResolverFactory(
116 self.ref_resolver_factory)
118 # Note: samples are super slow in the dev server because it doesn't support
119 # async fetch, so disable them.
120 if IsDevServer():
121 extension_samples_fs = EmptyDirFileSystem()
122 app_samples_fs = EmptyDirFileSystem()
123 else:
124 extension_samples_fs = host_fs_at_trunk
125 app_samples_fs = github_file_system_provider.Create(
126 'GoogleChrome', 'chrome-app-samples')
127 self.samples_data_source_factory = SamplesDataSource.Factory(
128 extension_samples_fs,
129 app_samples_fs,
130 CompiledFileSystem.Factory(object_store_creator),
131 self.ref_resolver_factory,
132 base_path)
134 self.api_data_source_factory.SetSamplesDataSourceFactory(
135 self.samples_data_source_factory)
137 self.path_canonicalizer = PathCanonicalizer(
138 self.compiled_fs_factory,
139 host_fs_at_trunk)
141 self.content_providers = ContentProviders(
142 self.compiled_fs_factory,
143 host_fs_at_trunk,
144 self.github_file_system_provider)
146 # TODO(kalman): Move all the remaining DataSources into DataSourceRegistry,
147 # then factor out the DataSource creation into a factory method, so that
148 # the entire ServerInstance doesn't need to be passed in here.
149 self.template_renderer = TemplateRenderer(self)
151 # TODO(kalman): It may be better for |document_renderer| to construct a
152 # TemplateDataSource itself rather than depending on template_renderer, but
153 # for that the above todo should be addressed.
154 self.document_renderer = DocumentRenderer(TableOfContentsRenderer(
155 host_fs_at_trunk,
156 compiled_fs_factory,
157 self.template_renderer))
159 @staticmethod
160 def ForTest(file_system=None, file_system_provider=None, base_path='/'):
161 object_store_creator = ObjectStoreCreator.ForTest()
162 if file_system is None and file_system_provider is None:
163 raise ValueError('Either |file_system| or |file_system_provider| '
164 'must be specified')
165 if file_system and file_system_provider:
166 raise ValueError('Only one of |file_system| and |file_system_provider| '
167 'can be specified')
168 if file_system_provider is None:
169 file_system_provider = HostFileSystemProvider.ForTest(
170 file_system,
171 object_store_creator)
172 return ServerInstance(object_store_creator,
173 CompiledFileSystem.Factory(object_store_creator),
174 TestBranchUtility.CreateWithCannedData(),
175 file_system_provider,
176 GithubFileSystemProvider.ForEmpty(),
177 base_path=base_path)
179 @staticmethod
180 def ForLocal():
181 object_store_creator = ObjectStoreCreator(start_empty=False,
182 store_type=TestObjectStore)
183 host_file_system_provider = HostFileSystemProvider.ForLocal(
184 object_store_creator)
185 return ServerInstance(
186 object_store_creator,
187 CompiledFileSystem.Factory(object_store_creator),
188 TestBranchUtility.CreateWithCannedData(),
189 host_file_system_provider,
190 GithubFileSystemProvider.ForEmpty())