diff options
Diffstat (limited to 'net/base')
21 files changed, 741 insertions, 747 deletions
diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc index ebad57e..320406c 100644 --- a/net/base/host_resolver.cc +++ b/net/base/host_resolver.cc @@ -26,7 +26,7 @@ SingleRequestHostResolver::~SingleRequestHostResolver() { int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info, AddressList* addresses, CompletionCallback* callback, - LoadLog* load_log) { + const BoundNetLog& net_log) { DCHECK(!cur_request_ && !cur_request_callback_) << "resolver already in use"; HostResolver::RequestHandle request = NULL; @@ -36,7 +36,7 @@ int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info, CompletionCallback* transient_callback = callback ? &callback_ : NULL; int rv = resolver_->Resolve( - info, addresses, transient_callback, &request, load_log); + info, addresses, transient_callback, &request, net_log); if (rv == ERR_IO_PENDING) { // Cleared in OnResolveCompletion(). diff --git a/net/base/host_resolver.h b/net/base/host_resolver.h index c4cdef6..8f7bd09 100644 --- a/net/base/host_resolver.h +++ b/net/base/host_resolver.h @@ -18,9 +18,9 @@ class MessageLoop; namespace net { class AddressList; +class BoundNetLog; class HostCache; class HostResolverImpl; -class LoadLog; class NetworkChangeNotifier; // This class represents the task of resolving hostnames (or IP address @@ -137,12 +137,12 @@ class HostResolver : public base::RefCountedThreadSafe<HostResolver> { // the async request. This handle is not valid after the request has // completed. // - // Profiling information for the request is saved to |load_log| if non-NULL. + // Profiling information for the request is saved to |net_log| if non-NULL. virtual int Resolve(const RequestInfo& info, AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log) = 0; + const BoundNetLog& net_log) = 0; // Cancels the specified request. |req| is the handle returned by Resolve(). // After a request is cancelled, its completion callback will not be called. @@ -198,7 +198,7 @@ class SingleRequestHostResolver { int Resolve(const HostResolver::RequestInfo& info, AddressList* addresses, CompletionCallback* callback, - LoadLog* load_log); + const BoundNetLog& net_log); // Cancels the in-progress request, if any. This prevents the callback // from being invoked. Resolve() can be called again after cancelling. diff --git a/net/base/host_resolver_impl.cc b/net/base/host_resolver_impl.cc index 6af3079..d5dce2f 100644 --- a/net/base/host_resolver_impl.cc +++ b/net/base/host_resolver_impl.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "net/base/host_resolver_impl.h" +#include "net/base/net_log.h" #include <cmath> #include <deque> @@ -18,7 +19,7 @@ #include "base/worker_pool.h" #include "net/base/address_list.h" #include "net/base/host_resolver_proc.h" -#include "net/base/load_log.h" +#include "net/base/net_log.h" #include "net/base/net_errors.h" #include "net/base/network_change_notifier.h" @@ -72,12 +73,12 @@ static int ResolveAddrInfo(HostResolverProc* resolver_proc, class HostResolverImpl::Request { public: - Request(LoadLog* load_log, + Request(const BoundNetLog& net_log, int id, const RequestInfo& info, CompletionCallback* callback, AddressList* addresses) - : load_log_(load_log), + : net_log_(net_log), id_(id), info_(info), job_(NULL), @@ -116,8 +117,8 @@ class HostResolverImpl::Request { return job_; } - LoadLog* load_log() const { - return load_log_; + const BoundNetLog& net_log() { + return net_log_; } int id() const { @@ -129,7 +130,7 @@ class HostResolverImpl::Request { } private: - scoped_refptr<LoadLog> load_log_; + BoundNetLog net_log_; // Unique ID for this request. Used by observers to identify requests. int id_; @@ -155,26 +156,31 @@ class HostResolverImpl::Request { class HostResolverImpl::RequestsTrace : public base::RefCountedThreadSafe<HostResolverImpl::RequestsTrace> { public: - RequestsTrace() : log_(new LoadLog(LoadLog::kUnbounded)) {} + RequestsTrace() {} void Add(const std::string& msg) { + NetLog::Entry entry; + entry.type = NetLog::Entry::TYPE_STRING; + entry.time = base::TimeTicks::Now(); + entry.string = msg; + AutoLock l(lock_); - LoadLog::AddString(log_, msg); + entries_.push_back(entry); } - void Get(LoadLog* out) { + void Get(std::vector<NetLog::Entry>* entries) { AutoLock l(lock_); - out->Append(log_); + *entries = entries_; } void Clear() { AutoLock l(lock_); - log_ = new LoadLog(LoadLog::kUnbounded); + entries_.clear(); } private: Lock lock_; - scoped_refptr<LoadLog> log_; + std::vector<NetLog::Entry> entries_; }; //----------------------------------------------------------------------------- @@ -613,15 +619,15 @@ int HostResolverImpl::Resolve(const RequestInfo& info, AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log) { + const BoundNetLog& net_log) { if (shutdown_) return ERR_UNEXPECTED; // Choose a unique ID number for observers to see. int request_id = next_request_id_++; - // Update the load log and notify registered observers. - OnStartRequest(load_log, request_id, info); + // Update the net log and notify registered observers. + OnStartRequest(net_log, request_id, info); // Build a key that identifies the request in the cache and in the // outstanding jobs map. @@ -636,8 +642,8 @@ int HostResolverImpl::Resolve(const RequestInfo& info, if (error == OK) addresses->SetFrom(cache_entry->addrlist, info.port()); - // Update the load log and notify registered observers. - OnFinishRequest(load_log, request_id, info, error); + // Update the net log and notify registered observers. + OnFinishRequest(net_log, request_id, info, error); return error; } @@ -657,15 +663,15 @@ int HostResolverImpl::Resolve(const RequestInfo& info, if (cache_.get()) cache_->Set(key, error, addrlist, base::TimeTicks::Now()); - // Update the load log and notify registered observers. - OnFinishRequest(load_log, request_id, info, error); + // Update the net log and notify registered observers. + OnFinishRequest(net_log, request_id, info, error); return error; } // Create a handle for this request, and pass it back to the user if they // asked for it (out_req != NULL). - Request* req = new Request(load_log, request_id, info, callback, addresses); + Request* req = new Request(net_log, request_id, info, callback, addresses); if (out_req) *out_req = reinterpret_cast<RequestHandle>(req); @@ -719,7 +725,7 @@ void HostResolverImpl::CancelRequest(RequestHandle req_handle) { // NULL out the fields of req, to mark it as cancelled. req->MarkAsCancelled(); - OnCancelRequest(req->load_log(), req->id(), req->info()); + OnCancelRequest(req->net_log(), req->id(), req->info()); } void HostResolverImpl::AddObserver(HostResolver::Observer* observer) { @@ -804,13 +810,11 @@ bool HostResolverImpl::IsRequestsTracingEnabled() const { return !!requests_trace_; // Cast to bool. } -scoped_refptr<LoadLog> HostResolverImpl::GetRequestsTrace() { +bool HostResolverImpl::GetRequestsTrace(std::vector<NetLog::Entry>* entries) { if (!requests_trace_) - return NULL; - - scoped_refptr<LoadLog> copy_of_log = new LoadLog(LoadLog::kUnbounded); - requests_trace_->Get(copy_of_log); - return copy_of_log; + return false; + requests_trace_->Get(entries); + return true; } void HostResolverImpl::SetPoolConstraints(JobPoolIndex pool_index, @@ -873,8 +877,8 @@ void HostResolverImpl::OnJobComplete(Job* job, if (!req->was_cancelled()) { DCHECK_EQ(job, req->job()); - // Update the load log and notify registered observers. - OnFinishRequest(req->load_log(), req->id(), req->info(), error); + // Update the net log and notify registered observers. + OnFinishRequest(req->net_log(), req->id(), req->info(), error); req->OnComplete(error, addrlist); @@ -888,10 +892,10 @@ void HostResolverImpl::OnJobComplete(Job* job, cur_completing_job_ = NULL; } -void HostResolverImpl::OnStartRequest(LoadLog* load_log, +void HostResolverImpl::OnStartRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info) { - LoadLog::BeginEvent(load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL); + net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL); if (requests_trace_) { requests_trace_->Add(StringPrintf( @@ -909,20 +913,18 @@ void HostResolverImpl::OnStartRequest(LoadLog* load_log, // Notify the observers of the start. if (!observers_.empty()) { - LoadLog::BeginEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART); + net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART); for (ObserversList::iterator it = observers_.begin(); it != observers_.end(); ++it) { (*it)->OnStartResolution(request_id, info); } - LoadLog::EndEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART); + net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART); } } -void HostResolverImpl::OnFinishRequest(LoadLog* load_log, +void HostResolverImpl::OnFinishRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info, int error) { @@ -933,8 +935,7 @@ void HostResolverImpl::OnFinishRequest(LoadLog* load_log, // Notify the observers of the completion. if (!observers_.empty()) { - LoadLog::BeginEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH); + net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH); bool was_resolved = error == OK; for (ObserversList::iterator it = observers_.begin(); @@ -942,36 +943,33 @@ void HostResolverImpl::OnFinishRequest(LoadLog* load_log, (*it)->OnFinishResolutionWithStatus(request_id, was_resolved, info); } - LoadLog::EndEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH); + net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH); } - LoadLog::EndEvent(load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL); + net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL); } -void HostResolverImpl::OnCancelRequest(LoadLog* load_log, +void HostResolverImpl::OnCancelRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info) { - LoadLog::AddEvent(load_log, LoadLog::TYPE_CANCELLED); + net_log.AddEvent(NetLog::TYPE_CANCELLED); if (requests_trace_) requests_trace_->Add(StringPrintf("Cancelled request r%d", request_id)); // Notify the observers of the cancellation. if (!observers_.empty()) { - LoadLog::BeginEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL); + net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL); for (ObserversList::iterator it = observers_.begin(); it != observers_.end(); ++it) { (*it)->OnCancelResolution(request_id, info); } - LoadLog::EndEvent( - load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL); + net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL); } - LoadLog::EndEvent(load_log, LoadLog::TYPE_HOST_RESOLVER_IMPL); + net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL); } void HostResolverImpl::OnIPAddressChanged() { @@ -1052,7 +1050,7 @@ int HostResolverImpl::EnqueueRequest(JobPool* pool, Request* req) { if (requests_trace_) requests_trace_->Add(StringPrintf("Evicted request r%d", r->id())); - OnFinishRequest(r->load_log(), r->id(), r->info(), error); + OnFinishRequest(r->net_log(), r->id(), r->info(), error); if (r == req) return error; diff --git a/net/base/host_resolver_impl.h b/net/base/host_resolver_impl.h index 3e273cd..de2cc14 100644 --- a/net/base/host_resolver_impl.h +++ b/net/base/host_resolver_impl.h @@ -12,6 +12,7 @@ #include "net/base/host_cache.h" #include "net/base/host_resolver.h" #include "net/base/host_resolver_proc.h" +#include "net/base/net_log.h" #include "net/base/network_change_notifier.h" namespace net { @@ -83,7 +84,7 @@ class HostResolverImpl : public HostResolver, AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log); + const BoundNetLog& net_log); virtual void CancelRequest(RequestHandle req); virtual void AddObserver(HostResolver::Observer* observer); virtual void RemoveObserver(HostResolver::Observer* observer); @@ -108,8 +109,8 @@ class HostResolverImpl : public HostResolver, bool IsRequestsTracingEnabled() const; - // Returns a copy of the requests trace log, or NULL if there is none. - scoped_refptr<LoadLog> GetRequestsTrace(); + // Gets a copy of the requests trace log. + bool GetRequestsTrace(std::vector<NetLog::Entry>* entries); // Applies a set of constraints for requests that belong to the specified // pool. NOTE: Don't call this after requests have been already been started. @@ -160,18 +161,18 @@ class HostResolverImpl : public HostResolver, void OnJobComplete(Job* job, int error, const AddressList& addrlist); // Called when a request has just been started. - void OnStartRequest(LoadLog* load_log, + void OnStartRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info); // Called when a request has just completed (before its callback is run). - void OnFinishRequest(LoadLog* load_log, + void OnFinishRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info, int error); // Called when a request has been cancelled. - void OnCancelRequest(LoadLog* load_log, + void OnCancelRequest(const BoundNetLog& net_log, int request_id, const RequestInfo& info); diff --git a/net/base/host_resolver_impl_unittest.cc b/net/base/host_resolver_impl_unittest.cc index ee44528..bd19f0b 100644 --- a/net/base/host_resolver_impl_unittest.cc +++ b/net/base/host_resolver_impl_unittest.cc @@ -12,10 +12,10 @@ #include "base/string_util.h" #include "net/base/address_list.h" #include "net/base/completion_callback.h" -#include "net/base/load_log_unittest.h" #include "net/base/mock_host_resolver.h" #include "net/base/mock_network_change_notifier.h" #include "net/base/net_errors.h" +#include "net/base/net_log_unittest.h" #include "net/base/net_util.h" #include "net/base/sys_addrinfo.h" #include "net/base/test_completion_callback.h" @@ -262,13 +262,15 @@ TEST_F(HostResolverImplTest, SynchronousLookup) { CreateHostResolverImpl(resolver_proc)); HostResolver::RequestInfo info("just.testing", kPortnum); - scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); - int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, log); + CapturingBoundNetLog log(CapturingNetLog::kUnbounded); + int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, log.bound()); EXPECT_EQ(OK, err); - EXPECT_EQ(2u, log->entries().size()); - EXPECT_TRUE(LogContainsBeginEvent(*log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL)); - EXPECT_TRUE(LogContainsEndEvent(*log, 1, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_EQ(2u, log.entries().size()); + EXPECT_TRUE(LogContainsBeginEvent( + log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_TRUE(LogContainsEndEvent( + log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); const struct addrinfo* ainfo = adrlist.head(); EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); @@ -292,20 +294,23 @@ TEST_F(HostResolverImplTest, AsynchronousLookup) { CreateHostResolverImpl(resolver_proc)); HostResolver::RequestInfo info("just.testing", kPortnum); - scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); - int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log); + CapturingBoundNetLog log(CapturingNetLog::kUnbounded); + int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, + log.bound()); EXPECT_EQ(ERR_IO_PENDING, err); - EXPECT_EQ(1u, log->entries().size()); - EXPECT_TRUE(LogContainsBeginEvent(*log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_EQ(1u, log.entries().size()); + EXPECT_TRUE(LogContainsBeginEvent( + log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); MessageLoop::current()->Run(); ASSERT_TRUE(callback_called_); ASSERT_EQ(OK, callback_result_); - EXPECT_EQ(2u, log->entries().size()); - EXPECT_TRUE(LogContainsEndEvent(*log, 1, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_EQ(2u, log.entries().size()); + EXPECT_TRUE(LogContainsEndEvent( + log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); const struct addrinfo* ainfo = adrlist.head(); EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); @@ -321,7 +326,7 @@ TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { scoped_refptr<WaitingHostResolverProc> resolver_proc = new WaitingHostResolverProc(NULL); - scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); + CapturingBoundNetLog log(CapturingNetLog::kUnbounded); { scoped_refptr<HostResolver> host_resolver( CreateHostResolverImpl(resolver_proc)); @@ -329,7 +334,8 @@ TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { const int kPortnum = 80; HostResolver::RequestInfo info("just.testing", kPortnum); - int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log); + int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, + log.bound()); EXPECT_EQ(ERR_IO_PENDING, err); // Make sure we will exit the queue even when callback is not called. @@ -341,11 +347,13 @@ TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { resolver_proc->Signal(); - EXPECT_EQ(3u, log->entries().size()); - EXPECT_TRUE(LogContainsBeginEvent(*log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_EQ(3u, log.entries().size()); + EXPECT_TRUE(LogContainsBeginEvent( + log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_TRUE(LogContainsEvent( - *log, 1, LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE)); - EXPECT_TRUE(LogContainsEndEvent(*log, 2, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + log.entries(), 1, NetLog::TYPE_CANCELLED, NetLog::PHASE_NONE)); + EXPECT_TRUE(LogContainsEndEvent( + log.entries(), 2, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_FALSE(callback_called_); } @@ -892,22 +900,23 @@ TEST_F(HostResolverImplTest, Observers) { // Resolve "host1". HostResolver::RequestInfo info1("host1", 70); - scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); - int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL, log); + CapturingBoundNetLog log(CapturingNetLog::kUnbounded); + int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL, log.bound()); EXPECT_EQ(OK, rv); - EXPECT_EQ(6u, log->entries().size()); - EXPECT_TRUE(LogContainsBeginEvent(*log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + EXPECT_EQ(6u, log.entries().size()); + EXPECT_TRUE(LogContainsBeginEvent( + log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_TRUE(LogContainsBeginEvent( - *log, 1, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART)); + log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART)); EXPECT_TRUE(LogContainsEndEvent( - *log, 2, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART)); + log.entries(), 2, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART)); EXPECT_TRUE(LogContainsBeginEvent( - *log, 3, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH)); + log.entries(), 3, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH)); EXPECT_TRUE(LogContainsEndEvent( - *log, 4, LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH)); + log.entries(), 4, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH)); EXPECT_TRUE(LogContainsEndEvent( - *log, 5, LoadLog::TYPE_HOST_RESOLVER_IMPL)); + log.entries(), 5, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_EQ(1U, observer.start_log.size()); EXPECT_EQ(1U, observer.finish_log.size()); diff --git a/net/base/load_log.cc b/net/base/load_log.cc deleted file mode 100644 index ec7f59b..0000000 --- a/net/base/load_log.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2009 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/load_log.h" -#include "base/logging.h" - -namespace net { - -LoadLog::LoadLog(size_t max_num_entries) - : num_entries_truncated_(0), max_num_entries_(max_num_entries) { - DCHECK_GT(max_num_entries, 0u); -} - -// static -const char* LoadLog::EventTypeToString(EventType event) { - switch (event) { -#define EVENT_TYPE(label) case TYPE_ ## label: return #label; -#include "net/base/load_log_event_type_list.h" -#undef EVENT_TYPE - } - return NULL; -} - -void LoadLog::Add(const Entry& entry) { - // Minor optimization. TODO(eroman): use StackVector instead. - if (entries_.empty()) - entries_.reserve(10); // It is likely we will have at least 10 entries. - - // Enforce a bound of |max_num_entries_| -- once we reach it, keep overwriting - // the final entry in the log. - - if (entries_.size() + 1 <= max_num_entries_ || - max_num_entries_ == kUnbounded) { - entries_.push_back(entry); - } else { - num_entries_truncated_ += 1; - entries_[max_num_entries_ - 1] = entry; - } -} - -void LoadLog::Append(const LoadLog* log) { - for (size_t i = 0; i < log->entries().size(); ++i) - Add(log->entries()[i]); - num_entries_truncated_ += log->num_entries_truncated(); -} - -} // namespace net diff --git a/net/base/load_log.h b/net/base/load_log.h deleted file mode 100644 index 49e0ef5..0000000 --- a/net/base/load_log.h +++ /dev/null @@ -1,196 +0,0 @@ -// Copyright (c) 2009 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_LOAD_LOG_H_ -#define NET_BASE_LOAD_LOG_H_ - -#include <string> -#include <vector> - -#include "base/ref_counted.h" -#include "base/time.h" - -namespace net { - -// LoadLog stores information associated with an individual request. This -// includes event traces (used to build up profiling information), error -// return codes from network modules, and arbitrary text messages. -// -// Note that LoadLog is NOT THREADSAFE, however it is RefCountedThreadSafe so -// that it can be AddRef() / Release() across threads. -class LoadLog : public base::RefCountedThreadSafe<LoadLog> { - public: - // TODO(eroman): Really, EventType and EventPhase should be - // Event::Type and Event::Phase, to be consisent with Entry. - // But there lots of consumers to change! - enum EventType { -#define EVENT_TYPE(label) TYPE_ ## label, -#include "net/base/load_log_event_type_list.h" -#undef EVENT_TYPE - }; - - // The 'phase' of an event trace (whether it marks the beginning or end - // of an event.). - enum EventPhase { - PHASE_NONE, - PHASE_BEGIN, - // TODO(eroman): DEPRECATED: Use TYPE_STRING_LITERAL instead. - PHASE_END, - }; - - struct Event { - Event(EventType type, EventPhase phase) : type(type), phase(phase) {} - Event() {} - - EventType type; - EventPhase phase; - }; - - struct Entry { - enum Type { - // This entry describes an event trace. - TYPE_EVENT, - - // This entry describes a network error code that was returned. - TYPE_ERROR_CODE, - - // This entry is a free-form std::string. - TYPE_STRING, - - // This entry is a C-string literal. - TYPE_STRING_LITERAL, - }; - - Entry(base::TimeTicks time, int error_code) - : type(TYPE_ERROR_CODE), time(time), error_code(error_code) { - } - - Entry(base::TimeTicks time, const Event& event) - : type(TYPE_EVENT), time(time), event(event) { - } - - Entry(base::TimeTicks time, const std::string& string) - : type(TYPE_STRING), time(time), string(string) { - } - - Entry(base::TimeTicks time, const char* literal) - : type(TYPE_STRING_LITERAL), time(time), literal(literal) { - } - - Type type; - base::TimeTicks time; - - // The following is basically a union, only one of them should be - // used depending on what |type| is. - Event event; // valid when (type == TYPE_EVENT). - int error_code; // valid when (type == TYPE_ERROR_CODE). - std::string string; // valid when (type == TYPE_STRING). - const char* literal; // valid when (type == TYPE_STRING_LITERAL). - }; - - // Ordered set of entries that were logged. - // TODO(eroman): use a StackVector or array to avoid allocations. - typedef std::vector<Entry> EntryList; - - // Value for max_num_entries to indicate the LoadLog has no size limit. - static const size_t kUnbounded = static_cast<size_t>(-1); - - // Creates a log, which can hold up to |max_num_entries| entries. - // If |max_num_entries| is |kUnbounded|, then the log can grow arbitrarily - // large. - // - // If entries are dropped because the log has grown too large, the final entry - // will be overwritten. - explicit LoadLog(size_t max_num_entries); - - // -------------------------------------------------------------------------- - - // The public interface for adding events to the log are static methods. - // This makes it easier to deal with optionally NULL LoadLog. - - // Adds an instantaneous event to the log. - // TODO(eroman): DEPRECATED: use AddStringLiteral() instead. - static void AddEvent(LoadLog* log, EventType event_type) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), Event(event_type, PHASE_NONE))); - } - - // Adds the start of an event to the log. Presumably this event type measures - // a time duration, and will be matched by a call to EndEvent(event_type). - static void BeginEvent(LoadLog* log, EventType event_type) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), Event(event_type, PHASE_BEGIN))); - } - - // Adds the end of an event to the log. Presumably this event type measures - // a time duration, and we are matching an earlier call to - // BeginEvent(event_type). - static void EndEvent(LoadLog* log, EventType event_type) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), Event(event_type, PHASE_END))); - } - - // |literal| should be a string literal (i.e. lives in static storage). - static void AddStringLiteral(LoadLog* log, const char* literal) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), literal)); - } - - static void AddString(LoadLog* log, const std::string& string) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), string)); - } - - static void AddErrorCode(LoadLog* log, int error) { - if (log) - log->Add(Entry(base::TimeTicks::Now(), error)); - } - - static bool IsUnbounded(const LoadLog* log) { - return log && log->is_unbounded(); - } - - // -------------------------------------------------------------------------- - - // Returns the list of all entries in the log. - const EntryList& entries() const { - return entries_; - } - - // Returns the number of entries that were dropped from the log because the - // maximum size had been reached. - size_t num_entries_truncated() const { - return num_entries_truncated_; - } - - // Returns the bound on the size of the log. - size_t max_num_entries() const { - return max_num_entries_; - } - - bool is_unbounded() const { - return max_num_entries_ == kUnbounded; - } - - // Returns a C-String symbolic name for |event|. - static const char* EventTypeToString(EventType event_type); - - void Add(const Entry& entry); - - // Copies all entries from |log|, appending it to the end of |this|. - void Append(const LoadLog* log); - - private: - friend class base::RefCountedThreadSafe<LoadLog>; - - ~LoadLog() {} - - EntryList entries_; - size_t num_entries_truncated_; - size_t max_num_entries_;; -}; - -} // namespace net - -#endif // NET_BASE_LOAD_LOG_H_ diff --git a/net/base/load_log_unittest.cc b/net/base/load_log_unittest.cc deleted file mode 100644 index c55cf9b..0000000 --- a/net/base/load_log_unittest.cc +++ /dev/null @@ -1,185 +0,0 @@ -// Copyright (c) 2009 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/load_log_unittest.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { - -namespace { - -TEST(LoadLogTest, Nullable) { - // Make sure that the static methods can be called with NULL (no-op). - // (Should not crash). - LoadLog::BeginEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL); - LoadLog::AddEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL); - LoadLog::EndEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL); -} - -TEST(LoadLogTest, Basic) { - scoped_refptr<LoadLog> log(new LoadLog(10)); - - // Logs start off empty. - EXPECT_EQ(0u, log->entries().size()); - EXPECT_EQ(0u, log->num_entries_truncated()); - - // Add 3 entries. - - log->Add(LoadLog::Entry(MakeTime(0), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN))); - log->Add(LoadLog::Entry(MakeTime(2), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log->Add( - LoadLog::Entry( - MakeTime(11), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_END))); - - EXPECT_EQ(3u, log->entries().size()); - EXPECT_EQ(0u, log->num_entries_truncated()); - - EXPECT_TRUE(LogContainsEventAtTime( - *log, 0, MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN)); - - EXPECT_TRUE(LogContainsEventAtTime( - *log, 1, MakeTime(2), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE)); - - EXPECT_TRUE(LogContainsEventAtTime( - *log, 2, MakeTime(11), LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_END)); -} - -// Test that the log's size is strictly bounded. -TEST(LoadLogTest, Truncation) { - size_t kMaxNumEntries = 10; - scoped_refptr<LoadLog> log(new LoadLog(kMaxNumEntries)); - - // Max it out. - for (size_t i = 0; i < kMaxNumEntries; ++i) { - log->Add(LoadLog::Entry(MakeTime(i), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - } - - EXPECT_EQ(kMaxNumEntries, log->entries().size()); - EXPECT_EQ(0u, log->num_entries_truncated()); - - // Check the last entry. - EXPECT_TRUE(LogContainsEventAtTime( - *log, 9, MakeTime(9), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE)); - - // Add three entries while maxed out (will cause truncation) - log->Add(LoadLog::Entry(MakeTime(0), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log->Add(LoadLog::Entry(MakeTime(1), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log->Add(LoadLog::Entry(MakeTime(2), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - - EXPECT_EQ(kMaxNumEntries, log->entries().size()); - EXPECT_EQ(3u, log->num_entries_truncated()); - - // Check the last entry -- it should be the final entry we added. - EXPECT_TRUE(LogContainsEventAtTime( - *log, 9, MakeTime(2), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE)); -} - -TEST(LoadLogTest, Append) { - scoped_refptr<LoadLog> log1(new LoadLog(10)); - scoped_refptr<LoadLog> log2(new LoadLog(10)); - - log1->Add(LoadLog::Entry(MakeTime(0), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN))); - - log2->Add(LoadLog::Entry(MakeTime(3), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log2->Add(LoadLog::Entry(MakeTime(9), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_END))); - - log1->Append(log2); - - // Add something else to log2 (should NOT be reflected in log1). - log2->Add(LoadLog::Entry(MakeTime(19), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - - EXPECT_EQ(3u, log1->entries().size()); - EXPECT_EQ(0u, log1->num_entries_truncated()); - - EXPECT_TRUE(LogContainsEventAtTime( - *log1, 0, MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN)); - - EXPECT_TRUE(LogContainsEventAtTime( - *log1, 1, MakeTime(3), LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE)); - - EXPECT_TRUE(LogContainsEventAtTime( - *log1, 2, MakeTime(9), LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_END)); -} - -TEST(LoadLogTest, AppendWithTruncation) { - // Append() should also respect the maximum number of entries bound. - // (This is basically the same test as LoadLogTest.Truncation). - - // Create two load logs, which both have 6 out of 10 entries filled. - size_t kMaxNumEntries = 10; - scoped_refptr<LoadLog> log1(new LoadLog(kMaxNumEntries)); - scoped_refptr<LoadLog> log2(new LoadLog(kMaxNumEntries)); - for (size_t i = 0; i < 6; ++i) { - log1->Add(LoadLog::Entry(MakeTime(i), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log2->Add(LoadLog::Entry(MakeTime(2 * i), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - } - - // Append log2 to log1. - log1->Append(log2); - - // The combined log totalled 12 entries, so 2 must have been dropped. - EXPECT_EQ(10u, log1->entries().size()); - EXPECT_EQ(2u, log1->num_entries_truncated()); - - // Combined log should end with the final entry of log2. - EXPECT_TRUE(LogContainsEventAtTime( - *log1, 9, MakeTime(10), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE)); -} - -TEST(LoadLogTest, EventTypeToString) { - EXPECT_STREQ("HOST_RESOLVER_IMPL", - LoadLog::EventTypeToString(LoadLog::TYPE_HOST_RESOLVER_IMPL)); - EXPECT_STREQ("HOST_RESOLVER_IMPL_OBSERVER_ONSTART", - LoadLog::EventTypeToString( - LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART)); -} - -TEST(LoadLogTest, String) { - scoped_refptr<LoadLog> log(new LoadLog(10)); - - // Make sure that AddStringLiteral() adds a literal and not a std::string. - // (in case there is any funny-business with implicit conversions). - LoadLog::AddStringLiteral(log, "This is a literal"); - log->Add(LoadLog::Entry(MakeTime(0), "Another literal")); - log->Add(LoadLog::Entry(MakeTime(0), std::string("Now a std::string"))); - - ASSERT_EQ(3u, log->entries().size()); - EXPECT_EQ(LoadLog::Entry::TYPE_STRING_LITERAL, log->entries()[0].type); - EXPECT_EQ(LoadLog::Entry::TYPE_STRING_LITERAL, log->entries()[1].type); - EXPECT_EQ(LoadLog::Entry::TYPE_STRING, log->entries()[2].type); -} - -} // namespace -} // namespace net diff --git a/net/base/load_log_util_unittest.cc b/net/base/load_log_util_unittest.cc deleted file mode 100644 index 7dba908..0000000 --- a/net/base/load_log_util_unittest.cc +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2009 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/load_log_unittest.h" -#include "net/base/load_log_util.h" -#include "net/base/net_errors.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { -namespace { - -TEST(LoadLogUtilTest, Basic) { - scoped_refptr<LoadLog> log(new LoadLog(10)); - - log->Add(LoadLog::Entry(MakeTime(1), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN))); - log->Add( - LoadLog::Entry( - MakeTime(5), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_BEGIN))); - log->Add( - LoadLog::Entry( - MakeTime(8), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_END))); - - log->Add(LoadLog::Entry(MakeTime(12), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - - log->Add(LoadLog::Entry(MakeTime(131), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_END))); - - EXPECT_EQ( - "t= 1: +HOST_RESOLVER_IMPL [dt=130]\n" - "t= 5: HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 3]\n" - "t= 12: CANCELLED\n" - "t=131: -HOST_RESOLVER_IMPL", - LoadLogUtil::PrettyPrintAsEventTree(log)); -} - -TEST(LoadLogUtilTest, Basic2) { - scoped_refptr<LoadLog> log(new LoadLog(10)); - - log->Add(LoadLog::Entry(MakeTime(1), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN))); - - log->Add(LoadLog::Entry(MakeTime(12), "Sup foo")); - log->Add(LoadLog::Entry(MakeTime(12), ERR_UNEXPECTED)); - log->Add(LoadLog::Entry(MakeTime(14), "Multiline\nString")); - - log->Add(LoadLog::Entry(MakeTime(131), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_END))); - - EXPECT_EQ( - "t= 1: +HOST_RESOLVER_IMPL [dt=130]\n" - "t= 12: \"Sup foo\"\n" - "t= 12: error code: -9 (net::ERR_UNEXPECTED)\n" - "t= 14: \"Multiline\n" - "String\"\n" - "t=131: -HOST_RESOLVER_IMPL", - LoadLogUtil::PrettyPrintAsEventTree(log)); -} - -TEST(LoadLogUtilTest, UnmatchedOpen) { - scoped_refptr<LoadLog> log(new LoadLog(10)); - - log->Add(LoadLog::Entry(MakeTime(3), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_BEGIN))); - // Note that there is no matching call to PHASE_END for all of the following. - log->Add( - LoadLog::Entry( - MakeTime(6), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_BEGIN))); - log->Add( - LoadLog::Entry( - MakeTime(7), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_BEGIN))); - log->Add( - LoadLog::Entry( - MakeTime(8), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, - LoadLog::PHASE_BEGIN))); - log->Add(LoadLog::Entry(MakeTime(10), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - log->Add(LoadLog::Entry(MakeTime(16), - LoadLog::Event(LoadLog::TYPE_HOST_RESOLVER_IMPL, - LoadLog::PHASE_END))); - - EXPECT_EQ( - "t= 3: +HOST_RESOLVER_IMPL [dt=13]\n" - "t= 6: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt=10]\n" - "t= 7: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 9]\n" - "t= 8: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 8]\n" - "t=10: CANCELLED\n" - "t=16: -HOST_RESOLVER_IMPL", - LoadLogUtil::PrettyPrintAsEventTree(log)); -} - -TEST(LoadLogUtilTest, DisplayOfTruncated) { - size_t kMaxNumEntries = 5; - scoped_refptr<LoadLog> log(new LoadLog(kMaxNumEntries)); - - // Add a total of 10 events. This means that 5 will be truncated. - log->Add(LoadLog::Entry(MakeTime(0), - LoadLog::Event(LoadLog::TYPE_TCP_CONNECT, - LoadLog::PHASE_BEGIN))); - for (size_t i = 1; i < 8; ++i) { - log->Add(LoadLog::Entry(MakeTime(i), - LoadLog::Event(LoadLog::TYPE_CANCELLED, - LoadLog::PHASE_NONE))); - } - log->Add(LoadLog::Entry(MakeTime(9), - LoadLog::Event(LoadLog::TYPE_TCP_CONNECT, - LoadLog::PHASE_END))); - - EXPECT_EQ( - "t=0: +TCP_CONNECT [dt=9]\n" - "t=1: CANCELLED\n" - "t=2: CANCELLED\n" - "t=3: CANCELLED\n" - " ... Truncated 4 entries ...\n" - "t=9: -TCP_CONNECT", - LoadLogUtil::PrettyPrintAsEventTree(log)); -} - -} // namespace -} // namespace net diff --git a/net/base/mapped_host_resolver.cc b/net/base/mapped_host_resolver.cc index 164d7c6..fdd9de7 100644 --- a/net/base/mapped_host_resolver.cc +++ b/net/base/mapped_host_resolver.cc @@ -18,11 +18,11 @@ int MappedHostResolver::Resolve(const RequestInfo& info, AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log) { + const BoundNetLog& net_log) { // Modify the request before forwarding it to |impl_|. RequestInfo modified_info = info; RewriteRequest(&modified_info); - return impl_->Resolve(modified_info, addresses, callback, out_req, load_log); + return impl_->Resolve(modified_info, addresses, callback, out_req, net_log); } void MappedHostResolver::CancelRequest(RequestHandle req) { diff --git a/net/base/mapped_host_resolver.h b/net/base/mapped_host_resolver.h index a637a40..7339f06 100644 --- a/net/base/mapped_host_resolver.h +++ b/net/base/mapped_host_resolver.h @@ -28,7 +28,7 @@ class MappedHostResolver : public HostResolver { AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log); + const BoundNetLog& net_log); virtual void CancelRequest(RequestHandle req); virtual void AddObserver(Observer* observer); virtual void RemoveObserver(Observer* observer); diff --git a/net/base/mapped_host_resolver_unittest.cc b/net/base/mapped_host_resolver_unittest.cc index 6da84e0..4b7281c 100644 --- a/net/base/mapped_host_resolver_unittest.cc +++ b/net/base/mapped_host_resolver_unittest.cc @@ -6,6 +6,7 @@ #include "net/base/mock_host_resolver.h" #include "net/base/net_errors.h" +#include "net/base/net_log.h" #include "net/base/net_util.h" #include "testing/gtest/include/gtest/gtest.h" @@ -31,7 +32,7 @@ TEST(MappedHostResolverTest, Inclusion) { // Try resolving "www.google.com:80". There are no mappings yet, so this // hits |resolver_impl| and fails. rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); // Remap *.google.com to baz.com. @@ -39,7 +40,7 @@ TEST(MappedHostResolverTest, Inclusion) { // Try resolving "www.google.com:80". Should be remapped to "baz.com:80". rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); EXPECT_EQ(80, address_list.GetPort()); @@ -47,7 +48,7 @@ TEST(MappedHostResolverTest, Inclusion) { // Try resolving "foo.com:77". This will NOT be remapped, so result // is "foo.com:77". rv = resolver->Resolve(HostResolver::RequestInfo("foo.com", 77), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.8", NetAddressToString(address_list.head())); EXPECT_EQ(77, address_list.GetPort()); @@ -57,7 +58,7 @@ TEST(MappedHostResolverTest, Inclusion) { // Try resolving "chromium.org:61". Should be remapped to "proxy:99". rv = resolver->Resolve(HostResolver::RequestInfo("chromium.org", 61), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.11", NetAddressToString(address_list.head())); EXPECT_EQ(99, address_list.GetPort()); @@ -85,14 +86,14 @@ TEST(MappedHostResolverTest, Exclusion) { // Try resolving "www.google.com". Should not be remapped due to exclusion). rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.3", NetAddressToString(address_list.head())); EXPECT_EQ(80, address_list.GetPort()); // Try resolving "chrome.com:80". Should be remapped to "baz:80". rv = resolver->Resolve(HostResolver::RequestInfo("chrome.com", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); EXPECT_EQ(80, address_list.GetPort()); @@ -116,14 +117,14 @@ TEST(MappedHostResolverTest, SetRulesFromString) { // Try resolving "www.google.com". Should be remapped to "baz". rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.7", NetAddressToString(address_list.head())); EXPECT_EQ(80, address_list.GetPort()); // Try resolving "chrome.net:80". Should be remapped to "bar:60". rv = resolver->Resolve(HostResolver::RequestInfo("chrome.net", 80), - &address_list, NULL, NULL, NULL); + &address_list, NULL, NULL, BoundNetLog()); EXPECT_EQ(OK, rv); EXPECT_EQ("192.168.1.9", NetAddressToString(address_list.head())); EXPECT_EQ(60, address_list.GetPort()); diff --git a/net/base/mock_host_resolver.cc b/net/base/mock_host_resolver.cc index 239275e..7f2512f 100644 --- a/net/base/mock_host_resolver.cc +++ b/net/base/mock_host_resolver.cc @@ -46,12 +46,12 @@ int MockHostResolverBase::Resolve(const RequestInfo& info, AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log) { + const BoundNetLog& net_log) { if (synchronous_mode_) { callback = NULL; out_req = NULL; } - return impl_->Resolve(info, addresses, callback, out_req, load_log); + return impl_->Resolve(info, addresses, callback, out_req, net_log); } void MockHostResolverBase::CancelRequest(RequestHandle req) { diff --git a/net/base/mock_host_resolver.h b/net/base/mock_host_resolver.h index c1f93f5..d08dc42 100644 --- a/net/base/mock_host_resolver.h +++ b/net/base/mock_host_resolver.h @@ -43,7 +43,7 @@ class MockHostResolverBase : public HostResolver { AddressList* addresses, CompletionCallback* callback, RequestHandle* out_req, - LoadLog* load_log); + const BoundNetLog& net_log); virtual void CancelRequest(RequestHandle req); virtual void AddObserver(Observer* observer); virtual void RemoveObserver(Observer* observer); diff --git a/net/base/net_log.cc b/net/base/net_log.cc new file mode 100644 index 0000000..1b1661e --- /dev/null +++ b/net/base/net_log.cc @@ -0,0 +1,154 @@ +// Copyright (c) 2010 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/net_log.h" +#include "base/logging.h" + +namespace net { + +// static +const char* NetLog::EventTypeToString(EventType event) { + switch (event) { +#define EVENT_TYPE(label) case TYPE_ ## label: return #label; +#include "net/base/net_log_event_type_list.h" +#undef EVENT_TYPE + } + return NULL; +} + +void BoundNetLog::AddEntry(const NetLog::Entry& entry) const { + if (net_log_) + net_log_->AddEntry(entry); +} + +bool BoundNetLog::HasListener() const { + if (net_log_) + return net_log_->HasListener(); + return false; +} + +void BoundNetLog::AddEvent(NetLog::EventType event_type) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = base::TimeTicks::Now(); + entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE); + AddEntry(entry); + } +} + +void BoundNetLog::BeginEvent(NetLog::EventType event_type) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = base::TimeTicks::Now(); + entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN); + AddEntry(entry); + } +} + +void BoundNetLog::BeginEventWithString(NetLog::EventType event_type, + const std::string& string) const { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = base::TimeTicks::Now(); + entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN); + entry.string = string; + AddEntry(entry); +} + +void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type, + int integer) const { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = base::TimeTicks::Now(); + entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE); + entry.error_code = integer; + AddEntry(entry); +} + +void BoundNetLog::EndEvent(NetLog::EventType event_type) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = base::TimeTicks::Now(); + entry.event = NetLog::Event(event_type, NetLog::PHASE_END); + AddEntry(entry); + } +} + +void BoundNetLog::AddStringLiteral(const char* literal) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_STRING_LITERAL; + entry.time = base::TimeTicks::Now(); + entry.literal = literal; + AddEntry(entry); + } +} + +void BoundNetLog::AddString(const std::string& string) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_STRING; + entry.time = base::TimeTicks::Now(); + entry.string = string; + AddEntry(entry); + } +} + +void BoundNetLog::AddErrorCode(int error) const { + if (net_log_) { + NetLog::Entry entry; + entry.source = source_; + entry.type = NetLog::Entry::TYPE_ERROR_CODE; + entry.time = base::TimeTicks::Now(); + entry.error_code = error; + AddEntry(entry); + } +} + +// static +BoundNetLog BoundNetLog::Make(NetLog* net_log, + NetLog::SourceType source_type) { + if (!net_log) + return BoundNetLog(); + + NetLog::Source source(source_type, net_log->NextID()); + return BoundNetLog(source, net_log); +} + +void CapturingNetLog::AddEntry(const Entry& entry) { + if (entries_.size() + 1 < max_num_entries_) + entries_.push_back(entry); +} + +int CapturingNetLog::NextID() { + return next_id_++; +} + +void CapturingNetLog::Clear() { + entries_.clear(); +} + +void CapturingBoundNetLog::Clear() { + capturing_net_log_->Clear(); +} + +void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const { + for (size_t i = 0; i < entries().size(); ++i) { + NetLog::Entry entry = entries()[i]; + entry.source = net_log.source(); + net_log.AddEntry(entry); + } +} + +} // namespace net diff --git a/net/base/net_log.h b/net/base/net_log.h new file mode 100644 index 0000000..79c6585 --- /dev/null +++ b/net/base/net_log.h @@ -0,0 +1,238 @@ +// Copyright (c) 2010 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_NET_LOG_H_ +#define NET_BASE_NET_LOG_H_ + +#include <string> +#include <vector> + +#include "base/scoped_ptr.h" +#include "base/time.h" +#include "net/base/net_log.h" + +namespace net { + +// NetLog is the destination for log messages generated by the network stack. +// Each log message has a "source" field which identifies the specific entity +// that generated the message (for example, which URLRequest or which +// SocketStream). +// +// To avoid needing to pass in the "source id" to the logging functions, NetLog +// is usually accessed through a BoundNetLog, which will always pass in a +// specific source ID. +// +// Note that NetLog is NOT THREADSAFE. +class NetLog { + public: + // TODO(eroman): Really, EventType and EventPhase should be + // Event::Type and Event::Phase, to be consisent with Entry. + // But there lots of consumers to change! + enum EventType { +#define EVENT_TYPE(label) TYPE_ ## label, +#include "net/base/net_log_event_type_list.h" +#undef EVENT_TYPE + }; + + // The 'phase' of an event trace (whether it marks the beginning or end + // of an event.). + enum EventPhase { + PHASE_NONE, + PHASE_BEGIN, + PHASE_END, + }; + + struct Event { + Event(EventType type, EventPhase phase) : type(type), phase(phase) {} + Event() {} + + EventType type; + EventPhase phase; + }; + + // The "source" identifies the entity that generated the log message. + enum SourceType { + SOURCE_NONE, + SOURCE_URL_REQUEST, + SOURCE_SOCKET_STREAM, + SOURCE_INIT_PROXY_RESOLVER, + SOURCE_CONNECT_JOB, + }; + + // Identifies the entity that generated this log. The |id| field should + // uniquely identify the source, and is used by log observers to infer + // message groupings. Can use NetLog::NextID() to create unique IDs. + struct Source { + Source() : type(SOURCE_NONE), id(-1) {} + Source(SourceType type, int id) : type(type), id(id) {} + + SourceType type; + int id; + }; + + // TODO(eroman): generalize the entries so events can specify multiple + // parameters, and TYPE_STRING is rarely needed. + struct Entry { + enum Type { + // This entry describes an event trace. + TYPE_EVENT, + + // This entry describes a network error code that was returned. + TYPE_ERROR_CODE, + + // This entry is a free-form std::string. + TYPE_STRING, + + // This entry is a C-string literal. + TYPE_STRING_LITERAL, + }; + + Source source; + + Type type; + base::TimeTicks time; + + // The following is basically a union, only one of them should be + // used depending on what |type| is. + Event event; // valid when (type == TYPE_EVENT). + int error_code; // valid when (type == TYPE_ERROR_CODE). + std::string string; // valid when (type == TYPE_STRING). + const char* literal; // valid when (type == TYPE_STRING_LITERAL). + }; + + NetLog() {} + virtual ~NetLog() {} + + // Adds a message to the log. + virtual void AddEntry(const Entry& entry) = 0; + + // Returns a unique ID which can be used as a source ID. + virtual int NextID() = 0; + + // Returns true if more complicated messages should be sent to the log. + // TODO(eroman): This is a carry-over from refactoring; figure out + // something better. + virtual bool HasListener() const = 0; + + // Returns a C-String symbolic name for |event_type|. + static const char* EventTypeToString(EventType event_type); + + private: + DISALLOW_COPY_AND_ASSIGN(NetLog); +}; + +// Helper that binds a Source to a NetLog, and exposes convenience methods to +// output log messages without needing to pass in the source. +class BoundNetLog { + public: + BoundNetLog() : net_log_(NULL) {} + + // TODO(eroman): This is a complete hack to allow passing in NULL in + // place of a BoundNetLog. I added this while refactoring to simplify the + // task of updating all the callers. + BoundNetLog(int) : net_log_(NULL) {} + + BoundNetLog(const NetLog::Source& source, NetLog* net_log) + : source_(source), net_log_(net_log) { + } + + void AddEntry(const NetLog::Entry& entry) const; + + // Convenience methods that call through to the NetLog, passing in the + // currently bound source. + void AddEvent(NetLog::EventType event_type) const; + bool HasListener() const; + void BeginEvent(NetLog::EventType event_type) const; + void BeginEventWithString(NetLog::EventType event_type, + const std::string& string) const; + void AddEventWithInteger(NetLog::EventType event_type, int integer) const; + void EndEvent(NetLog::EventType event_type) const; + void AddStringLiteral(const char* literal) const; + void AddString(const std::string& string) const; + void AddErrorCode(int error) const; + + // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care + // of creating a unique source ID, and handles the case of NULL net_log. + static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); + + const NetLog::Source& source() const { return source_; } + NetLog* net_log() const { return net_log_; } + + private: + NetLog::Source source_; + NetLog* net_log_; +}; + +// CapturingNetLog is an implementation of NetLog that saves messages to a +// bounded buffer. +class CapturingNetLog : public NetLog { + public: + // Ordered set of entries that were logged. + typedef std::vector<Entry> EntryList; + + enum { kUnbounded = -1 }; + + // Creates a CapturingNetLog that logs a maximum of |max_num_entries| + // messages. + explicit CapturingNetLog(size_t max_num_entries) + : next_id_(0), max_num_entries_(max_num_entries) {} + + // NetLog implementation: + virtual void AddEntry(const Entry& entry); + virtual int NextID(); + virtual bool HasListener() const { return true; } + + // Returns the list of all entries in the log. + const EntryList& entries() const { return entries_; } + + void Clear(); + + private: + int next_id_; + size_t max_num_entries_; + EntryList entries_; + + DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); +}; + +// Helper class that exposes a similar API as BoundNetLog, but uses a +// CapturingNetLog rather than the more generic NetLog. +// +// CapturingBoundNetLog can easily be converted to a BoundNetLog using the +// bound() method. +class CapturingBoundNetLog { + public: + CapturingBoundNetLog(const NetLog::Source& source, CapturingNetLog* net_log) + : source_(source), capturing_net_log_(net_log) { + } + + explicit CapturingBoundNetLog(size_t max_num_entries) + : capturing_net_log_(new CapturingNetLog(max_num_entries)) {} + + // The returned BoundNetLog is only valid while |this| is alive. + BoundNetLog bound() const { + return BoundNetLog(source_, capturing_net_log_.get()); + } + + // Returns the list of all entries in the log. + const CapturingNetLog::EntryList& entries() const { + return capturing_net_log_->entries(); + } + + void Clear(); + + // Sends all of captured messages to |net_log|, using the same source ID + // as |net_log|. + void AppendTo(const BoundNetLog& net_log) const; + + private: + NetLog::Source source_; + scoped_ptr<CapturingNetLog> capturing_net_log_; + + DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); +}; + +} // namespace net + +#endif // NET_BASE_NET_LOG_H_ diff --git a/net/base/load_log_event_type_list.h b/net/base/net_log_event_type_list.h index be43760..cee9466 100644 --- a/net/base/load_log_event_type_list.h +++ b/net/base/net_log_event_type_list.h @@ -3,7 +3,7 @@ // found in the LICENSE file. // NOTE: No header guards are used, since this file is intended to be expanded -// directly into load_log.h. DO NOT include this file anywhere else. +// directly into net_log.h. DO NOT include this file anywhere else. // -------------------------------------------------------------------------- // General pseudo-events @@ -13,6 +13,11 @@ // log context around it.) EVENT_TYPE(CANCELLED) +// Marks the creation/destruction of a request (URLRequest or SocketStream). +// In the begin phase of this event, the message will contain a string which +// is the URL. +EVENT_TYPE(REQUEST_ALIVE) + // ------------------------------------------------------------------------ // HostResolverImpl // ------------------------------------------------------------------------ @@ -124,6 +129,10 @@ EVENT_TYPE(SOCKET_BACKUP_CREATED) // A backup socket is created due to slow connect EVENT_TYPE(SOCKET_BACKUP_TIMER_EXTENDED) +// Identifies the NetLog::Source() for the ConnectJob that this socket ended +// up binding to. +EVENT_TYPE(SOCKET_POOL_CONNECT_JOB_ID) + // ------------------------------------------------------------------------ // URLRequest // ------------------------------------------------------------------------ diff --git a/net/base/load_log_unittest.h b/net/base/net_log_unittest.h index b1922ef..39a1f36 100644 --- a/net/base/load_log_unittest.h +++ b/net/base/net_log_unittest.h @@ -2,11 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef NET_BASE_LOAD_LOG_UNITTEST_H_ -#define NET_BASE_LOAD_LOG_UNITTEST_H_ +#ifndef NET_BASE_NET_LOG_UNITTEST_H_ +#define NET_BASE_NET_LOG_UNITTEST_H_ #include <cstddef> -#include "net/base/load_log.h" +#include <vector> +#include "net/base/net_log.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { @@ -19,23 +20,23 @@ inline base::TimeTicks MakeTime(int t) { } inline ::testing::AssertionResult LogContainsEventHelper( - const LoadLog& log, + const CapturingNetLog::EntryList& entries, int i, // Negative indices are reverse indices. const base::TimeTicks& expected_time, bool check_time, - LoadLog::EventType expected_event, - LoadLog::EventPhase expected_phase) { + NetLog::EventType expected_event, + NetLog::EventPhase expected_phase) { // Negative indices are reverse indices. - size_t j = (i < 0) ? log.entries().size() + i : i; - if (j >= log.entries().size()) + size_t j = (i < 0) ? entries.size() + i : i; + if (j >= entries.size()) return ::testing::AssertionFailure() << j << " is out of bounds."; - const LoadLog::Entry& entry = log.entries()[j]; - if (entry.type != LoadLog::Entry::TYPE_EVENT) + const NetLog::Entry& entry = entries[j]; + if (entry.type != NetLog::Entry::TYPE_EVENT) return ::testing::AssertionFailure() << "Not a TYPE_EVENT entry"; if (expected_event != entry.event.type) { return ::testing::AssertionFailure() - << "Actual event: " << LoadLog::EventTypeToString(entry.event.type) - << ". Expected event: " << LoadLog::EventTypeToString(expected_event) + << "Actual event: " << NetLog::EventTypeToString(entry.event.type) + << ". Expected event: " << NetLog::EventTypeToString(expected_event) << "."; } if (expected_phase != entry.event.phase) { @@ -55,50 +56,50 @@ inline ::testing::AssertionResult LogContainsEventHelper( } inline ::testing::AssertionResult LogContainsEventAtTime( - const LoadLog& log, + const CapturingNetLog::EntryList& log, int i, // Negative indices are reverse indices. const base::TimeTicks& expected_time, - LoadLog::EventType expected_event, - LoadLog::EventPhase expected_phase) { + NetLog::EventType expected_event, + NetLog::EventPhase expected_phase) { return LogContainsEventHelper(log, i, expected_time, true, expected_event, expected_phase); } // Version without timestamp. inline ::testing::AssertionResult LogContainsEvent( - const LoadLog& log, + const CapturingNetLog::EntryList& log, int i, // Negative indices are reverse indices. - LoadLog::EventType expected_event, - LoadLog::EventPhase expected_phase) { + NetLog::EventType expected_event, + NetLog::EventPhase expected_phase) { return LogContainsEventHelper(log, i, base::TimeTicks(), false, expected_event, expected_phase); } // Version for PHASE_BEGIN (and no timestamp). inline ::testing::AssertionResult LogContainsBeginEvent( - const LoadLog& log, + const CapturingNetLog::EntryList& log, int i, // Negative indices are reverse indices. - LoadLog::EventType expected_event) { - return LogContainsEvent(log, i, expected_event, LoadLog::PHASE_BEGIN); + NetLog::EventType expected_event) { + return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN); } // Version for PHASE_END (and no timestamp). inline ::testing::AssertionResult LogContainsEndEvent( - const LoadLog& log, + const CapturingNetLog::EntryList& log, int i, // Negative indices are reverse indices. - LoadLog::EventType expected_event) { - return LogContainsEvent(log, i, expected_event, LoadLog::PHASE_END); + NetLog::EventType expected_event) { + return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END); } inline ::testing::AssertionResult LogContainsEntryWithType( - const LoadLog& log, + const CapturingNetLog::EntryList& entries, int i, // Negative indices are reverse indices. - LoadLog::Entry::Type type) { + NetLog::Entry::Type type) { // Negative indices are reverse indices. - size_t j = (i < 0) ? log.entries().size() + i : i; - if (j >= log.entries().size()) + size_t j = (i < 0) ? entries.size() + i : i; + if (j >= entries.size()) return ::testing::AssertionFailure() << j << " is out of bounds."; - const LoadLog::Entry& entry = log.entries()[j]; + const NetLog::Entry& entry = entries[j]; if (entry.type != type) return ::testing::AssertionFailure() << "Type does not match."; return ::testing::AssertionSuccess(); @@ -108,23 +109,24 @@ inline ::testing::AssertionResult LogContainsEntryWithType( // Expect that the log contains an event, but don't care about where // as long as the index where it is found is greater than min_index. // Returns the position where the event was found. -inline size_t ExpectLogContainsSomewhere(const LoadLog* log, - size_t min_index, - LoadLog::EventType expected_event, - LoadLog::EventPhase expected_phase) { +inline size_t ExpectLogContainsSomewhere( + const CapturingNetLog::EntryList& entries, + size_t min_index, + NetLog::EventType expected_event, + NetLog::EventPhase expected_phase) { size_t i = 0; - for (; i < log->entries().size(); ++i) { - const LoadLog::Entry& entry = log->entries()[i]; - if (entry.type == LoadLog::Entry::TYPE_EVENT && + for (; i < entries.size(); ++i) { + const NetLog::Entry& entry = entries[i]; + if (entry.type == NetLog::Entry::TYPE_EVENT && entry.event.type == expected_event && entry.event.phase == expected_phase) break; } - EXPECT_LT(i, log->entries().size()); + EXPECT_LT(i, entries.size()); EXPECT_GE(i, min_index); return i; } } // namespace net -#endif // NET_BASE_LOAD_LOG_UNITTEST_H_ +#endif // NET_BASE_NET_LOG_UNITTEST_H_ diff --git a/net/base/load_log_util.cc b/net/base/net_log_util.cc index cad20a5..420fb2d 100644 --- a/net/base/load_log_util.cc +++ b/net/base/net_log_util.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "net/base/load_log_util.h" +#include "net/base/net_log_util.h" #include "base/format_macros.h" #include "base/string_util.h" @@ -13,12 +13,13 @@ namespace { class FormatHelper { public: - std::string ToString(const LoadLog* log) { + std::string ToString(const std::vector<NetLog::Entry>& entries, + size_t num_entries_truncated) { entries_.clear(); // Pass 1: Match the start/end of indentation blocks. Fills |entries_| // with the results. - PopulateEntries(log); + PopulateEntries(entries); // Pass 2: Figure out the maximum width of each column. This allows us // to right-justify text within each column. @@ -32,9 +33,9 @@ class FormatHelper { const int kSpacesPerIndentation = 2; for (size_t i = 0; i < entries_.size(); ++i) { - if (log->num_entries_truncated() > 0 && i + 1 == entries_.size()) { + if (num_entries_truncated > 0 && i + 1 == entries_.size()) { StringAppendF(&result, " ... Truncated %" PRIuS " entries ...\n", - log->num_entries_truncated()); + num_entries_truncated); } if (entries_[i].block_index != -1 && @@ -71,29 +72,29 @@ class FormatHelper { private: struct Entry { - explicit Entry(const LoadLog::Entry* log_entry) + explicit Entry(const NetLog::Entry* log_entry) : log_entry(log_entry), indentation(0), block_index(-1) {} bool IsBeginEvent() const { - return log_entry->type == LoadLog::Entry::TYPE_EVENT && - log_entry->event.phase == LoadLog::PHASE_BEGIN; + return log_entry->type == NetLog::Entry::TYPE_EVENT && + log_entry->event.phase == NetLog::PHASE_BEGIN; } bool IsEndEvent() const { - return log_entry->type == LoadLog::Entry::TYPE_EVENT && - log_entry->event.phase == LoadLog::PHASE_END; + return log_entry->type == NetLog::Entry::TYPE_EVENT && + log_entry->event.phase == NetLog::PHASE_END; } - const LoadLog::Entry* log_entry; + const NetLog::Entry* log_entry; size_t indentation; int block_index; // The index of the matching start / end of block. }; - void PopulateEntries(const LoadLog* log) { + void PopulateEntries(const std::vector<NetLog::Entry>& entries) { int current_indentation = 0; - for (size_t i = 0; i < log->entries().size(); ++i) { - Entry entry(&log->entries()[i]); + for (size_t i = 0; i < entries.size(); ++i) { + Entry entry(&entries[i]); entry.indentation = current_indentation; @@ -140,7 +141,7 @@ class FormatHelper { *max_time_width = *max_indentation = *max_type_width = *max_dt_width = 0; for (size_t i = 0; i < entries_.size(); ++i) { *max_time_width = std::max(*max_time_width, GetTimeString(i).size()); - if (entries_[i].log_entry->type == LoadLog::Entry::TYPE_EVENT) + if (entries_[i].log_entry->type == NetLog::Entry::TYPE_EVENT) *max_type_width = std::max(*max_type_width, GetEntryString(i).size()); *max_indentation = std::max(*max_indentation, entries_[i].indentation); @@ -168,32 +169,32 @@ class FormatHelper { } std::string GetEntryString(size_t index) { - const LoadLog::Entry* entry = entries_[index].log_entry; + const NetLog::Entry* entry = entries_[index].log_entry; std::string entry_str; - LoadLog::EventPhase phase = LoadLog::PHASE_NONE; + NetLog::EventPhase phase = NetLog::PHASE_NONE; switch (entry->type) { - case LoadLog::Entry::TYPE_EVENT: - entry_str = LoadLog::EventTypeToString(entry->event.type); + case NetLog::Entry::TYPE_EVENT: + entry_str = NetLog::EventTypeToString(entry->event.type); phase = entry->event.phase; - if (phase == LoadLog::PHASE_BEGIN && + if (phase == NetLog::PHASE_BEGIN && index + 1 < entries_.size() && static_cast<size_t>(entries_[index + 1].block_index) == index) { // If this starts an empty block, we will pretend it is a PHASE_NONE // so we don't print the "+" prefix. - phase = LoadLog::PHASE_NONE; + phase = NetLog::PHASE_NONE; } break; - case LoadLog::Entry::TYPE_ERROR_CODE: + case NetLog::Entry::TYPE_ERROR_CODE: entry_str = StringPrintf("error code: %d (%s)", entry->error_code, ErrorToString(entry->error_code)); break; - case LoadLog::Entry::TYPE_STRING: + case NetLog::Entry::TYPE_STRING: entry_str = StringPrintf("\"%s\"", entry->string.c_str()); break; - case LoadLog::Entry::TYPE_STRING_LITERAL: + case NetLog::Entry::TYPE_STRING_LITERAL: entry_str = StringPrintf("\"%s\"", entry->literal); break; default: @@ -201,11 +202,11 @@ class FormatHelper { } switch (phase) { - case LoadLog::PHASE_BEGIN: + case NetLog::PHASE_BEGIN: return std::string("+") + entry_str; - case LoadLog::PHASE_END: + case NetLog::PHASE_END: return std::string("-") + entry_str; - case LoadLog::PHASE_NONE: + case NetLog::PHASE_NONE: return std::string(" ") + entry_str; default: NOTREACHED(); @@ -226,9 +227,11 @@ class FormatHelper { } // namespace // static -std::string LoadLogUtil::PrettyPrintAsEventTree(const LoadLog* log) { +std::string NetLogUtil::PrettyPrintAsEventTree( + const std::vector<NetLog::Entry>& entries, + size_t num_entries_truncated) { FormatHelper helper; - return helper.ToString(log); + return helper.ToString(entries, num_entries_truncated); } } // namespace net diff --git a/net/base/load_log_util.h b/net/base/net_log_util.h index 363e219..716a941 100644 --- a/net/base/load_log_util.h +++ b/net/base/net_log_util.h @@ -2,24 +2,24 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef NET_BASE_LOAD_LOG_UTIL_H_ -#define NET_BASE_LOAD_LOG_UTIL_H_ +#ifndef NET_BASE_NET_LOG_UTIL_H_ +#define NET_BASE_NET_LOG_UTIL_H_ #include <string> #include <vector> #include "base/basictypes.h" -#include "net/base/load_log.h" +#include "net/base/net_log.h" namespace net { -// The LoadLogUtil utility class contains methods to analyze and visualize -// LoadLogs. +// The NetLogUtil utility class contains methods to analyze and visualize +// NetLog entries. -class LoadLogUtil { +class NetLogUtil { public: struct EventDuration { - LoadLog::EventType event; + NetLog::EventType event; base::TimeDelta duration; }; typedef std::vector<EventDuration> EventDurationList; @@ -63,12 +63,13 @@ class LoadLogUtil { // - Log entries added by AddEvent() have no prefix. // - Time units are given as milliseconds. // - static std::string PrettyPrintAsEventTree(const LoadLog* log); + static std::string PrettyPrintAsEventTree( + const std::vector<NetLog::Entry>& entries, size_t num_entries_truncated); private: - DISALLOW_IMPLICIT_CONSTRUCTORS(LoadLogUtil); + DISALLOW_IMPLICIT_CONSTRUCTORS(NetLogUtil); }; } // namespace net -#endif // NET_BASE_LOAD_LOG_UTIL_H_ +#endif // NET_BASE_NET_LOG_UTIL_H_ diff --git a/net/base/net_log_util_unittest.cc b/net/base/net_log_util_unittest.cc new file mode 100644 index 0000000..713773d --- /dev/null +++ b/net/base/net_log_util_unittest.cc @@ -0,0 +1,145 @@ +// Copyright (c) 2009 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/net_log_unittest.h" +#include "net/base/net_log_util.h" +#include "net/base/net_errors.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { +namespace { + +NetLog::Entry MakeEventEntry(int t, + NetLog::EventType event_type, + NetLog::EventPhase event_phase) { + NetLog::Entry entry; + entry.type = NetLog::Entry::TYPE_EVENT; + entry.time = MakeTime(t); + entry.event = NetLog::Event(event_type, event_phase); + return entry; +} + +NetLog::Entry MakeStringEntry(int t, const std::string& string) { + NetLog::Entry entry; + entry.type = NetLog::Entry::TYPE_STRING; + entry.time = MakeTime(t); + entry.string = string; + return entry; +} + +NetLog::Entry MakeErrorCodeEntry(int t, int error_code) { + NetLog::Entry entry; + entry.type = NetLog::Entry::TYPE_ERROR_CODE; + entry.time = MakeTime(t); + entry.error_code = error_code; + return entry; +} + +TEST(NetLogUtilTest, Basic) { + std::vector<NetLog::Entry> log; + + log.push_back(MakeEventEntry(1, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_BEGIN)); + log.push_back( + MakeEventEntry(5, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, + NetLog::PHASE_BEGIN)); + log.push_back( + MakeEventEntry(8, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, + NetLog::PHASE_END)); + + log.push_back(MakeEventEntry(12, NetLog::TYPE_CANCELLED, + NetLog::PHASE_NONE)); + + log.push_back(MakeEventEntry(131, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_END)); + + EXPECT_EQ( + "t= 1: +HOST_RESOLVER_IMPL [dt=130]\n" + "t= 5: HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 3]\n" + "t= 12: CANCELLED\n" + "t=131: -HOST_RESOLVER_IMPL", + NetLogUtil::PrettyPrintAsEventTree(log, 0)); +} + +TEST(NetLogUtilTest, Basic2) { + std::vector<NetLog::Entry> log; + + log.push_back(MakeEventEntry(1, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_BEGIN)); + + log.push_back(MakeStringEntry(12, "Sup foo")); + log.push_back(MakeErrorCodeEntry(12, ERR_UNEXPECTED)); + log.push_back(MakeStringEntry(14, "Multiline\nString")); + + log.push_back(MakeEventEntry(131, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_END)); + + EXPECT_EQ( + "t= 1: +HOST_RESOLVER_IMPL [dt=130]\n" + "t= 12: \"Sup foo\"\n" + "t= 12: error code: -9 (net::ERR_UNEXPECTED)\n" + "t= 14: \"Multiline\n" + "String\"\n" + "t=131: -HOST_RESOLVER_IMPL", + NetLogUtil::PrettyPrintAsEventTree(log, 0)); +} + +TEST(NetLogUtilTest, UnmatchedOpen) { + std::vector<NetLog::Entry> log; + + log.push_back(MakeEventEntry(3, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_BEGIN)); + // Note that there is no matching call to PHASE_END for all of the following. + log.push_back( + MakeEventEntry( + 6, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, + NetLog::PHASE_BEGIN)); + log.push_back( + MakeEventEntry(7, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, + NetLog::PHASE_BEGIN)); + log.push_back( + MakeEventEntry(8, NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, + NetLog::PHASE_BEGIN)); + log.push_back(MakeEventEntry(10, NetLog::TYPE_CANCELLED, + NetLog::PHASE_NONE)); + log.push_back(MakeEventEntry(16, NetLog::TYPE_HOST_RESOLVER_IMPL, + NetLog::PHASE_END)); + + EXPECT_EQ( + "t= 3: +HOST_RESOLVER_IMPL [dt=13]\n" + "t= 6: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt=10]\n" + "t= 7: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 9]\n" + "t= 8: +HOST_RESOLVER_IMPL_OBSERVER_ONSTART [dt= 8]\n" + "t=10: CANCELLED\n" + "t=16: -HOST_RESOLVER_IMPL", + NetLogUtil::PrettyPrintAsEventTree(log, 0)); +} + +TEST(NetLogUtilTest, DisplayOfTruncated) { + std::vector<NetLog::Entry> log; + + log.push_back(MakeEventEntry(0, + NetLog::TYPE_TCP_CONNECT, + NetLog::PHASE_BEGIN)); + for (size_t i = 1; i <= 3; ++i) { + log.push_back(MakeEventEntry(i, + NetLog::TYPE_CANCELLED, + NetLog::PHASE_NONE)); + } + log.push_back(MakeEventEntry(9, + NetLog::TYPE_TCP_CONNECT, + NetLog::PHASE_END)); + + EXPECT_EQ( + "t=0: +TCP_CONNECT [dt=9]\n" + "t=1: CANCELLED\n" + "t=2: CANCELLED\n" + "t=3: CANCELLED\n" + " ... Truncated 4 entries ...\n" + "t=9: -TCP_CONNECT", + NetLogUtil::PrettyPrintAsEventTree(log, 4)); +} + +} // namespace +} // namespace net |