blob: 225e2a37abee848f3947affe958ea33044535a62 [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
73 void RecordHistogramDelta(const Histogram& histogram,
74 const Histogram::SampleSet& snapshot);
75
76 // Stop writing to this record and generate the encoded representation.
77 // None of the Record* methods can be called after this is called.
78 void CloseLog();
79
80 // These methods allow retrieval of the encoded representation of the
81 // record. They can only be called after CloseLog() has been called.
82 // GetEncodedLog returns false if buffer_size is less than
83 // GetEncodedLogSize();
84 int GetEncodedLogSize();
85 bool GetEncodedLog(char* buffer, int buffer_size);
86
87 // Returns the amount of time in seconds that this log has been in use.
88 int GetElapsedSeconds();
89
90 int num_events() { return num_events_; }
91
92 // Creates an MD5 hash of the given value, and returns hash as a byte
93 // buffer encoded as a std::string.
94 static std::string CreateHash(const std::string& value);
95
96 // Return a base64-encoded MD5 hash of the given string.
97 static std::string CreateBase64Hash(const std::string& string);
98
99 protected:
100 // Returns a string containing the current time.
101 // Virtual so that it can be overridden for testing.
102 virtual std::string GetCurrentTimeString();
103
104 private:
105 // Helper class that invokes StartElement from constructor, and EndElement
106 // from destructor.
107 //
108 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems.
109 class ScopedElement {
110 public:
111 ScopedElement(MetricsLog* log, const std::string& name) : log_(log) {
112 DCHECK(log);
113 log->StartElement(name.c_str());
114 }
115
116 ScopedElement(MetricsLog* log, const char* name) : log_(log) {
117 DCHECK(log);
118 log->StartElement(name);
119 }
120
121 ~ScopedElement() {
122 log_->EndElement();
123 }
124
125 private:
126 MetricsLog* log_;
127 };
128 friend class ScopedElement;
129
[email protected]ffaf78a2008-11-12 17:38:33130 static const char* WindowEventTypeToString(WindowEventType type);
131
initial.commit09911bf2008-07-26 23:55:29132 // Convenience versions of xmlWriter functions
133 void StartElement(const char* name);
134 void EndElement();
135 void WriteAttribute(const std::string& name, const std::string& value);
136 void WriteIntAttribute(const std::string& name, int value);
137 void WriteInt64Attribute(const std::string& name, int64 value);
138
139 // Write the attributes that are common to every metrics event type.
140 void WriteCommonEventAttributes();
141
142 // Get the current version of the application as a string.
143 std::string GetVersionString() const;
144
145 // Returns the date at which the current metrics client ID was created as
146 // a string containing milliseconds since the epoch, or "0" if none was found.
147 std::string GetInstallDate() const;
148
149 // Writes application stability metrics (as part of the profile log).
150 // NOTE: Has the side-effect of clearing those counts.
151 void WriteStabilityElement();
152
153 // Writes the list of installed plugins.
154 void WritePluginList(const std::vector<WebPluginInfo>& plugin_list);
155
156 // Writes all profile metrics. This invokes WriteProfileMetrics for each key
157 // in all_profiles_metrics that starts with kProfilePrefix.
158 void WriteAllProfilesMetrics(const DictionaryValue& all_profiles_metrics);
159
160 // Writes metrics for the profile identified by key. This writes all
161 // key/value pairs in profile_metrics.
162 void WriteProfileMetrics(const std::wstring& key,
163 const DictionaryValue& profile_metrics);
164
[email protected]e1acf6f2008-10-27 20:43:33165 base::Time start_time_;
166 base::Time end_time_;
initial.commit09911bf2008-07-26 23:55:29167
168 std::string client_id_;
169 std::string session_id_;
170
171 // locked_ is true when record has been packed up for sending, and should
172 // no longer be written to. It is only used for sanity checking and is
173 // not a real lock.
174 bool locked_;
175
176 xmlBufferPtr buffer_;
177 xmlTextWriterPtr writer_;
178 int num_events_; // the number of events recorded in this log
179
180 DISALLOW_EVIL_CONSTRUCTORS(MetricsLog);
181};
182
183#endif // CHROME_BROWSER_METRICS_LOG_H__
license.botbf09a502008-08-24 00:55:55184