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.
4
5import json
6import logging
7import posixpath
8import re
9
10from extensions_paths import EXAMPLES
11from samples_data_source import SamplesDataSource
12import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
13import url_constants
14
15
16_DEFAULT_ICON_PATH = 'images/sample-default-icon.png'
17
18
19def _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,
27                           js_file):
28    var_name, api_prefix = match.groups()
29    for var_match in re.finditer(r'\b%s\.([\w.]+)\b' % re.escape(var_name),
30                                 js_file):
31      api_suffix, = var_match.groups()
32      calls.add('%s.%s' % (api_prefix, api_suffix))
33  return calls
34
35
36class SamplesModel(object):
37  def __init__(self,
38               extension_samples_file_system,
39               app_samples_file_system,
40               compiled_fs_factory,
41               reference_resolver,
42               base_path,
43               platform):
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(
47        self._samples_fs,
48        self._MakeSamplesList,
49        SamplesDataSource,
50        category=platform)
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
55
56  def GetCache(self):
57    return self._samples_cache
58
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|.
62    '''
63    try:
64      # TODO(rockot): This cache is probably not working as intended, since
65      # it can still lead to underlying filesystem (e.g. gitiles) access
66      # while processing live requests. Because this can fail, we at least
67      # trap and log exceptions to prevent 500s from being thrown.
68      samples_list = self._samples_cache.GetFromFileListing(
69          '' if self._platform == 'apps' else EXAMPLES).Get()
70    except Exception as e:
71      logging.warning('Unable to get samples listing. Skipping.')
72      samples_list = []
73
74    return [sample for sample in samples_list if any(
75        call['name'].startswith(api_name + '.')
76        for call in sample['api_calls'])]
77
78  def _GetDataFromManifest(self, path, file_system):
79    manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get()
80    try:
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))
84      return None
85    l10n_data = {
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),
90      'locales': {}
91    }
92    if not l10n_data['default_locale']:
93      return l10n_data
94    locales_path = path + '/_locales/'
95    locales_dir = file_system.ReadSingle(locales_path).Get()
96    if locales_dir:
97      def load_locale_json(path):
98        return (path, json.loads(self._text_cache.GetFromFile(path).Get()))
99
100      try:
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))
105      else:
106        for path, json_ in locales_json:
107          l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_
108    return l10n_data
109
110  def _MakeSamplesList(self, base_path, files):
111    samples_list = []
112    for filename in sorted(files):
113      if filename.rsplit('/')[-1] != 'manifest.json':
114        continue
115
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]
124      api_items = set()
125      for js in js_contents:
126        api_items.update(_GetAPIItems(js))
127
128      api_calls = []
129      for item in sorted(api_items):
130        if len(item.split('.')) < 3:
131          continue
132        if item.endswith('.removeListener') or item.endswith('.hasListener'):
133          continue
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
144        # to disambiguate.
145        if ref_data is None:
146          continue
147        api_calls.append({
148          'name': ref_data['text'],
149          'link': ref_data['href']
150        })
151
152      if self._platform == 'apps':
153        url = url_constants.GITHUB_BASE + '/' + sample_path
154        icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_path
155        download_url = url
156      else:
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'
161
162      manifest_data = self._GetDataFromManifest(
163          posixpath.join(base_path, sample_path),
164          self._samples_fs)
165      if manifest_data['icon'] is None:
166        icon_path = posixpath.join(
167            self._base_path, 'static', _DEFAULT_ICON_PATH)
168      else:
169        icon_path = '%s/%s' % (icon_base, manifest_data['icon'])
170      manifest_data.update({
171        'icon': icon_path,
172        'download_url': download_url,
173        'url': url,
174        'files': [f.replace(sample_path + '/', '') for f in sample_files],
175        'api_calls': api_calls
176      })
177      samples_list.append(manifest_data)
178
179    return samples_list
180