chrome_resource_bundle_helper.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "chrome/browser/chrome_resource_bundle_helper.h"
  5. #include "base/command_line.h"
  6. #include "base/files/file_util.h"
  7. #include "base/path_service.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/trace_event/trace_event.h"
  10. #include "build/build_config.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "chrome/browser/metrics/chrome_feature_list_creator.h"
  13. #include "chrome/browser/prefs/chrome_command_line_pref_store.h"
  14. #include "chrome/browser/prefs/chrome_pref_service_factory.h"
  15. #include "chrome/common/chrome_paths.h"
  16. #include "chrome/common/chrome_switches.h"
  17. #include "chrome/installer/util/google_update_settings.h"
  18. #include "components/language/core/browser/pref_names.h"
  19. #include "components/prefs/pref_registry_simple.h"
  20. #include "components/prefs/pref_service.h"
  21. #include "extensions/buildflags/buildflags.h"
  22. #include "ui/base/resource/resource_bundle.h"
  23. #if BUILDFLAG(IS_ANDROID)
  24. #include "ui/base/resource/resource_bundle_android.h"
  25. #endif
  26. #if BUILDFLAG(IS_CHROMEOS_ASH)
  27. #include "ash/constants/ash_switches.h"
  28. #include "chrome/common/pref_names.h"
  29. #include "ui/lottie/resource.h" // nogncheck
  30. #endif
  31. #if BUILDFLAG(IS_CHROMEOS_LACROS)
  32. #include "ui/base/ui_base_switches.h"
  33. #endif
  34. #if BUILDFLAG(ENABLE_EXTENSIONS)
  35. #include "extensions/common/extension_l10n_util.h"
  36. #endif
  37. namespace {
  38. extern void InitializeLocalState(
  39. ChromeFeatureListCreator* chrome_feature_list_creator) {
  40. TRACE_EVENT0("startup", "ChromeBrowserMainParts::InitializeLocalState");
  41. #if BUILDFLAG(IS_CHROMEOS_ASH)
  42. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  43. if (command_line->HasSwitch(ash::switches::kLoginManager)) {
  44. PrefService* local_state = chrome_feature_list_creator->local_state();
  45. DCHECK(local_state);
  46. std::string owner_locale = local_state->GetString(prefs::kOwnerLocale);
  47. // Ensure that we start with owner's locale.
  48. if (!owner_locale.empty() &&
  49. local_state->GetString(language::prefs::kApplicationLocale) !=
  50. owner_locale &&
  51. !local_state->IsManagedPreference(
  52. language::prefs::kApplicationLocale)) {
  53. local_state->SetString(language::prefs::kApplicationLocale, owner_locale);
  54. }
  55. }
  56. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  57. }
  58. // Initializes the shared instance of ResourceBundle and returns the application
  59. // locale. An empty |actual_locale| value indicates failure.
  60. std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
  61. bool is_running_tests) {
  62. #if BUILDFLAG(IS_ANDROID)
  63. // In order for SetLoadSecondaryLocalePaks() to work ResourceBundle must
  64. // not have been created yet.
  65. DCHECK(!ui::ResourceBundle::HasSharedInstance());
  66. // Auto-detect based on en-US whether secondary locale .pak files exist.
  67. bool in_split = false;
  68. bool log_error = false;
  69. ui::SetLoadSecondaryLocalePaks(
  70. !ui::GetPathForAndroidLocalePakWithinApk("en-US", in_split, log_error)
  71. .empty());
  72. #endif
  73. std::string preferred_locale;
  74. #if BUILDFLAG(IS_MAC)
  75. // TODO(markusheintz): Read preference pref::kApplicationLocale in order
  76. // to enforce the application locale.
  77. // Tests always get en-US.
  78. preferred_locale = is_running_tests ? "en-US" : std::string();
  79. #else
  80. preferred_locale =
  81. local_state->GetString(language::prefs::kApplicationLocale);
  82. #endif
  83. #if BUILDFLAG(IS_CHROMEOS_ASH)
  84. ui::ResourceBundle::SetLottieParsingFunctions(
  85. &lottie::ParseLottieAsStillImage, &lottie::ParseLottieAsThemedStillImage);
  86. #endif
  87. TRACE_EVENT0("startup",
  88. "ChromeBrowserMainParts::InitResourceBundleAndDetermineLocale");
  89. // On a POSIX OS other than ChromeOS, the parameter that is passed to the
  90. // method InitSharedInstance is ignored.
  91. std::string actual_locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
  92. preferred_locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  93. CHECK(!actual_locale.empty())
  94. << "Locale could not be found for " << preferred_locale;
  95. // First run prefs needs data from the ResourceBundle, so load it now.
  96. {
  97. TRACE_EVENT0("startup",
  98. "ChromeBrowserMainParts::InitResourceBundleAndDetermineLocale:"
  99. ":AddDataPack");
  100. base::FilePath resources_pack_path;
  101. base::PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
  102. #if BUILDFLAG(IS_ANDROID)
  103. ui::LoadMainAndroidPackFile("assets/resources.pak", resources_pack_path);
  104. // Avoid loading DFM native resources here, to keep startup lean. These
  105. // resources are loaded on-use, when an already-installed DFM loads.
  106. #elif BUILDFLAG(IS_CHROMEOS_LACROS)
  107. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  108. if (command_line->HasSwitch(switches::kEnableResourcesFileSharing)) {
  109. // If LacrosResourcesFileSharing feature is enabled, Lacros refers to ash
  110. // resources pak file.
  111. base::FilePath ash_resources_pack_path;
  112. base::PathService::Get(chrome::FILE_ASH_RESOURCES_PACK,
  113. &ash_resources_pack_path);
  114. base::FilePath shared_resources_pack_path;
  115. base::PathService::Get(chrome::FILE_RESOURCES_FOR_SHARING_PACK,
  116. &shared_resources_pack_path);
  117. ui::ResourceBundle::GetSharedInstance()
  118. .AddDataPackFromPathWithAshResources(
  119. shared_resources_pack_path, ash_resources_pack_path,
  120. resources_pack_path, ui::kScaleFactorNone);
  121. } else {
  122. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  123. resources_pack_path, ui::kScaleFactorNone);
  124. }
  125. #else
  126. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  127. resources_pack_path, ui::kScaleFactorNone);
  128. #endif // BUILDFLAG(IS_ANDROID)
  129. }
  130. #if BUILDFLAG(ENABLE_EXTENSIONS)
  131. extension_l10n_util::SetProcessLocale(actual_locale);
  132. extension_l10n_util::SetPreferredLocale(preferred_locale);
  133. #endif
  134. return actual_locale;
  135. }
  136. } // namespace
  137. std::string LoadLocalState(
  138. ChromeFeatureListCreator* chrome_feature_list_creator,
  139. bool is_running_tests) {
  140. base::FilePath user_data_dir;
  141. if (!base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
  142. return std::string();
  143. InitializeLocalState(chrome_feature_list_creator);
  144. chrome_feature_list_creator->local_state()->UpdateCommandLinePrefStore(
  145. new ChromeCommandLinePrefStore(base::CommandLine::ForCurrentProcess()));
  146. return InitResourceBundleAndDetermineLocale(
  147. chrome_feature_list_creator->local_state(), is_running_tests);
  148. }