blob: 1ba69ab537aed3e5b788084342eb92a53aa8775b [file] [log] [blame]
isherman@chromium.org1df44b72012-01-19 05:20:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
ben@chromium.orgcd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
thestig@chromium.org1eeb5e02010-07-20 23:02:117#include <string>
8#include <vector>
9
deanm@chromium.orgd1be67b2008-11-19 20:28:3810#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/file_util.h"
thakis@chromium.org054c8012011-11-16 00:12:4212#include "base/lazy_instance.h"
levin@chromium.org3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
zelidrag@chromium.org85ed9d42010-06-08 22:37:4414#include "base/perftimer.h"
isherman@chromium.orgb2a4812d2012-02-28 05:31:3115#include "base/string_number_conversions.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/string_util.h"
deanm@chromium.orgfadf97f2008-09-18 12:18:1417#include "base/sys_info.h"
jar@chromium.org37f39e42010-01-06 17:35:1718#include "base/third_party/nspr/prtime.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1119#include "base/time.h"
20#include "base/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/autocomplete/autocomplete.h"
scr@chromium.org9ac40092010-10-27 23:05:2622#include "chrome/browser/autocomplete/autocomplete_match.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/browser_process.h"
isherman@chromium.orgb2a4812d2012-02-28 05:31:3124#include "chrome/browser/gpu_performance_stats.h"
bauerb@chromium.org10084982011-08-19 17:56:5625#include "chrome/browser/plugin_prefs.h"
evan@chromium.org37858e52010-08-26 00:22:0226#include "chrome/browser/prefs/pref_service.h"
bauerb@chromium.org10084982011-08-19 17:56:5627#include "chrome/browser/profiles/profile_manager.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1128#include "chrome/common/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/logging_chrome.h"
isherman@chromium.orgb2a4812d2012-02-28 05:31:3130#include "chrome/common/metrics/proto/omnibox_event.pb.h"
31#include "chrome/common/metrics/proto/system_profile.pb.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/common/pref_names.h"
isherman@chromium.orgb2a4812d2012-02-28 05:31:3133#include "content/public/browser/content_browser_client.h"
jam@chromium.org79078df2012-02-16 01:22:3234#include "content/public/browser/gpu_data_manager.h"
35#include "content/public/common/gpu_info.h"
isherman@chromium.orgb2a4812d2012-02-28 05:31:3136#include "content/public/common/content_client.h"
deanm@google.com46072d42008-07-28 14:49:3537#include "googleurl/src/gurl.h"
derat@chromium.org6b7d954ff2011-10-25 00:39:3538#include "ui/gfx/screen.h"
cpu@chromium.org91d9f3d2011-08-14 05:24:4439#include "webkit/plugins/webplugininfo.h"
initial.commit09911bf2008-07-26 23:55:2940
41#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
42
deanm@chromium.orgd1be67b2008-11-19 20:28:3843// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
44#if defined(OS_WIN)
45extern "C" IMAGE_DOS_HEADER __ImageBase;
46#endif
47
jam@chromium.org79078df2012-02-16 01:22:3248using content::GpuDataManager;
isherman@chromium.orgb2a4812d2012-02-28 05:31:3149using metrics::OmniboxEventProto;
50using metrics::SystemProfileProto;
jam@chromium.org79078df2012-02-16 01:22:3251
isherman@chromium.org1df44b72012-01-19 05:20:3452namespace {
53
54// Returns the date at which the current metrics client ID was created as
55// a string containing milliseconds since the epoch, or "0" if none was found.
56std::string GetInstallDate() {
57 PrefService* pref = g_browser_process->local_state();
58 if (pref) {
59 return pref->GetString(prefs::kMetricsClientIDTimestamp);
60 } else {
61 NOTREACHED();
62 return "0";
63 }
64}
65
isherman@chromium.orgb2a4812d2012-02-28 05:31:3166OmniboxEventProto::InputType AsOmniboxEventInputType(
67 AutocompleteInput::Type type) {
68 switch (type) {
69 case AutocompleteInput::INVALID:
70 return OmniboxEventProto::INVALID;
71 case AutocompleteInput::UNKNOWN:
72 return OmniboxEventProto::UNKNOWN;
73 case AutocompleteInput::REQUESTED_URL:
74 return OmniboxEventProto::REQUESTED_URL;
75 case AutocompleteInput::URL:
76 return OmniboxEventProto::URL;
77 case AutocompleteInput::QUERY:
78 return OmniboxEventProto::QUERY;
79 case AutocompleteInput::FORCED_QUERY:
80 return OmniboxEventProto::FORCED_QUERY;
81 default:
82 NOTREACHED();
83 return OmniboxEventProto::INVALID;
84 }
85}
86
87OmniboxEventProto::Suggestion::ProviderType AsOmniboxEventProviderType(
88 const AutocompleteProvider* provider) {
89 if (!provider)
90 return OmniboxEventProto::Suggestion::UNKNOWN_PROVIDER;
91
92 const std::string& name = provider->name();
93 if (name == "HistoryURL")
94 return OmniboxEventProto::Suggestion::URL;
95 if (name == "HistoryContents")
96 return OmniboxEventProto::Suggestion::HISTORY_CONTENTS;
97 if (name == "HistoryQuickProvider")
98 return OmniboxEventProto::Suggestion::HISTORY_QUICK;
99 if (name == "Search")
100 return OmniboxEventProto::Suggestion::SEARCH;
101 if (name == "Keyword")
102 return OmniboxEventProto::Suggestion::KEYWORD;
103 if (name == "Builtin")
104 return OmniboxEventProto::Suggestion::BUILTIN;
105 if (name == "ShortcutsProvider")
106 return OmniboxEventProto::Suggestion::SHORTCUTS;
107 if (name == "ExtensionApps")
108 return OmniboxEventProto::Suggestion::EXTENSION_APPS;
109
110 NOTREACHED();
111 return OmniboxEventProto::Suggestion::UNKNOWN_PROVIDER;
112}
113
114OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
115 AutocompleteMatch::Type type) {
116 switch (type) {
117 case AutocompleteMatch::URL_WHAT_YOU_TYPED:
118 return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
119 case AutocompleteMatch::HISTORY_URL:
120 return OmniboxEventProto::Suggestion::HISTORY_URL;
121 case AutocompleteMatch::HISTORY_TITLE:
122 return OmniboxEventProto::Suggestion::HISTORY_TITLE;
123 case AutocompleteMatch::HISTORY_BODY:
124 return OmniboxEventProto::Suggestion::HISTORY_BODY;
125 case AutocompleteMatch::HISTORY_KEYWORD:
126 return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
127 case AutocompleteMatch::NAVSUGGEST:
128 return OmniboxEventProto::Suggestion::NAVSUGGEST;
129 case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED:
130 return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
131 case AutocompleteMatch::SEARCH_HISTORY:
132 return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
133 case AutocompleteMatch::SEARCH_SUGGEST:
134 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
135 case AutocompleteMatch::SEARCH_OTHER_ENGINE:
136 return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
137 case AutocompleteMatch::EXTENSION_APP:
138 return OmniboxEventProto::Suggestion::EXTENSION_APP;
139 default:
140 NOTREACHED();
141 return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
142 }
143}
144
isherman@chromium.org1df44b72012-01-19 05:20:34145// Returns the plugin preferences corresponding for this user, if available.
146// If multiple user profiles are loaded, returns the preferences corresponding
147// to an arbitrary one of the profiles.
148PluginPrefs* GetPluginPrefs() {
149 ProfileManager* profile_manager = g_browser_process->profile_manager();
150 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
151 if (profiles.empty())
152 return NULL;
153
154 return PluginPrefs::GetForProfile(profiles.front());
155}
156
isherman@chromium.orgb2a4812d2012-02-28 05:31:31157// Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|.
158void SetPluginInfo(const webkit::WebPluginInfo& plugin_info,
159 const PluginPrefs* plugin_prefs,
160 SystemProfileProto::Plugin* plugin) {
161 plugin->set_name(UTF16ToUTF8(plugin_info.name));
162 plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe());
163 plugin->set_version(UTF16ToUTF8(plugin_info.version));
164 if (plugin_prefs)
165 plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info));
166}
167
isherman@chromium.org1df44b72012-01-19 05:20:34168} // namespace
169
fischman@chromium.org67f92bc32012-01-26 01:56:19170static base::LazyInstance<std::string>::Leaky
thakis@chromium.org054c8012011-11-16 00:12:42171 g_version_extension = LAZY_INSTANCE_INITIALIZER;
172
ananta@chromium.org1226abb2010-06-10 18:01:28173MetricsLog::MetricsLog(const std::string& client_id, int session_id)
174 : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()) {}
jar@chromium.org5ed7d4572009-12-23 17:42:41175
ananta@chromium.org1226abb2010-06-10 18:01:28176MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:29177
178// static
179void MetricsLog::RegisterPrefs(PrefService* local_state) {
180 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29181}
182
isherman@chromium.org1df44b72012-01-19 05:20:34183// static
jar@chromium.org9165f742010-03-10 22:55:01184int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
185 base::TimeTicks now = base::TimeTicks::Now();
186 static base::TimeTicks last_updated_time(now);
187 int64 incremental_time = (now - last_updated_time).InSeconds();
188 last_updated_time = now;
189
190 if (incremental_time > 0) {
191 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
192 metrics_uptime += incremental_time;
193 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
194 }
195
196 return incremental_time;
197}
198
ananta@chromium.org1226abb2010-06-10 18:01:28199// static
200std::string MetricsLog::GetVersionString() {
evan@chromium.org0211f57e2010-08-27 20:28:42201 chrome::VersionInfo version_info;
dmaclach@chromium.org30c91802010-12-18 00:40:17202 if (!version_info.is_valid()) {
203 NOTREACHED() << "Unable to retrieve version info.";
204 return std::string();
205 }
206
evan@chromium.org0211f57e2010-08-27 20:28:42207 std::string version = version_info.Version();
thakis@chromium.org054c8012011-11-16 00:12:42208 if (!version_extension().empty())
209 version += version_extension();
evan@chromium.org0211f57e2010-08-27 20:28:42210 if (!version_info.IsOfficialBuild())
211 version.append("-devel");
212 return version;
ananta@chromium.org1226abb2010-06-10 18:01:28213}
214
thakis@chromium.org054c8012011-11-16 00:12:42215// static
216void MetricsLog::set_version_extension(const std::string& extension) {
217 g_version_extension.Get() = extension;
218}
219
220// static
221const std::string& MetricsLog::version_extension() {
222 return g_version_extension.Get();
223}
224
isherman@chromium.orgb2a4812d2012-02-28 05:31:31225void MetricsLog::RecordIncrementalStabilityElements(
226 const std::vector<webkit::WebPluginInfo>& plugin_list) {
jar@google.com0b33f80b2008-12-17 21:34:36227 DCHECK(!locked_);
228
229 PrefService* pref = g_browser_process->local_state();
230 DCHECK(pref);
231
jar@google.com147bbc0b2009-01-06 19:37:40232 OPEN_ELEMENT_FOR_SCOPE("profile");
233 WriteCommonEventAttributes();
234
jar@chromium.org9958a322011-03-08 20:04:17235 WriteInstallElement();
jar@google.com147bbc0b2009-01-06 19:37:40236
237 {
238 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
239 WriteRequiredStabilityAttributes(pref);
240 WriteRealtimeStabilityAttributes(pref);
241
isherman@chromium.orgb2a4812d2012-02-28 05:31:31242 WritePluginStabilityElements(plugin_list, pref);
jar@google.com147bbc0b2009-01-06 19:37:40243 }
jar@google.com0b33f80b2008-12-17 21:34:36244}
245
isherman@chromium.orgb2a4812d2012-02-28 05:31:31246void MetricsLog::WriteStabilityElement(
247 const std::vector<webkit::WebPluginInfo>& plugin_list,
248 PrefService* pref) {
initial.commit09911bf2008-07-26 23:55:29249 DCHECK(!locked_);
250
initial.commit09911bf2008-07-26 23:55:29251 // Get stability attributes out of Local State, zeroing out stored values.
252 // NOTE: This could lead to some data loss if this report isn't successfully
253 // sent, but that's true for all the metrics.
254
pkasting@chromium.orgffaf78a2008-11-12 17:38:33255 OPEN_ELEMENT_FOR_SCOPE("stability");
jar@google.com147bbc0b2009-01-06 19:37:40256 WriteRequiredStabilityAttributes(pref);
257 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29258
isherman@chromium.orgb2a4812d2012-02-28 05:31:31259 int incomplete_shutdown_count =
260 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
evanm@google.com8e674e42008-07-30 16:05:48261 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31262 int breakpad_registration_success_count =
263 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
cpu@google.come73c01972008-08-13 00:18:24264 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31265 int breakpad_registration_failure_count =
266 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
cpu@google.come73c01972008-08-13 00:18:24267 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31268 int debugger_present_count =
269 pref->GetInteger(prefs::kStabilityDebuggerPresent);
cpu@google.come73c01972008-08-13 00:18:24270 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31271 int debugger_not_present_count =
272 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
cpu@google.come73c01972008-08-13 00:18:24273 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29274
isherman@chromium.orgb2a4812d2012-02-28 05:31:31275 // TODO(jar): The following are all optional, so we *could* optimize them for
276 // values of zero (and not include them).
277
278 // Write the XML version.
279 WriteIntAttribute("incompleteshutdowncount", incomplete_shutdown_count);
280 WriteIntAttribute("breakpadregistrationok",
281 breakpad_registration_success_count);
282 WriteIntAttribute("breakpadregistrationfail",
283 breakpad_registration_failure_count);
284 WriteIntAttribute("debuggerpresent", debugger_present_count);
285 WriteIntAttribute("debuggernotpresent", debugger_not_present_count);
286
287 // Write the protobuf version.
288 SystemProfileProto::Stability* stability =
289 uma_proto_.mutable_system_profile()->mutable_stability();
290 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
291 stability->set_breakpad_registration_success_count(
292 breakpad_registration_success_count);
293 stability->set_breakpad_registration_failure_count(
294 breakpad_registration_failure_count);
295 stability->set_debugger_present_count(debugger_present_count);
296 stability->set_debugger_not_present_count(debugger_not_present_count);
297
298 WritePluginStabilityElements(plugin_list, pref);
jar@google.com147bbc0b2009-01-06 19:37:40299}
300
isherman@chromium.orgb2a4812d2012-02-28 05:31:31301void MetricsLog::WritePluginStabilityElements(
302 const std::vector<webkit::WebPluginInfo>& plugin_list,
303 PrefService* pref) {
jar@google.com147bbc0b2009-01-06 19:37:40304 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29305 const ListValue* plugin_stats_list = pref->GetList(
306 prefs::kStabilityPluginStats);
isherman@chromium.org1df44b72012-01-19 05:20:34307 if (!plugin_stats_list)
308 return;
initial.commit09911bf2008-07-26 23:55:29309
isherman@chromium.org1df44b72012-01-19 05:20:34310 OPEN_ELEMENT_FOR_SCOPE("plugins");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31311 SystemProfileProto::Stability* stability =
312 uma_proto_.mutable_system_profile()->mutable_stability();
313 PluginPrefs* plugin_prefs = GetPluginPrefs();
isherman@chromium.org1df44b72012-01-19 05:20:34314 for (ListValue::const_iterator iter = plugin_stats_list->begin();
315 iter != plugin_stats_list->end(); ++iter) {
316 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
317 NOTREACHED();
318 continue;
initial.commit09911bf2008-07-26 23:55:29319 }
isherman@chromium.org1df44b72012-01-19 05:20:34320 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29321
isherman@chromium.org1df44b72012-01-19 05:20:34322 std::string plugin_name;
323 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
324
isherman@chromium.orgb2a4812d2012-02-28 05:31:31325 std::string base64_name_hash;
326 uint64 numeric_name_hash_ignored;
327 CreateHashes(plugin_name, &base64_name_hash, &numeric_name_hash_ignored);
328
329 // Write the XML verison.
isherman@chromium.org1df44b72012-01-19 05:20:34330 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
331 // Use "filename" instead of "name", otherwise we need to update the
332 // UMA servers.
isherman@chromium.orgb2a4812d2012-02-28 05:31:31333 WriteAttribute("filename", base64_name_hash);
isherman@chromium.org1df44b72012-01-19 05:20:34334
335 int launches = 0;
336 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
337 WriteIntAttribute("launchcount", launches);
338
339 int instances = 0;
340 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
341 WriteIntAttribute("instancecount", instances);
342
343 int crashes = 0;
344 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
345 WriteIntAttribute("crashcount", crashes);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31346
347 // Write the protobuf version.
348 // Note that this search is potentially a quadratic operation, but given the
349 // low number of plugins installed on a "reasonable" setup, this should be
350 // fine.
351 // TODO(isherman): Verify that this does not show up as a hotspot in
352 // profiler runs.
353 const webkit::WebPluginInfo* plugin_info = NULL;
354 const string16 plugin_name_utf16 = UTF8ToUTF16(plugin_name);
355 for (std::vector<webkit::WebPluginInfo>::const_iterator iter =
356 plugin_list.begin();
357 iter != plugin_list.end(); ++iter) {
358 if (iter->name == plugin_name_utf16) {
359 plugin_info = &(*iter);
360 break;
361 }
362 }
363
364 if (!plugin_info) {
365 NOTREACHED();
366 continue;
367 }
368
369 SystemProfileProto::Stability::PluginStability* plugin_stability =
370 stability->add_plugin_stability();
371 SetPluginInfo(*plugin_info, plugin_prefs,
372 plugin_stability->mutable_plugin());
373 plugin_stability->set_launch_count(launches);
374 plugin_stability->set_instance_count(instances);
375 plugin_stability->set_crash_count(crashes);
initial.commit09911bf2008-07-26 23:55:29376 }
isherman@chromium.org1df44b72012-01-19 05:20:34377
378 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29379}
380
isherman@chromium.orgb2a4812d2012-02-28 05:31:31381// The server refuses data that doesn't have certain values. crashcount and
382// launchcount are currently "required" in the "stability" group.
383// TODO(isherman): Stop writing these attributes specially once the migration to
384// protobufs is complete.
jar@google.com147bbc0b2009-01-06 19:37:40385void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31386 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
jar@google.com0b33f80b2008-12-17 21:34:36387 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31388 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
jar@google.com0b33f80b2008-12-17 21:34:36389 pref->SetInteger(prefs::kStabilityCrashCount, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31390
391 // Write the XML version.
392 WriteIntAttribute("launchcount", launch_count);
393 WriteIntAttribute("crashcount", crash_count);
394
395 // Write the protobuf version.
396 SystemProfileProto::Stability* stability =
397 uma_proto_.mutable_system_profile()->mutable_stability();
398 stability->set_launch_count(launch_count);
399 stability->set_crash_count(crash_count);
jar@google.com0b33f80b2008-12-17 21:34:36400}
401
jar@google.com147bbc0b2009-01-06 19:37:40402void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
jar@google.com0b33f80b2008-12-17 21:34:36403 // Update the stats which are critical for real-time stability monitoring.
404 // Since these are "optional," only list ones that are non-zero, as the counts
405 // are aggergated (summed) server side.
406
isherman@chromium.orgb2a4812d2012-02-28 05:31:31407 SystemProfileProto::Stability* stability =
408 uma_proto_.mutable_system_profile()->mutable_stability();
jar@google.com0b33f80b2008-12-17 21:34:36409 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
410 if (count) {
411 WriteIntAttribute("pageloadcount", count);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31412 stability->set_page_load_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36413 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
414 }
415
416 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
417 if (count) {
418 WriteIntAttribute("renderercrashcount", count);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31419 stability->set_renderer_crash_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36420 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
421 }
422
asargent@chromium.org1f085622009-12-04 05:33:45423 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
424 if (count) {
425 WriteIntAttribute("extensionrenderercrashcount", count);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31426 stability->set_extension_renderer_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45427 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
428 }
429
jar@google.com0b33f80b2008-12-17 21:34:36430 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
431 if (count) {
432 WriteIntAttribute("rendererhangcount", count);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31433 stability->set_renderer_hang_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36434 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
435 }
asargent@chromium.org1f085622009-12-04 05:33:45436
437 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
438 if (count) {
439 WriteIntAttribute("childprocesscrashcount", count);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31440 stability->set_child_process_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45441 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
442 }
jar@chromium.org9165f742010-03-10 22:55:01443
petkov@chromium.orgc1834a92011-01-21 18:21:03444#if defined(OS_CHROMEOS)
445 count = pref->GetInteger(prefs::kStabilityOtherUserCrashCount);
446 if (count) {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31447 stability->set_other_user_crash_count(count);
petkov@chromium.orgc1834a92011-01-21 18:21:03448 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 0);
449 }
450
451 count = pref->GetInteger(prefs::kStabilityKernelCrashCount);
452 if (count) {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31453 stability->set_kernel_crash_count(count);
petkov@chromium.orgc1834a92011-01-21 18:21:03454 pref->SetInteger(prefs::kStabilityKernelCrashCount, 0);
455 }
456
457 count = pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount);
458 if (count) {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31459 stability->set_unclean_system_shutdown_count(count);
petkov@chromium.orgc1834a92011-01-21 18:21:03460 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 0);
461 }
462#endif // OS_CHROMEOS
463
jar@chromium.org9165f742010-03-10 22:55:01464 int64 recent_duration = GetIncrementalUptime(pref);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31465 if (recent_duration) {
jar@chromium.org9165f742010-03-10 22:55:01466 WriteInt64Attribute("uptimesec", recent_duration);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31467 stability->set_uptime_sec(recent_duration);
468 }
jar@google.com0b33f80b2008-12-17 21:34:36469}
470
initial.commit09911bf2008-07-26 23:55:29471void MetricsLog::WritePluginList(
cpu@chromium.org91d9f3d2011-08-14 05:24:44472 const std::vector<webkit::WebPluginInfo>& plugin_list) {
initial.commit09911bf2008-07-26 23:55:29473 DCHECK(!locked_);
474
isherman@chromium.org1df44b72012-01-19 05:20:34475 PluginPrefs* plugin_prefs = GetPluginPrefs();
bauerb@chromium.org10084982011-08-19 17:56:56476
pkasting@chromium.orgffaf78a2008-11-12 17:38:33477 OPEN_ELEMENT_FOR_SCOPE("plugins");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31478 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile();
cpu@chromium.org91d9f3d2011-08-14 05:24:44479 for (std::vector<webkit::WebPluginInfo>::const_iterator iter =
brettw@chromium.org191eb3f72010-12-21 06:27:50480 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29481 iter != plugin_list.end(); ++iter) {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31482 std::string base64_name_hash;
483 uint64 numeric_name_hash_ignored;
484 CreateHashes(UTF16ToUTF8(iter->name),
485 &base64_name_hash,
486 &numeric_name_hash_ignored);
487
488 std::string filename_bytes = iter->path.BaseName().AsUTF8Unsafe();
489 std::string base64_filename_hash;
490 uint64 numeric_filename_hash;
491 CreateHashes(filename_bytes,
492 &base64_filename_hash,
493 &numeric_filename_hash);
494
495 // Write the XML version.
pkasting@chromium.orgffaf78a2008-11-12 17:38:33496 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29497
498 // Plugin name and filename are hashed for the privacy of those
499 // testing unreleased new extensions.
isherman@chromium.orgb2a4812d2012-02-28 05:31:31500 WriteAttribute("name", base64_name_hash);
501 WriteAttribute("filename", base64_filename_hash);
stuartmorgan@chromium.orgc9d811372010-06-23 21:44:57502 WriteAttribute("version", UTF16ToUTF8(iter->version));
bauerb@chromium.org24c935e52011-09-01 12:06:26503 if (plugin_prefs)
504 WriteIntAttribute("disabled", !plugin_prefs->IsPluginEnabled(*iter));
isherman@chromium.orgb2a4812d2012-02-28 05:31:31505
506 // Write the protobuf version.
507 SystemProfileProto::Plugin* plugin = system_profile->add_plugin();
508 SetPluginInfo(*iter, plugin_prefs, plugin);
initial.commit09911bf2008-07-26 23:55:29509 }
initial.commit09911bf2008-07-26 23:55:29510}
511
jar@google.com147bbc0b2009-01-06 19:37:40512void MetricsLog::WriteInstallElement() {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31513 std::string install_date = GetInstallDate();
514
515 // Write the XML version.
jar@google.com147bbc0b2009-01-06 19:37:40516 OPEN_ELEMENT_FOR_SCOPE("install");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31517 WriteAttribute("installdate", install_date);
jar@google.com147bbc0b2009-01-06 19:37:40518 WriteIntAttribute("buildid", 0); // We're using appversion instead.
isherman@chromium.orgb2a4812d2012-02-28 05:31:31519
520 // Write the protobuf version.
521 int numeric_install_date;
522 bool success = base::StringToInt(install_date, &numeric_install_date);
523 DCHECK(success);
524 uma_proto_.mutable_system_profile()->set_install_date(numeric_install_date);
jar@google.com147bbc0b2009-01-06 19:37:40525}
526
initial.commit09911bf2008-07-26 23:55:29527void MetricsLog::RecordEnvironment(
cpu@chromium.org91d9f3d2011-08-14 05:24:44528 const std::vector<webkit::WebPluginInfo>& plugin_list,
initial.commit09911bf2008-07-26 23:55:29529 const DictionaryValue* profile_metrics) {
530 DCHECK(!locked_);
531
532 PrefService* pref = g_browser_process->local_state();
533
pkasting@chromium.orgffaf78a2008-11-12 17:38:33534 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29535 WriteCommonEventAttributes();
536
jar@google.com147bbc0b2009-01-06 19:37:40537 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29538
539 WritePluginList(plugin_list);
540
isherman@chromium.orgb2a4812d2012-02-28 05:31:31541 WriteStabilityElement(plugin_list, pref);
initial.commit09911bf2008-07-26 23:55:29542
isherman@chromium.orgb2a4812d2012-02-28 05:31:31543 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile();
544 system_profile->set_application_locale(
545 content::GetContentClient()->browser()->GetApplicationLocale());
546
547 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
initial.commit09911bf2008-07-26 23:55:29548 {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31549 std::string cpu_architecture = base::SysInfo::CPUArchitecture();
550
551 // Write the XML version.
initial.commit09911bf2008-07-26 23:55:29552 OPEN_ELEMENT_FOR_SCOPE("cpu");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31553 WriteAttribute("arch", cpu_architecture);
554
555 // Write the protobuf version.
556 hardware->set_cpu_architecture(cpu_architecture);
initial.commit09911bf2008-07-26 23:55:29557 }
558
559 {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31560 int system_memory_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
561
562 // Write the XML version.
initial.commit09911bf2008-07-26 23:55:29563 OPEN_ELEMENT_FOR_SCOPE("memory");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31564 WriteIntAttribute("mb", system_memory_mb);
deanm@chromium.orgd1be67b2008-11-19 20:28:38565#if defined(OS_WIN)
566 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
567#endif
isherman@chromium.orgb2a4812d2012-02-28 05:31:31568
569 // Write the protobuf version.
570 hardware->set_system_ram_mb(system_memory_mb);
571#if defined(OS_WIN)
572 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
573#endif
initial.commit09911bf2008-07-26 23:55:29574 }
575
576 {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31577 std::string os_name = base::SysInfo::OperatingSystemName();
578 std::string os_version = base::SysInfo::OperatingSystemVersion();
579
580 // Write the XML version.
initial.commit09911bf2008-07-26 23:55:29581 OPEN_ELEMENT_FOR_SCOPE("os");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31582 WriteAttribute("name", os_name);
583 WriteAttribute("version", os_version);
584
585 // Write the protobuf version.
586 SystemProfileProto::OS* os = system_profile->mutable_os();
587 os->set_name(os_name);
588 os->set_version(os_version);
initial.commit09911bf2008-07-26 23:55:29589 }
590
591 {
rlp@chromium.orge8c287c872010-07-20 00:49:42592 OPEN_ELEMENT_FOR_SCOPE("gpu");
isherman@chromium.orgb2a4812d2012-02-28 05:31:31593 const content::GPUInfo& gpu_info =
594 GpuDataManager::GetInstance()->GetGPUInfo();
595 GpuPerformanceStats gpu_performance_stats =
596 GpuPerformanceStats::RetrieveGpuPerformanceStats();
597
598 // Write the XML version.
jam@chromium.org79078df2012-02-16 01:22:32599 WriteIntAttribute("vendorid", gpu_info.vendor_id);
600 WriteIntAttribute("deviceid", gpu_info.device_id);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31601
602 // Write the protobuf version.
603 SystemProfileProto::Hardware::Graphics* gpu = hardware->mutable_gpu();
604 gpu->set_vendor_id(gpu_info.vendor_id);
605 gpu->set_device_id(gpu_info.device_id);
606 gpu->set_driver_version(gpu_info.driver_version);
607 gpu->set_driver_date(gpu_info.driver_date);
608 SystemProfileProto::Hardware::Graphics::PerformanceStatistics*
609 gpu_performance = gpu->mutable_performance_statistics();
610 gpu_performance->set_graphics_score(gpu_performance_stats.graphics);
611 gpu_performance->set_gaming_score(gpu_performance_stats.gaming);
612 gpu_performance->set_overall_score(gpu_performance_stats.overall);
rlp@chromium.orge8c287c872010-07-20 00:49:42613 }
614
615 {
derat@chromium.org6b7d954ff2011-10-25 00:39:35616 const gfx::Size display_size = gfx::Screen::GetPrimaryMonitorSize();
isherman@chromium.orgb2a4812d2012-02-28 05:31:31617 int display_width = display_size.width();
618 int display_height = display_size.height();
619 int screen_count = gfx::Screen::GetNumMonitors();
620
621 // Write the XML version.
622 OPEN_ELEMENT_FOR_SCOPE("display");
623 WriteIntAttribute("xsize", display_width);
624 WriteIntAttribute("ysize", display_height);
625 WriteIntAttribute("screens", screen_count);
626
627 // Write the protobuf version.
628 hardware->set_primary_screen_width(display_width);
629 hardware->set_primary_screen_height(display_height);
630 hardware->set_screen_count(screen_count);
initial.commit09911bf2008-07-26 23:55:29631 }
632
633 {
634 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
635 int num_bookmarks_on_bookmark_bar =
636 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
637 int num_folders_on_bookmark_bar =
638 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
639 int num_bookmarks_in_other_bookmarks_folder =
640 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
641 int num_folders_in_other_bookmarks_folder =
642 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
643 {
644 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
645 WriteAttribute("name", "full-tree");
646 WriteIntAttribute("foldercount",
647 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
648 WriteIntAttribute("itemcount",
649 num_bookmarks_on_bookmark_bar +
650 num_bookmarks_in_other_bookmarks_folder);
651 }
652 {
653 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
654 WriteAttribute("name", "toolbar");
655 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
656 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
657 }
658 }
659
660 {
661 OPEN_ELEMENT_FOR_SCOPE("keywords");
662 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
663 }
664
665 if (profile_metrics)
666 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29667}
668
669void MetricsLog::WriteAllProfilesMetrics(
670 const DictionaryValue& all_profiles_metrics) {
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:51671 const std::string profile_prefix(prefs::kProfilePrefix);
initial.commit09911bf2008-07-26 23:55:29672 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
673 i != all_profiles_metrics.end_keys(); ++i) {
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47674 const std::string& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29675 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
676 DictionaryValue* profile;
pkasting@chromium.org4dad9ad82009-11-25 20:47:52677 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
678 &profile))
initial.commit09911bf2008-07-26 23:55:29679 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
680 }
681 }
682}
683
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47684void MetricsLog::WriteProfileMetrics(const std::string& profileidhash,
initial.commit09911bf2008-07-26 23:55:29685 const DictionaryValue& profile_metrics) {
686 OPEN_ELEMENT_FOR_SCOPE("userprofile");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47687 WriteAttribute("profileidhash", profileidhash);
initial.commit09911bf2008-07-26 23:55:29688 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
689 i != profile_metrics.end_keys(); ++i) {
690 Value* value;
pkasting@chromium.org4dad9ad82009-11-25 20:47:52691 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47692 DCHECK(*i != "id");
initial.commit09911bf2008-07-26 23:55:29693 switch (value->GetType()) {
694 case Value::TYPE_STRING: {
scherkus@chromium.org5e324b72008-12-18 00:07:59695 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29696 if (value->GetAsString(&string_value)) {
697 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47698 WriteAttribute("name", *i);
scherkus@chromium.org5e324b72008-12-18 00:07:59699 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29700 }
701 break;
702 }
703
704 case Value::TYPE_BOOLEAN: {
705 bool bool_value;
706 if (value->GetAsBoolean(&bool_value)) {
707 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47708 WriteAttribute("name", *i);
initial.commit09911bf2008-07-26 23:55:29709 WriteIntAttribute("value", bool_value ? 1 : 0);
710 }
711 break;
712 }
713
714 case Value::TYPE_INTEGER: {
715 int int_value;
716 if (value->GetAsInteger(&int_value)) {
717 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47718 WriteAttribute("name", *i);
initial.commit09911bf2008-07-26 23:55:29719 WriteIntAttribute("value", int_value);
720 }
721 break;
722 }
723
724 default:
725 NOTREACHED();
726 break;
727 }
728 }
729 }
730}
731
732void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
733 DCHECK(!locked_);
734
isherman@chromium.orgb2a4812d2012-02-28 05:31:31735 // Write the XML version.
pkasting@chromium.orgffaf78a2008-11-12 17:38:33736 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29737 WriteAttribute("action", "autocomplete");
738 WriteAttribute("targetidhash", "");
739 // TODO(kochi): Properly track windows.
740 WriteIntAttribute("window", 0);
mpearson@chromium.org6ebc3162011-12-19 13:44:00741 if (log.tab_id != -1) {
742 // If we know what tab the autocomplete URL was opened in, log it.
743 WriteIntAttribute("tab", static_cast<int>(log.tab_id));
744 }
initial.commit09911bf2008-07-26 23:55:29745 WriteCommonEventAttributes();
746
pkasting@chromium.orgffaf78a2008-11-12 17:38:33747 {
748 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29749
pkasting@chromium.orgffaf78a2008-11-12 17:38:33750 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
mpearson@chromium.org0fdc15d2012-01-24 22:32:08751 std::vector<string16> terms;
752 WriteIntAttribute("numterms",
753 static_cast<int>(Tokenize(log.text, kWhitespaceUTF16, &terms)));
pkasting@chromium.orgffaf78a2008-11-12 17:38:33754 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
755 WriteIntAttribute("completedlength",
756 static_cast<int>(log.inline_autocompleted_length));
mpearson@chromium.org9e349762012-01-31 03:24:36757 if (log.elapsed_time_since_user_first_modified_omnibox !=
758 base::TimeDelta::FromMilliseconds(-1)) {
759 // Only upload the typing duration if it is set/valid.
760 WriteInt64Attribute("typingduration",
761 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
762 }
pkasting@chromium.org381e2992008-12-16 01:41:00763 const std::string input_type(
764 AutocompleteInput::TypeToString(log.input_type));
765 if (!input_type.empty())
766 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29767
pkasting@chromium.orgffaf78a2008-11-12 17:38:33768 for (AutocompleteResult::const_iterator i(log.result.begin());
769 i != log.result.end(); ++i) {
770 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
771 if (i->provider)
772 WriteAttribute("provider", i->provider->name());
pkasting@chromium.org381e2992008-12-16 01:41:00773 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
774 if (!result_type.empty())
775 WriteAttribute("resulttype", result_type);
pkasting@chromium.orgffaf78a2008-11-12 17:38:33776 WriteIntAttribute("relevance", i->relevance);
777 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
778 }
initial.commit09911bf2008-07-26 23:55:29779 }
initial.commit09911bf2008-07-26 23:55:29780
isherman@chromium.orgb2a4812d2012-02-28 05:31:31781 // Write the protobuf version.
782 OmniboxEventProto* omnibox_event = uma_proto_.add_omnibox_event();
783 omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
784 if (log.tab_id != -1) {
785 // If we know what tab the autocomplete URL was opened in, log it.
786 omnibox_event->set_tab_id(log.tab_id);
787 }
788 omnibox_event->set_typed_length(log.text.length());
789 omnibox_event->set_selected_index(log.selected_index);
790 omnibox_event->set_completed_length(log.inline_autocompleted_length);
791 omnibox_event->set_input_type(AsOmniboxEventInputType(log.input_type));
792 for (AutocompleteResult::const_iterator i(log.result.begin());
793 i != log.result.end(); ++i) {
794 OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
795 suggestion->set_provider(AsOmniboxEventProviderType(i->provider));
796 suggestion->set_result_type(AsOmniboxEventResultType(i->type));
797 suggestion->set_relevance(i->relevance);
798 suggestion->set_is_starred(i->starred);
799 }
800
initial.commit09911bf2008-07-26 23:55:29801 ++num_events_;
802}