Skip to content

Commit

Permalink
More cleanup to address TODOs in net_log.h.
Browse files Browse the repository at this point in the history
  * Removes 9 methods: AddEventWithParameters, BeginEventWithParameters, EndEventWithParameters, BeginEventWithString, BeginEventWithInteger, AddEventWithString, AddEventWithInteger, EndEventWithParameters, EndEventWithInteger. This was becoming ridiculous, instead made the EventParameters* a required parameter.
  * Moves CapturingBoundNetLog / CapturingNetLog to its own file.

BUG=37421

Review URL: http://codereview.chromium.org/1746012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45843 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
eroman@chromium.org committed Apr 28, 2010
1 parent 9bb75cc commit ec11be6
Show file tree
Hide file tree
Showing 31 changed files with 429 additions and 437 deletions.
43 changes: 43 additions & 0 deletions net/base/capturing_net_log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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/capturing_net_log.h"

namespace net {

CapturingNetLog::CapturingNetLog(size_t max_num_entries)
: next_id_(0), max_num_entries_(max_num_entries) {
}

void CapturingNetLog::AddEntry(EventType type,
const base::TimeTicks& time,
const Source& source,
EventPhase phase,
EventParameters* extra_parameters) {
Entry entry(type, time, source, phase, extra_parameters);
if (entries_.size() + 1 < max_num_entries_)
entries_.push_back(entry);
}

uint32 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) {
const CapturingNetLog::Entry& entry = entries()[i];
net_log.AddEntryWithTime(entry.type, entry.time, entry.phase,
entry.extra_parameters);
}
}

} // namespace net
109 changes: 109 additions & 0 deletions net/base/capturing_net_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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_CAPTURING_NET_LOG_H_
#define NET_BASE_CAPTURING_NET_LOG_H_

#include <vector>

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "net/base/net_log.h"

namespace net {

// CapturingNetLog is an implementation of NetLog that saves messages to a
// bounded buffer.
class CapturingNetLog : public NetLog {
public:
struct Entry {
Entry(EventType type,
const base::TimeTicks& time,
Source source,
EventPhase phase,
EventParameters* extra_parameters)
: type(type), time(time), source(source), phase(phase),
extra_parameters(extra_parameters) {
}

EventType type;
base::TimeTicks time;
Source source;
EventPhase phase;
scoped_refptr<EventParameters> extra_parameters;
};

// 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);

// NetLog implementation:
virtual void AddEntry(EventType type,
const base::TimeTicks& time,
const Source& source,
EventPhase phase,
EventParameters* extra_parameters);
virtual uint32 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:
uint32 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_CAPTURING_NET_LOG_H_

20 changes: 10 additions & 10 deletions net/base/host_resolver_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ void HostResolverImpl::OnJobComplete(Job* job,
void HostResolverImpl::OnStartRequest(const BoundNetLog& net_log,
int request_id,
const RequestInfo& info) {
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL);
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL);

if (requests_trace_) {
requests_trace_->Add(StringPrintf(
Expand All @@ -1021,14 +1021,14 @@ void HostResolverImpl::OnStartRequest(const BoundNetLog& net_log,

// Notify the observers of the start.
if (!observers_.empty()) {
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART);
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, NULL);

for (ObserversList::iterator it = observers_.begin();
it != observers_.end(); ++it) {
(*it)->OnStartResolution(request_id, info);
}

net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART);
net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART, NULL);
}
}

Expand All @@ -1043,41 +1043,41 @@ void HostResolverImpl::OnFinishRequest(const BoundNetLog& net_log,

// Notify the observers of the completion.
if (!observers_.empty()) {
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH);
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH, NULL);

bool was_resolved = error == OK;
for (ObserversList::iterator it = observers_.begin();
it != observers_.end(); ++it) {
(*it)->OnFinishResolutionWithStatus(request_id, was_resolved, info);
}

net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH);
net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONFINISH, NULL);
}

net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL);
net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL);
}

void HostResolverImpl::OnCancelRequest(const BoundNetLog& net_log,
int request_id,
const RequestInfo& info) {
net_log.AddEvent(NetLog::TYPE_CANCELLED);
net_log.AddEvent(NetLog::TYPE_CANCELLED, NULL);

if (requests_trace_)
requests_trace_->Add(StringPrintf("Cancelled request r%d", request_id));

// Notify the observers of the cancellation.
if (!observers_.empty()) {
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL);
net_log.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL, NULL);

for (ObserversList::iterator it = observers_.begin();
it != observers_.end(); ++it) {
(*it)->OnCancelResolution(request_id, info);
}

net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL);
net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONCANCEL, NULL);
}

net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL);
net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL);
}

