Home | History | Annotate | Download | only in metrics
      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 // This file defines a set of user experience metrics data recorded by
      6 // the MetricsService.  This is the unit of data that is sent to the server.
      7 
      8 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
      9 #define CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
     10 
     11 #include <string>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/metrics/histogram.h"
     15 #include "base/time/time.h"
     16 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h"
     17 #include "content/public/common/page_transition_types.h"
     18 
     19 class GURL;
     20 
     21 namespace base {
     22 class HistogramSamples;
     23 }  // namespace base
     24 
     25 // This class provides base functionality for logging metrics data.
     26 class MetricsLogBase {
     27  public:
     28   // TODO(asvitkine): Make this a field on the log and remove the NO_LOG value.
     29   enum LogType {
     30     INITIAL_LOG,  // The first log of a session.
     31     ONGOING_LOG,  // Subsequent logs in a session.
     32     NO_LOG,       // Placeholder value for when there is no log.
     33   };
     34 
     35   // Creates a new metrics log
     36   // client_id is the identifier for this profile on this installation
     37   // session_id is an integer that's incremented on each application launch
     38   MetricsLogBase(const std::string& client_id,
     39                  int session_id,
     40                  const std::string& version_string);
     41   virtual ~MetricsLogBase();
     42 
     43   // Computes the MD5 hash of the given string, and returns the first 8 bytes of
     44   // the hash.
     45   static uint64 Hash(const std::string& value);
     46 
     47   // Get the GMT buildtime for the current binary, expressed in seconds since
     48   // Januray 1, 1970 GMT.
     49   // The value is used to identify when a new build is run, so that previous
     50   // reliability stats, from other builds, can be abandoned.
     51   static int64 GetBuildTime();
     52 
     53   // Convenience function to return the current time at a resolution in seconds.
     54   // This wraps base::TimeTicks, and hence provides an abstract time that is
     55   // always incrementing for use in measuring time durations.
     56   static int64 GetCurrentTime();
     57 
     58   // Records a user-initiated action.
     59   void RecordUserAction(const char* key);
     60 
     61   // Record any changes in a given histogram for transmission.
     62   void RecordHistogramDelta(const std::string& histogram_name,
     63                             const base::HistogramSamples& snapshot);
     64 
     65   // Stop writing to this record and generate the encoded representation.
     66   // None of the Record* methods can be called after this is called.
     67   void CloseLog();
     68 
     69   // Fills |encoded_log| with the serialized protobuf representation of the
     70   // record.  Must only be called after CloseLog() has been called.
     71   void GetEncodedLog(std::string* encoded_log);
     72 
     73   int num_events() { return num_events_; }
     74 
     75   void set_hardware_class(const std::string& hardware_class) {
     76     uma_proto_.mutable_system_profile()->mutable_hardware()->set_hardware_class(
     77         hardware_class);
     78   }
     79 
     80  protected:
     81   bool locked() const { return locked_; }
     82 
     83   metrics::ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; }
     84   const metrics::ChromeUserMetricsExtension* uma_proto() const {
     85     return &uma_proto_;
     86   }
     87 
     88   // TODO(isherman): Remove this once the XML pipeline is outta here.
     89   int num_events_;  // the number of events recorded in this log
     90 
     91  private:
     92   // locked_ is true when record has been packed up for sending, and should
     93   // no longer be written to.  It is only used for sanity checking and is
     94   // not a real lock.
     95   bool locked_;
     96 
     97   // Stores the protocol buffer representation for this log.
     98   metrics::ChromeUserMetricsExtension uma_proto_;
     99 
    100   DISALLOW_COPY_AND_ASSIGN(MetricsLogBase);
    101 };
    102 
    103 #endif  // CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
    104