From ee6b14d1d84f76d31e8e6e44b1bf994ee655cbf5 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Fri, 5 Jun 2015 20:24:38 +0530 Subject: [PATCH] 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 0000000000..4090b9c108 --- /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 0000000000..37a992a2f1 --- /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 03a3fa156f..0fda650015 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 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',