| 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 #ifndef NET_BASE_CAPTURING_NET_LOG_H_ | 5 #ifndef NET_BASE_CAPTURING_NET_LOG_H_ |
| 6 #define NET_BASE_CAPTURING_NET_LOG_H_ | 6 #define NET_BASE_CAPTURING_NET_LOG_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class DictionaryValue; | 21 class DictionaryValue; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 | 25 |
| 26 // CapturingNetLog is an implementation of NetLog that saves messages to a | 26 // CapturingNetLogObserver is an implementation of NetLog::ThreadSafeObserver |
| 27 // bounded buffer. It is intended for testing only, and is part of the | 27 // that saves messages to a bounded buffer. It is intended for testing only, |
| 28 // net_test_support project. | 28 // and is part of the net_test_support project. |
| 29 class CapturingNetLog : public NetLog { | 29 class CapturingNetLogObserver : public NetLog::ThreadSafeObserver { |
| 30 public: | 30 public: |
| 31 struct CapturedEntry { | 31 struct CapturedEntry { |
| 32 CapturedEntry(EventType type, | 32 CapturedEntry(NetLog::EventType type, |
| 33 const base::TimeTicks& time, | 33 const base::TimeTicks& time, |
| 34 Source source, | 34 NetLog::Source source, |
| 35 EventPhase phase, | 35 NetLog::EventPhase phase, |
| 36 scoped_ptr<base::DictionaryValue> params); | 36 scoped_ptr<base::DictionaryValue> params); |
| 37 // Copy constructor needed to store in a std::vector because of the | 37 // Copy constructor needed to store in a std::vector because of the |
| 38 // scoped_ptr. | 38 // scoped_ptr. |
| 39 CapturedEntry(const CapturedEntry& entry); | 39 CapturedEntry(const CapturedEntry& entry); |
| 40 | 40 |
| 41 ~CapturedEntry(); | 41 ~CapturedEntry(); |
| 42 | 42 |
| 43 // Equality operator needed to store in a std::vector because of the | 43 // Equality operator needed to store in a std::vector because of the |
| 44 // scoped_ptr. | 44 // scoped_ptr. |
| 45 CapturedEntry& operator=(const CapturedEntry& entry); | 45 CapturedEntry& operator=(const CapturedEntry& entry); |
| 46 | 46 |
| 47 // Attempt to retrieve an value of the specified type with the given name | 47 // Attempt to retrieve an value of the specified type with the given name |
| 48 // from |params|. Returns true on success, false on failure. Does not | 48 // from |params|. Returns true on success, false on failure. Does not |
| 49 // modify |value| on failure. | 49 // modify |value| on failure. |
| 50 bool GetStringValue(const std::string& name, std::string* value) const; | 50 bool GetStringValue(const std::string& name, std::string* value) const; |
| 51 bool GetIntegerValue(const std::string& name, int* value) const; | 51 bool GetIntegerValue(const std::string& name, int* value) const; |
| 52 | 52 |
| 53 // Same as GetIntegerValue, but returns the error code associated with a | 53 // Same as GetIntegerValue, but returns the error code associated with a |
| 54 // log entry. | 54 // log entry. |
| 55 bool GetNetErrorCode(int* value) const; | 55 bool GetNetErrorCode(int* value) const; |
| 56 | 56 |
| 57 // Returns the parameters as a JSON string, or empty string if there are no | 57 // Returns the parameters as a JSON string, or empty string if there are no |
| 58 // parameters. | 58 // parameters. |
| 59 std::string GetParamsJson() const; | 59 std::string GetParamsJson() const; |
| 60 | 60 |
| 61 EventType type; | 61 NetLog::EventType type; |
| 62 base::TimeTicks time; | 62 base::TimeTicks time; |
| 63 Source source; | 63 NetLog::Source source; |
| 64 EventPhase phase; | 64 NetLog::EventPhase phase; |
| 65 scoped_ptr<base::DictionaryValue> params; | 65 scoped_ptr<base::DictionaryValue> params; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Ordered set of entries that were logged. | 68 // Ordered set of entries that were logged. |
| 69 typedef std::vector<CapturedEntry> CapturedEntryList; | 69 typedef std::vector<CapturedEntry> CapturedEntryList; |
| 70 | 70 |
| 71 CapturingNetLog(); | 71 CapturingNetLogObserver(); |
| 72 virtual ~CapturingNetLog(); | 72 virtual ~CapturingNetLogObserver(); |
| 73 | 73 |
| 74 // Returns the list of all entries in the log. | 74 // Returns the list of all entries in the log. |
| 75 void GetEntries(CapturedEntryList* entry_list) const; | 75 void GetEntries(CapturedEntryList* entry_list) const; |
| 76 | 76 |
| 77 // Fills |entry_list| with all entries in the log from the specified Source. | 77 // Fills |entry_list| with all entries in the log from the specified Source. |
| 78 void GetEntriesForSource(NetLog::Source source, | 78 void GetEntriesForSource(NetLog::Source source, |
| 79 CapturedEntryList* entry_list) const; | 79 CapturedEntryList* entry_list) const; |
| 80 | 80 |
| 81 // Returns number of entries in the log. | 81 // Returns number of entries in the log. |
| 82 size_t GetSize() const; | 82 size_t GetSize() const; |
| 83 | 83 |
| 84 void Clear(); | 84 void Clear(); |
| 85 | 85 |
| 86 void SetLogLevel(NetLog::LogLevel log_level); | 86 private: |
| 87 // NetLog::ThreadSafeObserver implementation: |
| 88 virtual void OnAddEntry(const NetLog::Entry& entry) OVERRIDE; |
| 87 | 89 |
| 88 // NetLog implementation: | |
| 89 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; | |
| 90 virtual uint32 NextID() OVERRIDE; | |
| 91 virtual LogLevel GetLogLevel() const OVERRIDE; | |
| 92 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, | |
| 93 LogLevel log_level) OVERRIDE; | |
| 94 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, | |
| 95 LogLevel log_level) OVERRIDE; | |
| 96 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; | |
| 97 | |
| 98 private: | |
| 99 // Needs to be "mutable" so can use it in GetEntries(). | 90 // Needs to be "mutable" so can use it in GetEntries(). |
| 100 mutable base::Lock lock_; | 91 mutable base::Lock lock_; |
| 101 | 92 |
| 102 // Last assigned source ID. Incremented to get the next one. | |
| 103 base::subtle::Atomic32 last_id_; | |
| 104 | |
| 105 CapturedEntryList captured_entries_; | 93 CapturedEntryList captured_entries_; |
| 106 | 94 |
| 107 NetLog::LogLevel log_level_; | 95 DISALLOW_COPY_AND_ASSIGN(CapturingNetLogObserver); |
| 96 }; |
| 108 | 97 |
| 109 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); | 98 // CapturingNetLog is a NetLog which instantiates CapturingNetLogObserver. |
| 99 // This is provided for compatilbility with old unittests. |
| 100 class CapturingNetLog : public NetLog { |
| 101 public: |
| 102 typedef CapturingNetLogObserver::CapturedEntry CapturedEntry; |
| 103 typedef CapturingNetLogObserver::CapturedEntryList CapturedEntryList; |
| 104 |
| 105 CapturingNetLog(); |
| 106 virtual ~CapturingNetLog(); |
| 107 |
| 108 void SetLogLevel(NetLog::LogLevel log_level); |
| 109 |
| 110 CapturingNetLogObserver& observer() { return capturing_net_log_observer_; } |
| 111 const CapturingNetLogObserver& observer() const { |
| 112 return capturing_net_log_observer_; |
| 113 } |
| 114 |
| 115 // below methods are forwarded to capturing_net_log_observer_ |
| 116 void GetEntries(CapturingNetLogObserver::CapturedEntryList* entry_list) const; |
| 117 size_t GetSize() const; |
| 118 |
| 119 private: |
| 120 CapturingNetLogObserver capturing_net_log_observer_; |
| 110 }; | 121 }; |
| 111 | 122 |
| 112 // Helper class that exposes a similar API as BoundNetLog, but uses a | 123 // Helper class that exposes a similar API as BoundNetLog, but uses a |
| 113 // CapturingNetLog rather than the more generic NetLog. | 124 // CapturingNetLogObserver rather than the more generic NetLog. |
| 114 // | 125 // |
| 115 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the | 126 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the |
| 116 // bound() method. | 127 // bound() method. |
| 117 class CapturingBoundNetLog { | 128 class CapturingBoundNetLog { |
| 118 public: | 129 public: |
| 119 CapturingBoundNetLog(); | 130 CapturingBoundNetLog(); |
| 120 ~CapturingBoundNetLog(); | 131 ~CapturingBoundNetLog(); |
| 121 | 132 |
| 122 // The returned BoundNetLog is only valid while |this| is alive. | 133 // The returned BoundNetLog is only valid while |this| is alive. |
| 123 BoundNetLog bound() const { return net_log_; } | 134 BoundNetLog bound() const { return bound_net_log_; } |
| 124 | 135 |
| 125 // Fills |entry_list| with all entries in the log. | 136 // Fills |entry_list| with all entries in the log. |
| 126 void GetEntries(CapturingNetLog::CapturedEntryList* entry_list) const; | 137 void GetEntries(CapturingNetLogObserver::CapturedEntryList* entry_list) const; |
| 127 | 138 |
| 128 // Fills |entry_list| with all entries in the log from the specified Source. | 139 // Fills |entry_list| with all entries in the log from the specified Source. |
| 129 void GetEntriesForSource( | 140 void GetEntriesForSource( |
| 130 NetLog::Source source, | 141 NetLog::Source source, |
| 131 CapturingNetLog::CapturedEntryList* entry_list) const; | 142 CapturingNetLogObserver::CapturedEntryList* entry_list) const; |
| 132 | 143 |
| 133 // Returns number of entries in the log. | 144 // Returns number of entries in the log. |
| 134 size_t GetSize() const; | 145 size_t GetSize() const; |
| 135 | 146 |
| 136 void Clear(); | 147 void Clear(); |
| 137 | 148 |
| 138 // Sets the log level of the underlying CapturingNetLog. | 149 // Sets the log level of the underlying CapturingNetLogObserver. |
| 139 void SetLogLevel(NetLog::LogLevel log_level); | 150 void SetLogLevel(NetLog::LogLevel log_level); |
| 140 | 151 |
| 141 private: | 152 private: |
| 142 CapturingNetLog capturing_net_log_; | 153 CapturingNetLog net_log_; |
| 143 const BoundNetLog net_log_; | 154 const BoundNetLog bound_net_log_; |
| 144 | 155 |
| 145 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | 156 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); |
| 146 }; | 157 }; |
| 147 | 158 |
| 148 } // namespace net | 159 } // namespace net |
| 149 | 160 |
| 150 #endif // NET_BASE_CAPTURING_NET_LOG_H_ | 161 #endif // NET_BASE_CAPTURING_NET_LOG_H_ |
| OLD | NEW |