1 # Copyright 2014 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.
10 from extensions_paths import EXAMPLES
11 from samples_data_source import SamplesDataSource
12 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
16 _DEFAULT_ICON_PATH = 'images/sample-default-icon.png'
19 def _GetAPIItems(js_file):
20 chrome_pattern = r'chrome[\w.]+'
21 # Add API calls that appear normally, like "chrome.runtime.connect".
22 calls = set(re.findall(chrome_pattern, js_file))
23 # Add API calls that have been assigned into variables, like
24 # "var storageArea = chrome.storage.sync; storageArea.get", which should
25 # be expanded like "chrome.storage.sync.get".
26 for match in re.finditer(r'var\s+(\w+)\s*=\s*(%s);' % chrome_pattern,
28 var_name, api_prefix = match.groups()
29 for var_match in re.finditer(r'\b%s\.([\w.]+)\b' % re.escape(var_name),
31 api_suffix, = var_match.groups()
32 calls.add('%s.%s' % (api_prefix, api_suffix))
36 class SamplesModel(object):
38 extension_samples_file_system,
39 app_samples_file_system,
44 self._samples_fs = (extension_samples_file_system if
45 platform == 'extensions' else app_samples_file_system)
46 self._samples_cache = compiled_fs_factory.Create(
48 self._MakeSamplesList,
51 self._text_cache = compiled_fs_factory.ForUnicode(self._samples_fs)
52 self._reference_resolver = reference_resolver
53 self._base_path = base_path
54 self._platform = platform
57 return self._samples_cache
59 def FilterSamples(self, api_name):
60 '''Fetches and filters the list of samples for this platform, returning
61 a Future to the only the samples that use the API |api_name|.
63 def filter_samples(samples_list):
64 return [sample for sample in samples_list
65 if any(call['name'].startswith(api_name + '.')
66 for call in sample['api_calls'])]
68 # TODO(rockot): This cache is probably not working as intended, since
69 # it can still lead to underlying filesystem (e.g. gitiles) access
70 # while processing live requests. Because this can fail, we at least
71 # trap and log exceptions to prevent 500s from being thrown.
72 logging.warning('Unable to get samples listing. Skipping.')
74 platform_for_samples = '' if self._platform == 'apps' else EXAMPLES
75 return (self._samples_cache.GetFromFileListing(platform_for_samples)
76 .Then(filter_samples, error_handler=handle_error))
78 def _GetDataFromManifest(self, path, file_system):
79 manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get()
81 manifest_json = json.loads(json_comment_eater.Nom(manifest))
82 except ValueError as e:
83 logging.error('Error parsing manifest.json for %s: %s' % (path, e))
86 'name': manifest_json.get('name', ''),
87 'description': manifest_json.get('description', None),
88 'icon': manifest_json.get('icons', {}).get('128', None),
89 'default_locale': manifest_json.get('default_locale', None),
92 if not l10n_data['default_locale']:
94 locales_path = path + '/_locales/'
95 locales_dir = file_system.ReadSingle(locales_path).Get()
97 def load_locale_json(path):
98 return (path, json.loads(self._text_cache.GetFromFile(path).Get()))
101 locales_json = [load_locale_json(locales_path + f + 'messages.json')
102 for f in locales_dir]
103 except ValueError as e:
104 logging.error('Error parsing locales files for %s: %s' % (path, e))
106 for path, json_ in locales_json:
107 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_
110 def _MakeSamplesList(self, base_path, files):
112 for filename in sorted(files):
113 if filename.rsplit('/')[-1] != 'manifest.json':
116 # This is a little hacky, but it makes a sample page.
117 sample_path = filename.rsplit('/', 1)[-2]
118 sample_files = [path for path in files
119 if path.startswith(sample_path + '/')]
120 js_files = [path for path in sample_files if path.endswith('.js')]
121 js_contents = [self._text_cache.GetFromFile(
122 posixpath.join(base_path, js_file)).Get()
123 for js_file in js_files]
125 for js in js_contents:
126 api_items.update(_GetAPIItems(js))
129 for item in sorted(api_items):
130 if len(item.split('.')) < 3:
132 if item.endswith('.removeListener') or item.endswith('.hasListener'):
134 if item.endswith('.addListener'):
135 item = item[:-len('.addListener')]
136 if item.startswith('chrome.'):
137 item = item[len('chrome.'):]
138 ref_data = self._reference_resolver.GetLink(item)
139 # TODO(kalman): What about references like chrome.storage.sync.get?
140 # That should link to either chrome.storage.sync or
141 # chrome.storage.StorageArea.get (or probably both).
142 # TODO(kalman): Filter out API-only references? This can happen when
143 # the API namespace is assigned to a variable, but it's very hard to
148 'name': ref_data['text'],
149 'link': ref_data['href']
152 if self._platform == 'apps':
153 url = url_constants.GITHUB_BASE + '/' + sample_path
154 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_path
157 extension_sample_path = posixpath.join('examples', sample_path)
158 url = extension_sample_path
159 icon_base = extension_sample_path
160 download_url = extension_sample_path + '.zip'
162 manifest_data = self._GetDataFromManifest(
163 posixpath.join(base_path, sample_path),
165 if manifest_data['icon'] is None:
166 icon_path = posixpath.join(
167 self._base_path, 'static', _DEFAULT_ICON_PATH)
169 icon_path = '%s/%s' % (icon_base, manifest_data['icon'])
170 manifest_data.update({
172 'download_url': download_url,
174 'files': [f.replace(sample_path + '/', '') for f in sample_files],
175 'api_calls': api_calls
177 samples_list.append(manifest_data)