Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / base / capturing_net_log.h
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 #ifndef NET_BASE_CAPTURING_NET_LOG_H_
6 #define NET_BASE_CAPTURING_NET_LOG_H_
7
8 #include <string>
9 #include <vector>
10
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"
19
20 namespace base {
21 class DictionaryValue;
22 class ListValue;
23 }
24
25 namespace net {
26
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);
42
43     ~CapturedEntry();
44
45     // Equality operator needed to store in a std::vector because of the
46     // scoped_ptr.
47     CapturedEntry& operator=(const CapturedEntry& entry);
48
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;
55
56     // Same as GetIntegerValue, but returns the error code associated with a
57     // log entry.
58     bool GetNetErrorCode(int* value) const;
59
60     // Returns the parameters as a JSON string, or empty string if there are no
61     // parameters.
62     std::string GetParamsJson() const;
63
64     EventType type;
65     base::TimeTicks time;
66     Source source;
67     EventPhase phase;
68     scoped_ptr<base::DictionaryValue> params;
69   };
70
71   // Ordered set of entries that were logged.
72   typedef std::vector<CapturedEntry> CapturedEntryList;
73
74   CapturingNetLog();
75   ~CapturingNetLog() override;
76
77   void SetLogLevel(LogLevel log_level);
78
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();
84
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     ~Observer() override;
93
94     // Returns the list of all entries in the log.
95     void GetEntries(CapturedEntryList* entry_list) const;
96
97     // Fills |entry_list| with all entries in the log from the specified Source.
98     void GetEntriesForSource(Source source,
99                              CapturedEntryList* entry_list) const;
100
101     // Returns number of entries in the log.
102     size_t GetSize() const;
103
104     void Clear();
105
106    private:
107     // ThreadSafeObserver implementation:
108     void OnAddEntry(const Entry& entry) override;
109
110     // Needs to be "mutable" so can use it in GetEntries().
111     mutable base::Lock lock_;
112
113     CapturedEntryList captured_entries_;
114
115     DISALLOW_COPY_AND_ASSIGN(Observer);
116   };
117
118   Observer capturing_net_log_observer_;
119
120   DISALLOW_COPY_AND_ASSIGN(CapturingNetLog);
121 };
122
123 // Helper class that exposes a similar API as BoundNetLog, but uses a
124 // CapturingNetLog rather than the more generic NetLog.
125 //
126 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the
127 // bound() method.
128 class CapturingBoundNetLog {
129  public:
130   CapturingBoundNetLog();
131   ~CapturingBoundNetLog();
132
133   // The returned BoundNetLog is only valid while |this| is alive.
134   BoundNetLog bound() const { return net_log_; }
135
136   // Fills |entry_list| with all entries in the log.
137   void GetEntries(CapturingNetLog::CapturedEntryList* entry_list) const;
138
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;
143
144   // Returns number of entries in the log.
145   size_t GetSize() const;
146
147   void Clear();
148
149   // Sets the log level of the underlying CapturingNetLog.
150   void SetLogLevel(NetLog::LogLevel log_level);
151
152  private:
153   CapturingNetLog capturing_net_log_;
154   const BoundNetLog net_log_;
155
156   DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog);
157 };
158
159 }  // namespace net
160
161 #endif  // NET_BASE_CAPTURING_NET_LOG_H_