diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc new file mode 100644 index 0000000000..6959ffbbe6 --- /dev/null +++ b/brightray/browser/net_log.cc @@ -0,0 +1,88 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "browser/net_log.h" + +#include "browser/browser_context.h" +#include "base/command_line.h" +#include "base/files/file_util.h" +#include "base/json/json_writer.h" +#include "base/logging.h" +#include "content/public/common/content_switches.h" +#include "net/log/net_log_util.h" +#include "net/url_request/url_request_context.h" + +namespace { + +base::Value* GetConstants() { + scoped_ptr constants = net::GetNetConstants(); + + // Adding client information to constants dictionary. + base::DictionaryValue* client_info = new base::DictionaryValue(); + + client_info->SetString("command_line", + base::CommandLine::ForCurrentProcess()->GetCommandLineString()); + + constants->Set("clientInfo", client_info); + + return constants.release(); +} + +} // namespace + +namespace brightray { + +NetLog::NetLog(net::URLRequestContext* context) + : added_events_(false), + context_(context) { + auto command_line = base::CommandLine::ForCurrentProcess(); + base::FilePath log_path = + command_line->GetSwitchValuePath(switches::kLogNetLog); + + #if defined(OS_WIN) + log_file_.reset(_wfopen(log_path.value().c_str(), L"w")); + #elif defined(OS_POSIX) + log_file_.reset(fopen(log_path.value().c_str(), "w")); + #endif + + if (!log_file_) { + LOG(ERROR) << "Could not open file: " << log_path.value() + << "for net logging"; + } else { + std::string json; + scoped_ptr constants(GetConstants()); + base::JSONWriter::Write(constants.get(), &json); + fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str()); + fprintf(log_file_.get(), "\"events\": [\n"); + + if (context_) { + DCHECK(context_->CalledOnValidThread()); + + std::set contexts; + contexts.insert(context_); + + net::CreateNetLogEntriesForActiveObjects(contexts, this); + } + + DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA); + } +} + +NetLog::~NetLog() { + DeprecatedRemoveObserver(this); + + // Ending events array. + fprintf(log_file_.get(), "]}"); + log_file_.reset(); +} + +void NetLog::OnAddEntry(const net::NetLog::Entry& entry) { + std::string json; + base::JSONWriter::Write(entry.ToValue(), &json); + + fprintf(log_file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); + added_events_ = true; +} + +} // namespace brightray diff --git a/brightray/browser/net_log.h b/brightray/browser/net_log.h new file mode 100644 index 0000000000..4aade754f3 --- /dev/null +++ b/brightray/browser/net_log.h @@ -0,0 +1,37 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef BROWSER_NET_LOG_H_ +#define BROWSER_NET_LOG_H_ + +#include "base/files/file_path.h" +#include "base/files/scoped_file.h" +#include "net/log/net_log.h" + +namespace net { +class URLRequestContext; +} + +namespace brightray { + +class NetLog : public net::NetLog, + public net::NetLog::ThreadSafeObserver { + public: + explicit NetLog(net::URLRequestContext* context); + ~NetLog() override; + + void OnAddEntry(const net::NetLog::Entry& entry) override; + + private: + bool added_events_; + // We use raw pointer to prevent reference cycle. + net::URLRequestContext* const context_; + base::ScopedFILE log_file_; + + DISALLOW_COPY_AND_ASSIGN(NetLog); +}; + +} // namespace brightray + +#endif // BROWSER_NET_LOG_H_ diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 03a3fa156f..256ec4005d 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -6,6 +6,7 @@ #include +#include "browser/net_log.h" #include "browser/network_delegate.h" #include "base/command_line.h" @@ -21,6 +22,7 @@ #include "net/dns/mapped_host_resolver.h" #include "net/http/http_auth_handler_factory.h" #include "net/http/http_server_properties_impl.h" +#include "net/log/net_log.h" #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" #include "net/proxy/proxy_config_service.h" #include "net/proxy/proxy_script_fetcher_impl.h" @@ -146,6 +148,13 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { auto& command_line = *base::CommandLine::ForCurrentProcess(); if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext); + + // --log-net-log + if (command_line.HasSwitch(switches::kLogNetLog)) { + net_log_.reset(new NetLog(url_request_context_.get())); + url_request_context_->set_net_log(net_log_.get()); + } + network_delegate_.reset(delegate_->CreateNetworkDelegate()); url_request_context_->set_network_delegate(network_delegate_.get()); @@ -161,7 +170,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { storage_->set_http_user_agent_settings(new net::StaticHttpUserAgentSettings( "en-us,en", base::EmptyString())); - scoped_ptr host_resolver(net::HostResolver::CreateDefaultResolver(NULL)); + scoped_ptr host_resolver(net::HostResolver::CreateDefaultResolver(nullptr)); // --host-resolver-rules if (command_line.HasSwitch(switches::kHostResolverRules)) { @@ -226,6 +235,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { url_request_context_->channel_id_service(); network_session_params.http_auth_handler_factory = url_request_context_->http_auth_handler_factory(); + network_session_params.net_log = url_request_context_->net_log(); // --ignore-certificate-errors if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index d1f847fc96..45906bbb72 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -17,6 +17,7 @@ class MessageLoop; } namespace net { +class NetLog; class HostMappingRules; class HostResolver; class NetworkDelegate; @@ -65,6 +66,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { base::MessageLoop* file_loop_; scoped_ptr proxy_config_service_; + scoped_ptr net_log_; scoped_ptr network_delegate_; scoped_ptr storage_; scoped_ptr url_request_context_; diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index e1a152521b..fda1bb8d1e 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -37,6 +37,8 @@ 'browser/media/media_capture_devices_dispatcher.h', 'browser/media/media_stream_devices_controller.cc', 'browser/media/media_stream_devices_controller.h', + 'browser/net_log.cc', + 'browser/net_log.h', 'browser/network_delegate.cc', 'browser/network_delegate.h', 'browser/notification_presenter.h',