Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/net_log.h" | 5 #include "net/base/net_log.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 9 #include "base/time.h" | 10 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 43 } | 44 } |
| 44 | 45 |
| 45 Value* NetLogBytesTransferredParameter::ToValue() const { | 46 Value* NetLogBytesTransferredParameter::ToValue() const { |
| 46 DictionaryValue* dict = new DictionaryValue(); | 47 DictionaryValue* dict = new DictionaryValue(); |
| 47 dict->SetInteger("byte_count", byte_count_); | 48 dict->SetInteger("byte_count", byte_count_); |
| 48 if (has_bytes_ && byte_count_ > 0) | 49 if (has_bytes_ && byte_count_ > 0) |
| 49 dict->SetString("hex_encoded_bytes", hex_encoded_bytes_); | 50 dict->SetString("hex_encoded_bytes", hex_encoded_bytes_); |
| 50 return dict; | 51 return dict; |
| 51 } | 52 } |
| 52 | 53 |
| 54 | |
| 55 Value* EventParametersCallback( | |
| 56 const scoped_refptr<NetLog::EventParameters>& params, | |
| 57 NetLog::LogLevel) { | |
| 58 if (!params.get()) | |
| 59 return NULL; | |
| 60 return params->ToValue(); | |
| 61 } | |
| 62 | |
| 53 } // namespace | 63 } // namespace |
| 54 | 64 |
| 55 Value* NetLog::Source::ToValue() const { | 65 Value* NetLog::Source::ToValue() const { |
| 56 DictionaryValue* dict = new DictionaryValue(); | 66 DictionaryValue* dict = new DictionaryValue(); |
| 57 dict->SetInteger("type", static_cast<int>(type)); | 67 dict->SetInteger("type", static_cast<int>(type)); |
| 58 dict->SetInteger("id", static_cast<int>(id)); | 68 dict->SetInteger("id", static_cast<int>(id)); |
| 59 return dict; | 69 return dict; |
| 60 } | 70 } |
| 61 | 71 |
| 72 void NetLog::Source::AddToEventParameters(DictionaryValue* event_params) const { | |
| 73 DictionaryValue* dict = new DictionaryValue(); | |
| 74 dict->SetInteger("type", static_cast<int>(type)); | |
| 75 dict->SetInteger("id", static_cast<int>(id)); | |
| 76 event_params->Set("source_dependency", dict); | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 bool NetLog::Source::FromEventParameters(Value* event_params, Source* source) { | |
| 81 DictionaryValue* dict; | |
| 82 DictionaryValue* source_dict; | |
| 83 int source_id; | |
| 84 int source_type; | |
| 85 if (!event_params || | |
| 86 !event_params->GetAsDictionary(&dict) || | |
| 87 !dict->GetDictionary("source_dependency", &source_dict) || | |
| 88 !source_dict->GetInteger("id", &source_id) || | |
| 89 !source_dict->GetInteger("type", &source_type)) { | |
| 90 *source = Source(); | |
| 91 return false; | |
| 92 } | |
| 93 DCHECK_LE(0, source_id); | |
| 94 DCHECK_LE(0, source_type); | |
| 95 DCHECK_LE(source_type, NetLog::SOURCE_COUNT); | |
| 96 *source = Source(static_cast<SourceType>(source_type), source_id); | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 Value* NetLog::Entry::ToValue() const { | |
| 101 DictionaryValue* entry_dict(new DictionaryValue()); | |
| 102 | |
| 103 entry_dict->SetString("time", TickCountToString(base::TimeTicks::Now())); | |
| 104 | |
| 105 // Set the entry source. | |
| 106 DictionaryValue* source_dict = new DictionaryValue(); | |
| 107 source_dict->SetInteger("id", source_.id); | |
| 108 source_dict->SetInteger("type", static_cast<int>(source_.type)); | |
| 109 entry_dict->Set("source", source_dict); | |
| 110 | |
| 111 // Set the event info. | |
| 112 entry_dict->SetInteger("type", static_cast<int>(type_)); | |
| 113 entry_dict->SetInteger("phase", static_cast<int>(phase_)); | |
| 114 | |
| 115 // Set the event-specific parameters. | |
| 116 if (parameters_callback_) { | |
| 117 Value* value = parameters_callback_->Run(log_level_); | |
| 118 if (value) | |
| 119 entry_dict->Set("params", value); | |
| 120 } | |
| 121 | |
| 122 return entry_dict; | |
| 123 } | |
| 124 | |
| 125 Value* NetLog::Entry::ParametersToValue() const { | |
| 126 if (parameters_callback_) | |
| 127 return parameters_callback_->Run(log_level_); | |
| 128 return NULL; | |
| 129 } | |
| 130 | |
| 131 NetLog::Entry::Entry( | |
| 132 EventType type, | |
| 133 Source source, | |
| 134 EventPhase phase, | |
| 135 const ParametersCallback* parameters_callback, | |
| 136 LogLevel log_level) | |
| 137 : type_(type), | |
| 138 source_(source), | |
| 139 phase_(phase), | |
| 140 parameters_callback_(parameters_callback), | |
| 141 log_level_(log_level) { | |
| 142 }; | |
| 143 | |
| 144 NetLog::Entry::~Entry() { | |
| 145 } | |
| 146 | |
| 62 NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC), | 147 NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC), |
| 63 net_log_(NULL) { | 148 net_log_(NULL) { |
| 64 } | 149 } |
| 65 | 150 |
| 66 NetLog::ThreadSafeObserver::~ThreadSafeObserver() { | 151 NetLog::ThreadSafeObserver::~ThreadSafeObserver() { |
| 67 // Make sure we aren't watching a NetLog on destruction. Because the NetLog | 152 // Make sure we aren't watching a NetLog on destruction. Because the NetLog |
| 68 // may pass events to each observer on multiple threads, we cannot safely | 153 // may pass events to each observer on multiple threads, we cannot safely |
| 69 // stop watching a NetLog automatically from a parent class. | 154 // stop watching a NetLog automatically from a parent class. |
| 70 DCHECK(!net_log_); | 155 DCHECK(!net_log_); |
| 71 } | 156 } |
| 72 | 157 |
| 73 NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const { | 158 NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const { |
| 74 DCHECK(net_log_); | 159 DCHECK(net_log_); |
| 75 return log_level_; | 160 return log_level_; |
| 76 } | 161 } |
| 77 | 162 |
| 78 NetLog* NetLog::ThreadSafeObserver::net_log() const { | 163 NetLog* NetLog::ThreadSafeObserver::net_log() const { |
| 79 return net_log_; | 164 return net_log_; |
| 80 } | 165 } |
| 81 | 166 |
| 82 void NetLog::AddGlobalEntry(EventType type, | 167 void NetLog::AddGlobalEntry(EventType type, |
| 83 const scoped_refptr<EventParameters>& params) { | 168 const scoped_refptr<EventParameters>& params) { |
| 169 ParametersCallback callback = base::Bind(&EventParametersCallback, params); | |
| 84 AddEntry(type, | 170 AddEntry(type, |
| 85 Source(net::NetLog::SOURCE_NONE, this->NextID()), | 171 Source(net::NetLog::SOURCE_NONE, NextID()), |
| 86 net::NetLog::PHASE_NONE, | 172 net::NetLog::PHASE_NONE, |
| 87 params); | 173 &callback); |
| 88 } | 174 } |
| 89 | 175 |
| 90 // static | 176 // static |
| 91 std::string NetLog::TickCountToString(const base::TimeTicks& time) { | 177 std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
| 92 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); | 178 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); |
| 93 return base::Int64ToString(delta_time); | 179 return base::Int64ToString(delta_time); |
| 94 } | 180 } |
| 95 | 181 |
| 96 // static | 182 // static |
| 97 const char* NetLog::EventTypeToString(EventType event) { | 183 const char* NetLog::EventTypeToString(EventType event) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 return "PHASE_BEGIN"; | 228 return "PHASE_BEGIN"; |
| 143 case PHASE_END: | 229 case PHASE_END: |
| 144 return "PHASE_END"; | 230 return "PHASE_END"; |
| 145 case PHASE_NONE: | 231 case PHASE_NONE: |
| 146 return "PHASE_NONE"; | 232 return "PHASE_NONE"; |
| 147 } | 233 } |
| 148 NOTREACHED(); | 234 NOTREACHED(); |
| 149 return NULL; | 235 return NULL; |
| 150 } | 236 } |
| 151 | 237 |
| 152 // static | |
| 153 Value* NetLog::EntryToDictionaryValue(NetLog::EventType type, | |
| 154 const base::TimeTicks& time, | |
| 155 const NetLog::Source& source, | |
| 156 NetLog::EventPhase phase, | |
| 157 NetLog::EventParameters* params, | |
| 158 bool use_strings) { | |
| 159 DictionaryValue* entry_dict = new DictionaryValue(); | |
| 160 | |
| 161 entry_dict->SetString("time", TickCountToString(time)); | |
| 162 | |
| 163 // Set the entry source. | |
| 164 DictionaryValue* source_dict = new DictionaryValue(); | |
| 165 source_dict->SetInteger("id", source.id); | |
| 166 if (!use_strings) { | |
| 167 source_dict->SetInteger("type", static_cast<int>(source.type)); | |
| 168 } else { | |
| 169 source_dict->SetString("type", | |
| 170 NetLog::SourceTypeToString(source.type)); | |
| 171 } | |
| 172 entry_dict->Set("source", source_dict); | |
| 173 | |
| 174 // Set the event info. | |
| 175 if (!use_strings) { | |
| 176 entry_dict->SetInteger("type", static_cast<int>(type)); | |
| 177 entry_dict->SetInteger("phase", static_cast<int>(phase)); | |
| 178 } else { | |
| 179 entry_dict->SetString("type", NetLog::EventTypeToString(type)); | |
| 180 entry_dict->SetString("phase", NetLog::EventPhaseToString(phase)); | |
| 181 } | |
| 182 | |
| 183 // Set the event-specific parameters. | |
| 184 if (params) | |
| 185 entry_dict->Set("params", params->ToValue()); | |
| 186 | |
| 187 return entry_dict; | |
| 188 } | |
| 189 | |
| 190 void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) { | 238 void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) { |
| 191 DCHECK(!observer->net_log_); | 239 DCHECK(!observer->net_log_); |
| 192 observer->net_log_ = this; | 240 observer->net_log_ = this; |
| 193 observer->log_level_ = log_level; | 241 observer->log_level_ = log_level; |
| 194 } | 242 } |
| 195 | 243 |
| 196 void NetLog::OnSetObserverLogLevel(ThreadSafeObserver* observer, | 244 void NetLog::OnSetObserverLogLevel(ThreadSafeObserver* observer, |
| 197 LogLevel log_level) { | 245 LogLevel log_level) { |
| 198 DCHECK_EQ(this, observer->net_log_); | 246 DCHECK_EQ(this, observer->net_log_); |
| 199 observer->log_level_ = log_level; | 247 observer->log_level_ = log_level; |
| 200 } | 248 } |
| 201 | 249 |
| 202 void NetLog::OnRemoveObserver(ThreadSafeObserver* observer) { | 250 void NetLog::OnRemoveObserver(ThreadSafeObserver* observer) { |
| 203 DCHECK_EQ(this, observer->net_log_); | 251 DCHECK_EQ(this, observer->net_log_); |
| 204 observer->net_log_ = NULL; | 252 observer->net_log_ = NULL; |
| 205 } | 253 } |
| 206 | 254 |
| 255 void NetLog::AddEntry(EventType type, | |
| 256 const Source& source, | |
| 257 EventPhase phase, | |
| 258 const NetLog::ParametersCallback* parameters_callback) { | |
| 259 Entry entry(type, source, phase, parameters_callback, GetLogLevel()); | |
| 260 OnAddEntry(entry); | |
| 261 } | |
| 262 | |
| 263 void BoundNetLog::AddEntry(NetLog::EventType type, NetLog::EventPhase phase) { | |
| 264 if (!net_log_) | |
| 265 return; | |
| 266 net_log_->AddEntry(type, source_, phase, NULL); | |
| 267 } | |
| 268 | |
| 269 void BoundNetLog::AddEntry(NetLog::EventType type, | |
| 270 NetLog::EventPhase phase, | |
| 271 const NetLog::ParametersCallback& get_parameters) { | |
| 272 if (!net_log_) | |
| 273 return; | |
| 274 net_log_->AddEntry(type, source_, phase, &get_parameters); | |
| 275 } | |
| 276 | |
| 277 void BoundNetLog::AddEvent(NetLog::EventType type) { | |
| 278 AddEntry(type, NetLog::PHASE_NONE); | |
| 279 } | |
| 280 | |
| 281 void BoundNetLog::AddEvent(NetLog::EventType type, | |
| 282 const NetLog::ParametersCallback& get_parameters) { | |
| 283 AddEntry(type, NetLog::PHASE_NONE, get_parameters); | |
| 284 } | |
| 285 | |
| 286 void BoundNetLog::BeginEvent(NetLog::EventType type) { | |
| 287 AddEntry(type, NetLog::PHASE_BEGIN); | |
| 288 } | |
| 289 | |
| 290 void BoundNetLog::BeginEvent(NetLog::EventType type, | |
| 291 const NetLog::ParametersCallback& get_parameters) { | |
| 292 AddEntry(type, NetLog::PHASE_BEGIN, get_parameters); | |
| 293 } | |
| 294 | |
| 295 void BoundNetLog::EndEvent(NetLog::EventType type) { | |
| 296 AddEntry(type, NetLog::PHASE_END); | |
| 297 } | |
| 298 | |
| 299 void BoundNetLog::EndEvent(NetLog::EventType type, | |
| 300 const NetLog::ParametersCallback& get_parameters) { | |
| 301 AddEntry(type, NetLog::PHASE_END, get_parameters); | |
| 302 } | |
| 303 | |
| 207 void BoundNetLog::AddEntry( | 304 void BoundNetLog::AddEntry( |
| 208 NetLog::EventType type, | 305 NetLog::EventType type, |
| 209 NetLog::EventPhase phase, | 306 NetLog::EventPhase phase, |
| 210 const scoped_refptr<NetLog::EventParameters>& params) const { | 307 const scoped_refptr<NetLog::EventParameters>& params) const { |
| 211 if (net_log_) | 308 if (!net_log_) |
| 212 net_log_->AddEntry(type, source_, phase, params); | 309 return; |
| 310 NetLog::ParametersCallback callback = | |
| 311 base::Bind(&EventParametersCallback, params); | |
| 312 net_log_->AddEntry(type, | |
|
eroman
2012/05/22 22:08:06
nit: perhaps this fits onto 1 line.
| |
| 313 source_, | |
| 314 phase, | |
| 315 &callback); | |
| 213 } | 316 } |
| 214 | 317 |
| 215 void BoundNetLog::AddEvent( | 318 void BoundNetLog::AddEvent( |
| 216 NetLog::EventType event_type, | 319 NetLog::EventType event_type, |
| 217 const scoped_refptr<NetLog::EventParameters>& params) const { | 320 const scoped_refptr<NetLog::EventParameters>& params) const { |
| 218 AddEntry(event_type, NetLog::PHASE_NONE, params); | 321 AddEntry(event_type, NetLog::PHASE_NONE, params); |
| 219 } | 322 } |
| 220 | 323 |
| 221 void BoundNetLog::BeginEvent( | 324 void BoundNetLog::BeginEvent( |
| 222 NetLog::EventType event_type, | 325 NetLog::EventType event_type, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 const scoped_refptr<NetLog::EventParameters>& end_event_params) { | 434 const scoped_refptr<NetLog::EventParameters>& end_event_params) { |
| 332 DCHECK(!end_event_params_.get()); | 435 DCHECK(!end_event_params_.get()); |
| 333 end_event_params_ = end_event_params; | 436 end_event_params_ = end_event_params; |
| 334 } | 437 } |
| 335 | 438 |
| 336 const BoundNetLog& ScopedNetLogEvent::net_log() const { | 439 const BoundNetLog& ScopedNetLogEvent::net_log() const { |
| 337 return net_log_; | 440 return net_log_; |
| 338 } | 441 } |
| 339 | 442 |
| 340 } // namespace net | 443 } // namespace net |
| OLD | NEW |