web_engine_main_delegate.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "fuchsia_web/webengine/web_engine_main_delegate.h"
  5. #include <utility>
  6. #include "base/base_paths.h"
  7. #include "base/command_line.h"
  8. #include "base/files/file_util.h"
  9. #include "base/fuchsia/intl_profile_watcher.h"
  10. #include "base/i18n/rtl.h"
  11. #include "base/path_service.h"
  12. #include "base/strings/string_split.h"
  13. #include "content/public/common/content_switches.h"
  14. #include "content/public/common/main_function_params.h"
  15. #include "fuchsia_web/common/init_logging.h"
  16. #include "fuchsia_web/webengine/browser/web_engine_browser_main.h"
  17. #include "fuchsia_web/webengine/browser/web_engine_content_browser_client.h"
  18. #include "fuchsia_web/webengine/common/cors_exempt_headers.h"
  19. #include "fuchsia_web/webengine/common/web_engine_content_client.h"
  20. #include "fuchsia_web/webengine/renderer/web_engine_content_renderer_client.h"
  21. #include "fuchsia_web/webengine/switches.h"
  22. #include "google_apis/google_api_keys.h"
  23. #include "ui/base/resource/resource_bundle.h"
  24. #include "ui/base/ui_base_paths.h"
  25. namespace {
  26. WebEngineMainDelegate* g_current_web_engine_main_delegate = nullptr;
  27. void InitializeResources() {
  28. constexpr char kCommonResourcesPakPath[] = "web_engine_common_resources.pak";
  29. constexpr char kWebUiGeneratedResourcesPakPath[] =
  30. "ui/resources/webui_generated_resources.pak";
  31. base::FilePath asset_root;
  32. bool result = base::PathService::Get(base::DIR_ASSETS, &asset_root);
  33. DCHECK(result);
  34. // Initialize the process-global ResourceBundle, and manually load the
  35. // WebEngine locale-agnostic resources.
  36. const std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
  37. base::i18n::GetConfiguredLocale(), nullptr,
  38. ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  39. ui::SetSupportedResourceScaleFactors({ui::k100Percent});
  40. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  41. asset_root.Append(kCommonResourcesPakPath), ui::kScaleFactorNone);
  42. VLOG(1) << "Loaded resources including locale: " << locale;
  43. // Conditionally load WebUI resource PAK if visible from namespace.
  44. const base::FilePath webui_generated_resources_path =
  45. asset_root.Append(kWebUiGeneratedResourcesPakPath);
  46. if (base::PathExists(webui_generated_resources_path)) {
  47. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  48. webui_generated_resources_path, ui::kScaleFactorNone);
  49. }
  50. }
  51. } // namespace
  52. // static
  53. WebEngineMainDelegate* WebEngineMainDelegate::GetInstanceForTest() {
  54. return g_current_web_engine_main_delegate;
  55. }
  56. WebEngineMainDelegate::WebEngineMainDelegate() {
  57. g_current_web_engine_main_delegate = this;
  58. }
  59. WebEngineMainDelegate::~WebEngineMainDelegate() = default;
  60. absl::optional<int> WebEngineMainDelegate::BasicStartupComplete() {
  61. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  62. if (!InitLoggingFromCommandLine(*command_line)) {
  63. return 1;
  64. }
  65. if (command_line->HasSwitch(switches::kGoogleApiKey)) {
  66. google_apis::SetAPIKey(
  67. command_line->GetSwitchValueASCII(switches::kGoogleApiKey));
  68. }
  69. SetCorsExemptHeaders(base::SplitString(
  70. base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
  71. switches::kCorsExemptHeaders),
  72. ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY));
  73. return absl::nullopt;
  74. }
  75. void WebEngineMainDelegate::PreSandboxStartup() {
  76. // Early during startup, configure the process with the primary locale and
  77. // load resources. If locale-specific resources are loaded then they must be
  78. // explicitly reloaded after each change to the primary locale.
  79. // In the browser process the locale determines the accept-language header
  80. // contents, and is supplied to renderers for Blink to report to web content.
  81. std::string initial_locale =
  82. base::FuchsiaIntlProfileWatcher::GetPrimaryLocaleIdForInitialization();
  83. base::i18n::SetICUDefaultLocale(initial_locale);
  84. InitializeResources();
  85. }
  86. absl::variant<int, content::MainFunctionParams>
  87. WebEngineMainDelegate::RunProcess(
  88. const std::string& process_type,
  89. content::MainFunctionParams main_function_params) {
  90. if (!process_type.empty())
  91. return std::move(main_function_params);
  92. return WebEngineBrowserMain(std::move(main_function_params));
  93. }
  94. content::ContentClient* WebEngineMainDelegate::CreateContentClient() {
  95. content_client_ = std::make_unique<WebEngineContentClient>();
  96. return content_client_.get();
  97. }
  98. content::ContentBrowserClient*
  99. WebEngineMainDelegate::CreateContentBrowserClient() {
  100. DCHECK(!browser_client_);
  101. browser_client_ = std::make_unique<WebEngineContentBrowserClient>();
  102. return browser_client_.get();
  103. }
  104. content::ContentRendererClient*
  105. WebEngineMainDelegate::CreateContentRendererClient() {
  106. renderer_client_ = std::make_unique<WebEngineContentRendererClient>();
  107. return renderer_client_.get();
  108. }