Commit 6bfc721c authored by Wez's avatar Wez Committed by Chromium LUCI CQ

[fuchsia] Provide system locale to web content & network stack.

ICU is configured with the currently-configured locale in each WebEngine
process when it starts up.

In the browser process the locale is monitored and changes applied
at run-time, to ensure that e.g. accept-language headers are up to date.

The locale is passed by the browser process to some child processes
(e.g. renderers) when they are started. Renderers in particular cannot
have their locale re-configured dynamically once started.

Bug: 1156404, 1157244
Bug: b/168161909
Change-Id: Id7ff526f1d371b103632c7a284bd16fa694654a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2595368
Commit-Queue: Wez <wez@chromium.org>
Auto-Submit: Wez <wez@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842064}
parent 66e21ab3
......@@ -11,6 +11,7 @@
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/i18n/rtl.h"
#include "base/path_service.h"
#include "base/system/sys_info.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
......@@ -23,6 +24,7 @@
#include "fuchsia/engine/switches.h"
#include "media/capabilities/in_memory_video_decode_stats_db_impl.h"
#include "media/mojo/services/video_decode_perf_history.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/network_switches.h"
namespace {
......@@ -191,6 +193,10 @@ WebEngineBrowserContext::GetVideoDecodePerfHistory() {
return BrowserContext::GetVideoDecodePerfHistory();
}
std::string WebEngineBrowserContext::GetPreferredLanguages() const {
return net::HttpUtil::ExpandLanguageList(base::i18n::GetConfiguredLocale());
}
media::VideoDecodePerfHistory*
WebEngineBrowserContext::GetInMemoryVideoDecodePerfHistory() {
constexpr char kUserDataKeyName[] = "video-decode-perf-history";
......
......@@ -44,6 +44,12 @@ class WebEngineBrowserContext : public content::BrowserContext {
override;
media::VideoDecodePerfHistory* GetVideoDecodePerfHistory() override;
// Returns a comma-separated list of language codes, in preference order.
// This is suitable for direct use setting the "sec-ch-lang" header, or
// passed to net::HttpUtil::GenerateAcceptLanguageHeader() to generate a
// legacy "accept-language" header value.
std::string GetPreferredLanguages() const;
private:
// Contains URLRequestContextGetter required for resource loading.
class ResourceContext;
......
......@@ -11,11 +11,14 @@
#include "base/command_line.h"
#include "base/files/important_file_writer_cleaner.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/intl_profile_watcher.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/histogram_fetcher.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/main_function_params.h"
#include "fuchsia/base/legacymetrics_client.h"
#include "fuchsia/engine/browser/context_impl.h"
......@@ -24,6 +27,7 @@
#include "fuchsia/engine/browser/web_engine_devtools_controller.h"
#include "fuchsia/engine/switches.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "ui/aura/screen_ozone.h"
#include "ui/gfx/switches.h"
#include "ui/ozone/public/ozone_platform.h"
......@@ -68,6 +72,11 @@ void WebEngineBrowserMainParts::PostEarlyInitialization() {
void WebEngineBrowserMainParts::PreMainMessageLoopRun() {
DCHECK(!screen_);
// Watch for changes to the user's locale setting.
intl_profile_watcher_ = std::make_unique<base::FuchsiaIntlProfileWatcher>(
base::BindRepeating(&WebEngineBrowserMainParts::OnIntlProfileChanged,
base::Unretained(this)));
screen_ = std::make_unique<aura::ScreenOzone>();
display::Screen::SetScreenInstance(screen_.get());
......@@ -161,6 +170,21 @@ void WebEngineBrowserMainParts::PostMainMessageLoopRun() {
context_binding_.reset();
browser_context_.reset();
screen_.reset();
intl_profile_watcher_.reset();
base::ImportantFileWriterCleaner::GetInstance().Stop();
}
void WebEngineBrowserMainParts::OnIntlProfileChanged(
const fuchsia::intl::Profile& profile) {
// Configure the ICU library in this process with the new primary locale.
std::string primary_locale =
base::FuchsiaIntlProfileWatcher::GetPrimaryLocaleIdFromProfile(profile);
base::i18n::SetICUDefaultLocale(primary_locale);
// Reconfigure the network process.
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
->GetNetworkContext()
->SetAcceptLanguage(net::HttpUtil::GenerateAcceptLanguageHeader(
browser_context_->GetPreferredLanguages()));
}
......@@ -8,6 +8,7 @@
#include <fuchsia/web/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/optional.h"
......@@ -15,6 +16,10 @@
#include "fuchsia/engine/browser/context_impl.h"
#include "fuchsia/engine/browser/web_engine_browser_context.h"
namespace base {
class FuchsiaIntlProfileWatcher;
}
namespace display {
class Screen;
}
......@@ -56,6 +61,8 @@ class WebEngineBrowserMainParts : public content::BrowserMainParts {
ContextImpl* context_for_test() const { return context_service_.get(); }
private:
void OnIntlProfileChanged(const fuchsia::intl::Profile& profile);
const content::MainFunctionParams& parameters_;
fidl::InterfaceRequest<fuchsia::web::Context> request_;
......@@ -69,6 +76,9 @@ class WebEngineBrowserMainParts : public content::BrowserMainParts {
std::unique_ptr<MediaResourceProviderService>
media_resource_provider_service_;
// Used to respond to changes to the system's current locale.
std::unique_ptr<base::FuchsiaIntlProfileWatcher> intl_profile_watcher_;
bool run_message_loop_ = true;
base::OnceClosure quit_closure_;
......
......@@ -8,6 +8,7 @@
#include <string>
#include <utility>
#include "base/i18n/rtl.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "components/policy/content/safe_sites_navigation_throttle.h"
......@@ -186,6 +187,18 @@ void WebEngineContentBrowserClient::AppendExtraCommandLineSwitches(
kSwitchesToCopy, base::size(kSwitchesToCopy));
}
std::string WebEngineContentBrowserClient::GetApplicationLocale() {
// ICU is configured with the system locale by WebEngineBrowserMainParts.
return base::i18n::GetConfiguredLocale();
}
std::string WebEngineContentBrowserClient::GetAcceptLangs(
content::BrowserContext* context) {
DCHECK_EQ(main_parts_->browser_context(), context);
return static_cast<WebEngineBrowserContext*>(context)
->GetPreferredLanguages();
}
std::vector<std::unique_ptr<content::NavigationThrottle>>
WebEngineContentBrowserClient::CreateThrottlesForNavigation(
content::NavigationHandle* navigation_handle) {
......@@ -238,9 +251,10 @@ void WebEngineContentBrowserClient::ConfigureNetworkContextParams(
const base::FilePath& relative_partition_path,
network::mojom::NetworkContextParams* network_context_params,
network::mojom::CertVerifierCreationParams* cert_verifier_creation_params) {
// Same as ContentBrowserClient::ConfigureNetworkContextParams().
network_context_params->user_agent = GetUserAgent();
network_context_params->accept_language = "en-us,en";
network_context_params
->accept_language = net::HttpUtil::GenerateAcceptLanguageHeader(
static_cast<WebEngineBrowserContext*>(context)->GetPreferredLanguages());
// Set the list of cors_exempt_headers which may be specified in a URLRequest,
// starting with the headers passed in via
......
......@@ -50,9 +50,11 @@ class WebEngineContentBrowserClient : public content::ContentBrowserClient {
bool ShouldEnableStrictSiteIsolation() final;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) final;
std::string GetApplicationLocale() final;
std::string GetAcceptLangs(content::BrowserContext* context) final;
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(
content::NavigationHandle* navigation_handle) override;
content::NavigationHandle* navigation_handle) final;
std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
CreateURLLoaderThrottles(
const network::ResourceRequest& request,
......@@ -66,7 +68,7 @@ class WebEngineContentBrowserClient : public content::ContentBrowserClient {
const base::FilePath& relative_partition_path,
network::mojom::NetworkContextParams* network_context_params,
network::mojom::CertVerifierCreationParams* cert_verifier_creation_params)
override;
final;
private:
fidl::InterfaceRequest<fuchsia::web::Context> request_;
......
......@@ -10,6 +10,7 @@
#include "base/base_paths_fuchsia.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/fuchsia/intl_profile_watcher.h"
#include "base/path_service.h"
#include "content/public/common/content_switches.h"
#include "fuchsia/base/init_logging.h"
......@@ -85,6 +86,15 @@ bool WebEngineMainDelegate::BasicStartupComplete(int* exit_code) {
}
void WebEngineMainDelegate::PreSandboxStartup() {
// Early during startup, configure the process with the primary locale and
// load resources. If locale-specific resources are loaded then they must be
// explicitly reloaded after each change to the primary locale.
// In the browser process the locale determines the accept-language header
// contents, and is supplied to renderers for Blink to report to web content.
std::string initial_locale =
base::FuchsiaIntlProfileWatcher::GetPrimaryLocaleIdForInitialization();
base::i18n::SetICUDefaultLocale(initial_locale);
InitializeResources();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment