chrome: Support CefResourceBundleHandler (see #3685)

This commit is contained in:
Marshall Greenblatt 2024-05-03 12:50:23 -04:00
parent 4e3668f93a
commit ca636bb96e
6 changed files with 169 additions and 21 deletions

View File

@ -12,6 +12,7 @@
#include "cef/libcef/common/app_manager.h"
#include "cef/libcef/common/chrome/chrome_content_client_cef.h"
#include "cef/libcef/common/main_runner_handler.h"
#include "cef/libcef/common/resource_bundle_delegate.h"
#include "cef/libcef/common/task_runner_manager.h"
#include "chrome/app/chrome_main_delegate.h"
@ -71,6 +72,11 @@ class ChromeMainDelegateCef : public ChromeMainDelegate,
scoped_refptr<base::SingleThreadTaskRunner> GetRenderTaskRunner() override;
scoped_refptr<base::SingleThreadTaskRunner> GetWebWorkerTaskRunner() override;
// ChromeMainDelegate overrides.
ui::ResourceBundle::Delegate* GetResourceBundleDelegate() override {
return &resource_bundle_delegate_;
}
private:
ChromeContentBrowserClientCef* content_browser_client() const;
ChromeContentRendererClientCef* content_renderer_client() const;
@ -81,6 +87,8 @@ class ChromeMainDelegateCef : public ChromeMainDelegate,
// We use this instead of ChromeMainDelegate::chrome_content_client_.
ChromeContentClientCef chrome_content_client_cef_;
CefResourceBundleDelegate resource_bundle_delegate_;
};
#endif // CEF_LIBCEF_COMMON_CHROME_CHROME_MAIN_DELEGATE_CEF_

View File

@ -1,24 +1,42 @@
#include "cef/libcef/common/resource_bundle_delegate.h"
#include "cef/libcef/common/app_manager.h"
#include "cef/libcef/features/runtime.h"
CefResourceBundleDelegate::CefResourceBundleDelegate() {
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
// Alloy bootstrap explicitly enables pack file loading in
// AlloyMainDelegate::InitializeResourceBundle, and it is otherwise disabled
// by default. Chrome bootstrap does not support this.
allow_pack_file_load_ = cef::IsChromeRuntimeEnabled();
#endif
}
base::FilePath CefResourceBundleDelegate::GetPathForResourcePack(
const base::FilePath& pack_path,
ui::ResourceScaleFactor scale_factor) {
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
// Only allow the cef pack file to load.
if (!pack_loading_disabled_ && allow_pack_file_load_) {
return pack_path;
}
return base::FilePath();
#else
return pack_path;
#endif
}
base::FilePath CefResourceBundleDelegate::GetPathForLocalePack(
const base::FilePath& pack_path,
const std::string& locale) {
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
if (!pack_loading_disabled_) {
return pack_path;
}
return base::FilePath();
#else
return pack_path;
#endif
}
gfx::Image CefResourceBundleDelegate::GetImageNamed(int resource_id) {
@ -63,7 +81,13 @@ bool CefResourceBundleDelegate::GetRawDataResource(
}
}
return (pack_loading_disabled_ || !value->empty());
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
if (pack_loading_disabled_) {
return true;
}
#endif
return !value->empty();
}
bool CefResourceBundleDelegate::GetLocalizedString(
@ -81,5 +105,11 @@ bool CefResourceBundleDelegate::GetLocalizedString(
}
}
return (pack_loading_disabled_ || !value->empty());
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
if (pack_loading_disabled_) {
return true;
}
#endif
return !value->empty();
}

View File

