From ee6b14d1d84f76d31e8e6e44b1bf994ee655cbf5 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Fri, 5 Jun 2015 20:24:38 +0530 Subject: [PATCH 1/5] adding support for kLogNetLog switch --- brightray/browser/net_log.cc | 66 +++++++++++++++++++ brightray/browser/net_log.h | 36 ++++++++++ .../browser/url_request_context_getter.cc | 5 +- brightray/filenames.gypi | 2 + 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 brightray/browser/net_log.cc create mode 100644 brightray/browser/net_log.h diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc new file mode 100644 index 00000000000..4090b9c1080 --- /dev/null +++ b/brightray/browser/net_log.cc @@ -0,0 +1,66 @@ +// 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 brightray { + +NetLog::NetLog(net::URLRequestContext* context) + : added_events_(false), + context_(context) { + auto command_line = base::CommandLine::ForCurrentProcess(); + + if (command_line->HasSwitch(switches::kLogNetLog)) { + 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"; + + fprintf(log_file_.get(), "{\"events\": [\n"); + + if (context_.get()) { + DCHECK(context_->CalledOnValidThread()); + + std::set contexts; + contexts.insert(context_.get()); + + net::CreateNetLogEntriesForActiveObjects(contexts, this); + } + + DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA); + } +} + +NetLog::~NetLog() { + DeprecatedRemoveObserver(this); + 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 00000000000..37a992a2f16 --- /dev/null +++ b/brightray/browser/net_log.h @@ -0,0 +1,36 @@ +// 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); + virtual ~NetLog(); + + void OnAddEntry(const net::NetLog::Entry& entry) override; + + private: + bool added_events_; + scoped_ptr 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 03a3fa156f7..0fda6500150 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" @@ -146,6 +147,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { auto& command_line = *base::CommandLine::ForCurrentProcess(); if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext); + url_request_context_->set_net_log(new NetLog(url_request_context_.get())); network_delegate_.reset(delegate_->CreateNetworkDelegate()); url_request_context_->set_network_delegate(network_delegate_.get()); @@ -161,7 +163,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 +228,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/filenames.gypi b/brightray/filenames.gypi index e1a152521bd..fda1bb8d1ef 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', From 10223577008510eececdc30b6b9f8f025a7b5c36 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Sat, 6 Jun 2015 00:13:10 +0530 Subject: [PATCH 2/5] adding netconstants to log --- brightray/browser/net_log.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc index 4090b9c1080..d0aa4abd216 100644 --- a/brightray/browser/net_log.cc +++ b/brightray/browser/net_log.cc @@ -34,7 +34,11 @@ NetLog::NetLog(net::URLRequestContext* context) LOG(ERROR) << "Could not open file: " << log_path.value() << "for net logging"; - fprintf(log_file_.get(), "{\"events\": [\n"); + std::string json; + scoped_ptr constants = net::GetNetConstants(); + base::JSONWriter::Write(constants.release(), &json); + fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str()); + fprintf(log_file_.get(), "\"events\": [\n"); if (context_.get()) { DCHECK(context_->CalledOnValidThread()); From 33f65ba98152c9ec8366f4a324f5f2f740de068f Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Sat, 6 Jun 2015 14:33:07 +0530 Subject: [PATCH 3/5] fix crash on quit --- brightray/browser/net_log.cc | 28 ++++++++++++++++--- brightray/browser/net_log.h | 5 ++-- .../browser/url_request_context_getter.cc | 4 ++- .../browser/url_request_context_getter.h | 2 ++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc index d0aa4abd216..e4be6c70035 100644 --- a/brightray/browser/net_log.cc +++ b/brightray/browser/net_log.cc @@ -13,6 +13,25 @@ #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("name", "Electron"); + 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) @@ -35,16 +54,15 @@ NetLog::NetLog(net::URLRequestContext* context) << "for net logging"; std::string json; - scoped_ptr constants = net::GetNetConstants(); - base::JSONWriter::Write(constants.release(), &json); + base::JSONWriter::Write(GetConstants(), &json); fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str()); fprintf(log_file_.get(), "\"events\": [\n"); - if (context_.get()) { + if (context_) { DCHECK(context_->CalledOnValidThread()); std::set contexts; - contexts.insert(context_.get()); + contexts.insert(context_); net::CreateNetLogEntriesForActiveObjects(contexts, this); } @@ -55,6 +73,8 @@ NetLog::NetLog(net::URLRequestContext* context) NetLog::~NetLog() { DeprecatedRemoveObserver(this); + + // Ending events array. fprintf(log_file_.get(), "]}"); log_file_.reset(); } diff --git a/brightray/browser/net_log.h b/brightray/browser/net_log.h index 37a992a2f16..4aade754f35 100644 --- a/brightray/browser/net_log.h +++ b/brightray/browser/net_log.h @@ -19,13 +19,14 @@ class NetLog : public net::NetLog, public net::NetLog::ThreadSafeObserver { public: explicit NetLog(net::URLRequestContext* context); - virtual ~NetLog(); + ~NetLog() override; void OnAddEntry(const net::NetLog::Entry& entry) override; private: bool added_events_; - scoped_ptr context_; + // We use raw pointer to prevent reference cycle. + net::URLRequestContext* const context_; base::ScopedFILE log_file_; DISALLOW_COPY_AND_ASSIGN(NetLog); diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 0fda6500150..0ab19af5217 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -22,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" @@ -147,7 +148,8 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { auto& command_line = *base::CommandLine::ForCurrentProcess(); if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext); - url_request_context_->set_net_log(new NetLog(url_request_context_.get())); + 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()); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index d1f847fc968..45906bbb720 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_; From 15255944b634c32127992af2bb445d9f6ae41c7e Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 8 Jun 2015 19:19:44 +0530 Subject: [PATCH 4/5] create net log instance only when needed --- brightray/browser/net_log.cc | 25 ++++++++----------- .../browser/url_request_context_getter.cc | 9 +++++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc index e4be6c70035..7861a96ecf0 100644 --- a/brightray/browser/net_log.cc +++ b/brightray/browser/net_log.cc @@ -21,7 +21,6 @@ base::Value* GetConstants() { // Adding client information to constants dictionary. base::DictionaryValue* client_info = new base::DictionaryValue(); - client_info->SetString("name", "Electron"); client_info->SetString("command_line", base::CommandLine::ForCurrentProcess()->GetCommandLineString()); @@ -38,21 +37,19 @@ 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 (command_line->HasSwitch(switches::kLogNetLog)) { - 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"; + #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; base::JSONWriter::Write(GetConstants(), &json); fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str()); diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 0ab19af5217..256ec4005d1 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -148,8 +148,13 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { auto& command_line = *base::CommandLine::ForCurrentProcess(); if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext); - net_log_.reset(new NetLog(url_request_context_.get())); - url_request_context_->set_net_log(net_log_.get()); + + // --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()); From 2ba119f3958cdf61fc8774121c530f922dd9daeb Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 8 Jun 2015 19:56:48 +0530 Subject: [PATCH 5/5] fix memory leak --- brightray/browser/net_log.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc index 7861a96ecf0..6959ffbbe64 100644 --- a/brightray/browser/net_log.cc +++ b/brightray/browser/net_log.cc @@ -51,7 +51,8 @@ NetLog::NetLog(net::URLRequestContext* context) << "for net logging"; } else { std::string json; - base::JSONWriter::Write(GetConstants(), &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");