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 // 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.
8 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
9 #define CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_
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"
22 class HistogramSamples
;
25 // This class provides base functionality for logging metrics data.
26 class MetricsLogBase
{
28 // TODO(asvitkine): Make this a field on the log and remove the NO_LOG value.
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.
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
,
40 const std::string
& version_string
);
41 virtual ~MetricsLogBase();
43 // Computes the MD5 hash of the given string, and returns the first 8 bytes of
45 static uint64
Hash(const std::string
& value
);
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();
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();
58 // Records a user-initiated action.
59 void RecordUserAction(const char* key
);
61 // Record any changes in a given histogram for transmission.
62 void RecordHistogramDelta(const std::string
& histogram_name
,
63 const base::HistogramSamples
& snapshot
);
65 // Stop writing to this record and generate the encoded representation.
66 // None of the Record* methods can be called after this is called.
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
);
73 int num_events() { return num_events_
; }
75 void set_hardware_class(const std::string
& hardware_class
) {
76 uma_proto_
.mutable_system_profile()->mutable_hardware()->set_hardware_class(
81 bool locked() const { return locked_
; }
83 metrics::ChromeUserMetricsExtension
* uma_proto() { return &uma_proto_
; }
84 const metrics::ChromeUserMetricsExtension
* uma_proto() const {
88 // TODO(isherman): Remove this once the XML pipeline is outta here.
89 int num_events_
; // the number of events recorded in this log
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
97 // Stores the protocol buffer representation for this log.
98 metrics::ChromeUserMetricsExtension uma_proto_
;
100 DISALLOW_COPY_AND_ASSIGN(MetricsLogBase
);
103 #endif // CHROME_COMMON_METRICS_METRICS_LOG_BASE_H_