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   // Creates a new metrics log
     29   // client_id is the identifier for this profile on this installation
     30   // session_id is an integer that's incremented on each application launch
     31   MetricsLogBase(const std::string& client_id,
     32                  int session_id,
     33                  const std::string& version_string);
     34   virtual ~MetricsLogBase();
     35 
     36   // Computes the MD5 hash of the given string, and returns the first 8 bytes of
     37   // the hash.
     38   static uint64 Hash(const std::string& value);
     39 
     40   // Get the GMT buildtime for the current binary, expressed in seconds since
     41   // Januray 1, 1970 GMT.
     42   // The value is used to identify when a new build is run, so that previous
     43   // reliability stats, from other builds, can be abandoned.
     44   static int64 GetBuildTime();
     45 
     46   // Convenience function to return the current time at a resolution in seconds.
     47   // This wraps base::TimeTicks, and hence provides an abstract time that is
     48   // always incrementing for use in measuring time durations.
     49   static int64 GetCurrentTime();
     50 
     51   // Records a user-initiated action.
     52   void RecordUserAction(const char* key);
     53 
     54   // Record any changes in a given histogram for transmission.
     55   void RecordHistogramDelta(const std::string& histogram_name,
     56                             const base::HistogramSamples& snapshot);
     57 
     58   // Stop writing to this record and generate the encoded representation.
     59   // None of the Record* methods can be called after this is called.
     60   void CloseLog();
     61 
     62   // Fills |encoded_log| with the serialized protobuf representation of the
     63   // record.  Must only be called after CloseLog() has been called.
     64   void GetEncodedLog(std::string* encoded_log);
     65 
     66   int num_events() { return num_events_; }
     67 
     68   void set_hardware_class(const std::string& hardware_class) {
     69     uma_proto_.mutable_system_profile()->mutable_hardware()->set_hardware_class(
     70         hardware_class);
     71   }
     72 
     73  protected:
     74   bool locked() const { return locked_; }
     75 
     76   metrics::ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; }
     77   const metrics::ChromeUserMetricsExtension* uma_proto() const {
     78     return &uma_proto_;
     79   }
     80 
     81   // TODO(isherman): Remove this once the XML pipeline is outta here.
     82   int num_events_;  // the number of events recorded in this log
     83 
     84  private:
     85   // locked_ is true when record has been packed up for sending, and should
     86   // no longer be written to.  It is only used for sanity checking and is
     87   // not a real lock.
     88   bool locked_;
     89 
     90   // Stores the protocol buffer representation for this log.
     91   metrics::ChromeUserMetricsExtension uma_proto_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(MetricsLogBase);
     94 };
     95 
     96 #endif  // CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
     97