REVIEW: access GetApplicationLocale on sequence that allows IO

This commit is contained in:
deepak1556 2017-12-16 14:51:29 +05:30 committed by Cheng Zhao
parent 1912fbb073
commit a1592446da
13 changed files with 70 additions and 19 deletions

View file

@ -842,7 +842,7 @@ void App::SetDesktopName(const std::string& desktop_name) {
} }
std::string App::GetLocale() { std::string App::GetLocale() {
return l10n_util::GetApplicationLocale(""); return g_browser_process->GetApplicationLocale();
} }
bool App::MakeSingleInstance( bool App::MakeSingleInstance(

View file

@ -126,10 +126,13 @@ net::HttpAuth::Scheme GetAuthSchemeFromString(const std::string& scheme) {
void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter, void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter,
const std::string& accept_lang, const std::string& accept_lang,
const std::string& user_agent) { const std::string& user_agent) {
std::string accept_lang_header = net::HttpUtil::GenerateAcceptLanguageHeader(
accept_lang.empty() ? getter->GetURLRequestContext()
->http_user_agent_settings()
->GetAcceptLanguage()
: accept_lang);
getter->GetURLRequestContext()->set_http_user_agent_settings( getter->GetURLRequestContext()->set_http_user_agent_settings(
new net::StaticHttpUserAgentSettings( new net::StaticHttpUserAgentSettings(accept_lang_header, user_agent));
net::HttpUtil::GenerateAcceptLanguageHeader(accept_lang),
user_agent));
} }
} // namespace } // namespace
@ -642,7 +645,7 @@ void Session::SetUserAgent(const std::string& user_agent,
mate::Arguments* args) { mate::Arguments* args) {
browser_context_->SetUserAgent(user_agent); browser_context_->SetUserAgent(user_agent);
std::string accept_lang = l10n_util::GetApplicationLocale(""); std::string accept_lang;
args->GetNext(&accept_lang); args->GetNext(&accept_lang);
scoped_refptr<brightray::URLRequestContextGetter> getter( scoped_refptr<brightray::URLRequestContextGetter> getter(

View file

@ -202,10 +202,6 @@ void AtomBrowserClient::OverrideWebkitPrefs(
WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs); WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs);
} }
std::string AtomBrowserClient::GetApplicationLocale() {
return l10n_util::GetApplicationLocale("");
}
void AtomBrowserClient::OverrideSiteInstanceForNavigation( void AtomBrowserClient::OverrideSiteInstanceForNavigation(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,

View file

@ -53,7 +53,6 @@ class AtomBrowserClient : public brightray::BrowserClient,
CreateSpeechRecognitionManagerDelegate() override; CreateSpeechRecognitionManagerDelegate() override;
void OverrideWebkitPrefs(content::RenderViewHost* render_view_host, void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
content::WebPreferences* prefs) override; content::WebPreferences* prefs) override;
std::string GetApplicationLocale() override;
void OverrideSiteInstanceForNavigation( void OverrideSiteInstanceForNavigation(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,

View file

@ -24,6 +24,7 @@
#include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/child_process_security_policy.h"
#include "device/geolocation/geolocation_delegate.h" #include "device/geolocation/geolocation_delegate.h"
#include "device/geolocation/geolocation_provider.h" #include "device/geolocation/geolocation_provider.h"
#include "ui/base/l10n/l10n_util.h"
#include "v8/include/v8-debug.h" #include "v8/include/v8-debug.h"
#if defined(USE_X11) #if defined(USE_X11)
@ -129,6 +130,12 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
JavascriptEnvironment::Initialize(); JavascriptEnvironment::Initialize();
} }
int AtomBrowserMainParts::PreCreateThreads() {
fake_browser_process_->SetApplicationLocale(
l10n_util::GetApplicationLocale(""));
return brightray::BrowserMainParts::PreCreateThreads();
}
void AtomBrowserMainParts::PreMainMessageLoopRun() { void AtomBrowserMainParts::PreMainMessageLoopRun() {
js_env_->OnMessageLoopCreated(); js_env_->OnMessageLoopCreated();

View file

@ -51,6 +51,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
// content::BrowserMainParts: // content::BrowserMainParts:
void PreEarlyInitialization() override; void PreEarlyInitialization() override;
void PostEarlyInitialization() override; void PostEarlyInitialization() override;
int PreCreateThreads() override;
void PreMainMessageLoopRun() override; void PreMainMessageLoopRun() override;
bool MainMessageLoopRun(int* result_code) override; bool MainMessageLoopRun(int* result_code) override;
void PostMainMessageLoopStart() override; void PostMainMessageLoopStart() override;

View file

@ -8,6 +8,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "content/public/browser/stream_handle.h" #include "content/public/browser/stream_handle.h"
#include "content/public/browser/stream_info.h" #include "content/public/browser/stream_info.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
@ -193,7 +194,7 @@ void PdfViewerHandler::GetStrings(const base::ListValue* args) {
SET_STRING("tooltipZoomOut", "Zoom out"); SET_STRING("tooltipZoomOut", "Zoom out");
#undef SET_STRING #undef SET_STRING
webui::SetLoadTimeDataDefaults(l10n_util::GetApplicationLocale(""), webui::SetLoadTimeDataDefaults(g_browser_process->GetApplicationLocale(),
result.get()); result.get());
ResolveJavascriptCallback(*callback_id, *result); ResolveJavascriptCallback(*callback_id, *result);
} }

View file

@ -4,6 +4,7 @@
#include "brightray/browser/browser_client.h" #include "brightray/browser/browser_client.h"
#include "base/lazy_instance.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "brightray/browser/browser_context.h" #include "brightray/browser/browser_context.h"
#include "brightray/browser/browser_main_parts.h" #include "brightray/browser/browser_main_parts.h"
@ -11,16 +12,41 @@
#include "brightray/browser/media/media_capture_devices_dispatcher.h" #include "brightray/browser/media/media_capture_devices_dispatcher.h"
#include "brightray/browser/notification_presenter.h" #include "brightray/browser/notification_presenter.h"
#include "brightray/browser/platform_notification_service.h" #include "brightray/browser/platform_notification_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
using content::BrowserThread;
namespace brightray { namespace brightray {
namespace { namespace {
BrowserClient* g_browser_client; BrowserClient* g_browser_client;
base::LazyInstance<std::string>::DestructorAtExit
g_io_thread_application_locale = LAZY_INSTANCE_INITIALIZER;
std::string g_application_locale;
void SetApplicationLocaleOnIOThread(const std::string& locale) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
g_io_thread_application_locale.Get() = locale;
}
} // namespace } // namespace
// static
void BrowserClient::SetApplicationLocale(const std::string& locale) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&SetApplicationLocaleOnIOThread, locale))) {
g_io_thread_application_locale.Get() = locale;
}
g_application_locale = locale;
}
BrowserClient* BrowserClient::Get() { BrowserClient* BrowserClient::Get() {
return g_browser_client; return g_browser_client;
} }
@ -88,4 +114,10 @@ content::DevToolsManagerDelegate* BrowserClient::GetDevToolsManagerDelegate() {
return new DevToolsManagerDelegate; return new DevToolsManagerDelegate;
} }
std::string BrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();
return g_application_locale;
}
} // namespace brightray } // namespace brightray