@ -7,18 +7,23 @@
#define CEF_LIBCEF_COMMON_RESOURCE_BUNDLE_DELEGATE_H_
#pragma once
#include "cef/libcef/features/features.h"
#include "ui/base/resource/resource_bundle.h"
class AlloyContentClient;
class CefResourceBundleDelegate : public ui::ResourceBundle::Delegate {
public:
CefResourceBundleDelegate() = default;
CefResourceBundleDelegate();
CefResourceBundleDelegate(const CefResourceBundleDelegate&) = delete;
CefResourceBundleDelegate& operator=(const CefResourceBundleDelegate&) =
delete;
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
void set_pack_loading_disabled(bool val) { pack_loading_disabled_ = val; }
bool pack_loading_disabled() const { return pack_loading_disabled_; }
void set_allow_pack_file_load(bool val) { allow_pack_file_load_ = val; }
bool allow_pack_file_load() const { return allow_pack_file_load_; }
#endif
private:
// ui::ResourceBundle::Delegate methods.
@ -38,9 +43,11 @@ class CefResourceBundleDelegate : public ui::ResourceBundle::Delegate {
std::string_view* value) const override;
bool GetLocalizedString(int message_id, std::u16string* value) const override;
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
private:
bool pack_loading_disabled_ = false;
bool allow_pack_file_load_ = false;
bool allow_pack_file_load_;
#endif
};
#endif // CEF_LIBCEF_COMMON_RESOURCE_BUNDLE_DELEGATE_H_

View File

