| 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/capturing_net_log.h" | 5 #include "net/base/capturing_net_log.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 CapturingNetLog::CapturedEntry::CapturedEntry( | 13 CapturingNetLogObserver::CapturedEntry::CapturedEntry( |
| 14 EventType type, | 14 NetLog::EventType type, |
| 15 const base::TimeTicks& time, | 15 const base::TimeTicks& time, |
| 16 Source source, | 16 NetLog::Source source, |
| 17 EventPhase phase, | 17 NetLog::EventPhase phase, |
| 18 scoped_ptr<DictionaryValue> params) | 18 scoped_ptr<DictionaryValue> params) |
| 19 : type(type), | 19 : type(type), |
| 20 time(time), | 20 time(time), |
| 21 source(source), | 21 source(source), |
| 22 phase(phase), | 22 phase(phase), |
| 23 params(params.Pass()) { | 23 params(params.Pass()) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 CapturingNetLog::CapturedEntry::CapturedEntry(const CapturedEntry& entry) { | 26 CapturingNetLogObserver::CapturedEntry::CapturedEntry( |
| 27 const CapturedEntry& entry) { |
| 27 *this = entry; | 28 *this = entry; |
| 28 } | 29 } |
| 29 | 30 |
| 30 CapturingNetLog::CapturedEntry::~CapturedEntry() {} | 31 CapturingNetLogObserver::CapturedEntry::~CapturedEntry() {} |
| 31 | 32 |
| 32 CapturingNetLog::CapturedEntry& | 33 CapturingNetLogObserver::CapturedEntry& |
| 33 CapturingNetLog::CapturedEntry::operator=(const CapturedEntry& entry) { | 34 CapturingNetLogObserver::CapturedEntry::operator=(const CapturedEntry& entry) { |
| 34 type = entry.type; | 35 type = entry.type; |
| 35 time = entry.time; | 36 time = entry.time; |
| 36 source = entry.source; | 37 source = entry.source; |
| 37 phase = entry.phase; | 38 phase = entry.phase; |
| 38 params.reset(entry.params ? entry.params->DeepCopy() : NULL); | 39 params.reset(entry.params ? entry.params->DeepCopy() : NULL); |
| 39 return *this; | 40 return *this; |
| 40 } | 41 } |
| 41 | 42 |
| 42 bool CapturingNetLog::CapturedEntry::GetStringValue( | 43 bool CapturingNetLogObserver::CapturedEntry::GetStringValue( |
| 43 const std::string& name, | 44 const std::string& name, |
| 44 std::string* value) const { | 45 std::string* value) const { |
| 45 if (!params) | 46 if (!params) |
| 46 return false; | 47 return false; |
| 47 return params->GetString(name, value); | 48 return params->GetString(name, value); |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool CapturingNetLog::CapturedEntry::GetIntegerValue( | 51 bool CapturingNetLogObserver::CapturedEntry::GetIntegerValue( |
| 51 const std::string& name, | 52 const std::string& name, |
| 52 int* value) const { | 53 int* value) const { |
| 53 if (!params) | 54 if (!params) |
| 54 return false; | 55 return false; |
| 55 return params->GetInteger(name, value); | 56 return params->GetInteger(name, value); |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool CapturingNetLog::CapturedEntry::GetNetErrorCode(int* value) const { | 59 bool CapturingNetLogObserver::CapturedEntry::GetNetErrorCode(int* value) const { |
| 59 return GetIntegerValue("net_error", value); | 60 return GetIntegerValue("net_error", value); |
| 60 } | 61 } |
| 61 | 62 |
| 62 std::string CapturingNetLog::CapturedEntry::GetParamsJson() const { | 63 std::string CapturingNetLogObserver::CapturedEntry::GetParamsJson() const { |
| 63 if (!params) | 64 if (!params) |
| 64 return std::string(); | 65 return std::string(); |
| 65 std::string json; | 66 std::string json; |
| 66 base::JSONWriter::Write(params.get(), &json); | 67 base::JSONWriter::Write(params.get(), &json); |
| 67 return json; | 68 return json; |
| 68 } | 69 } |
| 69 | 70 |
| 70 CapturingNetLog::CapturingNetLog() | 71 CapturingNetLogObserver::CapturingNetLogObserver() {} |
| 71 : last_id_(0), | |
| 72 log_level_(LOG_ALL_BUT_BYTES) { | |
| 73 } | |
| 74 | 72 |
| 75 CapturingNetLog::~CapturingNetLog() {} | 73 CapturingNetLogObserver::~CapturingNetLogObserver() {} |
| 76 | 74 |
| 77 void CapturingNetLog::GetEntries(CapturedEntryList* entry_list) const { | 75 void CapturingNetLogObserver::GetEntries(CapturedEntryList* entry_list) const { |
| 78 base::AutoLock lock(lock_); | 76 base::AutoLock lock(lock_); |
| 79 *entry_list = captured_entries_; | 77 *entry_list = captured_entries_; |
| 80 } | 78 } |
| 81 | 79 |
| 82 void CapturingNetLog::GetEntriesForSource(NetLog::Source source, | 80 void CapturingNetLogObserver::GetEntriesForSource(NetLog::Source source, |
| 83 CapturedEntryList* entry_list) const { | 81 CapturedEntryList* entry_list) const { |
| 84 base::AutoLock lock(lock_); | 82 base::AutoLock lock(lock_); |
| 85 entry_list->clear(); | 83 entry_list->clear(); |
| 86 for (CapturedEntryList::const_iterator entry = captured_entries_.begin(); | 84 for (CapturedEntryList::const_iterator entry = captured_entries_.begin(); |
| 87 entry != captured_entries_.end(); ++entry) { | 85 entry != captured_entries_.end(); ++entry) { |
| 88 if (entry->source.id == source.id) | 86 if (entry->source.id == source.id) |
| 89 entry_list->push_back(*entry); | 87 entry_list->push_back(*entry); |
| 90 } | 88 } |
| 91 } | 89 } |
| 92 | 90 |
| 93 size_t CapturingNetLog::GetSize() const { | 91 size_t CapturingNetLogObserver::GetSize() const { |
| 94 base::AutoLock lock(lock_); | 92 base::AutoLock lock(lock_); |
| 95 return captured_entries_.size(); | 93 return captured_entries_.size(); |
| 96 } | 94 } |
| 97 | 95 |
| 98 void CapturingNetLog::Clear() { | 96 void CapturingNetLogObserver::Clear() { |
| 99 base::AutoLock lock(lock_); | 97 base::AutoLock lock(lock_); |
| 100 captured_entries_.clear(); | 98 captured_entries_.clear(); |
| 101 } | 99 } |
| 102 | 100 |
| 103 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level) { | 101 void CapturingNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
| 104 base::AutoLock lock(lock_); | |
| 105 log_level_ = log_level; | |
| 106 } | |
| 107 | |
| 108 void CapturingNetLog::OnAddEntry(const net::NetLog::Entry& entry) { | |
| 109 // Only BoundNetLogs without a NetLog should have an invalid source. | 102 // Only BoundNetLogs without a NetLog should have an invalid source. |
| 110 CHECK(entry.source().IsValid()); | 103 CHECK(entry.source().IsValid()); |
| 111 | 104 |
| 112 // Using Dictionaries instead of Values makes checking values a little | 105 // Using Dictionaries instead of Values makes checking values a little |
| 113 // simpler. | 106 // simpler. |
| 114 DictionaryValue* param_dict = NULL; | 107 DictionaryValue* param_dict = NULL; |
| 115 Value* param_value = entry.ParametersToValue(); | 108 Value* param_value = entry.ParametersToValue(); |
| 116 if (param_value && !param_value->GetAsDictionary(¶m_dict)) | 109 if (param_value && !param_value->GetAsDictionary(¶m_dict)) |
| 117 delete param_value; | 110 delete param_value; |
| 118 | 111 |
| 119 // Only need to acquire the lock when accessing class variables. | 112 // Only need to acquire the lock when accessing class variables. |
| 120 base::AutoLock lock(lock_); | 113 base::AutoLock lock(lock_); |
| 121 captured_entries_.push_back( | 114 captured_entries_.push_back( |
| 122 CapturedEntry(entry.type(), | 115 CapturedEntry(entry.type(), |
| 123 base::TimeTicks::Now(), | 116 base::TimeTicks::Now(), |
| 124 entry.source(), | 117 entry.source(), |
| 125 entry.phase(), | 118 entry.phase(), |
| 126 scoped_ptr<DictionaryValue>(param_dict))); | 119 scoped_ptr<DictionaryValue>(param_dict))); |
| 127 } | 120 } |
| 128 | 121 |
| 129 uint32 CapturingNetLog::NextID() { | 122 CapturingNetLog::CapturingNetLog() { |
| 130 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); | 123 AddThreadSafeObserver(&capturing_net_log_observer_, LOG_ALL_BUT_BYTES); |
| 131 } | 124 } |
| 132 | 125 |
| 133 NetLog::LogLevel CapturingNetLog::GetLogLevel() const { | 126 CapturingNetLog::~CapturingNetLog() { |
| 134 base::AutoLock lock(lock_); | 127 RemoveThreadSafeObserver(&capturing_net_log_observer_); |
| 135 return log_level_; | |
| 136 } | 128 } |
| 137 | 129 |
| 138 void CapturingNetLog::AddThreadSafeObserver( | 130 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level) { |
| 139 NetLog::ThreadSafeObserver* observer, | 131 SetObserverLogLevel(&capturing_net_log_observer_, log_level); |
| 140 NetLog::LogLevel log_level) { | |
| 141 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | |
| 142 } | 132 } |
| 143 | 133 |
| 144 void CapturingNetLog::SetObserverLogLevel(ThreadSafeObserver* observer, | 134 void CapturingNetLog::GetEntries( |
| 145 LogLevel log_level) { | 135 CapturingNetLogObserver::CapturedEntryList* entry_list) const { |
| 146 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | 136 capturing_net_log_observer_.GetEntries(entry_list); |
| 147 } | 137 } |
| 148 | 138 |
| 149 void CapturingNetLog::RemoveThreadSafeObserver( | 139 size_t CapturingNetLog::GetSize() const { |
| 150 NetLog::ThreadSafeObserver* observer) { | 140 return capturing_net_log_observer_.GetSize(); |
| 151 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | |
| 152 } | 141 } |
| 153 | 142 |
| 154 CapturingBoundNetLog::CapturingBoundNetLog() | 143 CapturingBoundNetLog::CapturingBoundNetLog() |
| 155 : net_log_(BoundNetLog::Make(&capturing_net_log_, | 144 : bound_net_log_(BoundNetLog::Make(&net_log_, |
| 156 net::NetLog::SOURCE_NONE)) { | 145 net::NetLog::SOURCE_NONE)) { |
| 157 } | 146 } |
| 158 | 147 |
| 159 CapturingBoundNetLog::~CapturingBoundNetLog() {} | 148 CapturingBoundNetLog::~CapturingBoundNetLog() {} |
| 160 | 149 |
| 161 void CapturingBoundNetLog::GetEntries( | 150 void CapturingBoundNetLog::GetEntries( |
| 162 CapturingNetLog::CapturedEntryList* entry_list) const { | 151 CapturingNetLogObserver::CapturedEntryList* entry_list) const { |
| 163 capturing_net_log_.GetEntries(entry_list); | 152 net_log_.observer().GetEntries(entry_list); |
| 164 } | 153 } |
| 165 | 154 |
| 166 void CapturingBoundNetLog::GetEntriesForSource( | 155 void CapturingBoundNetLog::GetEntriesForSource( |
| 167 NetLog::Source source, | 156 NetLog::Source source, |
| 168 CapturingNetLog::CapturedEntryList* entry_list) const { | 157 CapturingNetLogObserver::CapturedEntryList* entry_list) const { |
| 169 capturing_net_log_.GetEntriesForSource(source, entry_list); | 158 net_log_.observer().GetEntriesForSource(source, entry_list); |
| 170 } | 159 } |
| 171 | 160 |
| 172 size_t CapturingBoundNetLog::GetSize() const { | 161 size_t CapturingBoundNetLog::GetSize() const { |
| 173 return capturing_net_log_.GetSize(); | 162 return net_log_.observer().GetSize(); |
| 174 } | 163 } |
| 175 | 164 |
| 176 void CapturingBoundNetLog::Clear() { | 165 void CapturingBoundNetLog::Clear() { |
| 177 capturing_net_log_.Clear(); | 166 net_log_.observer().Clear(); |
| 178 } | 167 } |
| 179 | 168 |
| 180 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level) { | 169 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level) { |
| 181 capturing_net_log_.SetLogLevel(log_level); | 170 net_log_.SetLogLevel(log_level); |
| 182 } | 171 } |
| 183 | 172 |
| 184 } // namespace net | 173 } // namespace net |
| OLD | NEW |