Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 1 | # Copyright (c) 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. |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 4 | """Top-level presubmit script for Blink. |
| 5 | |
Daniel McArdle | f883817 | 2019-12-06 21:36:48 | [diff] [blame] | 6 | See https://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 7 | for more details about the presubmit API built into gcl. |
| 8 | """ |
| 9 | |
| 10 | import imp |
| 11 | import inspect |
| 12 | import os |
Daniel McArdle | f883817 | 2019-12-06 21:36:48 | [diff] [blame] | 13 | import re |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 14 | |
Fabrice de Gans | 954c992 | 2021-06-23 15:26:12 | [diff] [blame] | 15 | USE_PYTHON3 = True |
| 16 | |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 17 | try: |
| 18 | # pylint: disable=C0103 |
| 19 | audit_non_blink_usage = imp.load_source( |
| 20 | 'audit_non_blink_usage', |
Fergal Daly | 5f61cbe | 2018-10-05 04:30:34 | [diff] [blame] | 21 | os.path.join(os.path.dirname(inspect.stack()[0][1]), |
| 22 | 'tools/blinkpy/presubmit/audit_non_blink_usage.py')) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 23 | except IOError: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 24 | # One of the presubmit upload tests tries to exec this script, which |
| 25 | # doesn't interact so well with the import hack... just ignore the |
| 26 | # exception here and hope for the best. |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 27 | pass |
| 28 | |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 29 | _EXCLUDED_PATHS = ( |
Robert Ma | ad740d9 | 2019-06-03 13:18:12 | [diff] [blame] | 30 | # These are third-party dependencies that we don't directly control. |
| 31 | r'^third_party[\\/]blink[\\/]tools[\\/]blinkpy[\\/]third_party[\\/]wpt[\\/]wpt[\\/].*', |
Robert Ma | ad740d9 | 2019-06-03 13:18:12 | [diff] [blame] | 32 | r'^third_party[\\/]blink[\\/]web_tests[\\/]external[\\/]wpt[\\/]resources[\\/]webidl2[\\/].*', |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | |
| 36 | def _CheckForWrongMojomIncludes(input_api, output_api): |
| 37 | # In blink the code should either use -blink.h or -shared.h mojom |
| 38 | # headers, except in public where only -shared.h headers should be |
| 39 | # used to avoid exporting Blink types outside Blink. |
| 40 | def source_file_filter(path): |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 41 | return input_api.FilterSourceFile( |
| 42 | path, |
| 43 | files_to_skip=[ |
Dave Tapuska | cf95aaa | 2022-07-20 17:00:54 | [diff] [blame] | 44 | r'.*_test.*\.(cc|h)$', |
Matt Menke | 132d70e | 2021-06-29 01:19:28 | [diff] [blame] | 45 | r'third_party[\\/]blink[\\/]common[\\/]', |
| 46 | r'third_party[\\/]blink[\\/]public[\\/]common[\\/]', |
| 47 | r'third_party[\\/]blink[\\/]renderer[\\/]platform[\\/]loader[\\/]fetch[\\/]url_loader[\\/]', |
Yuzu Saijo | e12e7ae | 2022-08-22 05:56:55 | [diff] [blame] | 48 | r'third_party[\\/]blink[\\/]renderer[\\/]core[\\/]frame[\\/]web_view_impl.*\.(cc|h)$', |
Dave Tapuska | cf95aaa | 2022-07-20 17:00:54 | [diff] [blame] | 49 | r'third_party[\\/]blink[\\/]renderer[\\/]core[\\/]frame[\\/]web.*frame.*\.(cc|h)$', |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 50 | ]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 51 | |
Dave Tapuska | 8cc2539 | 2020-01-22 18:36:48 | [diff] [blame] | 52 | pattern = input_api.re.compile(r'#include\s+[<"](.+)\.mojom(.*)\.h[>"]') |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 53 | public_folder = input_api.os_path.normpath('third_party/blink/public/') |
| 54 | non_blink_mojom_errors = [] |
| 55 | public_blink_mojom_errors = [] |
Dave Tapuska | 8cc2539 | 2020-01-22 18:36:48 | [diff] [blame] | 56 | |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 57 | # Allow including specific non-blink interfaces that are used in the |
| 58 | # public C++ API. Adding to these allowed interfaces should meet the |
| 59 | # following conditions: |
| 60 | # - Its pros/cons is discussed and have consensus on |
| 61 | # platform-architecture-dev@ and/or |
| 62 | # - It uses POD types that will not import STL (or base string) types into |
| 63 | # blink (such as no strings or vectors). |
Makoto Shimazu | 9e4b946a | 2020-03-09 05:19:17 | [diff] [blame] | 64 | # |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 65 | # So far, non-blink interfaces are allowed only for loading / loader and |
| 66 | # media interfaces so that we don't need type conversions to get through |
| 67 | # the boundary between Blink and non-Blink. |
| 68 | allowed_interfaces = ( |
| 69 | 'services/network/public/mojom/cross_origin_embedder_policy', |
Hans Wennborg | a61b304 | 2021-06-15 12:06:31 | [diff] [blame] | 70 | 'services/network/public/mojom/early_hints', |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 71 | 'services/network/public/mojom/fetch_api', |
| 72 | 'services/network/public/mojom/load_timing_info', |
| 73 | 'services/network/public/mojom/url_loader', |
| 74 | 'services/network/public/mojom/url_loader_factory', |
| 75 | 'services/network/public/mojom/url_response_head', |
| 76 | 'third_party/blink/public/mojom/blob/serialized_blob', |
| 77 | 'third_party/blink/public/mojom/fetch/fetch_api_request', |
| 78 | 'third_party/blink/public/mojom/loader/resource_load_info', |
| 79 | 'third_party/blink/public/mojom/loader/resource_load_info_notifier', |
| 80 | 'third_party/blink/public/mojom/worker/subresource_loader_updater', |
| 81 | 'third_party/blink/public/mojom/loader/transferrable_url_loader', |
Mythri A | fefc0c46 | 2021-05-26 08:37:26 | [diff] [blame] | 82 | 'third_party/blink/public/mojom/loader/code_cache', |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 83 | 'media/mojo/mojom/interface_factory', 'media/mojo/mojom/audio_decoder', |
Dale Curtis | 4c84c069 | 2022-05-12 21:03:14 | [diff] [blame] | 84 | 'media/mojo/mojom/audio_encoder', 'media/mojo/mojom/video_decoder', |
Guido Urdaneta | a5c57ea | 2021-03-02 10:53:57 | [diff] [blame] | 85 | 'media/mojo/mojom/media_metrics_provider') |
Dave Tapuska | 8cc2539 | 2020-01-22 18:36:48 | [diff] [blame] | 86 | |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 87 | for f in input_api.AffectedFiles(file_filter=source_file_filter): |
| 88 | for line_num, line in f.ChangedContents(): |
| 89 | error_list = None |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 90 | match = pattern.match(line) |
Dave Tapuska | 8cc2539 | 2020-01-22 18:36:48 | [diff] [blame] | 91 | if (match and match.group(1) not in allowed_interfaces): |
Eriko Kurimoto | 61f1281 | 2020-01-31 08:06:50 | [diff] [blame] | 92 | if match.group(2) not in ('-shared', '-forward'): |
Leon Han | b6bd8e6 | 2019-05-29 03:18:20 | [diff] [blame] | 93 | if f.LocalPath().startswith(public_folder): |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 94 | error_list = public_blink_mojom_errors |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 95 | elif match.group(2) not in ('-blink', '-blink-forward', |
| 96 | '-blink-test-utils'): |
| 97 | # Neither -shared.h, -blink.h, -blink-forward.h nor |
| 98 | # -blink-test-utils.h. |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 99 | error_list = non_blink_mojom_errors |
| 100 | |
| 101 | if error_list is not None: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 102 | error_list.append(' %s:%d %s' % |
| 103 | (f.LocalPath(), line_num, line)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 104 | |
| 105 | results = [] |
| 106 | if non_blink_mojom_errors: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 107 | results.append( |
| 108 | output_api.PresubmitError( |
| 109 | 'Files that include non-Blink variant mojoms found. ' |
| 110 | 'You must include .mojom-blink.h, .mojom-forward.h or ' |
| 111 | '.mojom-shared.h instead:', non_blink_mojom_errors)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 112 | |
| 113 | if public_blink_mojom_errors: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 114 | results.append( |
| 115 | output_api.PresubmitError( |
| 116 | 'Public blink headers using Blink variant mojoms found. ' |
| 117 | 'You must include .mojom-forward.h or .mojom-shared.h ' |
| 118 | 'instead:', public_blink_mojom_errors)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 119 | |
| 120 | return results |
| 121 | |
| 122 | |
| 123 | def _CommonChecks(input_api, output_api): |
| 124 | """Checks common to both upload and commit.""" |
| 125 | # We should figure out what license checks we actually want to use. |
| 126 | license_header = r'.*' |
| 127 | |
| 128 | results = [] |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 129 | results.extend( |
| 130 | input_api.canned_checks.PanProjectChecks( |
| 131 | input_api, |
| 132 | output_api, |
| 133 | excluded_paths=_EXCLUDED_PATHS, |
Bruce Dawson | ae257be | 2022-07-22 15:48:26 | [diff] [blame] | 134 | owners_check=False, |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 135 | maxlen=800, |
Bruce Dawson | e7f7956e | 2022-05-03 16:42:17 | [diff] [blame] | 136 | license_header=license_header, |
| 137 | global_checks=False)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 138 | results.extend(_CheckForWrongMojomIncludes(input_api, output_api)) |
| 139 | return results |
| 140 | |
| 141 | |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 142 | def _FilterPaths(input_api): |
| 143 | """Returns input files with certain paths removed.""" |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 144 | files = [] |
| 145 | for f in input_api.AffectedFiles(): |
| 146 | file_path = f.LocalPath() |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 147 | # Filter out changes in web_tests/. |
Kent Tamura | d910234 | 2022-06-15 19:47:35 | [diff] [blame] | 148 | if 'web_tests' + input_api.os_path.sep in file_path: |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 149 | continue |
| 150 | if '/PRESUBMIT' in file_path: |
| 151 | continue |
Daniel McArdle | f883817 | 2019-12-06 21:36:48 | [diff] [blame] | 152 | # Skip files that were generated by bison. |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 153 | if re.search( |
| 154 | 'third_party/blink/renderer/' + |
| 155 | 'core/xml/xpath_grammar_generated\.(cc|h)$', file_path): |
Dave Tapuska | 8cc2539 | 2020-01-22 18:36:48 | [diff] [blame] | 156 | continue |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 157 | files.append(input_api.os_path.join('..', '..', file_path)) |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 158 | return files |
| 159 | |
| 160 | |
| 161 | def _CheckStyle(input_api, output_api): |
| 162 | files = _FilterPaths(input_api) |
Kent Tamura | f76ea8f | 2018-04-17 04:45:07 | [diff] [blame] | 163 | # Do not call check_blink_style.py with empty affected file list if all |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 164 | # input_api.AffectedFiles got filtered. |
| 165 | if not files: |
| 166 | return [] |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 167 | |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 168 | style_checker_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 169 | 'tools', |
| 170 | 'check_blink_style.py') |
Bruce Dawson | 5f7cbff | 2022-05-25 17:36:44 | [diff] [blame] | 171 | # When running git cl presubmit --all this presubmit may be asked to check |
| 172 | # ~260 files, leading to a command line that is about 17,000 characters. |
| 173 | # This goes past the Windows 8191 character cmd.exe limit and causes cryptic |
| 174 | # failures. To avoid these we break the command up into smaller pieces. |
| 175 | # Depending on how long the command is on Windows the error may be: |
| 176 | # The command line is too long. |
| 177 | # Or it may be: |
| 178 | # OSError: Execution failed with error: [WinError 206] The filename or |
| 179 | # extension is too long. |
| 180 | # The latter error comes from CreateProcess hitting its 32768 character |
| 181 | # limit. |
Bruce Dawson | 5130baf | 2022-06-23 22:25:12 | [diff] [blame] | 182 | files_per_command = 40 if input_api.is_windows else 1000 |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 183 | results = [] |
Bruce Dawson | 5f7cbff | 2022-05-25 17:36:44 | [diff] [blame] | 184 | for i in range(0, len(files), files_per_command): |
| 185 | args = [ |
Bruce Dawson | 5130baf | 2022-06-23 22:25:12 | [diff] [blame] | 186 | input_api.python3_executable, style_checker_path, '--diff-files' |
Bruce Dawson | 5f7cbff | 2022-05-25 17:36:44 | [diff] [blame] | 187 | ] |
| 188 | args += files[i:i + files_per_command] |
| 189 | |
| 190 | try: |
| 191 | child = input_api.subprocess.Popen( |
| 192 | args, stderr=input_api.subprocess.PIPE) |
| 193 | _, stderrdata = child.communicate() |
| 194 | if child.returncode != 0: |
| 195 | results.append( |
| 196 | output_api.PresubmitError('check_blink_style.py failed', |
| 197 | [stderrdata])) |
| 198 | except Exception as e: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 199 | results.append( |
Bruce Dawson | 5f7cbff | 2022-05-25 17:36:44 | [diff] [blame] | 200 | output_api.PresubmitNotifyResult( |
| 201 | 'Could not run check_blink_style.py', [str(e)])) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 202 | |
| 203 | return results |
| 204 | |
| 205 | |
| 206 | def _CheckForPrintfDebugging(input_api, output_api): |
| 207 | """Generally speaking, we'd prefer not to land patches that printf |
| 208 | debug output. |
| 209 | """ |
| 210 | printf_re = input_api.re.compile(r'^\s*(printf\(|fprintf\(stderr,)') |
| 211 | errors = input_api.canned_checks._FindNewViolationsOfRule( |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 212 | lambda _, x: not printf_re.search(x), input_api, None) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 213 | errors = [' * %s' % violation for violation in errors] |
| 214 | if errors: |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 215 | return [ |
| 216 | output_api.PresubmitPromptOrNotify( |
| 217 | 'printf debugging is best debugging! That said, it might ' |
| 218 | 'be a good idea to drop the following occurences from ' |
| 219 | 'your patch before uploading:\n%s' % '\n'.join(errors)) |
| 220 | ] |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 221 | return [] |
| 222 | |
| 223 | |
| 224 | def _CheckForForbiddenChromiumCode(input_api, output_api): |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 225 | """Checks that Blink uses Chromium classes and namespaces only in |
| 226 | permitted code. |
| 227 | """ |
| 228 | # TODO(dcheng): This is pretty similar to _FindNewViolationsOfRule. |
| 229 | # Unfortunately, that can't be used directly because it doesn't give the |
| 230 | # callable enough information (namely the path to the file), so we |
| 231 | # duplicate the logic here... |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 232 | results = [] |
| 233 | for f in input_api.AffectedFiles(): |
| 234 | path = f.LocalPath() |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 235 | errors = audit_non_blink_usage.check( |
| 236 | path, [(i + 1, l) for i, l in enumerate(f.NewContents())]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 237 | if errors: |
| 238 | errors = audit_non_blink_usage.check(path, f.ChangedContents()) |
| 239 | if errors: |
Fergal Daly | 5f9d296 | 2018-10-05 08:46:07 | [diff] [blame] | 240 | for error in errors: |
| 241 | msg = '%s:%d uses disallowed identifier %s' % ( |
| 242 | path, error.line, error.identifier) |
| 243 | if error.advice: |
| 244 | msg += ". Advice: %s" % "\n".join(error.advice) |
Miyoung Shin | ba0f172b | 2020-01-23 09:10:41 | [diff] [blame] | 245 | if error.warning: |
| 246 | results.append(output_api.PresubmitPromptWarning(msg)) |
| 247 | else: |
| 248 | results.append(output_api.PresubmitError(msg)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 249 | return results |
| 250 | |
| 251 | |
| 252 | def CheckChangeOnUpload(input_api, output_api): |
| 253 | results = [] |
| 254 | results.extend(_CommonChecks(input_api, output_api)) |
| 255 | results.extend(_CheckStyle(input_api, output_api)) |
| 256 | results.extend(_CheckForPrintfDebugging(input_api, output_api)) |
| 257 | results.extend(_CheckForForbiddenChromiumCode(input_api, output_api)) |
| 258 | return results |
| 259 | |
| 260 | |
| 261 | def CheckChangeOnCommit(input_api, output_api): |
| 262 | results = [] |
| 263 | results.extend(_CommonChecks(input_api, output_api)) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 264 | return results |