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/environment.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
@ -121,6 +123,18 @@ bool IsSameWebSite(content::BrowserContext* browser_context,
src_url; 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 } // namespace
// static // static
@ -133,9 +147,32 @@ void AtomBrowserClient::SetCustomServiceWorkerSchemes(
*g_custom_service_worker_schemes = base::JoinString(schemes, ","); *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( content::WebContents* AtomBrowserClient::GetWebContentsFromProcessID(
int process_id) { int process_id) {
@ -579,7 +616,7 @@ net::NetLog* AtomBrowserClient::GetNetLog() {
return AtomBrowserMainParts::Get()->net_log(); return AtomBrowserMainParts::Get()->net_log();
} }
brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts( content::BrowserMainParts* AtomBrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& params) { const content::MainFunctionParams& params) {
return new AtomBrowserMainParts(params); return new AtomBrowserMainParts(params);
} }
@ -708,4 +745,19 @@ AtomBrowserClient::GetPlatformNotificationService() {
return notification_service_.get(); 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 } // namespace atom

View file

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

View file

@ -173,9 +173,14 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
int AtomBrowserMainParts::PreCreateThreads() { int AtomBrowserMainParts::PreCreateThreads() {
const int result = brightray::BrowserMainParts::PreCreateThreads(); const int result = brightray::BrowserMainParts::PreCreateThreads();
// Initialize the app locale.
AtomBrowserClient::SetApplicationLocale(
l10n_util::GetApplicationLocale(custom_locale_));
if (!result) { if (!result) {
fake_browser_process_->SetApplicationLocale( fake_browser_process_->SetApplicationLocale(
brightray::BrowserClient::Get()->GetApplicationLocale()); AtomBrowserClient::Get()->GetApplicationLocale());
} }
// Force MediaCaptureDevicesDispatcher to be created on UI thread. // 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->http_cache_enabled = use_cache;
network_context_params->accept_language = network_context_params->accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader( net::HttpUtil::GenerateAcceptLanguageHeader(
brightray::BrowserClient::Get()->GetApplicationLocale()); AtomBrowserClient::Get()->GetApplicationLocale());
network_context_params->enable_data_url_support = false; network_context_params->enable_data_url_support = false;
network_context_params->proxy_resolver_factory = network_context_params->proxy_resolver_factory =
ChromeMojoProxyResolverFactory::CreateWithStrongBinding().PassInterface(); ChromeMojoProxyResolverFactory::CreateWithStrongBinding().PassInterface();

View file

@ -19,8 +19,6 @@ static_library("brightray") {
sources = [ sources = [
"browser/brightray_paths.h", "browser/brightray_paths.h",
"browser/browser_client.cc",
"browser/browser_client.h",
"browser/browser_main_parts.cc", "browser/browser_main_parts.cc",
"browser/browser_main_parts.h", "browser/browser_main_parts.h",
"browser/browser_main_parts_mac.mm", "browser/browser_main_parts_mac.mm",

View file

@ -1,85 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "brightray/browser/browser_client.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "brightray/browser/browser_main_parts.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h"
using content::BrowserThread;
namespace brightray {
namespace {
BrowserClient* g_browser_client;
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
void BrowserClient::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;
}
BrowserClient* BrowserClient::Get() {
return g_browser_client;
}
BrowserClient::BrowserClient() : browser_main_parts_(nullptr) {
DCHECK(!g_browser_client);
g_browser_client = this;
}
BrowserClient::~BrowserClient() {}
BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) {
return new BrowserMainParts;
}
content::BrowserMainParts* BrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& parameters) {
DCHECK(!browser_main_parts_);
browser_main_parts_ = OverrideCreateBrowserMainParts(parameters);
return browser_main_parts_;
}
base::FilePath BrowserClient::GetDefaultDownloadDirectory() {
// ~/Downloads
base::FilePath path;
if (base::PathService::Get(base::DIR_HOME, &path))
path = path.Append(FILE_PATH_LITERAL("Downloads"));
return path;
}
std::string BrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();
return *g_application_locale;
}
} // namespace brightray

View file

@ -1,50 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#include <memory>
#include <string>
#include <vector>
#include "content/public/browser/content_browser_client.h"
namespace brightray {
class BrowserMainParts;
class BrowserClient : public content::ContentBrowserClient {
public:
static BrowserClient* Get();
static void SetApplicationLocale(const std::string& locale);
BrowserClient();
~BrowserClient() override;
BrowserMainParts* browser_main_parts() { return browser_main_parts_; }
// Subclasses that override this (e.g., to provide their own protocol
// handlers) should call this implementation after doing their own work.
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override;
base::FilePath GetDefaultDownloadDirectory() override;
std::string GetApplicationLocale() override;
protected:
// Subclasses should override this to provide their own BrowserMainParts
// implementation. The lifetime of the returned instance is managed by the
// caller.
virtual BrowserMainParts* OverrideCreateBrowserMainParts(
const content::MainFunctionParams&);
private:
BrowserMainParts* browser_main_parts_;
DISALLOW_COPY_AND_ASSIGN(BrowserClient);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_

View file

@ -22,7 +22,6 @@
#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/common/application_info.h" #include "brightray/common/application_info.h"
#include "brightray/common/main_delegate.h" #include "brightray/common/main_delegate.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
@ -305,10 +304,6 @@ 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(custom_locale_));
return 0; return 0;
} }

View file

@ -41,6 +41,8 @@ class BrowserMainParts : public content::BrowserMainParts {
void InitializeFeatureList(); void InitializeFeatureList();
std::string custom_locale_;
private: private:
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
void InitializeMainNib(); void InitializeMainNib();
@ -52,7 +54,6 @@ class BrowserMainParts : public content::BrowserMainParts {
#endif #endif
std::unique_ptr<views::LayoutProvider> layout_provider_; std::unique_ptr<views::LayoutProvider> layout_provider_;
std::string custom_locale_;
DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); DISALLOW_COPY_AND_ASSIGN(BrowserMainParts);
}; };

View file

@ -9,7 +9,6 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "brightray/browser/browser_client.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "electron/buildflags/buildflags.h" #include "electron/buildflags/buildflags.h"
#include "services/service_manager/embedder/switches.h" #include "services/service_manager/embedder/switches.h"
@ -97,13 +96,4 @@ void MainDelegate::PreSandboxStartup() {
} }
} }
content::ContentBrowserClient* MainDelegate::CreateContentBrowserClient() {
browser_client_ = CreateBrowserClient();
return browser_client_.get();
}
std::unique_ptr<BrowserClient> MainDelegate::CreateBrowserClient() {
return std::unique_ptr<BrowserClient>(new BrowserClient);
}
} // namespace brightray } // namespace brightray

View file

@ -21,8 +21,6 @@ class ResourceBundle;
namespace brightray { namespace brightray {
class BrowserClient;
void LoadResourceBundle(const std::string& locale); void LoadResourceBundle(const std::string& locale);
void LoadCommonResources(); void LoadCommonResources();
@ -32,10 +30,6 @@ class MainDelegate : public content::ContentMainDelegate {
~MainDelegate() override; ~MainDelegate() override;
protected: protected:
// Subclasses can override this to provide their own BrowserClient
// implementation.
virtual std::unique_ptr<BrowserClient> CreateBrowserClient();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Subclasses can override this to custom the paths of child process and // Subclasses can override this to custom the paths of child process and
// framework bundle. // framework bundle.
@ -47,10 +41,6 @@ class MainDelegate : public content::ContentMainDelegate {
void PreSandboxStartup() override; void PreSandboxStartup() override;
private: private:
content::ContentBrowserClient* CreateContentBrowserClient() override;
std::unique_ptr<BrowserClient> browser_client_;
DISALLOW_COPY_AND_ASSIGN(MainDelegate); DISALLOW_COPY_AND_ASSIGN(MainDelegate);
}; };