• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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.
4 
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_METRICS_LOG_H_
9 #define CHROME_BROWSER_METRICS_METRICS_LOG_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #include "base/basictypes.h"
15 #include "chrome/browser/metrics/metrics_network_observer.h"
16 #include "chrome/common/metrics/metrics_log_base.h"
17 #include "chrome/common/metrics/variations/variations_util.h"
18 #include "chrome/installer/util/google_update_settings.h"
19 #include "ui/gfx/size.h"
20 
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/metrics/perf_provider_chromeos.h"
23 #endif
24 
25 class MetricsNetworkObserver;
26 struct OmniboxLog;
27 class PrefService;
28 class PrefRegistrySimple;
29 
30 namespace base {
31 class DictionaryValue;
32 }
33 
34 namespace content {
35 struct WebPluginInfo;
36 }
37 
38 namespace device {
39 class BluetoothAdapter;
40 }
41 
42 namespace tracked_objects {
43 struct ProcessDataSnapshot;
44 }
45 
46 namespace chrome_variations {
47 struct ActiveGroupId;
48 }
49 
50 // This is a small helper struct to pass Google Update metrics in a single
51 // reference argument to MetricsLog::RecordEnvironment().
52 struct GoogleUpdateMetrics {
53     GoogleUpdateMetrics();
54     ~GoogleUpdateMetrics();
55 
56     // Defines whether this is a user-level or system-level install.
57     bool is_system_install;
58     // The time at which Google Update last started an automatic update check.
59     base::Time last_started_au;
60     // The time at which Google Update last successfully recieved update
61     // information from Google servers.
62     base::Time last_checked;
63     // Details about Google Update's attempts to update itself.
64     GoogleUpdateSettings::ProductData google_update_data;
65     // Details about Google Update's attempts to update this product.
66     GoogleUpdateSettings::ProductData product_data;
67 };
68 
69 class MetricsLog : public MetricsLogBase {
70  public:
71   // Creates a new metrics log
72   // client_id is the identifier for this profile on this installation
73   // session_id is an integer that's incremented on each application launch
74   MetricsLog(const std::string& client_id, int session_id);
75   virtual ~MetricsLog();
76 
77   static void RegisterPrefs(PrefRegistrySimple* registry);
78 
79   // Get the current version of the application as a string.
80   static std::string GetVersionString();
81 
82   // Use |extension| in all uploaded appversions in addition to the standard
83   // version string.
84   static void set_version_extension(const std::string& extension);
85   static const std::string& version_extension();
86 
87   // Records the current operating environment.  Takes the list of installed
88   // plugins, Google Update statistics, and synthetic trial IDs as parameters
89   // because those can't be obtained synchronously from the UI thread.
90   // A synthetic trial is one that is set up dynamically by code in Chrome. For
91   // example, a pref may be mapped to a synthetic trial such that the group
92   // is determined by the pref value.
93   void RecordEnvironment(
94       const std::vector<content::WebPluginInfo>& plugin_list,
95       const GoogleUpdateMetrics& google_update_metrics,
96       const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials);
97 
98   // Loads the environment proto that was saved by the last RecordEnvironment()
99   // call from prefs and clears the pref value. Returns true on success or false
100   // if there was no saved environment in prefs or it could not be decoded.
101   bool LoadSavedEnvironmentFromPrefs();
102 
103   // Records the input text, available choices, and selected entry when the
104   // user uses the Omnibox to open a URL.
105   void RecordOmniboxOpenedURL(const OmniboxLog& log);
106 
107   // Records the passed profiled data, which should be a snapshot of the
108   // browser's profiled performance during startup for a single process.
109   void RecordProfilerData(
110       const tracked_objects::ProcessDataSnapshot& process_data,
111       int process_type);
112 
113   // Writes application stability metrics (as part of the profile log). The
114   // system profile portion of the log must have already been filled in by a
115   // call to RecordEnvironment() or LoadSavedEnvironmentFromPrefs().
116   // NOTE: Has the side-effect of clearing the stability prefs..
117   //
118   // If |log_type| is INITIAL_LOG, records additional info such as number of
119   // incomplete shutdowns as well as extra breakpad and debugger stats.
120   void RecordStabilityMetrics(
121       base::TimeDelta incremental_uptime,
122       LogType log_type);
123 
creation_time()124   const base::TimeTicks& creation_time() const {
125     return creation_time_;
126   }
127 
128  protected:
129   // Exposed for the sake of mocking in test code.
130 
131   // Returns the PrefService from which to log metrics data.
132   virtual PrefService* GetPrefService();
133 
134   // Returns the screen size for the primary monitor.
135   virtual gfx::Size GetScreenSize() const;
136 
137   // Returns the device scale factor for the primary monitor.
138   virtual float GetScreenDeviceScaleFactor() const;
139 
140   // Returns the number of monitors the user is using.
141   virtual int GetScreenCount() const;
142 
143   // Fills |field_trial_ids| with the list of initialized field trials name and
144   // group ids.
145   virtual void GetFieldTrialIds(
146       std::vector<chrome_variations::ActiveGroupId>* field_trial_ids) const;
147 
148  private:
149   FRIEND_TEST_ALL_PREFIXES(MetricsLogTest, ChromeOSStabilityData);
150 
151   // Returns true if the environment has already been filled in by a call to
152   // RecordEnvironment() or LoadSavedEnvironmentFromPrefs().
153   bool HasEnvironment() const;
154 
155   // Returns true if the stability metrics have already been filled in by a
156   // call to RecordStabilityMetrics().
157   bool HasStabilityMetrics() const;
158 
159   // Within stability group, write plugin crash stats.
160   void WritePluginStabilityElements(PrefService* pref);
161 
162   // Within the stability group, write required attributes.
163   void WriteRequiredStabilityAttributes(PrefService* pref);
164 
165   // Within the stability group, write attributes that need to be updated asap
166   // and can't be delayed until the user decides to restart chromium.
167   // Delaying these stats would bias metrics away from happy long lived
168   // chromium processes (ones that don't crash, and keep on running).
169   void WriteRealtimeStabilityAttributes(PrefService* pref,
170                                         base::TimeDelta incremental_uptime);
171 
172   // Writes the list of installed plugins.
173   void WritePluginList(const std::vector<content::WebPluginInfo>& plugin_list);
174 
175   // Writes info about the Google Update install that is managing this client.
176   // This is a no-op if called on a non-Windows platform.
177   void WriteGoogleUpdateProto(const GoogleUpdateMetrics& google_update_metrics);
178 
179   // Sets the Bluetooth Adapter instance used for the WriteBluetoothProto()
180   // call.
181   void SetBluetoothAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
182 
183   // Writes info about paired Bluetooth devices on this system.
184   // This is a no-op if called on a non-Chrome OS platform.
185   void WriteBluetoothProto(metrics::SystemProfileProto::Hardware* hardware);
186 
187 #if defined(OS_CHROMEOS)
188   // Update the number of users logged into a multi-profile session.
189   // If the number of users change while the log is open, the call invalidates
190   // the user count value.
191   void UpdateMultiProfileUserCount();
192 #endif
193 
194   // Observes network state to provide values for SystemProfile::Network.
195   MetricsNetworkObserver network_observer_;
196 
197 #if defined(OS_CHROMEOS)
198   metrics::PerfProvider perf_provider_;
199 #endif
200 
201   // Bluetooth Adapter instance for collecting information about paired devices.
202   scoped_refptr<device::BluetoothAdapter> adapter_;
203 
204   // The time when the current log was created.
205   const base::TimeTicks creation_time_;
206 
207   DISALLOW_COPY_AND_ASSIGN(MetricsLog);
208 };
209 
210 #endif  // CHROME_BROWSER_METRICS_METRICS_LOG_H_
211