[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / PRESUBMIT.py
1 # Copyright 2014 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Presubmit script for Chromium browser code."""
6
7
8 import re
9
10 # Checks whether an autofill-related browsertest fixture class inherits from
11 # either InProcessBrowserTest or AndroidBrowserTest without having a member of
12 # type `autofill::test::AutofillBrowserTestEnvironment`. In that case, the
13 # functions registers a presubmit warning.
14 def _CheckNoAutofillBrowserTestsWithoutAutofillBrowserTestEnvironment(
15         input_api, output_api):
16   autofill_files_pattern = re.compile(
17       r'(autofill|password_manager).*\.(mm|cc|h)')
18   concerned_files = [(f, input_api.ReadFile(f))
19                      for f in input_api.AffectedFiles(include_deletes=False)
20                      if autofill_files_pattern.search(f.LocalPath())]
21
22   warning_files = []
23   class_name = r'^( *)(class|struct)\s+\w+\s*:\s*'
24   target_base = r'[^\{]*\bpublic\s+(InProcess|Android)BrowserTest[^\{]*\{'
25   class_declaration_pattern = re.compile(
26       class_name + target_base, re.MULTILINE)
27   for autofill_file, file_content in concerned_files:
28     for class_match in re.finditer(class_declaration_pattern, file_content):
29       indentation = class_match.group(1)
30       class_end_pattern = re.compile(
31           r'^' + indentation + r'\};$', re.MULTILINE)
32       class_end = class_end_pattern.search(file_content[class_match.start():])
33
34       corresponding_subclass = (
35           '' if class_end is None else
36           file_content[
37               class_match.start():
38               class_match.start() + class_end.end()])
39
40       required_member_pattern = re.compile(
41           r'^' + indentation +
42           r'  (::)?(autofill::)?test::AutofillBrowserTestEnvironment\s+\w+_;',
43           re.MULTILINE)
44       if not required_member_pattern.search(corresponding_subclass):
45         warning_files.append(autofill_file)
46
47   return [output_api.PresubmitPromptWarning(
48       'Consider adding a member autofill::test::AutofillBrowserTestEnvironment '
49       'to the test fixtures that derive from InProcessBrowserTest or '
50       'AndroidBrowserTest in order to disable kAutofillServerCommunication in '
51       'browser tests.',
52       warning_files)] if len(warning_files) else []
53
54 def _RunHistogramChecks(input_api, output_api, histogram_name):
55   try:
56     # Setup sys.path so that we can call histograms code.
57     import sys
58     original_sys_path = sys.path
59     sys.path = sys.path + [input_api.os_path.join(
60         input_api.change.RepositoryRoot(),
61         'tools', 'metrics', 'histograms')]
62
63     results = []
64
65     import presubmit_bad_message_reasons
66     results.extend(presubmit_bad_message_reasons.PrecheckBadMessage(input_api,
67         output_api, histogram_name))
68
69     return results
70   except:
71     return [output_api.PresubmitError('Could not verify histogram!')]
72   finally:
73     sys.path = original_sys_path
74
75
76 def _CheckUnwantedDependencies(input_api, output_api):
77   problems = []
78   for f in input_api.AffectedFiles():
79     if not f.LocalPath().endswith('DEPS'):
80       continue
81
82     for line_num, line in f.ChangedContents():
83       if not line.strip().startswith('#'):
84         m = re.search(r".*\/blink\/public\/web.*", line)
85         if m:
86           problems.append(m.group(0))
87
88   if not problems:
89     return []
90   return [output_api.PresubmitPromptWarning(
91       'chrome/browser cannot depend on blink/public/web interfaces. ' +
92       'Use blink/public/common instead.',
93       items=problems)]
94
95
96 def _CheckNoInteractiveUiTestLibInNonInteractiveUiTest(input_api, output_api):
97   """Makes sure that ui_controls related API are used only in
98   interactive_in_tests.
99   """
100   problems = []
101   # There are interactive tests whose name ends with `_browsertest.cc`
102   # or `_browser_test.cc`.
103   files_to_skip = ((r'.*interactive_.*test\.cc',) +
104                    input_api.DEFAULT_FILES_TO_SKIP)
105   def FileFilter(affected_file):
106     """Check non interactive_uitests only."""
107     return input_api.FilterSourceFile(
108         affected_file,
109         files_to_check=(
110             r'.*browsertest\.cc',
111             r'.*unittest\.cc'),
112         files_to_skip=files_to_skip)
113
114   ui_controls_includes =(
115     input_api.re.compile(
116         r'#include.*/(ui_controls.*h|interactive_test_utils.h)"'))
117
118   for f in input_api.AffectedFiles(include_deletes=False,
119                                    file_filter=FileFilter):
120     for line_num, line in f.ChangedContents():
121       m = re.search(ui_controls_includes, line)
122       if m:
123         problems.append('  %s:%d:%s' % (f.LocalPath(), line_num, m.group(0)))
124
125   if not problems:
126     return []
127
128   WARNING_MSG ="""
129   ui_controls API can be used only in interactive_ui_tests.
130   If the test is in the interactive_ui_tests, please consider renaming
131   to xxx_interactive_uitest.cc"""
132   return [output_api.PresubmitPromptWarning(WARNING_MSG, items=problems)]
133
134
135 def _CheckForUselessExterns(input_api, output_api):
136   """Makes sure developers don't copy "extern const char kFoo[]" from
137   foo.h to foo.cc.
138   """
139   problems = []
140   BAD_PATTERN = input_api.re.compile(r'^extern const')
141
142   def FileFilter(affected_file):
143     """Check only a particular list of files"""
144     return input_api.FilterSourceFile(
145         affected_file,
146         files_to_check=[r'chrome[/\\]browser[/\\]flag_descriptions\.cc']);
147
148   for f in input_api.AffectedFiles(include_deletes=False,
149                                    file_filter=FileFilter):
150     for _, line in f.ChangedContents():
151       if BAD_PATTERN.search(line):
152         problems.append(f)
153
154   if not problems:
155     return []
156
157   WARNING_MSG ="""Do not write "extern const char" in these .cc files:"""
158   return [output_api.PresubmitPromptWarning(WARNING_MSG, items=problems)]
159
160
161 def _CommonChecks(input_api, output_api):
162   """Checks common to both upload and commit."""
163   results = []
164   results.extend(
165     _CheckNoAutofillBrowserTestsWithoutAutofillBrowserTestEnvironment(
166         input_api, output_api))
167   results.extend(_CheckUnwantedDependencies(input_api, output_api))
168   results.extend(_RunHistogramChecks(input_api, output_api,
169                  "BadMessageReasonChrome"))
170   results.extend(_CheckNoInteractiveUiTestLibInNonInteractiveUiTest(
171       input_api, output_api))
172   results.extend(_CheckForUselessExterns(input_api, output_api))
173   return results
174
175 def CheckChangeOnUpload(input_api, output_api):
176   return _CommonChecks(input_api, output_api)
177
178 def CheckChangeOnCommit(input_api, output_api):
179   return _CommonChecks(input_api, output_api)