From 7496cb29fee3dfc8c0ab1a256ad78b27bf21a1d2 Mon Sep 17 00:00:00 2001 From: Robo Date: Tue, 14 Jul 2015 23:12:52 +0530 Subject: [PATCH] create netlog for devtoolsnetlogobserver --- brightray/browser/browser_client.cc | 4 ++ brightray/browser/browser_client.h | 1 + brightray/browser/browser_context.cc | 4 ++ brightray/browser/browser_context.h | 1 + brightray/browser/net_log.cc | 58 ++++++++++--------- .../browser/url_request_context_getter.cc | 6 +- .../browser/url_request_context_getter.h | 1 + 7 files changed, 44 insertions(+), 31 deletions(-) diff --git a/brightray/browser/browser_client.cc b/brightray/browser/browser_client.cc index 887ad591d5c6..493e896366fa 100644 --- a/brightray/browser/browser_client.cc +++ b/brightray/browser/browser_client.cc @@ -80,6 +80,10 @@ void BrowserClient::GetAdditionalAllowedSchemesForFileSystem( additional_schemes->push_back(content::kChromeUIScheme); } +net::NetLog* BrowserClient::GetNetLog() { + return browser_context()->GetNetLog(); +} + base::FilePath BrowserClient::GetDefaultDownloadDirectory() { // ~/Downloads base::FilePath path; diff --git a/brightray/browser/browser_client.h b/brightray/browser/browser_client.h index cd3c0222310e..f9b0f0d05852 100644 --- a/brightray/browser/browser_client.h +++ b/brightray/browser/browser_client.h @@ -43,6 +43,7 @@ class BrowserClient : public content::ContentBrowserClient { content::PlatformNotificationService* GetPlatformNotificationService() override; void GetAdditionalAllowedSchemesForFileSystem( std::vector* additional_schemes) override; + net::NetLog* GetNetLog() override; base::FilePath GetDefaultDownloadDirectory() override; content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override; diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 1c61e286b1b7..ec41caed2558 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -114,6 +114,10 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext( return url_request_getter_.get(); } +net::NetLog* BrowserContext::GetNetLog() { + return url_request_getter_->net_log(); +} + net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() { return new NetworkDelegate; } diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 0a7a4c314ac4..9244ac40c2f6 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -48,6 +48,7 @@ class BrowserContext : public content::BrowserContext, net::URLRequestContextGetter* CreateRequestContext( content::ProtocolHandlerMap* protocol_handlers, content::URLRequestInterceptorScopedVector protocol_interceptors); + net::NetLog* GetNetLog(); net::URLRequestContextGetter* url_request_context_getter() const { return url_request_getter_.get(); diff --git a/brightray/browser/net_log.cc b/brightray/browser/net_log.cc index f8c54342dfb1..6a2b9c3d76f8 100644 --- a/brightray/browser/net_log.cc +++ b/brightray/browser/net_log.cc @@ -36,44 +36,48 @@ 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 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().Pass()); - base::JSONWriter::Write(constants.get(), &json); - fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str()); - fprintf(log_file_.get(), "\"events\": [\n"); + if (!log_file_) { + LOG(ERROR) << "Could not open file: " << log_path.value() + << "for net logging"; + } else { + std::string json; + scoped_ptr constants(GetConstants().Pass()); + 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()); + if (context_) { + DCHECK(context_->CalledOnValidThread()); - std::set contexts; - contexts.insert(context_); + std::set contexts; + contexts.insert(context_); - net::CreateNetLogEntriesForActiveObjects(contexts, this); + net::CreateNetLogEntriesForActiveObjects(contexts, this); + } + + DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA); } - - DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA); } } NetLog::~NetLog() { - DeprecatedRemoveObserver(this); + if (log_file_) { + DeprecatedRemoveObserver(this); - // Ending events array. - fprintf(log_file_.get(), "]}"); - log_file_.reset(); + // Ending events array. + fprintf(log_file_.get(), "]}"); + log_file_.reset(); + } } void NetLog::OnAddEntry(const net::NetLog::Entry& entry) { diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 256ec4005d1a..12e4f348fe2f 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -150,10 +150,8 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { 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()); - } + 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 45906bbb720c..a901139da126 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -57,6 +57,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { scoped_refptr GetNetworkTaskRunner() const override; net::HostResolver* host_resolver(); + net::NetLog* net_log() { return net_log_.get(); } private: Delegate* delegate_;