OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2013 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 | |
5 import logging | |
6 from operator import itemgetter | |
7 import posixpath | |
8 | |
9 from chroot_file_system import ChrootFileSystem | |
10 from content_provider import ContentProvider | |
11 from svn_constants import JSON_PATH | |
12 from third_party.json_schema_compiler.memoize import memoize | |
13 | |
14 | |
15 _CONFIG_PATH = '%s/content_providers.json' % JSON_PATH | |
16 | |
17 | |
18 class ContentProviders(object): | |
19 '''Implements the content_providers.json configuration; see | |
20 chrome/common/extensions/docs/templates/json/content_providers.json for its | |
21 current state and a description of the format. | |
22 | |
23 Returns ContentProvider instances based on how they're configured there. | |
24 ''' | |
25 | |
26 def __init__(self, compiled_fs_factory, host_file_system): | |
27 self._compiled_fs_factory = compiled_fs_factory | |
28 self._host_file_system = host_file_system | |
29 self._cache = compiled_fs_factory.ForJson(host_file_system) | |
30 | |
31 @memoize | |
32 def GetByName(self, name): | |
33 '''Gets the ContentProvider keyed by |name| in content_providers.json, or | |
34 None of there is no such content provider. | |
35 ''' | |
36 config = self._GetConfig().get(name) | |
37 if config is None: | |
38 logging.error('No content provider found with name "%s"' % name) | |
39 return None | |
40 return self._CreateContentProvider(name, config) | |
41 | |
42 @memoize | |
43 def GetByServlet(self, path): | |
44 '''Gets a (content_provider, path_in_content_provider) tuple, where | |
45 content_provider is the ContentProvider with the longest "servlet" property | |
Jeffrey Yasskin
2013/11/04 23:45:21
s/servlet/serveFrom/?
| |
46 that is a subpath of |path|, and path_in_content_provider is the remainder | |
47 of |path|. | |
48 | |
49 For example, if content provider A matches servlet "foo" and content | |
50 provider B matches servlet "foo/bar", GetByServlet("foo/bar/baz") will | |
51 return (B, "baz"). | |
52 | |
53 Returns (None, |path|) if there is no ContentProvider matching |path|. | |
54 ''' | |
55 serve_from_to_config = dict( | |
56 (config['serveFrom'], (name, config)) | |
57 for name, config in self._GetConfig().iteritems()) | |
58 path_parts = path.split('/') | |
59 for i in xrange(len(path_parts), -1, -1): | |
60 name_and_config = serve_from_to_config.get('/'.join(path_parts[:i])) | |
61 if name_and_config is not None: | |
62 return (self._CreateContentProvider(name_and_config[0], | |
63 name_and_config[1]), | |
64 '/'.join(path_parts[i:])) | |
65 return None, path | |
66 | |
67 def _GetConfig(self): | |
68 return self._cache.GetFromFile(_CONFIG_PATH).Get() | |
69 | |
70 def _CreateContentProvider(self, name, config): | |
71 type_ = config.get('type') | |
72 chroot = config.get('chroot', '') | |
73 supports_templates = config.get('supportsTemplates', False) | |
74 supports_zip = config.get('supportsZip', False) | |
75 | |
76 if type_ == 'chromium': | |
77 file_system = self._host_file_system | |
78 else: | |
79 logging.error('Content provider type "%s" not supported', type_) | |
80 return None | |
81 | |
82 return ContentProvider(name, | |
83 self._compiled_fs_factory, | |
84 ChrootFileSystem(file_system, chroot), | |
85 supports_templates=supports_templates, | |
86 supports_zip=supports_zip) | |
87 | |
88 def Cron(self): | |
89 for name, config in self._GetConfig().iteritems(): | |
90 self._CreateContentProvider(name, config).Cron() | |
OLD | NEW |