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