View file

@ -20,6 +20,7 @@ class PlatformNotificationService;
class BrowserClient : public content::ContentBrowserClient { class BrowserClient : public content::ContentBrowserClient {
public: public:
static BrowserClient* Get(); static BrowserClient* Get();
static void SetApplicationLocale(const std::string& locale);
BrowserClient(); BrowserClient();
~BrowserClient(); ~BrowserClient();
@ -47,6 +48,7 @@ class BrowserClient : public content::ContentBrowserClient {
net::NetLog* GetNetLog() override; net::NetLog* GetNetLog() override;
base::FilePath GetDefaultDownloadDirectory() override; base::FilePath GetDefaultDownloadDirectory() override;
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override; content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
std::string GetApplicationLocale() override;
protected: protected:
// Subclasses should override this to provide their own BrowserMainParts // Subclasses should override this to provide their own BrowserMainParts

View file

@ -16,6 +16,7 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/browser_client.h"
#include "brightray/browser/browser_context.h" #include "brightray/browser/browser_context.h"
#include "brightray/browser/devtools_manager_delegate.h" #include "brightray/browser/devtools_manager_delegate.h"
#include "brightray/browser/media/media_capture_devices_dispatcher.h" #include "brightray/browser/media/media_capture_devices_dispatcher.h"
@ -275,6 +276,9 @@ int BrowserMainParts::PreCreateThreads() {
if (!views::LayoutProvider::Get()) if (!views::LayoutProvider::Get())
layout_provider_.reset(new views::LayoutProvider()); layout_provider_.reset(new views::LayoutProvider());
// Initialize the app locale.
BrowserClient::SetApplicationLocale(l10n_util::GetApplicationLocale(""));
return 0; return 0;
} }

View file

@ -12,6 +12,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h" #include "base/threading/worker_pool.h"
#include "brightray/browser/browser_client.h"
#include "brightray/browser/net/devtools_network_controller_handle.h" #include "brightray/browser/net/devtools_network_controller_handle.h"
#include "brightray/browser/net/devtools_network_transaction_factory.h" #include "brightray/browser/net/devtools_network_transaction_factory.h"
#include "brightray/browser/net/require_ct_delegate.h" #include "brightray/browser/net/require_ct_delegate.h"
@ -53,7 +54,6 @@
#include "net/url_request/url_request_intercepting_job_factory.h" #include "net/url_request/url_request_intercepting_job_factory.h"
#include "net/url_request/url_request_job_factory_impl.h" #include "net/url_request/url_request_job_factory_impl.h"
#include "storage/browser/quota/special_storage_policy.h" #include "storage/browser/quota/special_storage_policy.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/url_constants.h" #include "url/url_constants.h"
#if defined(USE_NSS_CERTS) #if defined(USE_NSS_CERTS)
@ -142,7 +142,7 @@ URLRequestContextGetter::URLRequestContextGetter(
protocol_interceptors_(std::move(protocol_interceptors)), protocol_interceptors_(std::move(protocol_interceptors)),
job_factory_(nullptr) { job_factory_(nullptr) {
// Must first be created on the UI thread. // Must first be created on the UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (protocol_handlers) if (protocol_handlers)
std::swap(protocol_handlers_, *protocol_handlers); std::swap(protocol_handlers_, *protocol_handlers);
@ -168,7 +168,7 @@ net::HostResolver* URLRequestContextGetter::host_resolver() {
} }
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!url_request_context_.get()) { if (!url_request_context_.get()) {
ct_delegate_.reset(new RequireCTDelegate); ct_delegate_.reset(new RequireCTDelegate);
@ -204,10 +204,10 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
storage_->set_channel_id_service(base::MakeUnique<net::ChannelIDService>( storage_->set_channel_id_service(base::MakeUnique<net::ChannelIDService>(
new net::DefaultChannelIDStore(nullptr))); new net::DefaultChannelIDStore(nullptr)));
std::string accept_lang = l10n_util::GetApplicationLocale(""); storage_->set_http_user_agent_settings(
storage_->set_http_user_agent_settings(base::WrapUnique( base::WrapUnique(new net::StaticHttpUserAgentSettings(
new net::StaticHttpUserAgentSettings( net::HttpUtil::GenerateAcceptLanguageHeader(
net::HttpUtil::GenerateAcceptLanguageHeader(accept_lang), BrowserClient::Get()->GetApplicationLocale()),
user_agent_))); user_agent_)));
std::unique_ptr<net::HostResolver> host_resolver( std::unique_ptr<net::HostResolver> host_resolver(

View file

@ -20,8 +20,12 @@ BrowserProcess::~BrowserProcess() {
g_browser_process = NULL; g_browser_process = NULL;
} }
void BrowserProcess::SetApplicationLocale(const std::string& locale) {
locale_ = locale;
}
std::string BrowserProcess::GetApplicationLocale() { std::string BrowserProcess::GetApplicationLocale() {
return l10n_util::GetApplicationLocale(""); return locale_;
} }
IconManager* BrowserProcess::GetIconManager() { IconManager* BrowserProcess::GetIconManager() {

View file

@ -28,6 +28,7 @@ class BrowserProcess {
BrowserProcess(); BrowserProcess();
~BrowserProcess(); ~BrowserProcess();
void SetApplicationLocale(const std::string& locale);
std::string GetApplicationLocale(); std::string GetApplicationLocale();
IconManager* GetIconManager(); IconManager* GetIconManager();
@ -36,6 +37,7 @@ class BrowserProcess {
private: private:
std::unique_ptr<printing::PrintJobManager> print_job_manager_; std::unique_ptr<printing::PrintJobManager> print_job_manager_;
std::unique_ptr<IconManager> icon_manager_; std::unique_ptr<IconManager> icon_manager_;
std::string locale_;
DISALLOW_COPY_AND_ASSIGN(BrowserProcess); DISALLOW_COPY_AND_ASSIGN(BrowserProcess);
}; };