| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file defines a set of user experience metrics data recorded by | 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. | 6 // the MetricsService. This is the unit of data that is sent to the server. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_METRICS_METRICS_LOG_H_ | 8 #ifndef CHROME_BROWSER_METRICS_METRICS_LOG_H_ |
| 9 #define CHROME_BROWSER_METRICS_METRICS_LOG_H_ | 9 #define CHROME_BROWSER_METRICS_METRICS_LOG_H_ |
| 10 | 10 |
| 11 #include <libxml/xmlwriter.h> | |
| 12 | |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 17 #include "base/histogram.h" | 12 #include "chrome/common/metrics_helpers.h" |
| 18 #include "base/time.h" | |
| 19 #include "chrome/common/page_transition_types.h" | 13 #include "chrome/common/page_transition_types.h" |
| 20 #include "webkit/glue/plugins/webplugininfo.h" | 14 #include "webkit/glue/plugins/webplugininfo.h" |
| 21 | 15 |
| 22 struct AutocompleteLog; | 16 struct AutocompleteLog; |
| 23 class DictionaryValue; | 17 class DictionaryValue; |
| 24 class GURL; | 18 class GURL; |
| 25 class PrefService; | 19 class PrefService; |
| 26 | 20 |
| 27 class MetricsLog { | 21 class MetricsLog : public MetricsLogBase { |
| 28 public: | 22 public: |
| 29 // Creates a new metrics log | 23 // Creates a new metrics log |
| 30 // client_id is the identifier for this profile on this installation | 24 // client_id is the identifier for this profile on this installation |
| 31 // session_id is an integer that's incremented on each application launch | 25 // session_id is an integer that's incremented on each application launch |
| 32 MetricsLog(const std::string& client_id, int session_id); | 26 MetricsLog(const std::string& client_id, int session_id); |
| 33 virtual ~MetricsLog(); | 27 virtual ~MetricsLog(); |
| 34 | 28 |
| 35 static void RegisterPrefs(PrefService* prefs); | 29 static void RegisterPrefs(PrefService* prefs); |
| 36 | 30 |
| 37 // Records a user-initiated action. | |
| 38 void RecordUserAction(const char* key); | |
| 39 | |
| 40 enum WindowEventType { | |
| 41 WINDOW_CREATE = 0, | |
| 42 WINDOW_OPEN, | |
| 43 WINDOW_CLOSE, | |
| 44 WINDOW_DESTROY | |
| 45 }; | |
| 46 | |
| 47 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 | |
| 54 void RecordLoadEvent(int window_id, | |
| 55 const GURL& url, | |
| 56 PageTransition::Type origin, | |
| 57 int session_index, | |
| 58 base::TimeDelta load_time); | |
| 59 | |
| 60 // Records the current operating environment. Takes the list of installed | 31 // Records the current operating environment. Takes the list of installed |
| 61 // plugins as a parameter because that can't be obtained synchronously | 32 // plugins as a parameter because that can't be obtained synchronously |
| 62 // from the UI thread. | 33 // from the UI thread. |
| 63 // profile_metrics, if non-null, gives a dictionary of all profile metrics | 34 // 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 | 35 // that are to be recorded. Each value in profile_metrics should be a |
| 65 // dictionary giving the metrics for the profile. | 36 // dictionary giving the metrics for the profile. |
| 66 void RecordEnvironment(const std::vector<WebPluginInfo>& plugin_list, | 37 void RecordEnvironment(const std::vector<WebPluginInfo>& plugin_list, |
| 67 const DictionaryValue* profile_metrics); | 38 const DictionaryValue* profile_metrics); |
| 68 | 39 |
| 69 // Records the input text, available choices, and selected entry when the | 40 // Records the input text, available choices, and selected entry when the |
| 70 // user uses the Omnibox to open a URL. | 41 // user uses the Omnibox to open a URL. |
| 71 void RecordOmniboxOpenedURL(const AutocompleteLog& log); | 42 void RecordOmniboxOpenedURL(const AutocompleteLog& log); |
| 72 | 43 |
| 73 // Record any changes in a given histogram for transmission. | |
| 74 void RecordHistogramDelta(const Histogram& histogram, | |
| 75 const Histogram::SampleSet& snapshot); | |
| 76 | |
| 77 // Record recent delta for critical stability metrics. We can't wait for a | 44 // 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 | 45 // 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 | 46 // users that run happily for a looooong time. We send increments with each |
| 80 // uma log upload, just as we send histogram data. | 47 // uma log upload, just as we send histogram data. |
| 81 void RecordIncrementalStabilityElements(); | 48 void RecordIncrementalStabilityElements(); |
| 82 | 49 |
| 83 // 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 void set_hardware_class(const std::string& hardware_class) { | 50 void set_hardware_class(const std::string& hardware_class) { |
| 99 hardware_class_ = hardware_class; | 51 hardware_class_ = hardware_class; |
| 100 } | 52 } |
| 101 | 53 |
| 102 // Creates an MD5 hash of the given value, and returns hash as a byte | |
| 103 // buffer encoded as a std::string. | |
| 104 static std::string CreateHash(const std::string& value); | |
| 105 | |
| 106 // Return a base64-encoded MD5 hash of the given string. | |
| 107 static std::string CreateBase64Hash(const std::string& string); | |
| 108 | |
| 109 // Get the current version of the application as a string. | |
| 110 static std::string GetVersionString(); | |
| 111 | |
| 112 // Get the GMT buildtime for the current binary, expressed in seconds since | |
| 113 // January 1, 1970 GMT. | |
| 114 // The value is used to identify when a new build is run, so that previous | |
| 115 // reliability stats, from other builds, can be abandoned. | |
| 116 static int64 GetBuildTime(); | |
| 117 | |
| 118 // Get the amount of uptime in seconds since this function was last called. | 54 // Get the amount of uptime in seconds since this function was last called. |
| 119 // This updates the cumulative uptime metric for uninstall as a side effect. | 55 // This updates the cumulative uptime metric for uninstall as a side effect. |
| 120 static int64 GetIncrementalUptime(PrefService* pref); | 56 static int64 GetIncrementalUptime(PrefService* pref); |
| 121 | 57 |
| 122 // Use |extension| in all uploaded appversions in addition to the standard | 58 // Get the current version of the application as a string. |
| 123 // version string. | 59 static std::string GetVersionString(); |
| 124 static void set_version_extension(const std::string& extension) { | 60 |
| 125 version_extension_ = extension; | 61 virtual MetricsLog* AsMetricsLog() { |
| 62 return this; |
| 126 } | 63 } |
| 127 | 64 |
| 128 protected: | |
| 129 // Returns a string containing the current time. | |
| 130 // Virtual so that it can be overridden for testing. | |
| 131 virtual std::string GetCurrentTimeString(); | |
| 132 | |
| 133 private: | 65 private: |
| 134 // Helper class that invokes StartElement from constructor, and EndElement | |
| 135 // from destructor. | |
| 136 // | |
| 137 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems. | |
| 138 class ScopedElement { | |
| 139 public: | |
| 140 ScopedElement(MetricsLog* log, const std::string& name) : log_(log) { | |
| 141 DCHECK(log); | |
| 142 log->StartElement(name.c_str()); | |
| 143 } | |
| 144 | |
| 145 ScopedElement(MetricsLog* log, const char* name) : log_(log) { | |
| 146 DCHECK(log); | |
| 147 log->StartElement(name); | |
| 148 } | |
| 149 | |
| 150 ~ScopedElement() { | |
| 151 log_->EndElement(); | |
| 152 } | |
| 153 | |
| 154 private: | |
| 155 MetricsLog* log_; | |
| 156 }; | |
| 157 friend class ScopedElement; | |
| 158 | |
| 159 static const char* WindowEventTypeToString(WindowEventType type); | |
| 160 | |
| 161 // Frees the resources allocated by the XML document writer: the | |
| 162 // main writer object as well as the XML tree structure, if | |
| 163 // applicable. | |
| 164 void FreeDocWriter(); | |
| 165 | |
| 166 // Convenience versions of xmlWriter functions | |
| 167 void StartElement(const char* name); | |
| 168 void EndElement(); | |
| 169 void WriteAttribute(const std::string& name, const std::string& value); | |
| 170 void WriteIntAttribute(const std::string& name, int value); | |
| 171 void WriteInt64Attribute(const std::string& name, int64 value); | |
| 172 | |
| 173 // Write the attributes that are common to every metrics event type. | |
| 174 void WriteCommonEventAttributes(); | |
| 175 | |
| 176 // Returns the date at which the current metrics client ID was created as | 66 // Returns the date at which the current metrics client ID was created as |
| 177 // a string containing milliseconds since the epoch, or "0" if none was found. | 67 // a string containing milliseconds since the epoch, or "0" if none was found. |
| 178 std::string GetInstallDate() const; | 68 std::string GetInstallDate() const; |
| 179 | 69 |
| 70 |
| 180 // Writes application stability metrics (as part of the profile log). | 71 // Writes application stability metrics (as part of the profile log). |
| 181 // NOTE: Has the side-effect of clearing those counts. | 72 // NOTE: Has the side-effect of clearing those counts. |
| 182 void WriteStabilityElement(); | 73 void WriteStabilityElement(); |
| 183 | 74 |
| 184 // Within stability group, write plugin crash stats. | 75 // Within stability group, write plugin crash stats. |
| 185 void WritePluginStabilityElements(PrefService* pref); | 76 void WritePluginStabilityElements(PrefService* pref); |
| 186 | 77 |
| 187 // Within the stability group, write required attributes. | 78 // Within the stability group, write required attributes. |
| 188 void WriteRequiredStabilityAttributes(PrefService* pref); | 79 void WriteRequiredStabilityAttributes(PrefService* pref); |
| 189 | 80 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 201 | 92 |
| 202 // Writes all profile metrics. This invokes WriteProfileMetrics for each key | 93 // Writes all profile metrics. This invokes WriteProfileMetrics for each key |
| 203 // in all_profiles_metrics that starts with kProfilePrefix. | 94 // in all_profiles_metrics that starts with kProfilePrefix. |
| 204 void WriteAllProfilesMetrics(const DictionaryValue& all_profiles_metrics); | 95 void WriteAllProfilesMetrics(const DictionaryValue& all_profiles_metrics); |
| 205 | 96 |
| 206 // Writes metrics for the profile identified by key. This writes all | 97 // Writes metrics for the profile identified by key. This writes all |
| 207 // key/value pairs in profile_metrics. | 98 // key/value pairs in profile_metrics. |
| 208 void WriteProfileMetrics(const std::wstring& key, | 99 void WriteProfileMetrics(const std::wstring& key, |
| 209 const DictionaryValue& profile_metrics); | 100 const DictionaryValue& profile_metrics); |
| 210 | 101 |
| 211 // An extension that is appended to the appversion in each log. | |
| 212 static std::string version_extension_; | |
| 213 | |
| 214 base::Time start_time_; | |
| 215 base::Time end_time_; | |
| 216 | |
| 217 std::string client_id_; | |
| 218 std::string session_id_; | |
| 219 std::string hardware_class_; | 102 std::string hardware_class_; |
| 220 | 103 |
| 221 // locked_ is true when record has been packed up for sending, and should | |
| 222 // no longer be written to. It is only used for sanity checking and is | |
| 223 // not a real lock. | |
| 224 bool locked_; | |
| 225 | |
| 226 xmlDocPtr doc_; | |
| 227 xmlBufferPtr buffer_; | |
| 228 xmlTextWriterPtr writer_; | |
| 229 int num_events_; // the number of events recorded in this log | |
| 230 | |
| 231 DISALLOW_COPY_AND_ASSIGN(MetricsLog); | 104 DISALLOW_COPY_AND_ASSIGN(MetricsLog); |
| 232 }; | 105 }; |
| 233 | 106 |
| 234 #endif // CHROME_BROWSER_METRICS_METRICS_LOG_H_ | 107 #endif // CHROME_BROWSER_METRICS_METRICS_LOG_H_ |
| OLD | NEW |