web_view_web_main_parts.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2014 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. #import "ios/web_view/internal/web_view_web_main_parts.h"
  5. #include "base/base_paths.h"
  6. #include "base/check.h"
  7. #include "base/feature_list.h"
  8. #include "base/path_service.h"
  9. #include "base/strings/string_util.h"
  10. #include "components/autofill/core/common/autofill_features.h"
  11. #include "components/autofill/core/common/autofill_payments_features.h"
  12. #include "components/component_updater/installer_policies/safety_tips_component_installer.h"
  13. #include "components/password_manager/core/common/password_manager_features.h"
  14. #include "components/sync/base/features.h"
  15. #include "components/variations/variations_ids_provider.h"
  16. #include "ios/web/public/webui/web_ui_ios_controller_factory.h"
  17. #include "ios/web_view/internal/app/application_context.h"
  18. #import "ios/web_view/internal/cwv_flags_internal.h"
  19. #import "ios/web_view/internal/cwv_web_view_configuration_internal.h"
  20. #include "ios/web_view/internal/translate/web_view_translate_service.h"
  21. #include "ios/web_view/internal/webui/web_view_web_ui_ios_controller_factory.h"
  22. #include "ui/base/l10n/l10n_util_mac.h"
  23. #include "ui/base/resource/resource_bundle.h"
  24. #if DCHECK_IS_ON()
  25. #include "ui/display/screen_base.h"
  26. #endif
  27. #if !defined(__has_feature) || !__has_feature(objc_arc)
  28. #error "This file requires ARC support."
  29. #endif
  30. namespace ios_web_view {
  31. WebViewWebMainParts::WebViewWebMainParts()
  32. : field_trial_list_(/*entropy_provider=*/nullptr) {}
  33. WebViewWebMainParts::~WebViewWebMainParts() {
  34. #if DCHECK_IS_ON()
  35. // The screen object is never deleted on IOS. Make sure that all display
  36. // observers are removed at the end.
  37. display::ScreenBase* screen =
  38. static_cast<display::ScreenBase*>(display::Screen::GetScreen());
  39. DCHECK(!screen->HasDisplayObservers());
  40. #endif
  41. }
  42. void WebViewWebMainParts::PreCreateMainMessageLoop() {
  43. l10n_util::OverrideLocaleWithCocoaLocale();
  44. ui::ResourceBundle::InitSharedInstanceWithLocale(
  45. std::string(), nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  46. LoadNonScalableResources();
  47. LoadScalableResources();
  48. }
  49. void WebViewWebMainParts::PreCreateThreads() {
  50. // Initialize local state.
  51. PrefService* local_state = ApplicationContext::GetInstance()->GetLocalState();
  52. DCHECK(local_state);
  53. // Flags are converted here to ensure it is set before being read by others.
  54. [[CWVFlags sharedInstance] convertFlagsToCommandLineSwitches];
  55. ApplicationContext::GetInstance()->PreCreateThreads();
  56. variations::VariationsIdsProvider::Create(
  57. variations::VariationsIdsProvider::Mode::kUseSignedInState);
  58. std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
  59. std::string enable_features = base::JoinString(
  60. {
  61. autofill::features::kAutofillUpstream.name,
  62. password_manager::features::kEnablePasswordsAccountStorage.name,
  63. syncer::kSyncTrustedVaultPassphraseiOSRPC.name,
  64. syncer::kSyncTrustedVaultPassphraseRecovery.name,
  65. },
  66. ",");
  67. std::string disabled_features = base::JoinString(
  68. {
  69. },
  70. ",");
  71. feature_list->InitializeFromCommandLine(
  72. /*enable_features=*/enable_features,
  73. /*disable_features=*/disabled_features);
  74. base::FeatureList::SetInstance(std::move(feature_list));
  75. }
  76. void WebViewWebMainParts::PreMainMessageLoopRun() {
  77. WebViewTranslateService::GetInstance()->Initialize();
  78. web::WebUIIOSControllerFactory::RegisterFactory(
  79. WebViewWebUIIOSControllerFactory::GetInstance());
  80. component_updater::ComponentUpdateService* cus =
  81. ApplicationContext::GetInstance()->GetComponentUpdateService();
  82. RegisterSafetyTipsComponent(cus);
  83. }
  84. void WebViewWebMainParts::PostMainMessageLoopRun() {
  85. ApplicationContext::GetInstance()->ShutdownSafeBrowsingServiceIfNecessary();
  86. // CWVWebViewConfiguration must destroy its WebViewBrowserStates before the
  87. // threads are stopped by ApplicationContext.
  88. [CWVWebViewConfiguration shutDown];
  89. // Translate must be shutdown AFTER CWVWebViewConfiguration since translate
  90. // may receive final callbacks during webstate shutdowns.
  91. WebViewTranslateService::GetInstance()->Shutdown();
  92. ApplicationContext::GetInstance()->SaveState();
  93. }
  94. void WebViewWebMainParts::PostDestroyThreads() {
  95. ApplicationContext::GetInstance()->PostDestroyThreads();
  96. }
  97. void WebViewWebMainParts::LoadNonScalableResources() {
  98. base::FilePath pak_file;
  99. base::PathService::Get(base::DIR_MODULE, &pak_file);
  100. pak_file = pak_file.Append(FILE_PATH_LITERAL("web_view_resources.pak"));
  101. ui::ResourceBundle& resource_bundle = ui::ResourceBundle::GetSharedInstance();
  102. resource_bundle.AddDataPackFromPath(pak_file, ui::kScaleFactorNone);
  103. }
  104. void WebViewWebMainParts::LoadScalableResources() {
  105. ui::ResourceBundle& resource_bundle = ui::ResourceBundle::GetSharedInstance();
  106. if (ui::ResourceBundle::IsScaleFactorSupported(ui::k100Percent)) {
  107. base::FilePath pak_file_100;
  108. base::PathService::Get(base::DIR_MODULE, &pak_file_100);
  109. pak_file_100 =
  110. pak_file_100.Append(FILE_PATH_LITERAL("web_view_100_percent.pak"));
  111. resource_bundle.AddDataPackFromPath(pak_file_100, ui::k100Percent);
  112. }
  113. if (ui::ResourceBundle::IsScaleFactorSupported(ui::k200Percent)) {
  114. base::FilePath pak_file_200;
  115. base::PathService::Get(base::DIR_MODULE, &pak_file_200);
  116. pak_file_200 =
  117. pak_file_200.Append(FILE_PATH_LITERAL("web_view_200_percent.pak"));
  118. resource_bundle.AddDataPackFromPath(pak_file_200, ui::k200Percent);
  119. }
  120. if (ui::ResourceBundle::IsScaleFactorSupported(ui::k300Percent)) {
  121. base::FilePath pak_file_300;
  122. base::PathService::Get(base::DIR_MODULE, &pak_file_300);
  123. pak_file_300 =
  124. pak_file_300.Append(FILE_PATH_LITERAL("web_view_300_percent.pak"));
  125. resource_bundle.AddDataPackFromPath(pak_file_300, ui::k300Percent);
  126. }
  127. }
  128. } // namespace ios_web_view