PRESUBMIT.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright 2022 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. """Presubmit tests for //android_webview/
  5. Gates against using Context#bindService API before upload.
  6. """
  7. USE_PYTHON3 = True
  8. def CheckChangeOnCommit(input_api, output_api):
  9. results = []
  10. results.extend(_CheckNo_Context_bindService_Added(input_api, output_api))
  11. return results
  12. def CheckChangeOnUpload(input_api, output_api):
  13. results = []
  14. results.extend(_CheckNo_Context_bindService_Added(input_api, output_api))
  15. return results
  16. def _CheckNo_Context_bindService_Added(input_api, output_api):
  17. """Checks that new no files under //android_webview directly use the
  18. Context#bindService. This is because Android platform disallows calling
  19. Context#bindService() from within a BroadcastReceiver context.
  20. """
  21. errors = []
  22. run_with_pattern_part_api = input_api.re.compile(
  23. r'.*\.bindService\(.*')
  24. def _FilterFile(affected_file):
  25. skipFiles = (input_api.DEFAULT_FILES_TO_SKIP +
  26. (r'.*android_webview[/\\]common[/\\]services[/\\]ServiceHelper\.java',
  27. r'.*android_webview[/\\]js_sandbox[/\\].*',))
  28. return input_api.FilterSourceFile(
  29. affected_file,
  30. files_to_skip=skipFiles,
  31. files_to_check=[r'.+\.java$'])
  32. for f in input_api.AffectedSourceFiles(_FilterFile):
  33. for line_num, line in f.ChangedContents():
  34. match = run_with_pattern_part_api.search(line)
  35. if match:
  36. if "ServiceHelper.bindService" not in line:
  37. errors.append("%s:%d:%s" % (f.LocalPath(), line_num, line))
  38. results = []
  39. if errors:
  40. results.append(output_api.PresubmitPromptWarning("""
  41. New code in //android_webview should not use \
  42. android.content.Context#bindService. Instead use \
  43. android_webview.common.services.ServiceHelper#bindService.
  44. """, errors))
  45. return results