blob: 1c02371ac27242b0ec8fc6598ee9b7ae71a1079c [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
47 static const char* WindowEventTypeToString(WindowEventType type);
48
49 void RecordWindowEvent(WindowEventType type, int window_id, int parent_id);
50
51 // Records a page load.
52 // window_id - the index of the tab in which the load took place
53 // url - which URL was loaded
54 // origin - what kind of action initiated the load
55 // load_time - how long it took to load the page
56 void MetricsLog::RecordLoadEvent(int window_id,
57 const GURL& url,
58 PageTransition::Type origin,
59 int session_index,
60 TimeDelta load_time);
61
62 // Records the current operating environment. Takes the list of installed
63 // plugins as a parameter because that can't be obtained synchronously
64 // from the UI thread.
65 // profile_metrics, if non-null, gives a dictionary of all profile metrics
66 // that are to be recorded. Each value in profile_metrics should be a
67 // dictionary giving the metrics for the profile.
68 void RecordEnvironment(const std::vector<WebPluginInfo>& plugin_list,
69 const DictionaryValue* profile_metrics);
70
71 // Records the input text, available choices, and selected entry when the
72 // user uses the Omnibox to open a URL.
73 void RecordOmniboxOpenedURL(const AutocompleteLog& log);
74
75 void RecordHistogramDelta(const Histogram& histogram,
76 const Histogram::SampleSet& snapshot);
77
78 // Stop writing to this record and generate the encoded representation.
79 // None of the Record* methods can be called after this is called.
80 void CloseLog();
81
82 // These methods allow retrieval of the encoded representation of the
83 // record. They can only be called after CloseLog() has been called.
84 // GetEncodedLog returns false if buffer_size is less than
85 // GetEncodedLogSize();
86 int GetEncodedLogSize();
87 bool GetEncodedLog(char* buffer, int buffer_size);
88
89 // Returns the amount of time in seconds that this log has been in use.
90 int GetElapsedSeconds();
91
92 int num_events() { return num_events_; }
93
94 // Creates an MD5 hash of the given value, and returns hash as a byte
95 // buffer encoded as a std::string.
96 static std::string CreateHash(const std::string& value);
97
98 // Return a base64-encoded MD5 hash of the given string.
99 static std::string CreateBase64Hash(const std::string& string);
100
101 protected:
102 // Returns a string containing the current time.
103 // Virtual so that it can be overridden for testing.
104 virtual std::string GetCurrentTimeString();
105
106 private:
107 // Helper class that invokes StartElement from constructor, and EndElement
108 // from destructor.
109 //
110 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems.
111 class ScopedElement {
112 public:
113 ScopedElement(MetricsLog* log, const std::string& name) : log_(log) {
114 DCHECK(log);
115 log->StartElement(name.c_str());
116 }
117
118 ScopedElement(MetricsLog* log, const char* name) : log_(log) {
119 DCHECK(log);
120 log->StartElement(name);
121 }
122
123 ~ScopedElement() {
124 log_->EndElement();
125 }
126
127 private:
128 MetricsLog* log_;
129 };
130 friend class ScopedElement;
131
132 // 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
165 Time start_time_;
166 Time end_time_;
167
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