blob: b5504214a19d4ae78d1e8807b4458d33fe4bdf34 [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"
initial.commit09911bf2008-07-26 23:55:2915#include "base/string_util.h"
deanm@chromium.orgfadf97f2008-09-18 12:18:1416#include "base/sys_info.h"
jar@chromium.org37f39e42010-01-06 17:35:1717#include "base/third_party/nspr/prtime.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1118#include "base/time.h"
19#include "base/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/autocomplete/autocomplete.h"
scr@chromium.org9ac40092010-10-27 23:05:2621#include "chrome/browser/autocomplete/autocomplete_match.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/browser_process.h"
bauerb@chromium.org10084982011-08-19 17:56:5623#include "chrome/browser/plugin_prefs.h"
evan@chromium.org37858e52010-08-26 00:22:0224#include "chrome/browser/prefs/pref_service.h"
bauerb@chromium.org10084982011-08-19 17:56:5625#include "chrome/browser/profiles/profile_manager.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1126#include "chrome/common/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/logging_chrome.h"
28#include "chrome/common/pref_names.h"
apatrick@chromium.orgd9f37932011-05-09 20:09:2429#include "content/browser/gpu/gpu_data_manager.h"
deanm@google.com46072d42008-07-28 14:49:3530#include "googleurl/src/gurl.h"
derat@chromium.org6b7d954ff2011-10-25 00:39:3531#include "ui/gfx/screen.h"
cpu@chromium.org91d9f3d2011-08-14 05:24:4432#include "webkit/plugins/webplugininfo.h"
initial.commit09911bf2008-07-26 23:55:2933
34#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
35
deanm@chromium.orgd1be67b2008-11-19 20:28:3836// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
37#if defined(OS_WIN)
38extern "C" IMAGE_DOS_HEADER __ImageBase;
39#endif
40
isherman@chromium.org1df44b72012-01-19 05:20:3441namespace {
42
43// Returns the date at which the current metrics client ID was created as
44// a string containing milliseconds since the epoch, or "0" if none was found.
45std::string GetInstallDate() {
46 PrefService* pref = g_browser_process->local_state();
47 if (pref) {
48 return pref->GetString(prefs::kMetricsClientIDTimestamp);
49 } else {
50 NOTREACHED();
51 return "0";
52 }
53}
54
55// Returns the plugin preferences corresponding for this user, if available.
56// If multiple user profiles are loaded, returns the preferences corresponding
57// to an arbitrary one of the profiles.
58PluginPrefs* GetPluginPrefs() {
59 ProfileManager* profile_manager = g_browser_process->profile_manager();
60 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
61 if (profiles.empty())
62 return NULL;
63
64 return PluginPrefs::GetForProfile(profiles.front());
65}
66
67} // namespace
68
thakis@chromium.org054c8012011-11-16 00:12:4269static base::LazyInstance<std::string,
70 base::LeakyLazyInstanceTraits<std::string > >
71 g_version_extension = LAZY_INSTANCE_INITIALIZER;
72
ananta@chromium.org1226abb2010-06-10 18:01:2873MetricsLog::MetricsLog(const std::string& client_id, int session_id)
74 : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()) {}
jar@chromium.org5ed7d4572009-12-23 17:42:4175
ananta@chromium.org1226abb2010-06-10 18:01:2876MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:2977
78// static
79void MetricsLog::RegisterPrefs(PrefService* local_state) {
80 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2981}
82
isherman@chromium.org1df44b72012-01-19 05:20:3483// static
jar@chromium.org9165f742010-03-10 22:55:0184int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
85 base::TimeTicks now = base::TimeTicks::Now();
86 static base::TimeTicks last_updated_time(now);
87 int64 incremental_time = (now - last_updated_time).InSeconds();
88 last_updated_time = now;
89
90 if (incremental_time > 0) {
91 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
92 metrics_uptime += incremental_time;
93 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
94 }
95
96 return incremental_time;
97}
98
ananta@chromium.org1226abb2010-06-10 18:01:2899// static
100std::string MetricsLog::GetVersionString() {
evan@chromium.org0211f57e2010-08-27 20:28:42101 chrome::VersionInfo version_info;
dmaclach@chromium.org30c91802010-12-18 00:40:17102 if (!version_info.is_valid()) {
103 NOTREACHED() << "Unable to retrieve version info.";
104 return std::string();
105 }
106
evan@chromium.org0211f57e2010-08-27 20:28:42107 std::string version = version_info.Version();
thakis@chromium.org054c8012011-11-16 00:12:42108 if (!version_extension().empty())
109 version += version_extension();
evan@chromium.org0211f57e2010-08-27 20:28:42110 if (!version_info.IsOfficialBuild())
111 version.append("-devel");
112 return version;
ananta@chromium.org1226abb2010-06-10 18:01:28113}
114
thakis@chromium.org054c8012011-11-16 00:12:42115// static
116void MetricsLog::set_version_extension(const std::string& extension) {
117 g_version_extension.Get() = extension;
118}
119
120// static
121const std::string& MetricsLog::version_extension() {
122 return g_version_extension.Get();
123}
124
jar@google.com0b33f80b2008-12-17 21:34:36125void MetricsLog::RecordIncrementalStabilityElements() {
126 DCHECK(!locked_);
127
128 PrefService* pref = g_browser_process->local_state();
129 DCHECK(pref);
130
jar@google.com147bbc0b2009-01-06 19:37:40131 OPEN_ELEMENT_FOR_SCOPE("profile");
132 WriteCommonEventAttributes();
133
jar@chromium.org9958a322011-03-08 20:04:17134 WriteInstallElement();
jar@google.com147bbc0b2009-01-06 19:37:40135
136 {
137 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
138 WriteRequiredStabilityAttributes(pref);
139 WriteRealtimeStabilityAttributes(pref);
140
141 WritePluginStabilityElements(pref);
142 }
jar@google.com0b33f80b2008-12-17 21:34:36143}
144
petkov@chromium.orgc1834a92011-01-21 18:21:03145void MetricsLog::WriteStabilityElement(PrefService* pref) {
initial.commit09911bf2008-07-26 23:55:29146 DCHECK(!locked_);
147
initial.commit09911bf2008-07-26 23:55:29148 DCHECK(pref);
149
150 // Get stability attributes out of Local State, zeroing out stored values.
151 // NOTE: This could lead to some data loss if this report isn't successfully
152 // sent, but that's true for all the metrics.
153
pkasting@chromium.orgffaf78a2008-11-12 17:38:33154 OPEN_ELEMENT_FOR_SCOPE("stability");
jar@google.com147bbc0b2009-01-06 19:37:40155 WriteRequiredStabilityAttributes(pref);
156 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29157
jar@google.com0b33f80b2008-12-17 21:34:36158 // TODO(jar): The following are all optional, so we *could* optimize them for
159 // values of zero (and not include them).
evanm@google.com8e674e42008-07-30 16:05:48160 WriteIntAttribute("incompleteshutdowncount",
161 pref->GetInteger(
162 prefs::kStabilityIncompleteSessionEndCount));
163 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
jar@google.com0b33f80b2008-12-17 21:34:36164
165
cpu@google.come73c01972008-08-13 00:18:24166 WriteIntAttribute("breakpadregistrationok",
167 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
168 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
169 WriteIntAttribute("breakpadregistrationfail",
170 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
171 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
172 WriteIntAttribute("debuggerpresent",
173 pref->GetInteger(prefs::kStabilityDebuggerPresent));
174 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
175 WriteIntAttribute("debuggernotpresent",
176 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
177 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29178
jar@google.com147bbc0b2009-01-06 19:37:40179 WritePluginStabilityElements(pref);
180}
181
182void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
183 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29184 const ListValue* plugin_stats_list = pref->GetList(
185 prefs::kStabilityPluginStats);
isherman@chromium.org1df44b72012-01-19 05:20:34186 if (!plugin_stats_list)
187 return;
initial.commit09911bf2008-07-26 23:55:29188
isherman@chromium.org1df44b72012-01-19 05:20:34189 OPEN_ELEMENT_FOR_SCOPE("plugins");
190 for (ListValue::const_iterator iter = plugin_stats_list->begin();
191 iter != plugin_stats_list->end(); ++iter) {
192 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
193 NOTREACHED();
194 continue;
initial.commit09911bf2008-07-26 23:55:29195 }
isherman@chromium.org1df44b72012-01-19 05:20:34196 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29197
isherman@chromium.org1df44b72012-01-19 05:20:34198 std::string plugin_name;
199 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
200
201 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
202 // Use "filename" instead of "name", otherwise we need to update the
203 // UMA servers.
204 WriteAttribute("filename", CreateBase64Hash(plugin_name));
205
206 int launches = 0;
207 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
208 WriteIntAttribute("launchcount", launches);
209
210 int instances = 0;
211 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
212 WriteIntAttribute("instancecount", instances);
213
214 int crashes = 0;
215 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
216 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29217 }
isherman@chromium.org1df44b72012-01-19 05:20:34218
219 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29220}
221
jar@google.com147bbc0b2009-01-06 19:37:40222void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
jar@google.com0b33f80b2008-12-17 21:34:36223 // The server refuses data that doesn't have certain values. crashcount and
224 // launchcount are currently "required" in the "stability" group.
225 WriteIntAttribute("launchcount",
226 pref->GetInteger(prefs::kStabilityLaunchCount));
227 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
228 WriteIntAttribute("crashcount",
229 pref->GetInteger(prefs::kStabilityCrashCount));
230 pref->SetInteger(prefs::kStabilityCrashCount, 0);
231}
232
jar@google.com147bbc0b2009-01-06 19:37:40233void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
jar@google.com0b33f80b2008-12-17 21:34:36234 // Update the stats which are critical for real-time stability monitoring.
235 // Since these are "optional," only list ones that are non-zero, as the counts
236 // are aggergated (summed) server side.
237
238 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
239 if (count) {
240 WriteIntAttribute("pageloadcount", count);
241 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
242 }
243
244 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
245 if (count) {
246 WriteIntAttribute("renderercrashcount", count);
247 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
248 }
249
asargent@chromium.org1f085622009-12-04 05:33:45250 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
251 if (count) {
252 WriteIntAttribute("extensionrenderercrashcount", count);
253 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
254 }
255
jar@google.com0b33f80b2008-12-17 21:34:36256 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
257 if (count) {
258 WriteIntAttribute("rendererhangcount", count);
259 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
260 }
asargent@chromium.org1f085622009-12-04 05:33:45261
262 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
263 if (count) {
264 WriteIntAttribute("childprocesscrashcount", count);
265 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
266 }
jar@chromium.org9165f742010-03-10 22:55:01267
petkov@chromium.orgc1834a92011-01-21 18:21:03268#if defined(OS_CHROMEOS)
269 count = pref->GetInteger(prefs::kStabilityOtherUserCrashCount);
270 if (count) {
271 // TODO(kmixter): Write attribute once log server supports it
272 // and remove warning log.
273 // WriteIntAttribute("otherusercrashcount", count);
274 LOG(WARNING) << "Not yet able to send otherusercrashcount="
275 << count;
276 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 0);
277 }
278
279 count = pref->GetInteger(prefs::kStabilityKernelCrashCount);
280 if (count) {
281 // TODO(kmixter): Write attribute once log server supports it
282 // and remove warning log.
283 // WriteIntAttribute("kernelcrashcount", count);
284 LOG(WARNING) << "Not yet able to send kernelcrashcount="
285 << count;
286 pref->SetInteger(prefs::kStabilityKernelCrashCount, 0);
287 }
288
289 count = pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount);
290 if (count) {
291 // TODO(kmixter): Write attribute once log server supports it
292 // and remove warning log.
293 // WriteIntAttribute("systemuncleanshutdowns", count);
294 LOG(WARNING) << "Not yet able to send systemuncleanshutdowns="
295 << count;
296 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 0);
297 }
298#endif // OS_CHROMEOS
299
jar@chromium.org9165f742010-03-10 22:55:01300 int64 recent_duration = GetIncrementalUptime(pref);
301 if (recent_duration)
302 WriteInt64Attribute("uptimesec", recent_duration);
jar@google.com0b33f80b2008-12-17 21:34:36303}
304
initial.commit09911bf2008-07-26 23:55:29305void MetricsLog::WritePluginList(
cpu@chromium.org91d9f3d2011-08-14 05:24:44306 const std::vector<webkit::WebPluginInfo>& plugin_list) {
initial.commit09911bf2008-07-26 23:55:29307 DCHECK(!locked_);
308
isherman@chromium.org1df44b72012-01-19 05:20:34309 PluginPrefs* plugin_prefs = GetPluginPrefs();
bauerb@chromium.org10084982011-08-19 17:56:56310
pkasting@chromium.orgffaf78a2008-11-12 17:38:33311 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29312
cpu@chromium.org91d9f3d2011-08-14 05:24:44313 for (std::vector<webkit::WebPluginInfo>::const_iterator iter =
brettw@chromium.org191eb3f72010-12-21 06:27:50314 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29315 iter != plugin_list.end(); ++iter) {
pkasting@chromium.orgffaf78a2008-11-12 17:38:33316 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29317
318 // Plugin name and filename are hashed for the privacy of those
319 // testing unreleased new extensions.
stuartmorgan@chromium.orgc9d811372010-06-23 21:44:57320 WriteAttribute("name", CreateBase64Hash(UTF16ToUTF8(iter->name)));
evan@chromium.org0abb1652011-02-07 23:37:55321 std::string filename_bytes =
322#if defined(OS_WIN)
323 UTF16ToUTF8(iter->path.BaseName().value());
324#else
325 iter->path.BaseName().value();
326#endif
327 WriteAttribute("filename", CreateBase64Hash(filename_bytes));
stuartmorgan@chromium.orgc9d811372010-06-23 21:44:57328 WriteAttribute("version", UTF16ToUTF8(iter->version));
bauerb@chromium.org24c935e52011-09-01 12:06:26329 if (plugin_prefs)
330 WriteIntAttribute("disabled", !plugin_prefs->IsPluginEnabled(*iter));
initial.commit09911bf2008-07-26 23:55:29331 }
initial.commit09911bf2008-07-26 23:55:29332}
333
jar@google.com147bbc0b2009-01-06 19:37:40334void MetricsLog::WriteInstallElement() {
335 OPEN_ELEMENT_FOR_SCOPE("install");
336 WriteAttribute("installdate", GetInstallDate());
337 WriteIntAttribute("buildid", 0); // We're using appversion instead.
jar@google.com147bbc0b2009-01-06 19:37:40338}
339
initial.commit09911bf2008-07-26 23:55:29340void MetricsLog::RecordEnvironment(
cpu@chromium.org91d9f3d2011-08-14 05:24:44341 const std::vector<webkit::WebPluginInfo>& plugin_list,
initial.commit09911bf2008-07-26 23:55:29342 const DictionaryValue* profile_metrics) {
343 DCHECK(!locked_);
344
345 PrefService* pref = g_browser_process->local_state();
346
pkasting@chromium.orgffaf78a2008-11-12 17:38:33347 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29348 WriteCommonEventAttributes();
349
jar@google.com147bbc0b2009-01-06 19:37:40350 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29351
352 WritePluginList(plugin_list);
353
petkov@chromium.orgc1834a92011-01-21 18:21:03354 WriteStabilityElement(pref);
initial.commit09911bf2008-07-26 23:55:29355
356 {
357 OPEN_ELEMENT_FOR_SCOPE("cpu");
mark@chromium.org05f9b682008-09-29 22:18:01358 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29359 }
360
361 {
initial.commit09911bf2008-07-26 23:55:29362 OPEN_ELEMENT_FOR_SCOPE("memory");
deanm@chromium.orged6fc352008-09-18 12:44:40363 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
deanm@chromium.orgd1be67b2008-11-19 20:28:38364#if defined(OS_WIN)
365 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
366#endif
initial.commit09911bf2008-07-26 23:55:29367 }
368
369 {
370 OPEN_ELEMENT_FOR_SCOPE("os");
371 WriteAttribute("name",
mark@chromium.org05f9b682008-09-29 22:18:01372 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29373 WriteAttribute("version",
mark@chromium.org05f9b682008-09-29 22:18:01374 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29375 }
376
377 {
rlp@chromium.orge8c287c872010-07-20 00:49:42378 OPEN_ELEMENT_FOR_SCOPE("gpu");
zmo@google.com27d8778e2011-03-09 22:00:59379 GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance();
380 if (gpu_data_manager) {
381 WriteIntAttribute("vendorid", gpu_data_manager->gpu_info().vendor_id);
382 WriteIntAttribute("deviceid", gpu_data_manager->gpu_info().device_id);
apatrick@chromium.org06b2fc92011-02-22 22:00:40383 }
rlp@chromium.orge8c287c872010-07-20 00:49:42384 }
385
386 {
initial.commit09911bf2008-07-26 23:55:29387 OPEN_ELEMENT_FOR_SCOPE("display");
derat@chromium.org6b7d954ff2011-10-25 00:39:35388 const gfx::Size display_size = gfx::Screen::GetPrimaryMonitorSize();
389 WriteIntAttribute("xsize", display_size.width());
390 WriteIntAttribute("ysize", display_size.height());
391 WriteIntAttribute("screens", gfx::Screen::GetNumMonitors());
initial.commit09911bf2008-07-26 23:55:29392 }
393
394 {
395 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
396 int num_bookmarks_on_bookmark_bar =
397 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
398 int num_folders_on_bookmark_bar =
399 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
400 int num_bookmarks_in_other_bookmarks_folder =
401 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
402 int num_folders_in_other_bookmarks_folder =
403 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
404 {
405 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
406 WriteAttribute("name", "full-tree");
407 WriteIntAttribute("foldercount",
408 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
409 WriteIntAttribute("itemcount",
410 num_bookmarks_on_bookmark_bar +
411 num_bookmarks_in_other_bookmarks_folder);
412 }
413 {
414 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
415 WriteAttribute("name", "toolbar");
416 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
417 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
418 }
419 }
420
421 {
422 OPEN_ELEMENT_FOR_SCOPE("keywords");
423 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
424 }
425
426 if (profile_metrics)
427 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29428}
429
430void MetricsLog::WriteAllProfilesMetrics(
431 const DictionaryValue& all_profiles_metrics) {
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:51432 const std::string profile_prefix(prefs::kProfilePrefix);
initial.commit09911bf2008-07-26 23:55:29433 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
434 i != all_profiles_metrics.end_keys(); ++i) {
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47435 const std::string& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29436 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
437 DictionaryValue* profile;
pkasting@chromium.org4dad9ad82009-11-25 20:47:52438 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
439 &profile))
initial.commit09911bf2008-07-26 23:55:29440 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
441 }
442 }
443}
444
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47445void MetricsLog::WriteProfileMetrics(const std::string& profileidhash,
initial.commit09911bf2008-07-26 23:55:29446 const DictionaryValue& profile_metrics) {
447 OPEN_ELEMENT_FOR_SCOPE("userprofile");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47448 WriteAttribute("profileidhash", profileidhash);
initial.commit09911bf2008-07-26 23:55:29449 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
450 i != profile_metrics.end_keys(); ++i) {
451 Value* value;
pkasting@chromium.org4dad9ad82009-11-25 20:47:52452 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47453 DCHECK(*i != "id");
initial.commit09911bf2008-07-26 23:55:29454 switch (value->GetType()) {
455 case Value::TYPE_STRING: {
scherkus@chromium.org5e324b72008-12-18 00:07:59456 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29457 if (value->GetAsString(&string_value)) {
458 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47459 WriteAttribute("name", *i);
scherkus@chromium.org5e324b72008-12-18 00:07:59460 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29461 }
462 break;
463 }
464
465 case Value::TYPE_BOOLEAN: {
466 bool bool_value;
467 if (value->GetAsBoolean(&bool_value)) {
468 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47469 WriteAttribute("name", *i);
initial.commit09911bf2008-07-26 23:55:29470 WriteIntAttribute("value", bool_value ? 1 : 0);
471 }
472 break;
473 }
474
475 case Value::TYPE_INTEGER: {
476 int int_value;
477 if (value->GetAsInteger(&int_value)) {
478 OPEN_ELEMENT_FOR_SCOPE("profileparam");
viettrungluu@chromium.orge7b418b2010-07-30 19:47:47479 WriteAttribute("name", *i);
initial.commit09911bf2008-07-26 23:55:29480 WriteIntAttribute("value", int_value);
481 }
482 break;
483 }
484
485 default:
486 NOTREACHED();
487 break;
488 }
489 }
490 }
491}
492
493void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
494 DCHECK(!locked_);
495
pkasting@chromium.orgffaf78a2008-11-12 17:38:33496 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29497 WriteAttribute("action", "autocomplete");
498 WriteAttribute("targetidhash", "");
499 // TODO(kochi): Properly track windows.
500 WriteIntAttribute("window", 0);
mpearson@chromium.org6ebc3162011-12-19 13:44:00501 if (log.tab_id != -1) {
502 // If we know what tab the autocomplete URL was opened in, log it.
503 WriteIntAttribute("tab", static_cast<int>(log.tab_id));
504 }
initial.commit09911bf2008-07-26 23:55:29505 WriteCommonEventAttributes();
506
pkasting@chromium.orgffaf78a2008-11-12 17:38:33507 {
508 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29509
pkasting@chromium.orgffaf78a2008-11-12 17:38:33510 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
mpearson@chromium.org0fdc15d2012-01-24 22:32:08511 std::vector<string16> terms;
512 WriteIntAttribute("numterms",
513 static_cast<int>(Tokenize(log.text, kWhitespaceUTF16, &terms)));
pkasting@chromium.orgffaf78a2008-11-12 17:38:33514 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
515 WriteIntAttribute("completedlength",
516 static_cast<int>(log.inline_autocompleted_length));
pkasting@chromium.org381e2992008-12-16 01:41:00517 const std::string input_type(
518 AutocompleteInput::TypeToString(log.input_type));
519 if (!input_type.empty())
520 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29521
pkasting@chromium.orgffaf78a2008-11-12 17:38:33522 for (AutocompleteResult::const_iterator i(log.result.begin());
523 i != log.result.end(); ++i) {
524 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
525 if (i->provider)
526 WriteAttribute("provider", i->provider->name());
pkasting@chromium.org381e2992008-12-16 01:41:00527 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
528 if (!result_type.empty())
529 WriteAttribute("resulttype", result_type);
pkasting@chromium.orgffaf78a2008-11-12 17:38:33530 WriteIntAttribute("relevance", i->relevance);
531 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
532 }
initial.commit09911bf2008-07-26 23:55:29533 }
initial.commit09911bf2008-07-26 23:55:29534
535 ++num_events_;
536}