Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import hashlib | 5 import hashlib |
| 6 import json | 6 import json |
| 7 import logging | 7 import logging |
| 8 import posixpath | 8 import posixpath |
| 9 import re | 9 import re |
| 10 import traceback | 10 import traceback |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 def Create(self, request): | 49 def Create(self, request): |
| 50 '''Returns a new SamplesDataSource bound to |request|. | 50 '''Returns a new SamplesDataSource bound to |request|. |
| 51 ''' | 51 ''' |
| 52 return SamplesDataSource(self._extensions_cache, | 52 return SamplesDataSource(self._extensions_cache, |
| 53 self._apps_cache, | 53 self._apps_cache, |
| 54 self._extension_samples_path, | 54 self._extension_samples_path, |
| 55 self._base_path, | 55 self._base_path, |
| 56 request) | 56 request) |
| 57 | 57 |
| 58 def _GetAPIItems(self, js_file): | 58 def _GetAPIItems(self, js_file): |
| 59 chrome_regex = '(chrome\.[a-zA-Z0-9\.]+)' | 59 chrome_pattern = r'chrome[\w.]+' |
| 60 calls = set(re.findall(chrome_regex, js_file)) | 60 # Add API calls that appear normally, like "chrome.runtime.connect". |
| 61 # Find APIs that have been assigned into variables. | 61 calls = set(re.findall(chrome_pattern, js_file)) |
| 62 assigned_vars = dict(re.findall('var\s*([^\s]+)\s*=\s*%s;' % chrome_regex, | 62 # Add API calls that have been assigned into variables, like |
| 63 js_file)) | 63 # "var storageArea = chrome.storage.sync; storageArea.get", which should |
| 64 # Replace the variable name with the full API name. | 64 # be expanded like "chrome.storage.sync.get". |
| 65 for var_name, value in assigned_vars.iteritems(): | 65 for match in re.finditer(r'var\s+(\w+)\s*=\s*(%s);' % chrome_pattern, |
| 66 js_file = js_file.replace(var_name, value) | 66 js_file): |
| 67 return calls.union(re.findall(chrome_regex, js_file)) | 67 var_name, api_prefix = match.groups() |
| 68 for var_match in re.finditer(r'\b%s\.([\w.]+)\b' % re.escape(var_name), | |
|
Jeffrey Yasskin
2013/11/04 22:50:30
You could probably speed this up a bit (remove the
not at google - send to devlin
2013/11/04 23:39:41
Yeah. Constructing regex's like this does scare me
| |
| 69 js_file): | |
| 70 api_suffix, = var_match.groups() | |
|
Jeffrey Yasskin
2013/11/04 22:50:30
You could replace the unpacking with var_match.gro
not at google - send to devlin
2013/11/04 23:39:41
I kind of like way that it matches line 67 here; a
| |
| 71 calls.add('%s.%s' % (api_prefix, api_suffix)) | |
| 72 return calls | |
| 68 | 73 |
| 69 def _GetDataFromManifest(self, path, file_system): | 74 def _GetDataFromManifest(self, path, file_system): |
| 70 manifest = file_system.ReadSingle(path + '/manifest.json').Get() | 75 manifest = file_system.ReadSingle(path + '/manifest.json').Get() |
| 71 try: | 76 try: |
| 72 manifest_json = json.loads(json_comment_eater.Nom(manifest)) | 77 manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
| 73 except ValueError as e: | 78 except ValueError as e: |
| 74 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) | 79 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) |
| 75 return None | 80 return None |
| 76 l10n_data = { | 81 l10n_data = { |
| 77 'name': manifest_json.get('name', ''), | 82 'name': manifest_json.get('name', ''), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 for item in sorted(api_items): | 128 for item in sorted(api_items): |
| 124 if len(item.split('.')) < 3: | 129 if len(item.split('.')) < 3: |
| 125 continue | 130 continue |
| 126 if item.endswith('.removeListener') or item.endswith('.hasListener'): | 131 if item.endswith('.removeListener') or item.endswith('.hasListener'): |
| 127 continue | 132 continue |
| 128 if item.endswith('.addListener'): | 133 if item.endswith('.addListener'): |
| 129 item = item[:-len('.addListener')] | 134 item = item[:-len('.addListener')] |
| 130 if item.startswith('chrome.'): | 135 if item.startswith('chrome.'): |
| 131 item = item[len('chrome.'):] | 136 item = item[len('chrome.'):] |
| 132 ref_data = self._ref_resolver.GetLink(item) | 137 ref_data = self._ref_resolver.GetLink(item) |
| 138 # TODO(kalman): What about references like chrome.storage.sync.get? | |
| 139 # That should link to either chrome.storage.sync or | |
| 140 # chrome.storage.StorageArea.get (or probably both). | |
| 141 # TODO(kalman): Filter out API-only references? This can happen when | |
| 142 # the API namespace is assigned to a variable, but it's very hard to | |
| 143 # to disambiguate. | |
| 133 if ref_data is None: | 144 if ref_data is None: |
| 134 continue | 145 continue |
| 135 api_calls.append({ | 146 api_calls.append({ |
| 136 'name': ref_data['text'], | 147 'name': ref_data['text'], |
| 137 'link': ref_data['href'] | 148 'link': ref_data['href'] |
| 138 }) | 149 }) |
| 139 | 150 |
| 140 sample_base_path = sample_path.split('/', 1)[1] | 151 sample_base_path = sample_path.split('/', 1)[1] |
| 141 if is_apps: | 152 if is_apps: |
| 142 url = url_constants.GITHUB_BASE + '/' + sample_base_path | 153 url = url_constants.GITHUB_BASE + '/' + sample_base_path |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 else: | 241 else: |
| 231 dict_['id'] = self._GetSampleId(name) | 242 dict_['id'] = self._GetSampleId(name) |
| 232 return_list.append(dict_) | 243 return_list.append(dict_) |
| 233 return return_list | 244 return return_list |
| 234 | 245 |
| 235 def get(self, key): | 246 def get(self, key): |
| 236 return { | 247 return { |
| 237 'apps': lambda: self._CreateSamplesDict('apps'), | 248 'apps': lambda: self._CreateSamplesDict('apps'), |
| 238 'extensions': lambda: self._CreateSamplesDict('extensions') | 249 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 239 }.get(key, lambda: {})() | 250 }.get(key, lambda: {})() |
| OLD | NEW |