| 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 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::string CapturingNetLog::CapturedEntry::GetParamsJson() const { | 62 std::string CapturingNetLog::CapturedEntry::GetParamsJson() const { |
| 63 if (!params) | 63 if (!params) |
| 64 return std::string(); | 64 return std::string(); |
| 65 std::string json; | 65 std::string json; |
| 66 base::JSONWriter::Write(params.get(), &json); | 66 base::JSONWriter::Write(params.get(), &json); |
| 67 return json; | 67 return json; |
| 68 } | 68 } |
| 69 | 69 |
| 70 CapturingNetLog::CapturingNetLog() | 70 CapturingNetLog::Observer::Observer() {} |
| 71 : last_id_(0), | |
| 72 log_level_(LOG_ALL_BUT_BYTES) { | |
| 73 } | |
| 74 | 71 |
| 75 CapturingNetLog::~CapturingNetLog() {} | 72 CapturingNetLog::Observer::~Observer() {} |
| 76 | 73 |
| 77 void CapturingNetLog::GetEntries(CapturedEntryList* entry_list) const { | 74 void CapturingNetLog::Observer::GetEntries( |
| 75 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 CapturingNetLog::Observer::GetEntriesForSource( |
| 83 CapturedEntryList* entry_list) const { | 81 NetLog::Source source, |
| 82 CapturedEntryList* entry_list) const { |
| 84 base::AutoLock lock(lock_); | 83 base::AutoLock lock(lock_); |
| 85 entry_list->clear(); | 84 entry_list->clear(); |
| 86 for (CapturedEntryList::const_iterator entry = captured_entries_.begin(); | 85 for (CapturedEntryList::const_iterator entry = captured_entries_.begin(); |
| 87 entry != captured_entries_.end(); ++entry) { | 86 entry != captured_entries_.end(); ++entry) { |
| 88 if (entry->source.id == source.id) | 87 if (entry->source.id == source.id) |
| 89 entry_list->push_back(*entry); | 88 entry_list->push_back(*entry); |
| 90 } | 89 } |
| 91 } | 90 } |
| 92 | 91 |
| 93 size_t CapturingNetLog::GetSize() const { | 92 size_t CapturingNetLog::Observer::GetSize() const { |
| 94 base::AutoLock lock(lock_); | 93 base::AutoLock lock(lock_); |
| 95 return captured_entries_.size(); | 94 return captured_entries_.size(); |
| 96 } | 95 } |
| 97 | 96 |
| 98 void CapturingNetLog::Clear() { | 97 void CapturingNetLog::Observer::Clear() { |
| 99 base::AutoLock lock(lock_); | 98 base::AutoLock lock(lock_); |
| 100 captured_entries_.clear(); | 99 captured_entries_.clear(); |
| 101 } | 100 } |
| 102 | 101 |
| 103 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level) { | 102 void CapturingNetLog::Observer::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. | 103 // Only BoundNetLogs without a NetLog should have an invalid source. |
| 110 CHECK(entry.source().IsValid()); | 104 CHECK(entry.source().IsValid()); |
| 111 | 105 |
| 112 // Using Dictionaries instead of Values makes checking values a little | 106 // Using Dictionaries instead of Values makes checking values a little |
| 113 // simpler. | 107 // simpler. |
| 114 DictionaryValue* param_dict = NULL; | 108 DictionaryValue* param_dict = NULL; |
| 115 Value* param_value = entry.ParametersToValue(); | 109 Value* param_value = entry.ParametersToValue(); |
| 116 if (param_value && !param_value->GetAsDictionary(¶m_dict)) | 110 if (param_value && !param_value->GetAsDictionary(¶m_dict)) |
| 117 delete param_value; | 111 delete param_value; |
| 118 | 112 |
| 119 // Only need to acquire the lock when accessing class variables. | 113 // Only need to acquire the lock when accessing class variables. |
| 120 base::AutoLock lock(lock_); | 114 base::AutoLock lock(lock_); |
| 121 captured_entries_.push_back( | 115 captured_entries_.push_back( |
| 122 CapturedEntry(entry.type(), | 116 CapturedEntry(entry.type(), |
| 123 base::TimeTicks::Now(), | 117 base::TimeTicks::Now(), |
| 124 entry.source(), | 118 entry.source(), |
| 125 entry.phase(), | 119 entry.phase(), |
| 126 scoped_ptr<DictionaryValue>(param_dict))); | 120 scoped_ptr<DictionaryValue>(param_dict))); |
| 127 } | 121 } |
| 128 | 122 |
| 129 uint32 CapturingNetLog::NextID() { | 123 CapturingNetLog::CapturingNetLog() { |
| 130 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); | 124 AddThreadSafeObserver(&capturing_net_log_observer_, LOG_ALL_BUT_BYTES); |
| 131 } | 125 } |
| 132 | 126 |
| 133 NetLog::LogLevel CapturingNetLog::GetLogLevel() const { | 127 CapturingNetLog::~CapturingNetLog() { |
| 134 base::AutoLock lock(lock_); | 128 RemoveThreadSafeObserver(&capturing_net_log_observer_); |
| 135 return log_level_; | |
| 136 } | 129 } |
| 137 | 130 |
| 138 void CapturingNetLog::AddThreadSafeObserver( | 131 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level) { |
| 139 NetLog::ThreadSafeObserver* observer, | 132 SetObserverLogLevel(&capturing_net_log_observer_, log_level); |
| 140 NetLog::LogLevel log_level) { | |
| 141 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | |
| 142 } | 133 } |
| 143 | 134 |
| 144 void CapturingNetLog::SetObserverLogLevel(ThreadSafeObserver* observer, | 135 void CapturingNetLog::GetEntries( |
| 145 LogLevel log_level) { | 136 CapturingNetLog::CapturedEntryList* entry_list) const { |
| 146 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | 137 capturing_net_log_observer_.GetEntries(entry_list); |
| 147 } | 138 } |
| 148 | 139 |
| 149 void CapturingNetLog::RemoveThreadSafeObserver( | 140 void CapturingNetLog::GetEntriesForSource( |
| 150 NetLog::ThreadSafeObserver* observer) { | 141 NetLog::Source source, |
| 151 NOTIMPLEMENTED() << "Not currently used by net unit tests."; | 142 CapturedEntryList* entry_list) const { |
| 143 capturing_net_log_observer_.GetEntriesForSource(source, entry_list); |
| 144 } |
| 145 |
| 146 size_t CapturingNetLog::GetSize() const { |
| 147 return capturing_net_log_observer_.GetSize(); |
| 148 } |
| 149 |
| 150 void CapturingNetLog::Clear() { |
| 151 capturing_net_log_observer_.Clear(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 CapturingBoundNetLog::CapturingBoundNetLog() | 154 CapturingBoundNetLog::CapturingBoundNetLog() |
| 155 : net_log_(BoundNetLog::Make(&capturing_net_log_, | 155 : net_log_(BoundNetLog::Make(&capturing_net_log_, |
| 156 net::NetLog::SOURCE_NONE)) { | 156 net::NetLog::SOURCE_NONE)) { |
| 157 } | 157 } |
| 158 | 158 |
| 159 CapturingBoundNetLog::~CapturingBoundNetLog() {} | 159 CapturingBoundNetLog::~CapturingBoundNetLog() {} |
| 160 | 160 |
| 161 void CapturingBoundNetLog::GetEntries( | 161 void CapturingBoundNetLog::GetEntries( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 | 175 |
| 176 void CapturingBoundNetLog::Clear() { | 176 void CapturingBoundNetLog::Clear() { |
| 177 capturing_net_log_.Clear(); | 177 capturing_net_log_.Clear(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level) { | 180 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level) { |
| 181 capturing_net_log_.SetLogLevel(log_level); | 181 capturing_net_log_.SetLogLevel(log_level); |
| 182 } | 182 } |
| 183 | 183 |
| 184 } // namespace net | 184 } // namespace net |
| OLD | NEW |