From 33f65ba98152c9ec8366f4a324f5f2f740de068f Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Sat, 6 Jun 2015 14:33:07 +0530 Subject: [PATCH] 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 d0aa4abd2161..e4be6c70035d 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 37a992a2f161..4aade754f354 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 0fda65001501..0ab19af5217a 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 d1f847fc968e..45906bbb720c 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_;