Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / base / capturing_net_log.h
blob06bc976eeaac03f8185618226e6338729b46934f
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.
5 #ifndef NET_BASE_CAPTURING_NET_LOG_H_
6 #define NET_BASE_CAPTURING_NET_LOG_H_
8 #include <string>
9 #include <vector>
11 #include "base/atomicops.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "base/time/time.h"
18 #include "net/base/net_log.h"
20 namespace base {
21 class DictionaryValue;
22 class ListValue;
25 namespace net {
27 // CapturingNetLog is a NetLog which instantiates Observer that saves messages
28 // to a bounded buffer. It is intended for testing only, and is part of the
29 // net_test_support project. This is provided for convinience and compatilbility
30 // with the old unittests.
31 class CapturingNetLog : public NetLog {
32 public:
33 struct CapturedEntry {
34 CapturedEntry(EventType type,
35 const base::TimeTicks& time,
36 Source source,
37 EventPhase phase,
38 scoped_ptr<base::DictionaryValue> params);
39 // Copy constructor needed to store in a std::vector because of the
40 // scoped_ptr.
41 CapturedEntry(const CapturedEntry& entry);
43 ~CapturedEntry();
45 // Equality operator needed to store in a std::vector because of the
46 // scoped_ptr.
47 CapturedEntry& operator=(const CapturedEntry& entry);
49 // Attempt to retrieve an value of the specified type with the given name
50 // from |params|. Returns true on success, false on failure. Does not
51 // modify |value| on failure.
52 bool GetStringValue(const std::string& name, std::string* value) const;
53 bool GetIntegerValue(const std::string& name, int* value) const;
54 bool GetListValue(const std::string& name, base::ListValue** value) const;
56 // Same as GetIntegerValue, but returns the error code associated with a
57 // log entry.
58 bool GetNetErrorCode(int* value) const;
60 // Returns the parameters as a JSON string, or empty string if there are no
61 // parameters.
62 std::string GetParamsJson() const;
64 EventType type;
65 base::TimeTicks time;
66 Source source;
67 EventPhase phase;
68 scoped_ptr<base::DictionaryValue> params;
71 // Ordered set of entries that were logged.
72 typedef std::vector<CapturedEntry> CapturedEntryList;
74 CapturingNetLog();
75 virtual ~CapturingNetLog();
77 void SetLogLevel(LogLevel log_level);
79 // Below methods are forwarded to capturing_net_log_observer_.
80 void GetEntries(CapturedEntryList* entry_list) const;
81 void GetEntriesForSource(Source source, CapturedEntryList* entry_list) const;
82 size_t GetSize() const;
83 void Clear();
85 private:
86 // Observer is an implementation of NetLog::ThreadSafeObserver
87 // that saves messages to a bounded buffer. It is intended for testing only,
88 // and is part of the net_test_support project.
89 class Observer : public NetLog::ThreadSafeObserver {
90 public:
91 Observer();
92 virtual ~Observer();
94 // Returns the list of all entries in the log.
95 void GetEntries(CapturedEntryList* entry_list) const;
97 // Fills |entry_list| with all entries in the log from the specified Source.
98 void GetEntriesForSource(Source source,
99 CapturedEntryList* entry_list) const;
101 // Returns number of entries in the log.
102 size_t GetSize() const;
104 void Clear();
106 private:
107 // ThreadSafeObserver implementation:
108 virtual void OnAddEntry(const Entry& entry) OVERRIDE;
110 // Needs to be "mutable" so can use it in GetEntries().
111 mutable base::Lock lock_;
113 CapturedEntryList captured_entries_;
115 DISALLOW_COPY_AND_ASSIGN(Observer);
118 Observer capturing_net_log_observer_;
120 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog);
123 // Helper class that exposes a similar API as BoundNetLog, but uses a
124 // CapturingNetLog rather than the more generic NetLog.
126 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the
127 // bound() method.
128 class CapturingBoundNetLog {
129 public:
130 CapturingBoundNetLog();
131 ~CapturingBoundNetLog();
133 // The returned BoundNetLog is only valid while |this| is alive.
134 BoundNetLog bound() const { return net_log_; }
136 // Fills |entry_list| with all entries in the log.
137 void GetEntries(CapturingNetLog::CapturedEntryList* entry_list) const;
139 // Fills |entry_list| with all entries in the log from the specified Source.
140 void GetEntriesForSource(
141 NetLog::Source source,
142 CapturingNetLog::CapturedEntryList* entry_list) const;
144 // Returns number of entries in the log.
145 size_t GetSize() const;
147 void Clear();
149 // Sets the log level of the underlying CapturingNetLog.
150 void SetLogLevel(NetLog::LogLevel log_level);
152 private:
153 CapturingNetLog capturing_net_log_;
154 const BoundNetLog net_log_;
156 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog);
159 } // namespace net
161 #endif // NET_BASE_CAPTURING_NET_LOG_H_