blob: 18e3b322f6d34587b382f6714759f49ef068097a [file] [log] [blame]
[email protected]e612d762010-03-31 04:32:301// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
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
[email protected]11f4857282009-11-13 19:56:178#ifndef CHROME_BROWSER_METRICS_METRICS_LOG_H_
9#define CHROME_BROWSER_METRICS_METRICS_LOG_H_
initial.commit09911bf2008-07-26 23:55:2910
11#include <libxml/xmlwriter.h>
12
13#include <string>
14#include <vector>
15
16#include "base/basictypes.h"
17#include "base/histogram.h"
18#include "base/time.h"
19#include "chrome/common/page_transition_types.h"
[email protected]e612d762010-03-31 04:32:3020#include "webkit/glue/plugins/webplugininfo.h"
initial.commit09911bf2008-07-26 23:55:2921
22struct AutocompleteLog;
23class DictionaryValue;
[email protected]46072d42008-07-28 14:49:3524class GURL;
initial.commit09911bf2008-07-26 23:55:2925class PrefService;
26
27class MetricsLog {
28 public:
29 // Creates a new metrics log
30 // client_id is the identifier for this profile on this installation
31 // session_id is an integer that's incremented on each application launch
32 MetricsLog(const std::string& client_id, int session_id);
33 virtual ~MetricsLog();
34
35 static void RegisterPrefs(PrefService* prefs);
36
37 // Records a user-initiated action.
[email protected]afe3a1672009-11-17 19:04:1238 void RecordUserAction(const char* key);
initial.commit09911bf2008-07-26 23:55:2939
40 enum WindowEventType {
41 WINDOW_CREATE = 0,
42 WINDOW_OPEN,
43 WINDOW_CLOSE,
44 WINDOW_DESTROY
45 };
46
initial.commit09911bf2008-07-26 23:55:2947 void RecordWindowEvent(WindowEventType type, int window_id, int parent_id);
48
49 // Records a page load.
50 // window_id - the index of the tab in which the load took place
51 // url - which URL was loaded
52 // origin - what kind of action initiated the load
53 // load_time - how long it took to load the page
[email protected]23afa0f2008-09-19 15:58:5554 void RecordLoadEvent(int window_id,
55 const GURL& url,
56 PageTransition::Type origin,
57 int session_index,
[email protected]e1acf6f2008-10-27 20:43:3358 base::TimeDelta load_time);
initial.commit09911bf2008-07-26 23:55:2959
60 // Records the current operating environment. Takes the list of installed
61 // plugins as a parameter because that can't be obtained synchronously
62 // from the UI thread.
63 // profile_metrics, if non-null, gives a dictionary of all profile metrics
64 // that are to be recorded. Each value in profile_metrics should be a
65 // dictionary giving the metrics for the profile.
66 void RecordEnvironment(const std::vector<WebPluginInfo>& plugin_list,
67 const DictionaryValue* profile_metrics);
68
69 // Records the input text, available choices, and selected entry when the
70 // user uses the Omnibox to open a URL.
71 void RecordOmniboxOpenedURL(const AutocompleteLog& log);
72
[email protected]0b33f80b2008-12-17 21:34:3673 // Record any changes in a given histogram for transmission.
initial.commit09911bf2008-07-26 23:55:2974 void RecordHistogramDelta(const Histogram& histogram,
75 const Histogram::SampleSet& snapshot);
76
[email protected]0b33f80b2008-12-17 21:34:3677 // Record recent delta for critical stability metrics. We can't wait for a
78 // restart to gather these, as that delay biases our observation away from
79 // users that run happily for a looooong time. We send increments with each
80 // uma log upload, just as we send histogram data.
81 void RecordIncrementalStabilityElements();
82
initial.commit09911bf2008-07-26 23:55:2983 // Stop writing to this record and generate the encoded representation.
84 // None of the Record* methods can be called after this is called.
85 void CloseLog();
86
87 // These methods allow retrieval of the encoded representation of the
88 // record. They can only be called after CloseLog() has been called.
89 // GetEncodedLog returns false if buffer_size is less than
90 // GetEncodedLogSize();
91 int GetEncodedLogSize();
92 bool GetEncodedLog(char* buffer, int buffer_size);
93
94 // Returns the amount of time in seconds that this log has been in use.
95 int GetElapsedSeconds();
96
97 int num_events() { return num_events_; }
98
99 // Creates an MD5 hash of the given value, and returns hash as a byte
100 // buffer encoded as a std::string.
101 static std::string CreateHash(const std::string& value);
102
103 // Return a base64-encoded MD5 hash of the given string.
104 static std::string CreateBase64Hash(const std::string& string);
105
[email protected]541f77922009-02-23 21:14:38106 // Get the current version of the application as a string.
107 static std::string GetVersionString();
108
[email protected]225c50842010-01-19 21:19:13109 // Get the GMT buildtime for the current binary, expressed in seconds since
110 // Januray 1, 1970 GMT.
111 // The value is used to identify when a new build is run, so that previous
112 // reliability stats, from other builds, can be abandoned.
113 static int64 GetBuildTime();
114
[email protected]9165f742010-03-10 22:55:01115 // Get the amount of uptime in seconds since this function was last called.
116 // This updates the cumulative uptime metric for uninstall as a side effect.
117 static int64 GetIncrementalUptime(PrefService* pref);
118
[email protected]5ed7d4572009-12-23 17:42:41119 // Use |extension| in all uploaded appversions in addition to the standard
120 // version string.
121 static void set_version_extension(const std::string& extension) {
122 version_extension_ = extension;
123 }
initial.commit09911bf2008-07-26 23:55:29124 protected:
125 // Returns a string containing the current time.
126 // Virtual so that it can be overridden for testing.
127 virtual std::string GetCurrentTimeString();
128
129 private:
130 // Helper class that invokes StartElement from constructor, and EndElement
131 // from destructor.
132 //
133 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems.
134 class ScopedElement {
135 public:
136 ScopedElement(MetricsLog* log, const std::string& name) : log_(log) {
137 DCHECK(log);
138 log->StartElement(name.c_str());
139 }
140
141 ScopedElement(MetricsLog* log, const char* name) : log_(log) {
142 DCHECK(log);
143 log->StartElement(name);
144 }
145
146 ~ScopedElement() {
147 log_->EndElement();
148 }
149
150 private:
151 MetricsLog* log_;
152 };
153 friend class ScopedElement;
154
[email protected]ffaf78a2008-11-12 17:38:33155 static const char* WindowEventTypeToString(WindowEventType type);
156
initial.commit09911bf2008-07-26 23:55:29157 // Convenience versions of xmlWriter functions
158 void StartElement(const char* name);
159 void EndElement();
160 void WriteAttribute(const std::string& name, const std::string& value);
161 void WriteIntAttribute(const std::string& name, int value);
162 void WriteInt64Attribute(const std::string& name, int64 value);
163
164 // Write the attributes that are common to every metrics event type.
165 void WriteCommonEventAttributes();
166
initial.commit09911bf2008-07-26 23:55:29167 // Returns the date at which the current metrics client ID was created as
168 // a string containing milliseconds since the epoch, or "0" if none was found.
169 std::string GetInstallDate() const;
170
171 // Writes application stability metrics (as part of the profile log).
172 // NOTE: Has the side-effect of clearing those counts.
173 void WriteStabilityElement();
174
[email protected]147bbc0b2009-01-06 19:37:40175 // Within stability group, write plugin crash stats.
176 void WritePluginStabilityElements(PrefService* pref);
177
178 // Within the stability group, write required attributes.
179 void WriteRequiredStabilityAttributes(PrefService* pref);
180
181 // Within the stability group, write attributes that need to be updated asap
[email protected]0b33f80b2008-12-17 21:34:36182 // and can't be delayed until the user decides to restart chromium.
183 // Delaying these stats would bias metrics away from happy long lived
184 // chromium processes (ones that don't crash, and keep on running).
[email protected]147bbc0b2009-01-06 19:37:40185 void WriteRealtimeStabilityAttributes(PrefService* pref);
[email protected]0b33f80b2008-12-17 21:34:36186
initial.commit09911bf2008-07-26 23:55:29187 // Writes the list of installed plugins.
188 void WritePluginList(const std::vector<WebPluginInfo>& plugin_list);
189
[email protected]147bbc0b2009-01-06 19:37:40190 // Within the profile group, write basic install info including appversion.
191 void WriteInstallElement();
192
initial.commit09911bf2008-07-26 23:55:29193 // Writes all profile metrics. This invokes WriteProfileMetrics for each key
194 // in all_profiles_metrics that starts with kProfilePrefix.
195 void WriteAllProfilesMetrics(const DictionaryValue& all_profiles_metrics);
196
197 // Writes metrics for the profile identified by key. This writes all
198 // key/value pairs in profile_metrics.
199 void WriteProfileMetrics(const std::wstring& key,
200 const DictionaryValue& profile_metrics);
201
[email protected]5ed7d4572009-12-23 17:42:41202 // An extension that is appended to the appversion in each log.
203 static std::string version_extension_;
204
[email protected]e1acf6f2008-10-27 20:43:33205 base::Time start_time_;
206 base::Time end_time_;
initial.commit09911bf2008-07-26 23:55:29207
208 std::string client_id_;
209 std::string session_id_;
210
211 // locked_ is true when record has been packed up for sending, and should
212 // no longer be written to. It is only used for sanity checking and is
213 // not a real lock.
214 bool locked_;
215
216 xmlBufferPtr buffer_;
217 xmlTextWriterPtr writer_;
218 int num_events_; // the number of events recorded in this log
219
[email protected]4d818fee2010-06-06 13:32:27220 DISALLOW_COPY_AND_ASSIGN(MetricsLog);
initial.commit09911bf2008-07-26 23:55:29221};
222
[email protected]11f4857282009-11-13 19:56:17223#endif // CHROME_BROWSER_METRICS_METRICS_LOG_H_