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 only the samples that use the API |api_name|.
63 samples_list = self._samples_cache.GetFromFileListing(
64 '' if self._platform == 'apps' else EXAMPLES).Get()
65 return [sample for sample in samples_list if any(
66 call['name'].startswith(api_name + '.')
67 for call in sample['api_calls'])]
69 def _GetDataFromManifest(self, path, file_system):
70 manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get()
72 manifest_json = json.loads(json_comment_eater.Nom(manifest))
73 except ValueError as e:
74 logging.error('Error parsing manifest.json for %s: %s' % (path, e))
77 'name': manifest_json.get('name', ''),
78 'description': manifest_json.get('description', None),
79 'icon': manifest_json.get('icons', {}).get('128', None),
80 'default_locale': manifest_json.get('default_locale', None),
83 if not l10n_data['default_locale']:
85 locales_path = path + '/_locales/'
86 locales_dir = file_system.ReadSingle(locales_path).Get()
88 def load_locale_json(path):
89 return (path, json.loads(self._text_cache.GetFromFile(path).Get()))
92 locales_json = [load_locale_json(locales_path + f + 'messages.json')
94 except ValueError as e:
95 logging.error('Error parsing locales files for %s: %s' % (path, e))
97 for path, json_ in locales_json:
98 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_
101 def _MakeSamplesList(self, base_path, files):
103 for filename in sorted(files):
104 if filename.rsplit('/')[-1] != 'manifest.json':
107 # This is a little hacky, but it makes a sample page.
108 sample_path = filename.rsplit('/', 1)[-2]
109 sample_files = [path for path in files
110 if path.startswith(sample_path + '/')]
111 js_files = [path for path in sample_files if path.endswith('.js')]
112 js_contents = [self._text_cache.GetFromFile(
113 posixpath.join(base_path, js_file)).Get()
114 for js_file in js_files]
116 for js in js_contents:
117 api_items.update(_GetAPIItems(js))
120 for item in sorted(api_items):
121 if len(item.split('.')) < 3:
123 if item.endswith('.removeListener') or item.endswith('.hasListener'):
125 if item.endswith('.addListener'):
126 item = item[:-len('.addListener')]
127 if item.startswith('chrome.'):
128 item = item[len('chrome.'):]
129 ref_data = self._reference_resolver.GetLink(item)
130 # TODO(kalman): What about references like chrome.storage.sync.get?
131 # That should link to either chrome.storage.sync or
132 # chrome.storage.StorageArea.get (or probably both).
133 # TODO(kalman): Filter out API-only references? This can happen when
134 # the API namespace is assigned to a variable, but it's very hard to
139 'name': ref_data['text'],
140 'link': ref_data['href']
143 if self._platform == 'apps':
144 url = url_constants.GITHUB_BASE + '/' + sample_path
145 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_path
148 extension_sample_path = posixpath.join('examples', sample_path)
149 url = extension_sample_path
150 icon_base = extension_sample_path
151 download_url = extension_sample_path + '.zip'
153 manifest_data = self._GetDataFromManifest(
154 posixpath.join(base_path, sample_path),
156 if manifest_data['icon'] is None:
157 icon_path = posixpath.join(
158 self._base_path, 'static', _DEFAULT_ICON_PATH)
160 icon_path = '%s/%s' % (icon_base, manifest_data['icon'])
161 manifest_data.update({
163 'download_url': download_url,
165 'files': [f.replace(sample_path + '/', '') for f in sample_files],
166 'api_calls': api_calls
168 samples_list.append(manifest_data)