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_
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"
21 class DictionaryValue
;
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
{
33 struct CapturedEntry
{
34 CapturedEntry(EventType type
,
35 const base::TimeTicks
& time
,
38 scoped_ptr
<base::DictionaryValue
> params
);
39 // Copy constructor needed to store in a std::vector because of the
41 CapturedEntry(const CapturedEntry
& entry
);
45 // Equality operator needed to store in a std::vector because of the
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
58 bool GetNetErrorCode(int* value
) const;
60 // Returns the parameters as a JSON string, or empty string if there are no
62 std::string
GetParamsJson() const;
68 scoped_ptr
<base::DictionaryValue
> params
;
71 // Ordered set of entries that were logged.
72 typedef std::vector
<CapturedEntry
> CapturedEntryList
;
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;
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
{
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;
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
128 class CapturingBoundNetLog
{
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;
149 // Sets the log level of the underlying CapturingNetLog.
150 void SetLogLevel(NetLog::LogLevel log_level
);
153 CapturingNetLog capturing_net_log_
;
154 const BoundNetLog net_log_
;
156 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog
);
161 #endif // NET_BASE_CAPTURING_NET_LOG_H_