void HostResolverImpl::OnIPAddressChanged() {
Expand Down
1 change: 1 addition & 0 deletions net/base/host_resolver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "base/scoped_ptr.h"
#include "net/base/capturing_net_log.h"
#include "net/base/host_cache.h"
#include "net/base/host_resolver.h"
#include "net/base/host_resolver_proc.h"
Expand Down
109 changes: 13 additions & 96 deletions net/base/net_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

#include "net/base/net_log.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/values.h"

Expand All @@ -28,22 +27,22 @@ std::vector<NetLog::EventType> NetLog::GetAllEventTypes() {
return types;
}

void BoundNetLog::AddEntry(NetLog::EventType type,
NetLog::EventPhase phase,
NetLog::EventParameters* extra_parameters) const {
void BoundNetLog::AddEntry(
NetLog::EventType type,
NetLog::EventPhase phase,
const scoped_refptr<NetLog::EventParameters>& params) const {
if (net_log_) {
net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase,
extra_parameters);
net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params);
}
}

void BoundNetLog::AddEntryWithTime(
NetLog::EventType type,
const base::TimeTicks& time,
NetLog::EventPhase phase,
NetLog::EventParameters* extra_parameters) const {
const scoped_refptr<NetLog::EventParameters>& params) const {
if (net_log_) {
net_log_->AddEntry(type, time, source_, phase, extra_parameters);
net_log_->AddEntry(type, time, source_, phase, params);
}
}

Expand All @@ -53,76 +52,24 @@ bool BoundNetLog::HasListener() const {
return false;
}

void BoundNetLog::AddEvent(NetLog::EventType event_type) const {
AddEventWithParameters(event_type, NULL);
}

void BoundNetLog::AddEventWithParameters(
void BoundNetLog::AddEvent(
NetLog::EventType event_type,
NetLog::EventParameters* params) const {
const scoped_refptr<NetLog::EventParameters>& params) const {
AddEntry(event_type, NetLog::PHASE_NONE, params);
}

void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type,
const char* name,
int value) const {
scoped_refptr<NetLog::EventParameters> params =
new NetLogIntegerParameter(name, value);
AddEventWithParameters(event_type, params);
}

void BoundNetLog::AddEventWithString(NetLog::EventType event_type,
const char* name,
const std::string& value) const {
scoped_refptr<NetLog::EventParameters> params =
new NetLogStringParameter(name, value);
AddEventWithParameters(event_type, params);
}

void BoundNetLog::BeginEvent(NetLog::EventType event_type) const {
BeginEventWithParameters(event_type, NULL);
}

void BoundNetLog::BeginEventWithParameters(
void BoundNetLog::BeginEvent(
NetLog::EventType event_type,
NetLog::EventParameters* params) const {
const scoped_refptr<NetLog::EventParameters>& params) const {
AddEntry(event_type, NetLog::PHASE_BEGIN, params);
}

void BoundNetLog::BeginEventWithString(NetLog::EventType event_type,
const char* name,
const std::string& value) const {
scoped_refptr<NetLog::EventParameters> params =
new NetLogStringParameter(name, value);
BeginEventWithParameters(event_type, params);
}

void BoundNetLog::BeginEventWithInteger(NetLog::EventType event_type,
const char* name,
int value) const {
scoped_refptr<NetLog::EventParameters> params =
new NetLogIntegerParameter(name, value);
BeginEventWithParameters(event_type, params);
}

void BoundNetLog::EndEvent(NetLog::EventType event_type) const {
EndEventWithParameters(event_type, NULL);
}

void BoundNetLog::EndEventWithParameters(
void BoundNetLog::EndEvent(
NetLog::EventType event_type,
NetLog::EventParameters* params) const {
const scoped_refptr<NetLog::EventParameters>& params) const {
AddEntry(event_type, NetLog::PHASE_END, params);
}

void BoundNetLog::EndEventWithInteger(NetLog::EventType event_type,
const char* name,
int value) const {
scoped_refptr<NetLog::EventParameters> params =
new NetLogIntegerParameter(name, value);
EndEventWithParameters(event_type, params);
}

// static
BoundNetLog BoundNetLog::Make(NetLog* net_log,
NetLog::SourceType source_type) {
Expand Down Expand Up @@ -150,34 +97,4 @@ Value* NetLogStringParameter::ToValue() const {
return dict;
}

void CapturingNetLog::AddEntry(EventType type,
const base::TimeTicks& time,
const Source& source,
EventPhase phase,
EventParameters* extra_parameters) {
Entry entry(type, time, source, phase, extra_parameters);
if (entries_.size() + 1 < max_num_entries_)
entries_.push_back(entry);
}

uint32 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) {
const CapturingNetLog::Entry& entry = entries()[i];
net_log.AddEntryWithTime(entry.type, entry.time, entry.phase,
entry.extra_parameters);
}
}

} // namespace net
Loading

0 comments on commit ec11be6

Please sign in to comment.