vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 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/log/test_net_log.h" |
| 6 | |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 7 | #include "base/synchronization/lock.h" |
| 8 | #include "base/values.h" |
| 9 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10 | namespace net { |
| 11 | |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 12 | // TestNetLog::Observer is an implementation of NetLog::ThreadSafeObserver |
| 13 | // that saves messages to a buffer. |
| 14 | class TestNetLog::Observer : public NetLog::ThreadSafeObserver { |
| 15 | public: |
| 16 | Observer() {} |
| 17 | ~Observer() override {} |
| 18 | |
| 19 | // Returns the list of all entries in the log. |
| 20 | void GetEntries(TestNetLogEntry::List* entry_list) const { |
| 21 | base::AutoLock lock(lock_); |
| 22 | *entry_list = entry_list_; |
| 23 | } |
| 24 | |
| 25 | // Fills |entry_list| with all entries in the log from the specified Source. |
| 26 | void GetEntriesForSource(NetLog::Source source, |
| 27 | TestNetLogEntry::List* entry_list) const { |
| 28 | base::AutoLock lock(lock_); |
| 29 | entry_list->clear(); |
| 30 | for (const auto& entry : entry_list_) { |
| 31 | if (entry.source.id == source.id) |
| 32 | entry_list->push_back(entry); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Returns the number of entries in the log. |
| 37 | size_t GetSize() const { |
| 38 | base::AutoLock lock(lock_); |
| 39 | return entry_list_.size(); |
| 40 | } |
| 41 | |
| 42 | void Clear() { |
| 43 | base::AutoLock lock(lock_); |
| 44 | entry_list_.clear(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | // ThreadSafeObserver implementation: |
| 49 | void OnAddEntry(const NetLog::Entry& entry) override { |
| 50 | // Using Dictionaries instead of Values makes checking values a little |
| 51 | // simpler. |
| 52 | base::DictionaryValue* param_dict = nullptr; |
| 53 | base::Value* param_value = entry.ParametersToValue(); |
| 54 | if (param_value && !param_value->GetAsDictionary(¶m_dict)) |
| 55 | delete param_value; |
| 56 | |
| 57 | // Only need to acquire the lock when accessing class variables. |
| 58 | base::AutoLock lock(lock_); |
| 59 | entry_list_.push_back(TestNetLogEntry( |
| 60 | entry.type(), base::TimeTicks::Now(), entry.source(), entry.phase(), |
| 61 | scoped_ptr<base::DictionaryValue>(param_dict))); |
| 62 | } |
| 63 | |
| 64 | // Needs to be "mutable" to use it in GetEntries(). |
| 65 | mutable base::Lock lock_; |
| 66 | |
| 67 | TestNetLogEntry::List entry_list_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(Observer); |
| 70 | }; |
| 71 | |
| 72 | TestNetLog::TestNetLog() : observer_(new Observer()) { |
| 73 | DeprecatedAddObserver(observer_.get(), |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 74 | NetLogCaptureMode::IncludeCookiesAndCredentials()); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TestNetLog::~TestNetLog() { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 78 | DeprecatedRemoveObserver(observer_.get()); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 79 | } |
| 80 | |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 81 | void TestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 82 | SetObserverCaptureMode(observer_.get(), capture_mode); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 83 | } |
| 84 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 85 | void TestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 86 | observer_->GetEntries(entry_list); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 87 | } |
| 88 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 89 | void TestNetLog::GetEntriesForSource(NetLog::Source source, |
| 90 | TestNetLogEntry::List* entry_list) const { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 91 | observer_->GetEntriesForSource(source, entry_list); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | size_t TestNetLog::GetSize() const { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 95 | return observer_->GetSize(); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void TestNetLog::Clear() { |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame^] | 99 | observer_->Clear(); |
| 100 | } |
| 101 | |
| 102 | NetLog::ThreadSafeObserver* TestNetLog::GetObserver() const { |
| 103 | return observer_.get(); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | BoundTestNetLog::BoundTestNetLog() |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 107 | : net_log_(BoundNetLog::Make(&test_net_log_, NetLog::SOURCE_NONE)) { |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | BoundTestNetLog::~BoundTestNetLog() { |
| 111 | } |
| 112 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 113 | void BoundTestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const { |
| 114 | test_net_log_.GetEntries(entry_list); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void BoundTestNetLog::GetEntriesForSource( |
| 118 | NetLog::Source source, |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 119 | TestNetLogEntry::List* entry_list) const { |
| 120 | test_net_log_.GetEntriesForSource(source, entry_list); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | size_t BoundTestNetLog::GetSize() const { |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 124 | return test_net_log_.GetSize(); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void BoundTestNetLog::Clear() { |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 128 | test_net_log_.Clear(); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 129 | } |
| 130 | |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 131 | void BoundTestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) { |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 132 | test_net_log_.SetCaptureMode(capture_mode); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace net |