diff options
author | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-12 16:21:45 +0000 |
---|---|---|
committer | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-12 16:21:45 +0000 |
commit | cd56514d9a698e7c185fd1f4850e6c8d21df4d61 (patch) | |
tree | c6ddf593d001a19093406f460ef588832f7545b2 | |
parent | ee4dd68fe9bd0b42421cacf87d989d6cc65f982e (diff) | |
download | chromium_src-cd56514d9a698e7c185fd1f4850e6c8d21df4d61.zip chromium_src-cd56514d9a698e7c185fd1f4850e6c8d21df4d61.tar.gz chromium_src-cd56514d9a698e7c185fd1f4850e6c8d21df4d61.tar.bz2 |
NetLogEventParameter to Callback refactoring 1,
Get rid of all uses of NetLogEventParameters in net/base,
with the exception of net_log itself, of course.
R=eroman@chromium.org
BUG=126243
Review URL: https://chromiumcodereview.appspot.com/10539094
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141666 0039d316-1c4b-4281-b951-d872f2087c98
24 files changed, 241 insertions, 359 deletions
diff --git a/net/base/address_list.cc b/net/base/address_list.cc index e5935c7..93acb96 100644 --- a/net/base/address_list.cc +++ b/net/base/address_list.cc @@ -4,12 +4,32 @@ #include "net/base/address_list.h" +#include "base/bind.h" #include "base/logging.h" +#include "base/values.h" #include "net/base/net_util.h" #include "net/base/sys_addrinfo.h" namespace net { +namespace { + +base::Value* NetLogAddressListCallback(const AddressList* address_list, + NetLog::LogLevel log_level) { + DictionaryValue* dict = new DictionaryValue(); + ListValue* list = new ListValue(); + + for (AddressList::const_iterator it = address_list->begin(); + it != address_list->end(); ++it) { + list->Append(Value::CreateStringValue(it->ToString())); + } + + dict->Set("address_list", list); + return dict; +} + +} // namespace + AddressList::AddressList() {} AddressList::~AddressList() {} @@ -59,11 +79,14 @@ void AddressList::SetDefaultCanonicalName() { set_canonical_name(front().ToStringWithoutPort()); } +NetLog::ParametersCallback AddressList::CreateNetLogCallback() const { + return base::Bind(&NetLogAddressListCallback, this); +} + void SetPortOnAddressList(uint16 port, AddressList* list) { DCHECK(list); - for (AddressList::iterator it = list->begin(); it != list->end(); ++it) { + for (AddressList::iterator it = list->begin(); it != list->end(); ++it) *it = IPEndPoint(it->address(), port); - } } } // namespace net diff --git a/net/base/address_list.h b/net/base/address_list.h index 8d544b6..f213058 100644 --- a/net/base/address_list.h +++ b/net/base/address_list.h @@ -13,6 +13,7 @@ #include "base/compiler_specific.h" #include "net/base/ip_endpoint.h" #include "net/base/net_export.h" +#include "net/base/net_log.h" #include "net/base/net_util.h" struct addrinfo; @@ -50,6 +51,11 @@ class NET_EXPORT AddressList // Sets canonical name to the literal of the first IP address on the list. void SetDefaultCanonicalName(); + // Creates a callback for use with the NetLog that returns a Value + // representation of the address list. The callback must be destroyed before + // |this| is. + NetLog::ParametersCallback CreateNetLogCallback() const; + // Exposed methods from std::vector. using std::vector<IPEndPoint>::size; using std::vector<IPEndPoint>::empty; @@ -70,7 +76,7 @@ class NET_EXPORT AddressList using std::vector<IPEndPoint>::rend; private: - // TODO(szym): Remove. http://crbug.com/126134 + // TODO(szym): Remove. http://crbug.com/126134 std::string canonical_name_; }; diff --git a/net/base/address_list_net_log_param.cc b/net/base/address_list_net_log_param.cc deleted file mode 100644 index 78fa660..0000000 --- a/net/base/address_list_net_log_param.cc +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "net/base/address_list_net_log_param.h" - -#include "base/values.h" -#include "net/base/net_util.h" - -namespace net { - -AddressListNetLogParam::AddressListNetLogParam(const AddressList& address_list) - : address_list_(address_list) { -} - -Value* AddressListNetLogParam::ToValue() const { - DictionaryValue* dict = new DictionaryValue(); - ListValue* list = new ListValue(); - - for (size_t i = 0; i < address_list_.size() ; ++i) { - list->Append(Value::CreateStringValue(address_list_[i].ToString())); - } - - dict->Set("address_list", list); - return dict; -} - -} // namespace diff --git a/net/base/address_list_net_log_param.h b/net/base/address_list_net_log_param.h deleted file mode 100644 index c64150c..0000000 --- a/net/base/address_list_net_log_param.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef NET_BASE_ADDRESS_LIST_NET_LOG_PARAM_H_ -#define NET_BASE_ADDRESS_LIST_NET_LOG_PARAM_H_ -#pragma once - -#include "net/base/address_list.h" -#include "net/base/net_log.h" - -namespace net { - -// NetLog parameter to describe an address list. -// Note that AddressList uses ref-counted data, so this doesn't introduce -// much of a memory overhead. -class AddressListNetLogParam : public NetLog::EventParameters { - public: - explicit AddressListNetLogParam(const AddressList& address_list); - - virtual base::Value* ToValue() const OVERRIDE; - - protected: - virtual ~AddressListNetLogParam() {} - - private: - AddressList address_list_; -}; - -} // namespace net - -#endif // NET_BASE_ADDRESS_LIST_NET_LOG_PARAM_H_ diff --git a/net/base/file_stream_net_log_parameters.cc b/net/base/file_stream_net_log_parameters.cc index 731e739..c268ecc 100644 --- a/net/base/file_stream_net_log_parameters.cc +++ b/net/base/file_stream_net_log_parameters.cc @@ -8,19 +8,16 @@ namespace net { -FileStreamErrorParameters::FileStreamErrorParameters( - const std::string& operation, int os_error, net::Error net_error) - : operation_(operation), os_error_(os_error), net_error_(net_error) { -} - -FileStreamErrorParameters::~FileStreamErrorParameters() {} - -Value* FileStreamErrorParameters::ToValue() const { +base::Value* NetLogFileStreamErrorCallback( + FileErrorSource source, + int os_error, + net::Error net_error, + NetLog::LogLevel /* log_level */) { DictionaryValue* dict = new DictionaryValue(); - dict->SetString("operation", operation_); - dict->SetInteger("os_error", os_error_); - dict->SetInteger("net_error", net_error_); + dict->SetString("operation", GetFileErrorSourceName(source)); + dict->SetInteger("os_error", os_error); + dict->SetInteger("net_error", net_error); return dict; } diff --git a/net/base/file_stream_net_log_parameters.h b/net/base/file_stream_net_log_parameters.h index a1630b8..4c15a38 100644 --- a/net/base/file_stream_net_log_parameters.h +++ b/net/base/file_stream_net_log_parameters.h @@ -10,29 +10,18 @@ #include <string> +#include "net/base/file_stream_metrics.h" #include "net/base/net_errors.h" #include "net/base/net_log.h" namespace net { -// NetLog parameters when a FileStream has an error. -class FileStreamErrorParameters : public net::NetLog::EventParameters { - public: - FileStreamErrorParameters(const std::string& operation, - int os_error, - net::Error net_error); - virtual base::Value* ToValue() const OVERRIDE; - - protected: - virtual ~FileStreamErrorParameters(); - - private: - std::string operation_; - int os_error_; - net::Error net_error_; - - DISALLOW_COPY_AND_ASSIGN(FileStreamErrorParameters); -}; +// Creates NetLog parameters when a FileStream has an error. +base::Value* NetLogFileStreamErrorCallback( + FileErrorSource source, + int os_error, + net::Error net_error, + NetLog::LogLevel log_level); } // namespace net diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index bbec855..26e3fed 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -59,10 +59,8 @@ int RecordAndMapError(int error, bound_net_log.AddEvent( net::NetLog::TYPE_FILE_STREAM_ERROR, - make_scoped_refptr( - new FileStreamErrorParameters(GetFileErrorSourceName(source), - error, - net_error))); + base::Bind(&NetLogFileStreamErrorCallback, + source, error, net_error)); RecordFileError(error, source, record_uma); @@ -77,11 +75,10 @@ void OpenFile(const FilePath& path, base::PlatformFile* file, int* result, const net::BoundNetLog& bound_net_log) { + std::string file_name = path.AsUTF8Unsafe(); bound_net_log.BeginEvent( net::NetLog::TYPE_FILE_STREAM_OPEN, - make_scoped_refptr( - new net::NetLogStringParameter("file_name", - path.AsUTF8Unsafe()))); + NetLog::StringCallback("file_name", &file_name)); *result = OK; *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); @@ -615,15 +612,11 @@ void FileStreamPosix::SetBoundNetLogSource( bound_net_log_.AddEvent( net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, - make_scoped_refptr( - new net::NetLogSourceParameter("source_dependency", - owner_bound_net_log.source()))); + owner_bound_net_log.source().ToEventParametersCallback()); owner_bound_net_log.AddEvent( net::NetLog::TYPE_FILE_STREAM_SOURCE, - make_scoped_refptr( - new net::NetLogSourceParameter("source_dependency", - bound_net_log_.source()))); + bound_net_log_.source().ToEventParametersCallback()); } base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index 29a7e3d..fc41673 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -48,10 +48,8 @@ int RecordAndMapError(int error, bound_net_log.AddEvent( net::NetLog::TYPE_FILE_STREAM_ERROR, - make_scoped_refptr( - new FileStreamErrorParameters(GetFileErrorSourceName(source), - error, - net_error))); + base::Bind(&NetLogFileStreamErrorCallback, + source, error, net_error)); RecordFileError(error, source, record_uma); @@ -66,11 +64,10 @@ void OpenFile(const FilePath& path, base::PlatformFile* file, int* result, const net::BoundNetLog& bound_net_log) { + std::string file_name = path.AsUTF8Unsafe(); bound_net_log.BeginEvent( net::NetLog::TYPE_FILE_STREAM_OPEN, - make_scoped_refptr( - new net::NetLogStringParameter("file_name", - path.AsUTF8Unsafe()))); + NetLog::StringCallback("file_name", &file_name)); *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); if (*file == base::kInvalidPlatformFileValue) { @@ -656,15 +653,11 @@ void FileStreamWin::SetBoundNetLogSource( bound_net_log_.AddEvent( net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, - make_scoped_refptr( - new net::NetLogSourceParameter("source_dependency", - owner_bound_net_log.source()))); + owner_bound_net_log.source().ToEventParametersCallback()); owner_bound_net_log.AddEvent( net::NetLog::TYPE_FILE_STREAM_SOURCE, - make_scoped_refptr( - new net::NetLogSourceParameter("source_dependency", - bound_net_log_.source()))); + bound_net_log_.source().ToEventParametersCallback()); } base::PlatformFile FileStreamWin::GetPlatformFileForTesting() { diff --git a/net/base/host_resolver_impl.cc b/net/base/host_resolver_impl.cc index a66f950..5591d06 100644 --- a/net/base/host_resolver_impl.cc +++ b/net/base/host_resolver_impl.cc @@ -32,7 +32,6 @@ #include "base/values.h" #include "net/base/address_family.h" #include "net/base/address_list.h" -#include "net/base/address_list_net_log_param.h" #include "net/base/dns_reloader.h" #include "net/base/host_port_pair.h" #include "net/base/host_resolver_proc.h" @@ -177,177 +176,93 @@ void EnsurePortOnAddressList(uint16 port, AddressList* list) { SetPortOnAddressList(port, list); } -// Extra parameters to attach to the NetLog when the resolve failed. -class ProcTaskFailedParams : public NetLog::EventParameters { - public: - ProcTaskFailedParams(uint32 attempt_number, int net_error, int os_error) - : attempt_number_(attempt_number), - net_error_(net_error), - os_error_(os_error) { - } +// Creates NetLog parameters when the resolve failed. +base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number, + int net_error, + int os_error, + NetLog::LogLevel /* log_level */) { + DictionaryValue* dict = new DictionaryValue(); + if (attempt_number) + dict->SetInteger("attempt_number", attempt_number); - virtual Value* ToValue() const OVERRIDE { - DictionaryValue* dict = new DictionaryValue(); - if (attempt_number_) - dict->SetInteger("attempt_number", attempt_number_); + dict->SetInteger("net_error", net_error); - dict->SetInteger("net_error", net_error_); - - if (os_error_) { - dict->SetInteger("os_error", os_error_); + if (os_error) { + dict->SetInteger("os_error", os_error); #if defined(OS_POSIX) - dict->SetString("os_error_string", gai_strerror(os_error_)); + dict->SetString("os_error_string", gai_strerror(os_error)); #elif defined(OS_WIN) - // Map the error code to a human-readable string. - LPWSTR error_string = NULL; - int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM, - 0, // Use the internal message table. - os_error_, - 0, // Use default language. - (LPWSTR)&error_string, - 0, // Buffer size. - 0); // Arguments (unused). - dict->SetString("os_error_string", WideToUTF8(error_string)); - LocalFree(error_string); + // Map the error code to a human-readable string. + LPWSTR error_string = NULL; + int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM, + 0, // Use the internal message table. + os_error, + 0, // Use default language. + (LPWSTR)&error_string, + 0, // Buffer size. + 0); // Arguments (unused). + dict->SetString("os_error_string", WideToUTF8(error_string)); + LocalFree(error_string); #endif - } - - return dict; - } - - protected: - virtual ~ProcTaskFailedParams() {} - - private: - const uint32 attempt_number_; - const int net_error_; - const int os_error_; -}; - -// Extra parameters to attach to the NetLog when the DnsTask failed. -class DnsTaskFailedParams : public NetLog::EventParameters { - public: - DnsTaskFailedParams(int net_error, int dns_error) - : net_error_(net_error), dns_error_(dns_error) { - } - - virtual Value* ToValue() const OVERRIDE { - DictionaryValue* dict = new DictionaryValue(); - dict->SetInteger("net_error", net_error_); - if (dns_error_) - dict->SetInteger("dns_error", dns_error_); - return dict; } - protected: - virtual ~DnsTaskFailedParams() {} - - private: - const int net_error_; - const int dns_error_; -}; - -// Parameters representing the information in a RequestInfo object, along with -// the associated NetLog::Source. -class RequestInfoParameters : public NetLog::EventParameters { - public: - RequestInfoParameters(const HostResolver::RequestInfo& info, - const NetLog::Source& source) - : info_(info), source_(source) {} - - virtual Value* ToValue() const OVERRIDE { - DictionaryValue* dict = new DictionaryValue(); - dict->SetString("host", info_.host_port_pair().ToString()); - dict->SetInteger("address_family", - static_cast<int>(info_.address_family())); - dict->SetBoolean("allow_cached_response", info_.allow_cached_response()); - dict->SetBoolean("is_speculative", info_.is_speculative()); - dict->SetInteger("priority", info_.priority()); - - if (source_.is_valid()) - dict->Set("source_dependency", source_.ToValue()); - - return dict; - } - - protected: - virtual ~RequestInfoParameters() {} - - private: - const HostResolver::RequestInfo info_; - const NetLog::Source source_; -}; - -// Parameters associated with the creation of a HostResolverImpl::Job. -class JobCreationParameters : public NetLog::EventParameters { - public: - JobCreationParameters(const std::string& host, - const NetLog::Source& source) - : host_(host), source_(source) {} - - virtual Value* ToValue() const OVERRIDE { - DictionaryValue* dict = new DictionaryValue(); - dict->SetString("host", host_); - dict->Set("source_dependency", source_.ToValue()); - return dict; - } - - protected: - virtual ~JobCreationParameters() {} - - private: - const std::string host_; - const NetLog::Source source_; -}; - -// Parameters of the HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH event. -class JobAttachParameters : public NetLog::EventParameters { - public: - JobAttachParameters(const NetLog::Source& source, - RequestPriority priority) - : source_(source), priority_(priority) {} - - virtual Value* ToValue() const OVERRIDE { - DictionaryValue* dict = new DictionaryValue(); - dict->Set("source_dependency", source_.ToValue()); - dict->SetInteger("priority", priority_); - return dict; - } - - protected: - virtual ~JobAttachParameters() {} + return dict; +} - private: - const NetLog::Source source_; - const RequestPriority priority_; +// Creates NetLog parameters when the DnsTask failed. +base::Value* NetLogDnsTaskFailedCallback(int net_error, + int dns_error, + NetLog::LogLevel /* log_level */) { + DictionaryValue* dict = new DictionaryValue(); + dict->SetInteger("net_error", net_error); + if (dns_error) + dict->SetInteger("dns_error", dns_error); + return dict; }; -// Parameters of the DNS_CONFIG_CHANGED event. -class DnsConfigParameters : public NetLog::EventParameters { - public: - explicit DnsConfigParameters(const DnsConfig& config) - : num_hosts_(config.hosts.size()) { - config_.CopyIgnoreHosts(config); - } +// Creates NetLog parameters containing the information in a RequestInfo object, +// along with the associated NetLog::Source. +base::Value* NetLogRequestInfoCallback(const NetLog::Source& source, + const HostResolver::RequestInfo* info, + NetLog::LogLevel /* log_level */) { + DictionaryValue* dict = new DictionaryValue(); + source.AddToEventParameters(dict); + + dict->SetString("host", info->host_port_pair().ToString()); + dict->SetInteger("address_family", + static_cast<int>(info->address_family())); + dict->SetBoolean("allow_cached_response", info->allow_cached_response()); + dict->SetBoolean("is_speculative", info->is_speculative()); + dict->SetInteger("priority", info->priority()); + return dict; +} - virtual Value* ToValue() const OVERRIDE { - Value* value = config_.ToValue(); - if (!value) - return NULL; - DictionaryValue* dict; - if (value->GetAsDictionary(&dict)) - dict->SetInteger("num_hosts", num_hosts_); - return value; - } +// Creates NetLog parameters for the creation of a HostResolverImpl::Job. +base::Value* NetLogJobCreationCallback(const NetLog::Source& source, + const std::string* host, + NetLog::LogLevel /* log_level */) { + DictionaryValue* dict = new DictionaryValue(); + source.AddToEventParameters(dict); + dict->SetString("host", *host); + return dict; +} - protected: - virtual ~DnsConfigParameters() {} +// Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events. +base::Value* NetLogJobAttachCallback(const NetLog::Source& source, + RequestPriority priority, + NetLog::LogLevel /* log_level */) { + DictionaryValue* dict = new DictionaryValue(); + source.AddToEventParameters(dict); + dict->SetInteger("priority", priority); + return dict; +} - private: - DnsConfig config_; // Does not include DnsHosts to save memory and work. - const size_t num_hosts_; -}; +// Creates NetLog parameters for the DNS_CONFIG_CHANGED event. +base::Value* NetLogDnsConfigCallback(const DnsConfig* config, + NetLog::LogLevel /* log_level */) { + return config->ToValue(); +} // The logging routines are defined here because some requests are resolved // without a Request object. @@ -358,13 +273,11 @@ void LogStartRequest(const BoundNetLog& source_net_log, const HostResolver::RequestInfo& info) { source_net_log.BeginEvent( NetLog::TYPE_HOST_RESOLVER_IMPL, - make_scoped_refptr(new NetLogSourceParameter( - "source_dependency", request_net_log.source()))); + request_net_log.source().ToEventParametersCallback()); request_net_log.BeginEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, - make_scoped_refptr(new RequestInfoParameters( - info, source_net_log.source()))); + base::Bind(&NetLogRequestInfoCallback, source_net_log.source(), &info)); } // Logs when a request has just completed (before its callback is run). @@ -684,8 +597,7 @@ class HostResolverImpl::ProcTask net_log_.AddEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED, - make_scoped_refptr(new NetLogIntegerParameter( - "attempt_number", attempt_number_))); + NetLog::IntegerCallback("attempt_number", attempt_number_)); // If we don't get the results within a given time, RetryIfNotComplete // will start a new attempt on a different worker thread if none of our @@ -758,13 +670,18 @@ class HostResolverImpl::ProcTask if (was_canceled()) return; - scoped_refptr<NetLog::EventParameters> params; + NetLog::ParametersCallback net_log_callback; if (error != OK) { - params = new ProcTaskFailedParams(attempt_number, error, os_error); + net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, + attempt_number, + error, + os_error); } else { - params = new NetLogIntegerParameter("attempt_number", attempt_number); + net_log_callback = NetLog::IntegerCallback("attempt_number", + attempt_number); } - net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, params); + net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, + net_log_callback); if (was_completed()) return; @@ -781,11 +698,13 @@ class HostResolverImpl::ProcTask } if (error != OK) { - params = new ProcTaskFailedParams(0, error, os_error); + net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, + 0, error, os_error); } else { - params = new AddressListNetLogParam(results_); + net_log_callback = results_.CreateNetLogCallback(); } - net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, params); + net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, + net_log_callback); callback_.Run(error, results_); } @@ -1099,7 +1018,7 @@ class HostResolverImpl::DnsTask { DnsResponse::DNS_PARSE_RESULT_MAX); if (result == DnsResponse::DNS_SUCCESS) { net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, - new AddressListNetLogParam(addr_list)); + addr_list.CreateNetLogCallback()); callback_.Run(net_error, addr_list, ttl); return; } @@ -1108,8 +1027,9 @@ class HostResolverImpl::DnsTask { DNS_HISTOGRAM("AsyncDNS.TransactionFailure", base::TimeTicks::Now() - start_time); } - net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, - new DnsTaskFailedParams(net_error, result)); + net_log_.EndEvent( + NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, + base::Bind(&NetLogDnsTaskFailedCallback, net_error, result)); callback_.Run(net_error, AddressList(), base::TimeDelta()); } @@ -1142,8 +1062,9 @@ class HostResolverImpl::Job : public PrioritizedDispatcher::Job { net_log_.BeginEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, - make_scoped_refptr(new JobCreationParameters( - key_.hostname, request_net_log.source()))); + base::Bind(&NetLogJobCreationCallback, + request_net_log.source(), + &key_.hostname)); } virtual ~Job() { @@ -1191,13 +1112,13 @@ class HostResolverImpl::Job : public PrioritizedDispatcher::Job { req->request_net_log().AddEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, - make_scoped_refptr(new NetLogSourceParameter( - "source_dependency", net_log_.source()))); + net_log_.source().ToEventParametersCallback()); net_log_.AddEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, - make_scoped_refptr(new JobAttachParameters( - req->request_net_log().source(), priority()))); + base::Bind(&NetLogJobAttachCallback, + req->request_net_log().source(), + priority())); // TODO(szym): Check if this is still needed. if (!req->info().is_speculative()) { @@ -1226,8 +1147,9 @@ class HostResolverImpl::Job : public PrioritizedDispatcher::Job { priority_tracker_.Remove(req->info().priority()); net_log_.AddEvent( NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, - make_scoped_refptr(new JobAttachParameters( - req->request_net_log().source(), priority()))); + base::Bind(&NetLogJobAttachCallback, + req->request_net_log().source(), + priority())); if (num_active_requests() > 0) { if (is_queued()) @@ -1962,7 +1884,7 @@ void HostResolverImpl::OnDnsConfigChanged(const DnsConfig& dns_config) { if (net_log_) { net_log_->AddGlobalEntry( NetLog::TYPE_DNS_CONFIG_CHANGED, - make_scoped_refptr(new DnsConfigParameters(dns_config))); + base::Bind(&NetLogDnsConfigCallback, &dns_config)); } // TODO(szym): Remove once http://crbug.com/125599 is resolved. diff --git a/net/base/multi_threaded_cert_verifier.cc b/net/base/multi_threaded_cert_verifier.cc index d614590..0a45c94 100644 --- a/net/base/multi_threaded_cert_verifier.cc +++ b/net/base/multi_threaded_cert_verifier.cc @@ -263,9 +263,10 @@ class CertVerifierJob { : start_time_(base::TimeTicks::Now()), worker_(worker), net_log_(net_log) { - scoped_refptr<NetLog::EventParameters> params( - new X509CertificateNetLogParam(worker_->certificate())); - net_log_.BeginEvent(NetLog::TYPE_CERT_VERIFIER_JOB, params); + net_log_.BeginEvent( + NetLog::TYPE_CERT_VERIFIER_JOB, + base::Bind(&NetLogX509CertificateCallback, + base::Unretained(worker_->certificate()))); } ~CertVerifierJob() { @@ -280,8 +281,7 @@ class CertVerifierJob { void AddRequest(CertVerifierRequest* request) { request->net_log().AddEvent( NetLog::TYPE_CERT_VERIFIER_REQUEST_BOUND_TO_JOB, - make_scoped_refptr(new NetLogSourceParameter( - "source_dependency", net_log_.source()))); + net_log_.source().ToEventParametersCallback()); requests_.push_back(request); } diff --git a/net/base/net_log.cc b/net/base/net_log.cc index 4f18e46c..703fece 100644 --- a/net/base/net_log.cc +++ b/net/base/net_log.cc @@ -45,16 +45,22 @@ Value* SourceEventParametersCallback(const NetLog::Source source, return event_params; } -Value* SingleIntegerCallback(const char* name, +Value* NetLogIntegerCallback(const char* name, int value, NetLog::LogLevel /* log_level */) { - if (!value) - return NULL; DictionaryValue* event_params = new DictionaryValue(); event_params->SetInteger(name, value); return event_params; } +Value* NetLogStringCallback(const char* name, + const std::string* value, + NetLog::LogLevel /* log_level */) { + DictionaryValue* event_params = new DictionaryValue(); + event_params->SetString(name, *value); + return event_params; +} + } // namespace Value* NetLog::Source::ToValue() const { @@ -263,7 +269,14 @@ bool NetLog::IsLoggingAllEvents(LogLevel log_level) { // static NetLog::ParametersCallback NetLog::IntegerCallback(const char* name, int value) { - return base::Bind(&SingleIntegerCallback, name, value); + return base::Bind(&NetLogIntegerCallback, name, value); +} + +// static +NetLog::ParametersCallback NetLog::StringCallback(const char* name, + const std::string* value) { + DCHECK(value); + return base::Bind(&NetLogStringCallback, name, value); } void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) { @@ -376,7 +389,11 @@ void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type, void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type, int net_error) const { DCHECK_NE(ERR_IO_PENDING, net_error); - EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error)); + if (net_error >= 0) { + EndEvent(event_type); + } else { + EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error)); + } } void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, diff --git a/net/base/net_log.h b/net/base/net_log.h index d0eee53..1f7f9b2 100644 --- a/net/base/net_log.h +++ b/net/base/net_log.h @@ -296,6 +296,13 @@ class NET_EXPORT NetLog { // Warning: |name| must remain valid for the life of the callback. static ParametersCallback IntegerCallback(const char* name, int value); + // Creates a ParametersCallback that encapsulates a single UTF8 string. Takes + // |value| as a pointer to avoid copying, and emphasize it must be valid for + // the life of the callback. |value| may not be NULL. + // Warning: |name| and |value| must remain valid for the life of the callback. + static ParametersCallback StringCallback(const char* name, + const std::string* value); + protected: // Child classes should respond to the new entry here. This includes // creating the Entry object and alerting their observers. diff --git a/net/base/x509_certificate_net_log_param.cc b/net/base/x509_certificate_net_log_param.cc index 693a7ff..b44f11e 100644 --- a/net/base/x509_certificate_net_log_param.cc +++ b/net/base/x509_certificate_net_log_param.cc @@ -4,25 +4,24 @@ #include "net/base/x509_certificate_net_log_param.h" +#include <string> +#include <vector> + #include "base/values.h" #include "net/base/x509_certificate.h" namespace net { -X509CertificateNetLogParam::X509CertificateNetLogParam( - X509Certificate* certificate) { - certificate->GetPEMEncodedChain(&encoded_chain_); -} - -base::Value* X509CertificateNetLogParam::ToValue() const { +base::Value* NetLogX509CertificateCallback(const X509Certificate* certificate, + NetLog::LogLevel log_level) { DictionaryValue* dict = new DictionaryValue(); ListValue* certs = new ListValue(); - for (size_t i = 0; i < encoded_chain_.size(); ++i) - certs->Append(base::Value::CreateStringValue(encoded_chain_[i])); + std::vector<std::string> encoded_chain; + certificate->GetPEMEncodedChain(&encoded_chain); + for (size_t i = 0; i < encoded_chain.size(); ++i) + certs->Append(base::Value::CreateStringValue(encoded_chain[i])); dict->Set("certificates", certs); return dict; } -X509CertificateNetLogParam::~X509CertificateNetLogParam() {} - } // namespace net diff --git a/net/base/x509_certificate_net_log_param.h b/net/base/x509_certificate_net_log_param.h index a6ec6a4..73b1e3c 100644 --- a/net/base/x509_certificate_net_log_param.h +++ b/net/base/x509_certificate_net_log_param.h @@ -6,31 +6,16 @@ #define NET_BASE_X509_CERT_NET_LOG_PARAM_H_ #pragma once -#include <string> -#include <vector> - -#include "base/compiler_specific.h" #include "net/base/net_log.h" namespace net { class X509Certificate; -// NetLog parameter to describe an X509Certificate. -// Note: These parameters are memory intensive, due to PEM-encoding each -// certificate, thus should only be used when logging at NetLog::LOG_ALL. -class X509CertificateNetLogParam : public NetLog::EventParameters { - public: - explicit X509CertificateNetLogParam(X509Certificate* certificate); - - virtual base::Value* ToValue() const OVERRIDE; - - protected: - virtual ~X509CertificateNetLogParam(); - - private: - std::vector<std::string> encoded_chain_; -}; +// Creates NetLog parameter to describe an X509Certificate. +base::Value* NetLogX509CertificateCallback( + const X509Certificate* certificate, + NetLog::LogLevel log_level); } // namespace net diff --git a/net/net.gyp b/net/net.gyp index 116cf65..af65034 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -50,8 +50,6 @@ 'base/address_family.h', 'base/address_list.cc', 'base/address_list.h', - 'base/address_list_net_log_param.cc', - 'base/address_list_net_log_param.h', 'base/asn1_util.cc', 'base/asn1_util.h', 'base/auth.cc', diff --git a/net/socket/ssl_client_socket_mac.cc b/net/socket/ssl_client_socket_mac.cc index fb69726..907b05e 100644 --- a/net/socket/ssl_client_socket_mac.cc +++ b/net/socket/ssl_client_socket_mac.cc @@ -1297,7 +1297,8 @@ int SSLClientSocketMac::DidCompleteHandshake() { return ERR_UNEXPECTED; net_log_.AddEvent( NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, - make_scoped_refptr(new X509CertificateNetLogParam(new_server_cert))); + base::Bind(&NetLogX509CertificateCallback, + base::Unretained(new_server_cert.get()))); if (renegotiating_ && X509Certificate::IsSameOSCert(server_cert_->os_cert_handle(), diff --git a/net/socket/ssl_client_socket_nss.cc b/net/socket/ssl_client_socket_nss.cc index ff2f259..86c7747 100644 --- a/net/socket/ssl_client_socket_nss.cc +++ b/net/socket/ssl_client_socket_nss.cc @@ -385,6 +385,20 @@ void DestroyCertificates(CERTCertificate** certs, size_t len) { // Helper function to make it possible to log events from within the // SSLClientSocketNSS::Core. Can't use Bind with BoundNetLog::AddEntry directly // on Windows because it is overloaded. +// TODO(mmenke): Other than shutdown, NetLog is threadsafe. Figure out if this +// is needed. +void AddLogEventWithCallback(BoundNetLog* net_log, + NetLog::EventType event_type, + const NetLog::ParametersCallback& callback) { + if (!net_log) + return; + net_log->AddEvent(event_type, callback); +} + +// Helper functions to make it possible to log events from within the +// SSLClientSocketNSS::Core. Can't use Bind with BoundNetLog::AddEntry directly +// on Windows because it is overloaded. +// TODO(mmenke): This function is deprecated, delete it. void AddLogEvent(BoundNetLog* net_log, NetLog::EventType event_type, const scoped_refptr<NetLog::EventParameters>& event_params) { @@ -2515,13 +2529,14 @@ void SSLClientSocketNSS::Core::UpdateServerCert() { nss_handshake_state_.server_cert = X509Certificate::CreateFromDERCertChain( nss_handshake_state_.server_cert_chain.AsStringPieceVector()); if (nss_handshake_state_.server_cert) { + NetLog::ParametersCallback net_log_callback = + base::Bind(&NetLogX509CertificateCallback, + base::Unretained(nss_handshake_state_.server_cert.get())); PostOrRunCallback( FROM_HERE, - base::Bind(&AddLogEvent, weak_net_log_, + base::Bind(&AddLogEventWithCallback, weak_net_log_, NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, - make_scoped_refptr( - new X509CertificateNetLogParam( - nss_handshake_state_.server_cert)))); + net_log_callback)); } } diff --git a/net/socket/ssl_client_socket_openssl.cc b/net/socket/ssl_client_socket_openssl.cc index d77f883..8f83ef3 100644 --- a/net/socket/ssl_client_socket_openssl.cc +++ b/net/socket/ssl_client_socket_openssl.cc @@ -822,7 +822,8 @@ int SSLClientSocketOpenSSL::DoHandshake() { DCHECK(got_cert); net_log_.AddEvent( NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, - make_scoped_refptr(new X509CertificateNetLogParam(server_cert_))); + base::Bind(&NetLogX509CertificateCallback, + base::Unretained(server_cert_.get()))); GotoState(STATE_VERIFY_CERT); } else { int ssl_error = SSL_get_error(ssl_, rv); diff --git a/net/socket/ssl_client_socket_win.cc b/net/socket/ssl_client_socket_win.cc index 5b27670..fa7e848 100644 --- a/net/socket/ssl_client_socket_win.cc +++ b/net/socket/ssl_client_socket_win.cc @@ -1563,7 +1563,8 @@ int SSLClientSocketWin::DidCompleteHandshake() { X509Certificate::CreateFromHandle(server_cert_handle, intermediates)); net_log_.AddEvent( NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, - make_scoped_refptr(new X509CertificateNetLogParam(new_server_cert))); + base::Bind(&NetLogX509CertificateCallback, + base::Unretained(new_server_cert.get()))); if (renegotiating_ && IsCertificateChainIdentical(server_cert_, new_server_cert)) { // We already verified the server certificate. Either it is good or the diff --git a/net/socket/tcp_client_socket_libevent.cc b/net/socket/tcp_client_socket_libevent.cc index 5b71f6b..f77a691 100644 --- a/net/socket/tcp_client_socket_libevent.cc +++ b/net/socket/tcp_client_socket_libevent.cc @@ -18,7 +18,6 @@ #include "base/message_loop.h" #include "base/metrics/stats_counters.h" #include "base/string_util.h" -#include "net/base/address_list_net_log_param.h" #include "net/base/connection_type_histograms.h" #include "net/base/io_buffer.h" #include "net/base/ip_endpoint.h" @@ -213,9 +212,8 @@ int TCPClientSocketLibevent::Connect(const CompletionCallback& callback) { DCHECK(!waiting_connect()); - net_log_.BeginEvent( - NetLog::TYPE_TCP_CONNECT, - make_scoped_refptr(new AddressListNetLogParam(addresses_))); + net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, + addresses_.CreateNetLogCallback()); // We will try to connect to each address in addresses_. Start with the // first one in the list. diff --git a/net/socket/tcp_client_socket_win.cc b/net/socket/tcp_client_socket_win.cc index 3ccad12..c518c2b 100644 --- a/net/socket/tcp_client_socket_win.cc +++ b/net/socket/tcp_client_socket_win.cc @@ -12,7 +12,6 @@ #include "base/string_util.h" #include "base/win/object_watcher.h" #include "base/win/windows_version.h" -#include "net/base/address_list_net_log_param.h" #include "net/base/connection_type_histograms.h" #include "net/base/io_buffer.h" #include "net/base/ip_endpoint.h" @@ -406,7 +405,7 @@ int TCPClientSocketWin::Connect(const CompletionCallback& callback) { connects.Increment(); net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, - new AddressListNetLogParam(addresses_)); + addresses_.CreateNetLogCallback()); // We will try to connect to each address in addresses_. Start with the // first one in the list. diff --git a/net/udp/udp_data_transfer_param.h b/net/udp/udp_data_transfer_param.h index 1cc6561..02cbcea 100644 --- a/net/udp/udp_data_transfer_param.h +++ b/net/udp/udp_data_transfer_param.h @@ -41,4 +41,4 @@ class UDPDataTransferNetLogParam : public NetLog::EventParameters { } // namespace net -#endif // NET_BASE_ADDRESS_LIST_NET_LOG_PARAM_H_ +#endif // NET_UDP_UDP_DATA_TRANSFER_PARAM_H_ diff --git a/net/udp/udp_socket_libevent.h b/net/udp/udp_socket_libevent.h index b6fe51f..2101abb 100644 --- a/net/udp/udp_socket_libevent.h +++ b/net/udp/udp_socket_libevent.h @@ -10,7 +10,6 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/threading/non_thread_safe.h" -#include "net/base/address_list_net_log_param.h" #include "net/base/completion_callback.h" #include "net/base/net_export.h" #include "net/base/rand_callback.h" diff --git a/net/udp/udp_socket_win.cc b/net/udp/udp_socket_win.cc index 423ba00..4ed85ec 100644 --- a/net/udp/udp_socket_win.cc +++ b/net/udp/udp_socket_win.cc @@ -11,7 +11,6 @@ #include "base/message_loop.h" #include "base/metrics/stats_counters.h" #include "base/rand_util.h" -#include "net/base/address_list_net_log_param.h" #include "net/base/io_buffer.h" #include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" |