blob: 9177884fdd98adefb2bb93394921d23372d1ee4d [file] [log] [blame]
// Copyright 2018 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 "headless/lib/browser/headless_request_context_manager.h"
#include "base/bind.h"
#include "base/task/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/resource_context.h"
#include "headless/app/headless_shell_switches.h"
#include "headless/lib/browser/headless_browser_context_options.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "net/http/http_auth_preferences.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "services/network/network_service.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/url_request_context_builder_mojo.h"
#if defined(HEADLESS_USE_PREFS)
#include "components/os_crypt/os_crypt.h"
#include "content/public/common/network_service_util.h"
#endif
namespace headless {
namespace {
// TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
// of lacros-chrome is complete.
#if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
constexpr char kProductName[] = "HeadlessChrome";
#endif
net::NetworkTrafficAnnotationTag GetProxyConfigTrafficAnnotationTag() {
static net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("proxy_config_headless", R"(
semantics {
sender: "Proxy Config"
description:
"Creates a proxy based on configuration received from headless "
"command prompt."
trigger:
"User starts headless with proxy config."
data:
"Proxy configurations."
destination: OTHER
destination_other:
"The proxy server specified in the configuration."
}
policy {
cookies_allowed: NO
setting:
"This config is only used for headless mode and provided by user."
policy_exception_justification:
"This config is only used for headless mode and provided by user."
})");
return traffic_annotation;
}
void SetCryptConfigOnce(const base::FilePath& user_data_path) {
static bool done_once = false;
if (done_once)
return;
done_once = true;
// TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
// of lacros-chrome is complete.
#if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
::network::mojom::CryptConfigPtr config =
::network::mojom::CryptConfig::New();
config->store = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kPasswordStore);
config->product_name = kProductName;
config->should_use_preference = false;
config->user_data_path = user_data_path;
content::GetNetworkService()->SetCryptConfig(std::move(config));
#elif defined(OS_WIN) && defined(HEADLESS_USE_PREFS)
// The OSCrypt keys are process bound, so if network service is out of
// process, send it the required key if it is available.
if (content::IsOutOfProcessNetworkService() &&
OSCrypt::IsEncryptionAvailable()) {
content::GetNetworkService()->SetEncryptionKey(
OSCrypt::GetRawEncryptionKey());
}
#endif
}
} // namespace
// Tracks the ProxyConfig to use, and passes any updates to a NetworkContext's
// ProxyConfigClient.
class HeadlessProxyConfigMonitor
: public net::ProxyConfigService::Observer,
public ::network::mojom::ProxyConfigPollerClient {
public:
static void DeleteSoon(std::unique_ptr<HeadlessProxyConfigMonitor> instance) {
instance->task_runner_->DeleteSoon(FROM_HERE, instance.release());
}
explicit HeadlessProxyConfigMonitor(
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: task_runner_(task_runner) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// We must create the proxy config service on the UI loop on Linux because
// it must synchronously run on the glib message loop.
proxy_config_service_ =
net::ConfiguredProxyResolutionService::CreateSystemProxyConfigService(
task_runner_);
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&net::ProxyConfigService::AddObserver,
base::Unretained(proxy_config_service_.get()),
base::Unretained(this)));
}
~HeadlessProxyConfigMonitor() override {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
proxy_config_service_->RemoveObserver(this);
}
// Populates proxy-related fields of |network_context_params|. Updated
// ProxyConfigs will be sent to a NetworkContext created with those params
// whenever the configuration changes. Can be called more than once to inform
// multiple NetworkContexts of proxy changes.
void AddToNetworkContextParams(
::network::mojom::NetworkContextParams* network_context_params) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
DCHECK(!proxy_config_client_);
network_context_params->proxy_config_client_receiver =
proxy_config_client_.BindNewPipeAndPassReceiver();
poller_receiver_.Bind(network_context_params->proxy_config_poller_client
.InitWithNewPipeAndPassReceiver());
net::ProxyConfigWithAnnotation proxy_config;
net::ProxyConfigService::ConfigAvailability availability =
proxy_config_service_->GetLatestProxyConfig(&proxy_config);
if (availability != net::ProxyConfigService::CONFIG_PENDING)
network_context_params->initial_proxy_config = proxy_config;
}
private:
// net::ProxyConfigService::Observer implementation:
void OnProxyConfigChanged(
const net::ProxyConfigWithAnnotation& config,
net::ProxyConfigService::ConfigAvailability availability) override {
if (!proxy_config_client_)
return;
switch (availability) {
case net::ProxyConfigService::CONFIG_VALID:
proxy_config_client_->OnProxyConfigUpdated(config);
break;
case net::ProxyConfigService::CONFIG_UNSET:
proxy_config_client_->OnProxyConfigUpdated(
net::ProxyConfigWithAnnotation::CreateDirect());
break;
case net::ProxyConfigService::CONFIG_PENDING:
NOTREACHED();
break;
}
}
// network::mojom::ProxyConfigPollerClient implementation:
void OnLazyProxyConfigPoll() override { proxy_config_service_->OnLazyPoll(); }
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
mojo::Receiver<::network::mojom::ProxyConfigPollerClient> poller_receiver_{
this};
mojo::Remote<::network::mojom::ProxyConfigClient> proxy_config_client_;
DISALLOW_COPY_AND_ASSIGN(HeadlessProxyConfigMonitor);
};
// static
std::unique_ptr<HeadlessRequestContextManager>
HeadlessRequestContextManager::CreateSystemContext(
const HeadlessBrowserContextOptions* options) {
auto manager = std::make_unique<HeadlessRequestContextManager>(
options, base::FilePath());
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
auto auth_params = ::network::mojom::HttpAuthDynamicParams::New();
auth_params->server_allowlist =
command_line->GetSwitchValueASCII(switches::kAuthServerAllowlist);
auto* network_service = content::GetNetworkService();
network_service->ConfigureHttpAuthPrefs(std::move(auth_params));
::network::mojom::NetworkContextParamsPtr network_context_params =
::network::mojom::NetworkContextParams::New();
::network::mojom::CertVerifierCreationParamsPtr
cert_verifier_creation_params =
::network::mojom::CertVerifierCreationParams::New();
manager->ConfigureNetworkContextParamsInternal(
network_context_params.get(), cert_verifier_creation_params.get());
network_context_params->cert_verifier_params =
content::GetCertVerifierParams(std::move(cert_verifier_creation_params));
network_service->CreateNetworkContext(
manager->system_context_.InitWithNewPipeAndPassReceiver(),
std::move(network_context_params));
return manager;
}
HeadlessRequestContextManager::HeadlessRequestContextManager(
const HeadlessBrowserContextOptions* options,
base::FilePath user_data_path)
:
// On Windows, Cookie encryption requires access to local_state prefs.
#if defined(OS_WIN) && !defined(HEADLESS_USE_PREFS)
cookie_encryption_enabled_(false),
#else
cookie_encryption_enabled_(
!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableCookieEncryption)),
#endif
user_data_path_(std::move(user_data_path)),
accept_language_(options->accept_language()),
user_agent_(options->user_agent()),
proxy_config_(
options->proxy_config()
? std::make_unique<net::ProxyConfig>(*options->proxy_config())
: nullptr),
resource_context_(std::make_unique<content::ResourceContext>()) {
if (!proxy_config_) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNoSystemProxyConfigService)) {
proxy_config_ = std::make_unique<net::ProxyConfig>();
} else {
proxy_config_monitor_ = std::make_unique<HeadlessProxyConfigMonitor>(
base::ThreadTaskRunnerHandle::Get());
}
}
SetCryptConfigOnce(user_data_path_);
}
HeadlessRequestContextManager::~HeadlessRequestContextManager() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (proxy_config_monitor_)
HeadlessProxyConfigMonitor::DeleteSoon(std::move(proxy_config_monitor_));
}
void HeadlessRequestContextManager::ConfigureNetworkContextParams(
bool in_memory,
const base::FilePath& relative_partition_path,
::network::mojom::NetworkContextParams* network_context_params,
::network::mojom::CertVerifierCreationParams*
cert_verifier_creation_params) {
ConfigureNetworkContextParamsInternal(network_context_params,
cert_verifier_creation_params);
}
void HeadlessRequestContextManager::ConfigureNetworkContextParamsInternal(
::network::mojom::NetworkContextParams* context_params,
::network::mojom::CertVerifierCreationParams*
cert_verifier_creation_params) {
context_params->user_agent = user_agent_;
context_params->accept_language = accept_language_;
// TODO(https://crbug.com/458508): Allow
// context_params->http_auth_static_network_context_params->allow_default_credentials
// to be controllable by a flag.
context_params->http_auth_static_network_context_params =
::network::mojom::HttpAuthStaticNetworkContextParams::New();
if (!user_data_path_.empty()) {
context_params->enable_encrypted_cookies = cookie_encryption_enabled_;
context_params->cookie_path =
user_data_path_.Append(FILE_PATH_LITERAL("Cookies"));
}
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kDiskCacheDir)) {
context_params->http_cache_path =
command_line->GetSwitchValuePath(switches::kDiskCacheDir);
} else if (!user_data_path_.empty()) {
context_params->http_cache_path =
user_data_path_.Append(FILE_PATH_LITERAL("Cache"));
}
if (proxy_config_) {
context_params->initial_proxy_config = net::ProxyConfigWithAnnotation(
*proxy_config_, GetProxyConfigTrafficAnnotationTag());
} else {
proxy_config_monitor_->AddToNetworkContextParams(context_params);
}
}
} // namespace headless