Commit 38662e2f authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Enhance configuration of the NetworkService-owned NetLog

This CL moves code related to opening files out of the NetworkService
(which will eventually be sandboxed), as well as factoring out some of
the shared features of the NetLog subclasses, to make code deduplication
easier.

Implementations are provided for ChromeContentClient and
ShellContentClient classes.

Bug: 847961
Change-Id: I0a332b4429a2f20cbb38ae1671b8df0fbee28ba1
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Reviewed-on: https://chromium-review.googlesource.com/1161470
Commit-Queue: Matt Menke <mmenke@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581201}
parent 24987fbb
...@@ -242,6 +242,7 @@ static_library("common") { ...@@ -242,6 +242,7 @@ static_library("common") {
"//components/nacl/common:buildflags", "//components/nacl/common:buildflags",
"//components/nacl/common:process_type", "//components/nacl/common:process_type",
"//components/nacl/common:switches", "//components/nacl/common:switches",
"//components/net_log",
"//components/network_session_configurator/common", "//components/network_session_configurator/common",
"//components/ntp_tiles", "//components/ntp_tiles",
"//components/offline_pages/buildflags", "//components/offline_pages/buildflags",
......
...@@ -26,6 +26,7 @@ include_rules = [ ...@@ -26,6 +26,7 @@ include_rules = [
"+components/metrics/client_info.h", "+components/metrics/client_info.h",
"+components/metrics/metrics_pref_names.h", "+components/metrics/metrics_pref_names.h",
"+components/nacl/common", "+components/nacl/common",
"+components/net_log",
"+components/network_session_configurator/common", "+components/network_session_configurator/common",
"+components/ntp_tiles", "+components/ntp_tiles",
"+components/nux_google_apps", "+components/nux_google_apps",
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/version.h" #include "base/version.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/child_process_logging.h" #include "chrome/common/child_process_logging.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
...@@ -37,6 +38,7 @@ ...@@ -37,6 +38,7 @@
#include "chrome/grit/common_resources.h" #include "chrome/grit/common_resources.h"
#include "components/crash/core/common/crash_key.h" #include "components/crash/core/common/crash_key.h"
#include "components/dom_distiller/core/url_constants.h" #include "components/dom_distiller/core/url_constants.h"
#include "components/net_log/chrome_net_log.h"
#include "components/services/heap_profiling/public/cpp/client.h" #include "components/services/heap_profiling/public/cpp/client.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "content/public/common/cdm_info.h" #include "content/public/common/cdm_info.h"
...@@ -679,6 +681,16 @@ gfx::Image& ChromeContentClient::GetNativeImageNamed(int resource_id) const { ...@@ -679,6 +681,16 @@ gfx::Image& ChromeContentClient::GetNativeImageNamed(int resource_id) const {
resource_id); resource_id);
} }
base::DictionaryValue ChromeContentClient::GetNetLogConstants() const {
auto platform_dict = net_log::ChromeNetLog::GetPlatformConstants(
base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
chrome::GetChannelName());
if (platform_dict)
return std::move(*platform_dict);
else
return base::DictionaryValue();
}
std::string ChromeContentClient::GetProcessTypeNameInEnglish(int type) { std::string ChromeContentClient::GetProcessTypeNameInEnglish(int type) {
#if BUILDFLAG(ENABLE_NACL) #if BUILDFLAG(ENABLE_NACL)
switch (type) { switch (type) {
......
...@@ -87,6 +87,7 @@ class ChromeContentClient : public content::ContentClient { ...@@ -87,6 +87,7 @@ class ChromeContentClient : public content::ContentClient {
base::RefCountedMemory* GetDataResourceBytes( base::RefCountedMemory* GetDataResourceBytes(
int resource_id) const override; int resource_id) const override;
gfx::Image& GetNativeImageNamed(int resource_id) const override; gfx::Image& GetNativeImageNamed(int resource_id) const override;
base::DictionaryValue GetNetLogConstants() const override;
std::string GetProcessTypeNameInEnglish(int type) override; std::string GetProcessTypeNameInEnglish(int type) override;
bool AllowScriptExtensionForServiceWorker(const GURL& script_url) override; bool AllowScriptExtensionForServiceWorker(const GURL& script_url) override;
......
...@@ -10,8 +10,10 @@ ...@@ -10,8 +10,10 @@
#include "content/public/browser/content_browser_client.h" #include "content/public/browser/content_browser_client.h"
#include "content/public/common/service_manager_connection.h" #include "content/public/common/service_manager_connection.h"
#include "content/public/common/service_names.mojom.h" #include "content/public/common/service_names.mojom.h"
#include "net/log/net_log_util.h"
#include "services/network/network_service.h" #include "services/network/network_service.h"
#include "services/network/public/cpp/features.h" #include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
namespace content { namespace content {
...@@ -59,6 +61,25 @@ network::mojom::NetworkService* GetNetworkService() { ...@@ -59,6 +61,25 @@ network::mojom::NetworkService* GetNetworkService() {
g_client = new NetworkServiceClient(mojo::MakeRequest(&client_ptr)); g_client = new NetworkServiceClient(mojo::MakeRequest(&client_ptr));
(*g_network_service_ptr)->SetClient(std::move(client_ptr)); (*g_network_service_ptr)->SetClient(std::move(client_ptr));
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(network::switches::kLogNetLog)) {
base::FilePath log_path =
command_line->GetSwitchValuePath(network::switches::kLogNetLog);
base::DictionaryValue client_constants =
GetContentClient()->GetNetLogConstants();
base::File file(
log_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
LOG_IF(ERROR, !file.IsValid())
<< "Failed opening: " << log_path.value();
(*g_network_service_ptr)
->StartNetLog(std::move(file), std::move(client_constants));
}
}
GetContentClient()->browser()->OnNetworkServiceCreated( GetContentClient()->browser()->OnNetworkServiceCreated(
g_network_service_ptr->get()); g_network_service_ptr->get());
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/public/common/origin_util.h" #include "content/public/common/origin_util.h"
#include "content/public/common/user_agent.h" #include "content/public/common/user_agent.h"
...@@ -102,6 +103,10 @@ std::string ContentClient::GetProcessTypeNameInEnglish(int type) { ...@@ -102,6 +103,10 @@ std::string ContentClient::GetProcessTypeNameInEnglish(int type) {
return std::string(); return std::string();
} }
base::DictionaryValue ContentClient::GetNetLogConstants() const {
return base::DictionaryValue();
}
blink::OriginTrialPolicy* ContentClient::GetOriginTrialPolicy() { blink::OriginTrialPolicy* ContentClient::GetOriginTrialPolicy() {
return nullptr; return nullptr;
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
namespace base { namespace base {
class RefCountedMemory; class RefCountedMemory;
class DictionaryValue;
} }
namespace blink { namespace blink {
...@@ -173,6 +174,14 @@ class CONTENT_EXPORT ContentClient { ...@@ -173,6 +174,14 @@ class CONTENT_EXPORT ContentClient {
// doesn't know about because they're from the embedder. // doesn't know about because they're from the embedder.
virtual std::string GetProcessTypeNameInEnglish(int type); virtual std::string GetProcessTypeNameInEnglish(int type);
// Called once during initialization of NetworkService to provide constants
// to NetLog. (Though it may be called multiples times if NetworkService
// crashes and needs to be reinitialized). The return value is merged with
// |GetNetConstants()| and passed to FileNetLogObserver - see documentation
// of |FileNetLogObserver::CreateBounded()| for more information. The
// convention is to put new constants under a subdict at the key "clientInfo".
virtual base::DictionaryValue GetNetLogConstants() const;
// Returns whether or not V8 script extensions should be allowed for a // Returns whether or not V8 script extensions should be allowed for a
// service worker. // service worker.
virtual bool AllowScriptExtensionForServiceWorker(const GURL& script_url); virtual bool AllowScriptExtensionForServiceWorker(const GURL& script_url);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/app/resources/grit/content_resources.h" #include "content/app/resources/grit/content_resources.h"
#include "content/app/strings/grit/content_strings.h" #include "content/app/strings/grit/content_strings.h"
...@@ -87,6 +88,17 @@ gfx::Image& ShellContentClient::GetNativeImageNamed(int resource_id) const { ...@@ -87,6 +88,17 @@ gfx::Image& ShellContentClient::GetNativeImageNamed(int resource_id) const {
resource_id); resource_id);
} }
base::DictionaryValue ShellContentClient::GetNetLogConstants() const {
base::DictionaryValue client_constants;
client_constants.SetString("name", "content_shell");
client_constants.SetString(
"command_line",
base::CommandLine::ForCurrentProcess()->GetCommandLineString());
base::DictionaryValue constants;
constants.SetKey("clientInfo", std::move(client_constants));
return constants;
}
blink::OriginTrialPolicy* ShellContentClient::GetOriginTrialPolicy() { blink::OriginTrialPolicy* ShellContentClient::GetOriginTrialPolicy() {
return &origin_trial_policy_; return &origin_trial_policy_;
} }
......
...@@ -29,6 +29,7 @@ class ShellContentClient : public ContentClient { ...@@ -29,6 +29,7 @@ class ShellContentClient : public ContentClient {
base::RefCountedMemory* GetDataResourceBytes( base::RefCountedMemory* GetDataResourceBytes(
int resource_id) const override; int resource_id) const override;
gfx::Image& GetNativeImageNamed(int resource_id) const override; gfx::Image& GetNativeImageNamed(int resource_id) const override;
base::DictionaryValue GetNetLogConstants() const override;
blink::OriginTrialPolicy* GetOriginTrialPolicy() override; blink::OriginTrialPolicy* GetOriginTrialPolicy() override;
private: private:
......
...@@ -29,19 +29,14 @@ MojoNetLog::~MojoNetLog() { ...@@ -29,19 +29,14 @@ MojoNetLog::~MojoNetLog() {
base::OnceClosure()); base::OnceClosure());
} }
void MojoNetLog::ProcessCommandLine(const base::CommandLine& command_line) { void MojoNetLog::ObserveFileWithConstants(base::File file,
if (!command_line.HasSwitch(switches::kLogNetLog)) base::Value constants) {
return;
base::FilePath log_path =
command_line.GetSwitchValuePath(switches::kLogNetLog);
// TODO(eroman): Should get capture mode from the command line. // TODO(eroman): Should get capture mode from the command line.
net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode capture_mode =
net::NetLogCaptureMode::IncludeCookiesAndCredentials(); net::NetLogCaptureMode::IncludeCookiesAndCredentials();
file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded( file_net_log_observer_ = net::FileNetLogObserver::CreateUnboundedPreExisting(
log_path, nullptr /* constants */); std::move(file), std::make_unique<base::Value>(std::move(constants)));
file_net_log_observer_->StartObserving(this, capture_mode); file_net_log_observer_->StartObserving(this, capture_mode);
} }
......
...@@ -12,10 +12,6 @@ ...@@ -12,10 +12,6 @@
#include "net/log/net_log.h" #include "net/log/net_log.h"
#include "services/network/public/mojom/network_service.mojom.h" #include "services/network/public/mojom/network_service.mojom.h"
namespace base {
class CommandLine;
} // namespace base
namespace net { namespace net {
class FileNetLogObserver; class FileNetLogObserver;
} // namespace net } // namespace net
...@@ -35,7 +31,7 @@ class MojoNetLog : public net::NetLog { ...@@ -35,7 +31,7 @@ class MojoNetLog : public net::NetLog {
// If specified by the command line, stream network events (NetLog) to a // If specified by the command line, stream network events (NetLog) to a
// file on disk. This will last for the duration of the process. // file on disk. This will last for the duration of the process.
void ProcessCommandLine(const base::CommandLine& command_line); void ObserveFileWithConstants(base::File file, base::Value constants);
private: private:
std::unique_ptr<net::FileNetLogObserver> file_net_log_observer_; std::unique_ptr<net::FileNetLogObserver> file_net_log_observer_;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "net/dns/host_resolver.h" #include "net/dns/host_resolver.h"
#include "net/dns/mapped_host_resolver.h" #include "net/dns/mapped_host_resolver.h"
#include "net/http/http_auth_handler_factory.h" #include "net/http/http_auth_handler_factory.h"
#include "net/log/file_net_log_observer.h"
#include "net/log/net_log.h" #include "net/log/net_log.h"
#include "net/log/net_log_util.h" #include "net/log/net_log_util.h"
#include "net/url_request/url_request_context_builder.h" #include "net/url_request/url_request_context_builder.h"
...@@ -138,7 +139,6 @@ NetworkService::NetworkService( ...@@ -138,7 +139,6 @@ NetworkService::NetworkService(
// Note: The command line switches are only checked when not using the // Note: The command line switches are only checked when not using the
// embedder's NetLog, as it may already be writing to the destination log // embedder's NetLog, as it may already be writing to the destination log
// file. // file.
owned_net_log_->ProcessCommandLine(*command_line);
net_log_ = owned_net_log_.get(); net_log_ = owned_net_log_.get();
} }
...@@ -237,6 +237,16 @@ void NetworkService::SetClient(mojom::NetworkServiceClientPtr client) { ...@@ -237,6 +237,16 @@ void NetworkService::SetClient(mojom::NetworkServiceClientPtr client) {
client_ = std::move(client); client_ = std::move(client);
} }
void NetworkService::StartNetLog(base::File file,
base::Value client_constants) {
DCHECK(client_constants.is_dict());
std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
constants->MergeDictionary(&client_constants);
owned_net_log_->ObserveFileWithConstants(std::move(file),
std::move(*constants));
}
void NetworkService::CreateNetworkContext( void NetworkService::CreateNetworkContext(
mojom::NetworkContextRequest request, mojom::NetworkContextRequest request,
mojom::NetworkContextParamsPtr params) { mojom::NetworkContextParamsPtr params) {
......
...@@ -119,6 +119,7 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkService ...@@ -119,6 +119,7 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkService
// mojom::NetworkService implementation: // mojom::NetworkService implementation:
void SetClient(mojom::NetworkServiceClientPtr client) override; void SetClient(mojom::NetworkServiceClientPtr client) override;
void StartNetLog(base::File file, base::Value constants) override;
void CreateNetworkContext(mojom::NetworkContextRequest request, void CreateNetworkContext(mojom::NetworkContextRequest request,
mojom::NetworkContextParamsPtr params) override; mojom::NetworkContextParamsPtr params) override;
void ConfigureStubHostResolver( void ConfigureStubHostResolver(
......
...@@ -5,7 +5,10 @@ ...@@ -5,7 +5,10 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_file_value_serializer.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/path_service.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
...@@ -729,6 +732,38 @@ TEST_F(NetworkServiceTestWithService, Basic) { ...@@ -729,6 +732,38 @@ TEST_F(NetworkServiceTestWithService, Basic) {
EXPECT_EQ(net::OK, client()->completion_status().error_code); EXPECT_EQ(net::OK, client()->completion_status().error_code);
} }
// Verifies that a passed net log file is successfully opened and sane data
// written to it.
TEST_F(NetworkServiceTestWithService, StartsNetLog) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath log_dir = temp_dir.GetPath();
base::FilePath log_path = log_dir.Append(FILE_PATH_LITERAL("test_log.json"));
base::DictionaryValue dict;
dict.SetString("amiatest", "iamatest");
base::File log_file(log_path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
network_service_->StartNetLog(std::move(log_file), std::move(dict));
CreateNetworkContext();
LoadURL(test_server()->GetURL("/echo"));
EXPECT_EQ(net::OK, client()->completion_status().error_code);
// |log_file| is closed on destruction of the NetworkService.
Shutdown();
// |log_file| is closed on another thread, so have to wait for that to happen.
RunUntilIdle();
JSONFileValueDeserializer deserializer(log_path);
std::unique_ptr<base::Value> log_dict =
deserializer.Deserialize(nullptr, nullptr);
ASSERT_TRUE(log_dict);
ASSERT_EQ(log_dict->FindKey("constants")->FindKey("amiatest")->GetString(),
"iamatest");
}
// Verifies that raw headers are only reported if requested. // Verifies that raw headers are only reported if requested.
TEST_F(NetworkServiceTestWithService, RawRequestHeadersAbsent) { TEST_F(NetworkServiceTestWithService, RawRequestHeadersAbsent) {
CreateNetworkContext(); CreateNetworkContext();
......
...@@ -6,6 +6,7 @@ module network.mojom; ...@@ -6,6 +6,7 @@ module network.mojom;
import "mojo/public/mojom/base/file.mojom"; import "mojo/public/mojom/base/file.mojom";
import "mojo/public/mojom/base/file_path.mojom"; import "mojo/public/mojom/base/file_path.mojom";
import "mojo/public/mojom/base/values.mojom";
import "services/network/public/mojom/cookie_manager.mojom"; import "services/network/public/mojom/cookie_manager.mojom";
import "services/network/public/mojom/network_change_manager.mojom"; import "services/network/public/mojom/network_change_manager.mojom";
import "services/network/public/mojom/network_context.mojom"; import "services/network/public/mojom/network_context.mojom";
...@@ -183,6 +184,15 @@ struct CryptConfig { ...@@ -183,6 +184,15 @@ struct CryptConfig {
interface NetworkService { interface NetworkService {
SetClient(NetworkServiceClient client); SetClient(NetworkServiceClient client);
// Starts observing the NetLog event stream and writing entries to |file|.
// |constants| is a legend used for decoding constant values in the log; it
// will be merged with the |GetNetConstants()| dictionary before being passed
// through to the FileObserver. (See |FileNetLogObserver::CreateBounded()|
// for more details). Most clients will just be adding a dictionary under
// the key "clientInfo".
StartNetLog(mojo_base.mojom.File file,
mojo_base.mojom.DictionaryValue constants);
// Creates a new network context with the given parameters. // Creates a new network context with the given parameters.
CreateNetworkContext(NetworkContext& context, CreateNetworkContext(NetworkContext& context,
NetworkContextParams params); NetworkContextParams params);
......
...@@ -60,6 +60,15 @@ void ServiceTest::OnStartCalled(Connector* connector, ...@@ -60,6 +60,15 @@ void ServiceTest::OnStartCalled(Connector* connector,
initialize_called_.Run(); initialize_called_.Run();
} }
void ServiceTest::Shutdown() {
background_service_manager_.reset();
context_.reset();
}
void ServiceTest::RunUntilIdle() {
scoped_task_environment_.RunUntilIdle();
}
void ServiceTest::SetUp() { void ServiceTest::SetUp() {
background_service_manager_ = background_service_manager_ =
std::make_unique<service_manager::BackgroundServiceManager>( std::make_unique<service_manager::BackgroundServiceManager>(
...@@ -80,8 +89,7 @@ void ServiceTest::SetUp() { ...@@ -80,8 +89,7 @@ void ServiceTest::SetUp() {
} }
void ServiceTest::TearDown() { void ServiceTest::TearDown() {
background_service_manager_.reset(); Shutdown();
context_.reset();
} }
} // namespace test } // namespace test
......
...@@ -91,6 +91,15 @@ class ServiceTest : public testing::Test { ...@@ -91,6 +91,15 @@ class ServiceTest : public testing::Test {
const std::string& name, const std::string& name,
const std::string& userid); const std::string& userid);
// Explicitly shuts down the ServiceManager and |context_|. This is called
// from TearDown(), but may be called explicitly to test shutdown behavior.
void Shutdown();
// Calls RunUntilIdle() on the current process's ScopedTaskEnvironment. Does
// not wait until the task environments of other processes, if there are any,
// are idle.
void RunUntilIdle();
// testing::Test: // testing::Test:
void SetUp() override; void SetUp() override;
void TearDown() override; void TearDown() override;
......
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