refactor: eliminate brightray::BrowserClient (#15320)

This commit is contained in:
Milan Burda 2018-10-23 10:45:41 +02:00 committed by Alexey Kuzmin
parent a4fefbe836
commit fbbb704146
11 changed files with 74 additions and 174 deletions

View file

@ -38,7 +38,9 @@
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
@ -121,6 +123,18 @@ bool IsSameWebSite(content::BrowserContext* browser_context,
src_url;
}
AtomBrowserClient* g_browser_client = nullptr;
base::LazyInstance<std::string>::DestructorAtExit
g_io_thread_application_locale = LAZY_INSTANCE_INITIALIZER;
base::NoDestructor<std::string> g_application_locale;
void SetApplicationLocaleOnIOThread(const std::string& locale) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
g_io_thread_application_locale.Get() = locale;
}
} // namespace
// static
@ -133,9 +147,32 @@ void AtomBrowserClient::SetCustomServiceWorkerSchemes(
*g_custom_service_worker_schemes = base::JoinString(schemes, ",");
}
AtomBrowserClient::AtomBrowserClient() {}
AtomBrowserClient* AtomBrowserClient::Get() {
return g_browser_client;
}
AtomBrowserClient::~AtomBrowserClient() {}
// static
void AtomBrowserClient::SetApplicationLocale(const std::string& locale) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!BrowserThread::IsThreadInitialized(BrowserThread::IO) ||
!BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&SetApplicationLocaleOnIOThread, locale))) {
g_io_thread_application_locale.Get() = locale;
}
*g_application_locale = locale;
}
AtomBrowserClient::AtomBrowserClient() {
DCHECK(!g_browser_client);
g_browser_client = this;
}
AtomBrowserClient::~AtomBrowserClient() {
DCHECK(g_browser_client);
g_browser_client = nullptr;
}
content::WebContents* AtomBrowserClient::GetWebContentsFromProcessID(
int process_id) {
@ -579,7 +616,7 @@ net::NetLog* AtomBrowserClient::GetNetLog() {
return AtomBrowserMainParts::Get()->net_log();
}
brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
content::BrowserMainParts* AtomBrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& params) {
return new AtomBrowserMainParts(params);
}
@ -708,4 +745,19 @@ AtomBrowserClient::GetPlatformNotificationService() {
return notification_service_.get();
}
base::FilePath AtomBrowserClient::GetDefaultDownloadDirectory() {
// ~/Downloads
base::FilePath path;
if (base::PathService::Get(base::DIR_HOME, &path))
path = path.Append(FILE_PATH_LITERAL("Downloads"));
return path;
}
std::string AtomBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();
return *g_application_locale;
}
} // namespace atom

View file

@ -11,7 +11,7 @@
#include <string>
#include <vector>
#include "brightray/browser/browser_client.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host_observer.h"
#include "net/ssl/client_cert_identity.h"
@ -30,9 +30,12 @@ class AtomResourceDispatcherHostDelegate;
class NotificationPresenter;
class PlatformNotificationService;
class AtomBrowserClient : public brightray::BrowserClient,
class AtomBrowserClient : public content::ContentBrowserClient,
public content::RenderProcessHostObserver {
public:
static AtomBrowserClient* Get();
static void SetApplicationLocale(const std::string& locale);
AtomBrowserClient();
~AtomBrowserClient() override;
@ -58,8 +61,10 @@ class AtomBrowserClient : public brightray::BrowserClient,
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
protected:
// content::ContentBrowserClient:
std::string GetApplicationLocale() override;
protected:
void RenderProcessWillLaunch(
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) override;
@ -131,10 +136,9 @@ class AtomBrowserClient : public brightray::BrowserClient,
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
content::PlatformNotificationService* GetPlatformNotificationService()
override;
// brightray::BrowserClient:
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override;
base::FilePath GetDefaultDownloadDirectory() override;
// content::RenderProcessHostObserver:
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

View file

@ -173,9 +173,14 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
int AtomBrowserMainParts::PreCreateThreads() {
const int result = brightray::BrowserMainParts::PreCreateThreads();
// Initialize the app locale.
AtomBrowserClient::SetApplicationLocale(
l10n_util::GetApplicationLocale(custom_locale_));
if (!result) {
fake_browser_process_->SetApplicationLocale(
brightray::BrowserClient::Get()->GetApplicationLocale());
AtomBrowserClient::Get()->GetApplicationLocale());
}
// Force MediaCaptureDevicesDispatcher to be created on UI thread.

View file

@ -81,7 +81,7 @@ network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams(
network_context_params->http_cache_enabled = use_cache;
network_context_params->accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader(
brightray::BrowserClient::Get()->GetApplicationLocale());
AtomBrowserClient::Get()->GetApplicationLocale());
network_context_params->enable_data_url_support = false;
network_context_params->proxy_resolver_factory =
ChromeMojoProxyResolverFactory::CreateWithStrongBinding().PassInterface();