Commit 5c4f812b authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Add BrowserProcess object

For the upcoming integration of the bad clock interstitial in WebLayer,
we will need to have a NetworkTimeTracker instance. In //chrome, that
object is owned by and accessible via the BrowserProcess global.

This CL adds a BrowserProcess object to //weblayer and populates it
with a NetworkTimeTracker instance and its dependencies, local state
and the system-level URLLoaderFactory. Both of the latter will shortly
be needed for metrics as well (and presumably for other use cases in
the fullness of time). The architecture of BrowserProcess is adapted
from that in //chrome.

system_network_context_manager.* was originally written by
alexclarke@chromium.org; it is an adaptation of //chromecast's
cast_network_contexts.*.

Finally, we note that the implementation of the PrefService in this CL
is deliberately as simple as possible for the initial purpose of the
bad clock interstitial. This can be extended as other use cases find
necessary, including the introduction of a FeatureListCreator if that
proves to be useful.

Bug: 1025059
Change-Id: I54f90551343b34e813fc5e6c97f0421926a30b43
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1985761Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarAlex Clarke <alexclarke@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#730086}
parent baa10287
......@@ -108,6 +108,8 @@ jumbo_static_library("weblayer_lib") {
"browser/browser_context_impl.h",
"browser/browser_main_parts_impl.cc",
"browser/browser_main_parts_impl.h",
"browser/browser_process.cc",
"browser/browser_process.h",
"browser/content_browser_client_impl.cc",
"browser/content_browser_client_impl.h",
"browser/download_manager_delegate_impl.cc",
......@@ -129,6 +131,8 @@ jumbo_static_library("weblayer_lib") {
"browser/ssl_error_handler.h",
"browser/ssl_host_state_delegate_impl.cc",
"browser/ssl_host_state_delegate_impl.h",
"browser/system_network_context_manager.cc",
"browser/system_network_context_manager.h",
"browser/tab_impl.cc",
"browser/tab_impl.h",
"browser/weblayer_browser_interface_binders.cc",
......@@ -210,6 +214,7 @@ jumbo_static_library("weblayer_lib") {
"//components/crash/content/app",
"//components/crash/content/browser",
"//components/embedder_support:switches",
"//components/network_time",
"//components/prefs",
"//components/security_interstitials/content:security_interstitial_page",
"//components/security_interstitials/content/renderer:security_interstitial_page_controller",
......
......@@ -10,6 +10,7 @@ include_rules = [
"+components/autofill/core/common",
"+components/crash/content/browser",
"+components/embedder_support",
"+components/network_time",
"+components/prefs",
"+components/user_prefs",
"+components/safe_browsing",
......
......@@ -22,6 +22,7 @@
#include "services/service_manager/embedder/result_codes.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h"
#include "weblayer/browser/browser_process.h"
#include "weblayer/browser/webui/web_ui_controller_factory.h"
#include "weblayer/public/main.h"
......@@ -87,6 +88,8 @@ void BrowserMainPartsImpl::PreMainMessageLoopStart() {
}
int BrowserMainPartsImpl::PreEarlyInitialization() {
browser_process_ = std::make_unique<BrowserProcess>();
#if defined(USE_X11)
ui::SetDefaultX11ErrorHandlers();
#endif
......
......@@ -14,6 +14,7 @@
#include "content/public/common/main_function_params.h"
namespace weblayer {
class BrowserProcess;
struct MainParams;
class BrowserMainPartsImpl : public content::BrowserMainParts {
......@@ -33,6 +34,8 @@ class BrowserMainPartsImpl : public content::BrowserMainParts {
private:
MainParams* params_;
std::unique_ptr<BrowserProcess> browser_process_;
// For running weblayer_browsertests.
const content::MainFunctionParams main_function_params_;
bool run_message_loop_ = true;
......
// Copyright 2020 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 file.
#include "weblayer/browser/browser_process.h"
#include "base/memory/ref_counted.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "components/network_time/network_time_tracker.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h"
#include "weblayer/browser/system_network_context_manager.h"
namespace weblayer {
namespace {
BrowserProcess* g_browser_process = nullptr;
// Creates the PrefService that will be used as the browser process's local
// state.
std::unique_ptr<PrefService> CreatePrefService() {
auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
PrefServiceFactory pref_service_factory;
pref_service_factory.set_user_prefs(
base::MakeRefCounted<InMemoryPrefStore>());
return pref_service_factory.Create(pref_registry);
}
} // namespace
BrowserProcess::BrowserProcess() {
g_browser_process = this;
}
BrowserProcess::~BrowserProcess() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
g_browser_process = nullptr;
SystemNetworkContextManager::DeleteInstance();
}
// static
BrowserProcess* BrowserProcess::GetInstance() {
return g_browser_process;
}
PrefService* BrowserProcess::GetLocalState() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!local_state_)
local_state_ = CreatePrefService();
return local_state_.get();
}
scoped_refptr<network::SharedURLLoaderFactory>
BrowserProcess::GetSharedURLLoaderFactory() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return SystemNetworkContextManager::GetInstance()
->GetSharedURLLoaderFactory();
}
network_time::NetworkTimeTracker* BrowserProcess::GetNetworkTimeTracker() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!network_time_tracker_) {
network_time_tracker_ = std::make_unique<network_time::NetworkTimeTracker>(
base::WrapUnique(new base::DefaultClock()),
base::WrapUnique(new base::DefaultTickClock()), GetLocalState(),
GetSharedURLLoaderFactory());
}
return network_time_tracker_.get();
}
} // namespace weblayer
// Copyright 2020 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 file.
#ifndef WEBLAYER_BROWSER_BROWSER_PROCESS_H_
#define WEBLAYER_BROWSER_BROWSER_PROCESS_H_
#include <memory>
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
class PrefService;
namespace network_time {
class NetworkTimeTracker;
}
namespace network {
class SharedURLLoaderFactory;
}
namespace weblayer {
// Class that holds global state in the browser process. Should be used only on
// the UI thread.
class BrowserProcess {
public:
BrowserProcess();
~BrowserProcess();
static BrowserProcess* GetInstance();
PrefService* GetLocalState();
scoped_refptr<network::SharedURLLoaderFactory> GetSharedURLLoaderFactory();
network_time::NetworkTimeTracker* GetNetworkTimeTracker();
private:
std::unique_ptr<PrefService> local_state_;
std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(BrowserProcess);
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_BROWSER_PROCESS_H_
......@@ -42,6 +42,7 @@
#include "weblayer/browser/i18n_util.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/ssl_error_handler.h"
#include "weblayer/browser/system_network_context_manager.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/browser/weblayer_browser_interface_binders.h"
#include "weblayer/browser/weblayer_content_browser_overlay_manifest.h"
......@@ -238,6 +239,16 @@ void ContentBrowserClientImpl::OnNetworkServiceCreated(
network::mojom::CryptConfigPtr config = network::mojom::CryptConfig::New();
content::GetNetworkService()->SetCryptConfig(std::move(config));
#endif
// Create SystemNetworkContextManager if it has not been created yet. We need
// to set up global NetworkService state before anything else uses it and this
// is the first opportunity to initialize SystemNetworkContextManager with the
// NetworkService.
if (!SystemNetworkContextManager::HasInstance())
SystemNetworkContextManager::CreateInstance(GetUserAgent());
SystemNetworkContextManager::GetInstance()->OnNetworkServiceCreated(
network_service);
}
std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
......
// Copyright 2020 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 file.
#include "weblayer/browser/system_network_context_manager.h"
#include "content/public/browser/network_service_instance.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace weblayer {
namespace {
// The global instance of the SystemNetworkContextmanager.
SystemNetworkContextManager* g_system_network_context_manager = nullptr;
network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams(
const std::string& user_agent) {
network::mojom::NetworkContextParamsPtr network_context_params =
network::mojom::NetworkContextParams::New();
network_context_params->user_agent = user_agent;
return network_context_params;
}
} // namespace
// static
SystemNetworkContextManager* SystemNetworkContextManager::CreateInstance(
const std::string& user_agent) {
DCHECK(!g_system_network_context_manager);
g_system_network_context_manager =
new SystemNetworkContextManager(user_agent);
return g_system_network_context_manager;
}
// static
bool SystemNetworkContextManager::HasInstance() {
return !!g_system_network_context_manager;
}
// static
SystemNetworkContextManager* SystemNetworkContextManager::GetInstance() {
if (!g_system_network_context_manager) {
// Initialize the network service, which will trigger
// ChromeContentBrowserClient::OnNetworkServiceCreated(), which calls
// CreateInstance() to initialize |g_system_network_context_manager|.
content::GetNetworkService();
// TODO(crbug.com/981057): There should be a DCHECK() here to make sure
// |g_system_network_context_manager| has been created, but that is not
// true in many unit tests.
}
return g_system_network_context_manager;
}
// static
void SystemNetworkContextManager::DeleteInstance() {
DCHECK(g_system_network_context_manager);
delete g_system_network_context_manager;
g_system_network_context_manager = nullptr;
}
SystemNetworkContextManager::SystemNetworkContextManager(
const std::string& user_agent)
: user_agent_(user_agent) {}
SystemNetworkContextManager::~SystemNetworkContextManager() = default;
network::mojom::NetworkContext*
SystemNetworkContextManager::GetSystemNetworkContext() {
if (!system_network_context_ || !system_network_context_.is_connected()) {
// This should call into OnNetworkServiceCreated(), which will re-create
// the network service, if needed. There's a chance that it won't be
// invoked, if the NetworkContext has encountered an error but the
// NetworkService has not yet noticed its pipe was closed. In that case,
// trying to create a new NetworkContext would fail, anyways, and hopefully
// a new NetworkContext will be created on the next GetContext() call.
content::GetNetworkService();
DCHECK(system_network_context_);
}
return system_network_context_.get();
}
void SystemNetworkContextManager::OnNetworkServiceCreated(
network::mojom::NetworkService* network_service) {
// The system NetworkContext must be created first, since it sets
// |primary_network_context| to true.
network_service->CreateNetworkContext(
system_network_context_.BindNewPipeAndPassReceiver(),
CreateSystemNetworkContextManagerParams());
}
network::mojom::NetworkContextParamsPtr
SystemNetworkContextManager::CreateSystemNetworkContextManagerParams() {
network::mojom::NetworkContextParamsPtr network_context_params =
CreateDefaultNetworkContextParams(user_agent_);
content::UpdateCorsExemptHeader(network_context_params.get());
network_context_params->context_name = std::string("system");
network_context_params->primary_network_context = true;
return network_context_params;
}
scoped_refptr<network::SharedURLLoaderFactory>
SystemNetworkContextManager::GetSharedURLLoaderFactory() {
if (!url_loader_factory_) {
auto url_loader_factory_params =
network::mojom::URLLoaderFactoryParams::New();
url_loader_factory_params->process_id = network::mojom::kBrowserProcessId;
url_loader_factory_params->is_corb_enabled = false;
GetSystemNetworkContext()->CreateURLLoaderFactory(
url_loader_factory_.BindNewPipeAndPassReceiver(),
std::move(url_loader_factory_params));
shared_url_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
url_loader_factory_.get());
}
return shared_url_loader_factory_;
}
} // namespace weblayer
// Copyright 2020 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 file.
#ifndef WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
#define WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
#include "base/memory/scoped_refptr.h"
#include "content/public/browser/cors_exempt_headers.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/network_service.mojom.h"
namespace network {
class SharedURLLoaderFactory;
} // namespace network
namespace weblayer {
// Manages a system-wide network context that's not tied to a profile.
class SystemNetworkContextManager {
public:
// Creates the global instance of SystemNetworkContextManager.
static SystemNetworkContextManager* CreateInstance(
const std::string& user_agent);
// Checks if the global SystemNetworkContextManager has been created.
static bool HasInstance();
// Gets the global SystemNetworkContextManager instance. If it has not been
// created yet, NetworkService is called, which will cause the
// SystemNetworkContextManager to be created.
static SystemNetworkContextManager* GetInstance();
// Destroys the global SystemNetworkContextManager instance.
static void DeleteInstance();
~SystemNetworkContextManager();
// Returns the System NetworkContext. Does any initialization of the
// NetworkService that may be needed when first called.
network::mojom::NetworkContext* GetSystemNetworkContext();
// Called when content creates a NetworkService. Creates the
// system NetworkContext, if the network service is enabled.
void OnNetworkServiceCreated(network::mojom::NetworkService* network_service);
// Returns a SharedURLLoaderFactory owned by the SystemNetworkContextManager
// that is backed by the SystemNetworkContext.
scoped_refptr<network::SharedURLLoaderFactory> GetSharedURLLoaderFactory();
private:
explicit SystemNetworkContextManager(const std::string& user_agent);
network::mojom::NetworkContextParamsPtr
CreateSystemNetworkContextManagerParams();
std::string user_agent_;
mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_;
scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
shared_url_loader_factory_;
mojo::Remote<network::mojom::NetworkContext> system_network_context_;
DISALLOW_COPY_AND_ASSIGN(SystemNetworkContextManager);
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
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