browsertest_util.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2018 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. #include "extensions/browser/browsertest_util.h"
  5. #include "content/public/browser/browser_context.h"
  6. #include "content/public/browser/service_worker_context.h"
  7. #include "content/public/browser/storage_partition.h"
  8. #include "content/public/test/service_worker_test_helpers.h"
  9. #include "extensions/browser/background_script_executor.h"
  10. #include "extensions/browser/extension_host.h"
  11. #include "extensions/browser/extension_registry.h"
  12. #include "extensions/browser/process_manager.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace extensions::browsertest_util {
  15. namespace {
  16. // Returns a log-friendly script string.
  17. std::string GetScriptToLog(const std::string& script) {
  18. // The maximum script size for which to print on failure.
  19. static constexpr int kMaxFailingScriptSizeToLog = 1000;
  20. return (script.size() < kMaxFailingScriptSizeToLog) ? script
  21. : "<script too large>";
  22. }
  23. } // namespace
  24. std::string ExecuteScriptInBackgroundPage(
  25. content::BrowserContext* context,
  26. const std::string& extension_id,
  27. const std::string& script,
  28. ScriptUserActivation script_user_activation) {
  29. BackgroundScriptExecutor script_executor(context);
  30. // Legacy scripts were written to pass the (string) result via
  31. // window.domAutomationController.send().
  32. base::Value value = script_executor.ExecuteScript(
  33. extension_id, script,
  34. BackgroundScriptExecutor::ResultCapture::kWindowDomAutomationController,
  35. script_user_activation);
  36. if (!value.is_string()) {
  37. ADD_FAILURE() << "Bad return value: " << value.type()
  38. << "; script: " << GetScriptToLog(script);
  39. return "";
  40. }
  41. return value.GetString();
  42. }
  43. bool ExecuteScriptInBackgroundPageNoWait(content::BrowserContext* context,
  44. const std::string& extension_id,
  45. const std::string& script) {
  46. return BackgroundScriptExecutor::ExecuteScriptAsync(
  47. context, extension_id, script, ScriptUserActivation::kActivate);
  48. }
  49. void StopServiceWorkerForExtensionGlobalScope(content::BrowserContext* context,
  50. const std::string& extension_id) {
  51. const Extension* extension =
  52. ExtensionRegistry::Get(context)->GetExtensionById(
  53. extension_id, ExtensionRegistry::ENABLED);
  54. ASSERT_TRUE(extension) << "Unknown extension ID.";
  55. base::RunLoop run_loop;
  56. content::ServiceWorkerContext* service_worker_context =
  57. context->GetDefaultStoragePartition()->GetServiceWorkerContext();
  58. content::StopServiceWorkerForScope(service_worker_context, extension->url(),
  59. run_loop.QuitClosure());
  60. run_loop.Run();
  61. }
  62. } // namespace extensions::browsertest_util