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