webview_apk_process.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021 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/nonembedded/webview_apk_process.h"
  5. #include "android_webview/common/aw_paths.h"
  6. #include "base/android/library_loader/library_loader_hooks.h"
  7. #include "base/base_paths_android.h"
  8. #include "base/message_loop/message_pump_type.h"
  9. #include "base/path_service.h"
  10. #include "base/task/single_thread_task_executor.h"
  11. #include "base/task/thread_pool/thread_pool_instance.h"
  12. #include "components/component_updater/component_updater_paths.h"
  13. #include "components/prefs/json_pref_store.h"
  14. #include "components/prefs/pref_registry_simple.h"
  15. #include "components/prefs/pref_service.h"
  16. #include "components/prefs/pref_service_factory.h"
  17. #include "components/update_client/update_client.h"
  18. namespace android_webview {
  19. namespace {
  20. static WebViewApkProcess* g_webview_apk_process = nullptr;
  21. } // namespace
  22. // static
  23. WebViewApkProcess* WebViewApkProcess::GetInstance() {
  24. DCHECK(g_webview_apk_process);
  25. return g_webview_apk_process;
  26. }
  27. // static
  28. // Must be called exactly once during the process startup.
  29. void WebViewApkProcess::Init() {
  30. // TODO(crbug.com/1179303): Add check to assert this is only loaded by
  31. // LibraryProcessType PROCESS_WEBVIEW_NONEMBEDDED.
  32. // This doesn't have to be thread safe, because it should only happen once on
  33. // the main thread before any GetInstances calls are made.
  34. DCHECK(!g_webview_apk_process);
  35. g_webview_apk_process = new WebViewApkProcess();
  36. }
  37. WebViewApkProcess::WebViewApkProcess() {
  38. base::ThreadPoolInstance::CreateAndStartWithDefaultParams(
  39. "WebViewApkProcess");
  40. // There's no UI message pump in nonembedded WebView, using
  41. // `base::MessagePumpType::JAVA` so that the `SingleThreadExecutor` will bind
  42. // to the java thread the `WebViewApkProcess` is created on.
  43. main_task_executor_ = std::make_unique<base::SingleThreadTaskExecutor>(
  44. base::MessagePumpType::JAVA);
  45. RegisterPathProvider();
  46. component_updater::RegisterPathProvider(
  47. /*components_system_root_key=*/android_webview::DIR_COMPONENTS_ROOT,
  48. /*components_system_root_key_alt=*/android_webview::DIR_COMPONENTS_ROOT,
  49. /*components_user_root_key=*/android_webview::DIR_COMPONENTS_ROOT);
  50. CreatePrefService();
  51. }
  52. WebViewApkProcess::~WebViewApkProcess() = default;
  53. PrefService* WebViewApkProcess::GetPrefService() const {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. return pref_service_.get();
  56. }
  57. void WebViewApkProcess::CreatePrefService() {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
  60. PrefServiceFactory pref_service_factory;
  61. RegisterPrefs(pref_registry.get());
  62. base::FilePath app_data_dir;
  63. base::PathService::Get(base::DIR_ANDROID_APP_DATA, &app_data_dir);
  64. pref_service_factory.set_user_prefs(
  65. base::MakeRefCounted<JsonPrefStore>(app_data_dir.Append(
  66. FILE_PATH_LITERAL("WebView Nonembedded Preferences"))));
  67. pref_service_ = pref_service_factory.Create(pref_registry);
  68. }
  69. void WebViewApkProcess::RegisterPrefs(PrefRegistrySimple* pref_registry) {
  70. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  71. update_client::RegisterPrefs(pref_registry);
  72. }
  73. } // namespace android_webview