Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(541)

Unified Diff: net/base/net_log.cc

Issue 10399083: Make NetLog take in callbacks that return Values (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix net-internals Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/net_log.cc
===================================================================
--- net/base/net_log.cc (revision 137673)
+++ net/base/net_log.cc (working copy)
@@ -4,6 +4,7 @@
#include "net/base/net_log.h"
+#include "base/bind.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/time.h"
@@ -50,6 +51,15 @@
return dict;
}
+
+Value* EventParametersCallback(
+ const scoped_refptr<NetLog::EventParameters>& params,
+ NetLog::LogLevel) {
+ if (!params.get())
+ return NULL;
+ return params->ToValue();
+}
+
} // namespace
Value* NetLog::Source::ToValue() const {
@@ -59,6 +69,81 @@
return dict;
}
+void NetLog::Source::AddToEventParameters(DictionaryValue* event_params) const {
+ DictionaryValue* dict = new DictionaryValue();
+ dict->SetInteger("type", static_cast<int>(type));
+ dict->SetInteger("id", static_cast<int>(id));
+ event_params->Set("source_dependency", dict);
+}
+
+// static
+bool NetLog::Source::FromEventParameters(Value* event_params, Source* source) {
+ DictionaryValue* dict;
+ DictionaryValue* source_dict;
+ int source_id;
+ int source_type;
+ if (!event_params ||
+ !event_params->GetAsDictionary(&dict) ||
+ !dict->GetDictionary("source_dependency", &source_dict) ||
+ !source_dict->GetInteger("id", &source_id) ||
+ !source_dict->GetInteger("type", &source_type)) {
+ *source = Source();
+ return false;
+ }
+ DCHECK_LE(0, source_id);
+ DCHECK_LE(0, source_type);
+ DCHECK_LE(source_type, NetLog::SOURCE_COUNT);
+ *source = Source(static_cast<SourceType>(source_type), source_id);
+ return true;
+}
+
+Value* NetLog::Entry::ToValue() const {
+ DictionaryValue* entry_dict(new DictionaryValue());
+
+ entry_dict->SetString("time", TickCountToString(base::TimeTicks::Now()));
+
+ // Set the entry source.
+ DictionaryValue* source_dict = new DictionaryValue();
+ source_dict->SetInteger("id", source_.id);
+ source_dict->SetInteger("type", static_cast<int>(source_.type));
+ entry_dict->Set("source", source_dict);
+
+ // Set the event info.
+ entry_dict->SetInteger("type", static_cast<int>(type_));
+ entry_dict->SetInteger("phase", static_cast<int>(phase_));
+
+ // Set the event-specific parameters.
+ if (parameters_callback_) {
+ Value* value = parameters_callback_->Run(log_level_);
+ if (value)
+ entry_dict->Set("params", value);
+ }
+
+ return entry_dict;
+}
+
+Value* NetLog::Entry::ParametersToValue() const {
+ if (parameters_callback_)
+ return parameters_callback_->Run(log_level_);
+ return NULL;
+}
+
+NetLog::Entry::Entry(
+ EventType type,
+ Source source,
+ EventPhase phase,
+ const ParametersCallback* parameters_callback,
+ LogLevel log_level)
+ : type_(type),
+ source_(source),
+ phase_(phase),
+ parameters_callback_(parameters_callback),
+ log_level_(log_level) {
+};
+
+NetLog::Entry::~Entry() {
+}
+
NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC),
net_log_(NULL) {
}
@@ -81,10 +166,11 @@
void NetLog::AddGlobalEntry(EventType type,
const scoped_refptr<EventParameters>& params) {
+ ParametersCallback callback = base::Bind(&EventParametersCallback, params);
AddEntry(type,
- Source(net::NetLog::SOURCE_NONE, this->NextID()),
+ Source(net::NetLog::SOURCE_NONE, NextID()),
net::NetLog::PHASE_NONE,
- params);
+ &callback);
}
// static
@@ -149,44 +235,6 @@
return NULL;
}
-// static
-Value* NetLog::EntryToDictionaryValue(NetLog::EventType type,
- const base::TimeTicks& time,
- const NetLog::Source& source,
- NetLog::EventPhase phase,
- NetLog::EventParameters* params,
- bool use_strings) {
- DictionaryValue* entry_dict = new DictionaryValue();
-
- entry_dict->SetString("time", TickCountToString(time));
-
- // Set the entry source.
- DictionaryValue* source_dict = new DictionaryValue();
- source_dict->SetInteger("id", source.id);
- if (!use_strings) {
- source_dict->SetInteger("type", static_cast<int>(source.type));
- } else {
- source_dict->SetString("type",
- NetLog::SourceTypeToString(source.type));
- }
- entry_dict->Set("source", source_dict);
-
- // Set the event info.
- if (!use_strings) {
- entry_dict->SetInteger("type", static_cast<int>(type));
- entry_dict->SetInteger("phase", static_cast<int>(phase));
- } else {
- entry_dict->SetString("type", NetLog::EventTypeToString(type));
- entry_dict->SetString("phase", NetLog::EventPhaseToString(phase));
- }
-
- // Set the event-specific parameters.
- if (params)
- entry_dict->Set("params", params->ToValue());
-
- return entry_dict;
-}
-
void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) {
DCHECK(!observer->net_log_);
observer->net_log_ = this;
@@ -204,12 +252,67 @@
observer->net_log_ = NULL;
}
+void NetLog::AddEntry(EventType type,
+ const Source& source,
+ EventPhase phase,
+ const NetLog::ParametersCallback* parameters_callback) {
+ Entry entry(type, source, phase, parameters_callback, GetLogLevel());
+ OnAddEntry(entry);
+}
+
+void BoundNetLog::AddEntry(NetLog::EventType type, NetLog::EventPhase phase) {
+ if (!net_log_)
+ return;
+ net_log_->AddEntry(type, source_, phase, NULL);
+}
+
+void BoundNetLog::AddEntry(NetLog::EventType type,
+ NetLog::EventPhase phase,
+ const NetLog::ParametersCallback& get_parameters) {
+ if (!net_log_)
+ return;
+ net_log_->AddEntry(type, source_, phase, &get_parameters);
+}
+
+void BoundNetLog::AddEvent(NetLog::EventType type) {
+ AddEntry(type, NetLog::PHASE_NONE);
+}
+
+void BoundNetLog::AddEvent(NetLog::EventType type,
+ const NetLog::ParametersCallback& get_parameters) {
+ AddEntry(type, NetLog::PHASE_NONE, get_parameters);
+}
+
+void BoundNetLog::BeginEvent(NetLog::EventType type) {
+ AddEntry(type, NetLog::PHASE_BEGIN);
+}
+
+void BoundNetLog::BeginEvent(NetLog::EventType type,
+ const NetLog::ParametersCallback& get_parameters) {
+ AddEntry(type, NetLog::PHASE_BEGIN, get_parameters);
+}
+
+void BoundNetLog::EndEvent(NetLog::EventType type) {
+ AddEntry(type, NetLog::PHASE_END);
+}
+
+void BoundNetLog::EndEvent(NetLog::EventType type,
+ const NetLog::ParametersCallback& get_parameters) {
+ AddEntry(type, NetLog::PHASE_END, get_parameters);
+}
+
void BoundNetLog::AddEntry(
NetLog::EventType type,
NetLog::EventPhase phase,
const scoped_refptr<NetLog::EventParameters>& params) const {
- if (net_log_)
- net_log_->AddEntry(type, source_, phase, params);
+ if (!net_log_)
+ return;
+ NetLog::ParametersCallback callback =
+ base::Bind(&EventParametersCallback, params);
+ net_log_->AddEntry(type,
eroman 2012/05/22 22:08:06 nit: perhaps this fits onto 1 line.
+ source_,
+ phase,
+ &callback);
}
void BoundNetLog::AddEvent(

Powered by Google App Engine
This is Rietveld 408576698