aw_browser_main_parts.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2012 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 "android_webview/browser/aw_browser_main_parts.h"
  5. #include <memory>
  6. #include <set>
  7. #include <string>
  8. #include <utility>
  9. #include "android_webview/browser/aw_browser_context.h"
  10. #include "android_webview/browser/aw_browser_terminator.h"
  11. #include "android_webview/browser/aw_content_browser_client.h"
  12. #include "android_webview/browser/aw_web_ui_controller_factory.h"
  13. #include "android_webview/browser/metrics/aw_metrics_service_accessor.h"
  14. #include "android_webview/browser/metrics/aw_metrics_service_client.h"
  15. #include "android_webview/browser/network_service/aw_network_change_notifier_factory.h"
  16. #include "android_webview/browser/tracing/background_tracing_field_trial.h"
  17. #include "android_webview/common/aw_descriptors.h"
  18. #include "android_webview/common/aw_paths.h"
  19. #include "android_webview/common/aw_resource.h"
  20. #include "android_webview/common/aw_switches.h"
  21. #include "android_webview/common/crash_reporter/aw_crash_reporter_client.h"
  22. #include "base/android/apk_assets.h"
  23. #include "base/android/build_info.h"
  24. #include "base/android/bundle_utils.h"
  25. #include "base/android/memory_pressure_listener_android.h"
  26. #include "base/base_paths_android.h"
  27. #include "base/callback_helpers.h"
  28. #include "base/command_line.h"
  29. #include "base/files/file_path.h"
  30. #include "base/files/file_util.h"
  31. #include "base/i18n/rtl.h"
  32. #include "base/logging.h"
  33. #include "base/message_loop/message_pump_type.h"
  34. #include "base/path_service.h"
  35. #include "base/task/current_thread.h"
  36. #include "components/crash/content/browser/child_exit_observer_android.h"
  37. #include "components/crash/core/common/crash_key.h"
  38. #include "components/embedder_support/android/metrics/memory_metrics_logger.h"
  39. #include "components/heap_profiling/multi_process/supervisor.h"
  40. #include "components/metrics/metrics_service.h"
  41. #include "components/services/heap_profiling/public/cpp/settings.h"
  42. #include "components/user_prefs/user_prefs.h"
  43. #include "components/variations/synthetic_trials.h"
  44. #include "components/variations/synthetic_trials_active_group_id_provider.h"
  45. #include "components/variations/variations_crash_keys.h"
  46. #include "components/variations/variations_ids_provider.h"
  47. #include "content/public/browser/android/synchronous_compositor.h"
  48. #include "content/public/browser/render_frame_host.h"
  49. #include "content/public/browser/render_process_host.h"
  50. #include "content/public/common/content_client.h"
  51. #include "content/public/common/content_switches.h"
  52. #include "content/public/common/result_codes.h"
  53. #include "net/android/network_change_notifier_factory_android.h"
  54. #include "net/base/network_change_notifier.h"
  55. #include "ui/base/l10n/l10n_util.h"
  56. #include "ui/base/layout.h"
  57. #include "ui/gl/gl_surface.h"
  58. namespace android_webview {
  59. AwBrowserMainParts::AwBrowserMainParts(AwContentBrowserClient* browser_client)
  60. : browser_client_(browser_client) {
  61. }
  62. AwBrowserMainParts::~AwBrowserMainParts() {
  63. }
  64. int AwBrowserMainParts::PreEarlyInitialization() {
  65. // Network change notifier factory must be singleton, only set factory
  66. // instance while it is not been created.
  67. // In most cases, this check is not necessary because SetFactory should be
  68. // called only once, but both webview and native cronet calls this function,
  69. // in case of building both webview and cronet to one app, it is required to
  70. // avoid crashing the app.
  71. if (!net::NetworkChangeNotifier::GetFactory()) {
  72. net::NetworkChangeNotifier::SetFactory(
  73. new AwNetworkChangeNotifierFactory());
  74. }
  75. // Creates a SingleThreadTaskExecutor for Android WebView if doesn't exist.
  76. DCHECK(!main_task_executor_.get());
  77. if (!base::CurrentThread::IsSet()) {
  78. main_task_executor_ = std::make_unique<base::SingleThreadTaskExecutor>(
  79. base::MessagePumpType::UI);
  80. }
  81. browser_process_ = std::make_unique<AwBrowserProcess>(
  82. browser_client_->aw_feature_list_creator());
  83. return content::RESULT_CODE_NORMAL_EXIT;
  84. }
  85. int AwBrowserMainParts::PreCreateThreads() {
  86. base::android::MemoryPressureListenerAndroid::Initialize(
  87. base::android::AttachCurrentThread());
  88. child_exit_observer_ =
  89. std::make_unique<::crash_reporter::ChildExitObserver>();
  90. // We need to create the safe browsing specific directory even if the
  91. // AwSafeBrowsingConfigHelper::GetSafeBrowsingEnabled() is false
  92. // initially, because safe browsing can be enabled later at runtime
  93. // on a per-webview basis.
  94. base::FilePath safe_browsing_dir;
  95. if (base::PathService::Get(android_webview::DIR_SAFE_BROWSING,
  96. &safe_browsing_dir)) {
  97. if (!base::PathExists(safe_browsing_dir))
  98. base::CreateDirectory(safe_browsing_dir);
  99. }
  100. base::FilePath crash_dir;
  101. if (base::PathService::Get(android_webview::DIR_CRASH_DUMPS, &crash_dir)) {
  102. if (!base::PathExists(crash_dir)) {
  103. base::CreateDirectory(crash_dir);
  104. }
  105. }
  106. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  107. switches::kWebViewSandboxedRenderer)) {
  108. // Create the renderers crash manager on the UI thread.
  109. child_exit_observer_->RegisterClient(
  110. std::make_unique<AwBrowserTerminator>());
  111. }
  112. crash_reporter::InitializeCrashKeys();
  113. variations::InitCrashKeys();
  114. RegisterSyntheticTrials();
  115. return content::RESULT_CODE_NORMAL_EXIT;
  116. }
  117. void AwBrowserMainParts::RegisterSyntheticTrials() {
  118. metrics::MetricsService* metrics =
  119. AwMetricsServiceClient::GetInstance()->GetMetricsService();
  120. metrics->GetSyntheticTrialRegistry()->AddSyntheticTrialObserver(
  121. variations::VariationsIdsProvider::GetInstance());
  122. metrics->GetSyntheticTrialRegistry()->AddSyntheticTrialObserver(
  123. variations::SyntheticTrialsActiveGroupIdProvider::GetInstance());
  124. static constexpr char kWebViewApkTypeTrial[] = "WebViewApkType";
  125. ApkType apk_type = AwBrowserProcess::GetApkType();
  126. std::string apk_type_string;
  127. switch (apk_type) {
  128. case ApkType::TRICHROME:
  129. apk_type_string = "Trichrome";
  130. break;
  131. case ApkType::MONOCHROME:
  132. apk_type_string = "Monochrome";
  133. break;
  134. case ApkType::STANDALONE:
  135. apk_type_string = "Standalone";
  136. break;
  137. }
  138. AwMetricsServiceAccessor::RegisterSyntheticFieldTrial(
  139. metrics, kWebViewApkTypeTrial, apk_type_string,
  140. variations::SyntheticTrialAnnotationMode::kNextLog);
  141. }
  142. int AwBrowserMainParts::PreMainMessageLoopRun() {
  143. TRACE_EVENT0("startup", "AwBrowserMainParts::PreMainMessageLoopRun");
  144. AwBrowserProcess::GetInstance()->PreMainMessageLoopRun();
  145. browser_client_->InitBrowserContext();
  146. content::WebUIControllerFactory::RegisterFactory(
  147. AwWebUIControllerFactory::GetInstance());
  148. content::RenderFrameHost::AllowInjectingJavaScript();
  149. metrics_logger_ = std::make_unique<metrics::MemoryMetricsLogger>();
  150. return content::RESULT_CODE_NORMAL_EXIT;
  151. }
  152. void AwBrowserMainParts::WillRunMainMessageLoop(
  153. std::unique_ptr<base::RunLoop>& run_loop) {
  154. NOTREACHED();
  155. }
  156. void AwBrowserMainParts::PostCreateThreads() {
  157. heap_profiling::Mode mode = heap_profiling::GetModeForStartup();
  158. if (mode != heap_profiling::Mode::kNone)
  159. heap_profiling::Supervisor::GetInstance()->Start(base::NullCallback());
  160. MaybeSetupSystemTracing();
  161. }
  162. } // namespace android_webview