blob: 63ac23d399c72eddddd3371d97c97478ec6cd5f2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
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
8#ifndef CHROME_BROWSER_METRICS_LOG_H__
9#define CHROME_BROWSER_METRICS_LOG_H__
10
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"
initial.commit09911bf2008-07-26 23:55:2920#include "webkit/glue/webplugin.h"
21
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.
38 void RecordUserAction(const wchar_t* key);
39
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
106 protected:
107 // Returns a string containing the current time.
108 // Virtual so that it can be overridden for testing.
109 virtual std::string GetCurrentTimeString();
110
111 private:
112 // Helper class that invokes StartElement from constructor, and EndElement
113 // from destructor.
114 //
115 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems.
116 class ScopedElement {
117 public:
118 ScopedElement(MetricsLog* log, const std::string& name) : log_(log) {
119 DCHECK(log);
120 log->StartElement(name.c_str());
121 }
122
123 ScopedElement(MetricsLog* log, const char* name) : log_(log) {
124 DCHECK(log);
125 log->StartElement(name);
126 }
127
128 ~ScopedElement() {
129 log_->EndElement();
130 }
131
132 private:
133 MetricsLog* log_;
134 };
135 friend class ScopedElement;
136
[email protected]ffaf78a2008-11-12 17:38:33137 static const char* WindowEventTypeToString(WindowEventType type);
138
initial.commit09911bf2008-07-26 23:55:29139 // Convenience versions of xmlWriter functions
140 void StartElement(const char* name);
141 void EndElement();
142 void WriteAttribute(const std::string& name, const std::string& value);
143 void WriteIntAttribute(const std::string& name, int value);
144 void WriteInt64Attribute(const std::string& name, int64 value);
145
146 // Write the attributes that are common to every metrics event type.
147 void WriteCommonEventAttributes();
148
149 // Get the current version of the application as a string.
150 std::string GetVersionString() const;
151
152 // Returns the date at which the current metrics client ID was created as
153 // a string containing milliseconds since the epoch, or "0" if none was found.
154 std::string GetInstallDate() const;
155
156 // Writes application stability metrics (as part of the profile log).
157 // NOTE: Has the side-effect of clearing those counts.
158 void WriteStabilityElement();
159
[email protected]147bbc0b2009-01-06 19:37:40160 // Within stability group, write plugin crash stats.
161 void WritePluginStabilityElements(PrefService* pref);
162
163 // Within the stability group, write required attributes.
164 void WriteRequiredStabilityAttributes(PrefService* pref);
165
166 // Within the stability group, write attributes that need to be updated asap
[email protected]0b33f80b2008-12-17 21:34:36167 // and can't be delayed until the user decides to restart chromium.
168 // Delaying these stats would bias metrics away from happy long lived
169 // chromium processes (ones that don't crash, and keep on running).
[email protected]147bbc0b2009-01-06 19:37:40170 void WriteRealtimeStabilityAttributes(PrefService* pref);
[email protected]0b33f80b2008-12-17 21:34:36171
initial.commit09911bf2008-07-26 23:55:29172 // Writes the list of installed plugins.
173 void WritePluginList(const std::vector<WebPluginInfo>& plugin_list);
174
[email protected]147bbc0b2009-01-06 19:37:40175 // Within the profile group, write basic install info including appversion.
176 void WriteInstallElement();
177
initial.commit09911bf2008-07-26 23:55:29178 // Writes all profile metrics. This invokes WriteProfileMetrics for each key
179 // in all_profiles_metrics that starts with kProfilePrefix.
180 void WriteAllProfilesMetrics(const DictionaryValue& all_profiles_metrics);
181
182 // Writes metrics for the profile identified by key. This writes all
183 // key/value pairs in profile_metrics.
184 void WriteProfileMetrics(const std::wstring& key,
185 const DictionaryValue& profile_metrics);
186
[email protected]e1acf6f2008-10-27 20:43:33187 base::Time start_time_;
188 base::Time end_time_;
initial.commit09911bf2008-07-26 23:55:29189
190 std::string client_id_;
191 std::string session_id_;
192
193 // locked_ is true when record has been packed up for sending, and should
194 // no longer be written to. It is only used for sanity checking and is
195 // not a real lock.
196 bool locked_;
197
198 xmlBufferPtr buffer_;
199 xmlTextWriterPtr writer_;
200 int num_events_; // the number of events recorded in this log
201
202 DISALLOW_EVIL_CONSTRUCTORS(MetricsLog);
203};
204
205#endif // CHROME_BROWSER_METRICS_LOG_H__
license.botbf09a502008-08-24 00:55:55206