Merge pull request #113 from deepak1556/net_log_patch

adding support for kLogNetLog switch
This commit is contained in:
Cheng Zhao 2015-06-08 23:13:03 +08:00
commit a56c132835
5 changed files with 140 additions and 1 deletions

View file

@ -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<base::DictionaryValue> 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<base::Value> 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<net::URLRequestContext*> 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

View file

@ -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_

View file

@ -6,6 +6,7 @@
#include <algorithm>
#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<net::HostResolver> host_resolver(net::HostResolver::CreateDefaultResolver(NULL));
scoped_ptr<net::HostResolver> 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))

View file

@ -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<net::ProxyConfigService> proxy_config_service_;
scoped_ptr<net::NetLog> net_log_;
scoped_ptr<net::NetworkDelegate> network_delegate_;
scoped_ptr<net::URLRequestContextStorage> storage_;
scoped_ptr<net::URLRequestContext> url_request_context_;

View file

@ -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',