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