aw_resource_bundle.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 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/common/aw_resource_bundle.h"
  5. #include "android_webview/common/aw_descriptors.h"
  6. #include "base/android/locale_utils.h"
  7. #include "base/files/file.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/memory_mapped_file.h"
  10. #include "base/i18n/rtl.h"
  11. #include "base/logging.h"
  12. #include "base/path_service.h"
  13. #include "base/posix/global_descriptors.h"
  14. #include "base/trace_event/trace_event.h"
  15. #include "ui/base/resource/resource_bundle.h"
  16. #include "ui/base/resource/resource_bundle_android.h"
  17. #include "ui/base/ui_base_paths.h"
  18. namespace android_webview {
  19. void InitIcuAndResourceBundleBrowserSide() {
  20. TRACE_EVENT0("startup", "InitIcuAndResourceBundleBrowserSide");
  21. ui::SetLocalePaksStoredInApk(true);
  22. std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
  23. base::android::GetDefaultLocaleString(), NULL,
  24. ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  25. if (locale.empty()) {
  26. LOG(WARNING) << "Failed to load locale .pak from apk.";
  27. }
  28. base::i18n::SetICUDefaultLocale(locale);
  29. // Try to directly mmap the resources.pak from the apk. Fall back to load
  30. // from file, using PATH_SERVICE, otherwise.
  31. base::FilePath pak_file_path;
  32. base::PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_file_path);
  33. pak_file_path = pak_file_path.AppendASCII("resources.pak");
  34. ui::LoadMainAndroidPackFile("assets/resources.pak", pak_file_path);
  35. }
  36. void InitResourceBundleRendererSide() {
  37. auto* global_descriptors = base::GlobalDescriptors::GetInstance();
  38. int pak_fd = global_descriptors->Get(kAndroidWebViewLocalePakDescriptor);
  39. base::MemoryMappedFile::Region pak_region =
  40. global_descriptors->GetRegion(kAndroidWebViewLocalePakDescriptor);
  41. ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
  42. pak_region);
  43. std::pair<int, ui::ResourceScaleFactor> extra_paks[] = {
  44. {kAndroidWebViewMainPakDescriptor, ui::kScaleFactorNone},
  45. {kAndroidWebView100PercentPakDescriptor, ui::k100Percent}};
  46. for (const auto& pak_info : extra_paks) {
  47. pak_fd = global_descriptors->Get(pak_info.first);
  48. pak_region = global_descriptors->GetRegion(pak_info.first);
  49. ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
  50. base::File(pak_fd), pak_region, pak_info.second);
  51. }
  52. }
  53. } // namespace android_webview