@ -217,6 +217,9 @@ patches = [
#
# Apply dynamic light/dark theme changes to web content.
# https://issues.chromium.org/issues/332328864#comment3
#
# Pass ui::ResourceBundle::Delegate to InitSharedInstanceWithLocale.
# https://github.com/chromiumembedded/cef/issues/3685
'name': 'chrome_runtime',
},
{
@ -735,6 +738,9 @@ patches = [
#
# mac: Add fallback for unsupported --lang values.
# https://github.com/chromiumembedded/cef/issues/3653
#
# Pass ui::ResourceBundle::Delegate to InitSharedInstanceWithLocale.
# https://github.com/chromiumembedded/cef/issues/3685
'name': 'mac_chrome_locale_3623'
},
{

View File

@ -1,5 +1,5 @@
diff --git chrome/app/chrome_main_delegate.cc chrome/app/chrome_main_delegate.cc
index c09a805c4ed7b..de89b16690593 100644
index c09a805c4ed7b..3db1403d60ed5 100644
--- chrome/app/chrome_main_delegate.cc
+++ chrome/app/chrome_main_delegate.cc
@@ -37,6 +37,7 @@
@ -40,7 +40,17 @@ index c09a805c4ed7b..de89b16690593 100644
// In the case the process is not the singleton process, the uninstall tasks
// need to be executed here. A window will be displayed asking to close all
// running instances.
@@ -1069,7 +1077,8 @@ std::optional<int> ChromeMainDelegate::PostEarlyInitialization(
@@ -1050,7 +1058,8 @@ std::optional<int> ChromeMainDelegate::PostEarlyInitialization(
// Initializes the resource bundle and determines the locale.
std::string actual_locale = LoadLocalState(
- chrome_feature_list_creator, invoked_in_browser->is_running_test);
+ chrome_feature_list_creator, GetResourceBundleDelegate(),
+ invoked_in_browser->is_running_test);
chrome_feature_list_creator->SetApplicationLocale(actual_locale);
chrome_feature_list_creator->OverrideCachedUIStrings();
@@ -1069,7 +1078,8 @@ std::optional<int> ChromeMainDelegate::PostEarlyInitialization(
if (base::FeatureList::IsEnabled(
features::kWriteBasicSystemProfileToPersistentHistogramsFile)) {
@ -50,7 +60,7 @@ index c09a805c4ed7b..de89b16690593 100644
#if BUILDFLAG(IS_ANDROID)
record =
base::FeatureList::IsEnabled(chrome::android::kUmaBackgroundSessions);
@@ -1505,6 +1514,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
@@ -1505,6 +1515,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
std::string process_type =
command_line.GetSwitchValueASCII(switches::kProcessType);
@ -58,7 +68,7 @@ index c09a805c4ed7b..de89b16690593 100644
crash_reporter::InitializeCrashKeys();
#if BUILDFLAG(IS_CHROMEOS_LACROS)
@@ -1523,6 +1533,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
@@ -1523,6 +1534,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
InitMacCrashReporter(command_line, process_type);
SetUpInstallerPreferences(command_line);
#endif
@ -66,7 +76,17 @@ index c09a805c4ed7b..de89b16690593 100644
#if BUILDFLAG(IS_WIN)
child_process_logging::Init();
@@ -1733,6 +1744,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
@@ -1703,7 +1715,8 @@ void ChromeMainDelegate::PreSandboxStartup() {
#else
const std::string loaded_locale =
ui::ResourceBundle::InitSharedInstanceWithLocale(
- locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
+ locale, GetResourceBundleDelegate(),
+ ui::ResourceBundle::LOAD_COMMON_RESOURCES);
base::FilePath resources_pack_path;
base::PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
@@ -1733,6 +1746,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
}
@ -74,7 +94,7 @@ index c09a805c4ed7b..de89b16690593 100644
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
// Zygote needs to call InitCrashReporter() in RunZygote().
if (process_type != switches::kZygoteProcess) {
@@ -1768,6 +1780,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
@@ -1768,6 +1782,7 @@ void ChromeMainDelegate::PreSandboxStartup() {
// After all the platform Breakpads have been initialized, store the command
// line for crash reporting.
crash_keys::SetCrashKeysFromCommandLine(command_line);
@ -82,7 +102,7 @@ index c09a805c4ed7b..de89b16690593 100644
#if BUILDFLAG(ENABLE_PDF)
MaybePatchGdiGetFontData();
@@ -1893,6 +1906,7 @@ void ChromeMainDelegate::ZygoteForked() {
@@ -1893,6 +1908,7 @@ void ChromeMainDelegate::ZygoteForked() {
SetUpProfilingShutdownHandler();
}
@ -90,7 +110,7 @@ index c09a805c4ed7b..de89b16690593 100644
// Needs to be called after we have chrome::DIR_USER_DATA. BrowserMain sets
// this up for the browser process in a different manner.
const base::CommandLine* command_line =
@@ -1905,6 +1919,7 @@ void ChromeMainDelegate::ZygoteForked() {
@@ -1905,6 +1921,7 @@ void ChromeMainDelegate::ZygoteForked() {
// Reset the command line for the newly spawned process.
crash_keys::SetCrashKeysFromCommandLine(*command_line);
@ -98,7 +118,7 @@ index c09a805c4ed7b..de89b16690593 100644
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
@@ -2001,6 +2016,7 @@ void ChromeMainDelegate::InitializeMemorySystem() {
@@ -2001,6 +2018,7 @@ void ChromeMainDelegate::InitializeMemorySystem() {
const bool is_browser_process = process_type.empty();
const bool gwp_asan_boost_sampling = is_browser_process || IsCanaryDev();
@ -106,7 +126,7 @@ index c09a805c4ed7b..de89b16690593 100644
memory_system::Initializer()
.SetGwpAsanParameters(gwp_asan_boost_sampling, process_type)
.SetProfilingClientParameters(chrome::GetChannel(),
@@ -2010,5 +2026,5 @@ void ChromeMainDelegate::InitializeMemorySystem() {
@@ -2010,5 +2028,5 @@ void ChromeMainDelegate::InitializeMemorySystem() {
memory_system::DispatcherParameters::
AllocationTraceRecorderInclusion::kDynamic,
process_type)
@ -114,10 +134,18 @@ index c09a805c4ed7b..de89b16690593 100644
+ .Initialize(*memory_system_);
}
diff --git chrome/app/chrome_main_delegate.h chrome/app/chrome_main_delegate.h
index 3553377e96017..5207128b768f2 100644
index 3553377e96017..9f6edc70ef1d4 100644
--- chrome/app/chrome_main_delegate.h
+++ chrome/app/chrome_main_delegate.h
@@ -49,6 +49,8 @@ class ChromeMainDelegate : public content::ContentMainDelegate {
@@ -16,6 +16,7 @@
#include "chrome/common/chrome_content_client.h"
#include "components/memory_system/memory_system.h"
#include "content/public/app/content_main_delegate.h"
+#include "ui/base/resource/resource_bundle.h"
namespace base {
class CommandLine;
@@ -49,6 +50,8 @@ class ChromeMainDelegate : public content::ContentMainDelegate {
~ChromeMainDelegate() override;
@ -126,7 +154,17 @@ index 3553377e96017..5207128b768f2 100644
protected:
// content::ContentMainDelegate:
std::optional<int> BasicStartupComplete() override;
@@ -98,7 +100,7 @@ class ChromeMainDelegate : public content::ContentMainDelegate {
@@ -92,13 +95,17 @@ class ChromeMainDelegate : public content::ContentMainDelegate {
void InitializeMemorySystem();
+ virtual ui::ResourceBundle::Delegate* GetResourceBundleDelegate() {
+ return nullptr;
+ }
+
std::unique_ptr<ChromeContentBrowserClient> chrome_content_browser_client_;
std::unique_ptr<ChromeContentUtilityClient> chrome_content_utility_client_;
std::unique_ptr<tracing::TracingSamplerProfiler> tracing_sampler_profiler_;
ChromeContentClient chrome_content_client_;

View File

@ -1,8 +1,21 @@
diff --git chrome/browser/chrome_resource_bundle_helper.cc chrome/browser/chrome_resource_bundle_helper.cc
index 0cfc966050b60..8268a8b1f4fcb 100644
index 0cfc966050b60..bbc20cffaee1a 100644
--- chrome/browser/chrome_resource_bundle_helper.cc
+++ chrome/browser/chrome_resource_bundle_helper.cc
@@ -82,16 +82,8 @@ std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
@@ -68,8 +68,10 @@ extern void InitializeLocalState(
// Initializes the shared instance of ResourceBundle and returns the application
// locale. An empty |actual_locale| value indicates failure.
-std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
- bool is_running_tests) {
+std::string InitResourceBundleAndDetermineLocale(
+ PrefService* local_state,
+ ui::ResourceBundle::Delegate* resource_bundle_delegate,
+ bool is_running_tests) {
#if BUILDFLAG(IS_ANDROID)
// In order for SetLoadSecondaryLocalePaks() to work ResourceBundle must
// not have been created yet.
@@ -82,16 +84,8 @@ std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
.empty());
#endif
@ -20,6 +33,52 @@ index 0cfc966050b60..8268a8b1f4fcb 100644
#if BUILDFLAG(IS_CHROMEOS_ASH)
ui::ResourceBundle::SetLottieParsingFunctions(
@@ -103,7 +97,8 @@ std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
// On a POSIX OS other than ChromeOS, the parameter that is passed to the
// method InitSharedInstance is ignored.
std::string actual_locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
- preferred_locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
+ preferred_locale, resource_bundle_delegate,
+ ui::ResourceBundle::LOAD_COMMON_RESOURCES);
CHECK(!actual_locale.empty())
<< "Locale could not be found for " << preferred_locale;
@@ -155,6 +150,7 @@ std::string InitResourceBundleAndDetermineLocale(PrefService* local_state,
std::string LoadLocalState(
ChromeFeatureListCreator* chrome_feature_list_creator,
+ ui::ResourceBundle::Delegate* resource_bundle_delegate,
bool is_running_tests) {
base::FilePath user_data_dir;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
@@ -166,5 +162,6 @@ std::string LoadLocalState(
new ChromeCommandLinePrefStore(base::CommandLine::ForCurrentProcess()));
return InitResourceBundleAndDetermineLocale(
- chrome_feature_list_creator->local_state(), is_running_tests);
+ chrome_feature_list_creator->local_state(), resource_bundle_delegate,
+ is_running_tests);
}
diff --git chrome/browser/chrome_resource_bundle_helper.h chrome/browser/chrome_resource_bundle_helper.h
index 0b22e445bc3ff..1d7c6b319ba4b 100644
--- chrome/browser/chrome_resource_bundle_helper.h
+++ chrome/browser/chrome_resource_bundle_helper.h
@@ -7,12 +7,15 @@
#include <string>
+#include "ui/base/resource/resource_bundle.h"
+
class ChromeFeatureListCreator;
// Loads the local state, and returns the application locale. An empty return
// value indicates the ResouceBundle couldn't be loaded.
std::string LoadLocalState(
ChromeFeatureListCreator* chrome_feature_list_creator,
+ ui::ResourceBundle::Delegate* resource_bundle_delegate,
bool is_running_tests);
#endif // CHROME_BROWSER_CHROME_RESOURCE_BUNDLE_HELPER_H_
diff --git components/language/core/browser/locale_util.cc components/language/core/browser/locale_util.cc
index aa43742055b04..e84f21ab963cc 100644
--- components/language/core/browser/locale_util.cc