blob: 7170b0347e723196cb20a8c5eab2f49ab86689f3 [file] [log] [blame]
[email protected]1df44b72012-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
[email protected]cd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]1eeb5e02010-07-20 23:02:117#include <string>
8#include <vector>
9
[email protected]d1be67b2008-11-19 20:28:3810#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/file_util.h"
[email protected]054c8012011-11-16 00:12:4212#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
[email protected]85ed9d42010-06-08 22:37:4414#include "base/perftimer.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/string_util.h"
[email protected]fadf97f2008-09-18 12:18:1416#include "base/sys_info.h"
[email protected]37f39e42010-01-06 17:35:1717#include "base/third_party/nspr/prtime.h"
[email protected]1eeb5e02010-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"
[email protected]9ac40092010-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"
[email protected]10084982011-08-19 17:56:5623#include "chrome/browser/plugin_prefs.h"
[email protected]37858e52010-08-26 00:22:0224#include "chrome/browser/prefs/pref_service.h"
[email protected]10084982011-08-19 17:56:5625#include "chrome/browser/profiles/profile_manager.h"
[email protected]1eeb5e02010-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"
[email protected]79078df2012-02-16 01:22:3229#include "content/public/browser/gpu_data_manager.h"
30#include "content/public/common/gpu_info.h"
[email protected]46072d42008-07-28 14:49:3531#include "googleurl/src/gurl.h"
[email protected]6b7d954ff2011-10-25 00:39:3532#include "ui/gfx/screen.h"
[email protected]91d9f3d2011-08-14 05:24:4433#include "webkit/plugins/webplugininfo.h"
initial.commit09911bf2008-07-26 23:55:2934
35#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
36
[email protected]d1be67b2008-11-19 20:28:3837// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
38#if defined(OS_WIN)
39extern "C" IMAGE_DOS_HEADER __ImageBase;
40#endif
41
[email protected]79078df2012-02-16 01:22:3242using content::GpuDataManager;
43
[email protected]1df44b72012-01-19 05:20:3444namespace {
45
46// Returns the date at which the current metrics client ID was created as
47// a string containing milliseconds since the epoch, or "0" if none was found.
48std::string GetInstallDate() {
49 PrefService* pref = g_browser_process->local_state();
50 if (pref) {
51 return pref->GetString(prefs::kMetricsClientIDTimestamp);
52 } else {
53 NOTREACHED();
54 return "0";
55 }
56}
57
58// Returns the plugin preferences corresponding for this user, if available.
59// If multiple user profiles are loaded, returns the preferences corresponding
60// to an arbitrary one of the profiles.
61PluginPrefs* GetPluginPrefs() {
62 ProfileManager* profile_manager = g_browser_process->profile_manager();
63 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
64 if (profiles.empty())
65 return NULL;
66
67 return PluginPrefs::GetForProfile(profiles.front());
68}
69
70} // namespace
71
[email protected]67f92bc32012-01-26 01:56:1972static base::LazyInstance<std::string>::Leaky
[email protected]054c8012011-11-16 00:12:4273 g_version_extension = LAZY_INSTANCE_INITIALIZER;
74
[email protected]1226abb2010-06-10 18:01:2875MetricsLog::MetricsLog(const std::string& client_id, int session_id)
76 : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()) {}
[email protected]5ed7d4572009-12-23 17:42:4177
[email protected]1226abb2010-06-10 18:01:2878MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:2979
80// static
81void MetricsLog::RegisterPrefs(PrefService* local_state) {
82 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2983}
84
[email protected]1df44b72012-01-19 05:20:3485// static
[email protected]9165f742010-03-10 22:55:0186int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
87 base::TimeTicks now = base::TimeTicks::Now();
88 static base::TimeTicks last_updated_time(now);
89 int64 incremental_time = (now - last_updated_time).InSeconds();
90 last_updated_time = now;
91
92 if (incremental_time > 0) {
93 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
94 metrics_uptime += incremental_time;
95 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
96 }
97
98 return incremental_time;
99}
100
[email protected]1226abb2010-06-10 18:01:28101// static
102std::string MetricsLog::GetVersionString() {
[email protected]0211f57e2010-08-27 20:28:42103 chrome::VersionInfo version_info;
[email protected]30c91802010-12-18 00:40:17104 if (!version_info.is_valid()) {
105 NOTREACHED() << "Unable to retrieve version info.";
106 return std::string();
107 }
108
[email protected]0211f57e2010-08-27 20:28:42109 std::string version = version_info.Version();
[email protected]054c8012011-11-16 00:12:42110 if (!version_extension().empty())
111 version += version_extension();
[email protected]0211f57e2010-08-27 20:28:42112 if (!version_info.IsOfficialBuild())
113 version.append("-devel");
114 return version;
[email protected]1226abb2010-06-10 18:01:28115}
116
[email protected]054c8012011-11-16 00:12:42117// static
118void MetricsLog::set_version_extension(const std::string& extension) {
119 g_version_extension.Get() = extension;
120}
121
122// static
123const std::string& MetricsLog::version_extension() {
124 return g_version_extension.Get();
125}
126
[email protected]e324f6b2012-02-28 05:43:37127void MetricsLog::RecordIncrementalStabilityElements() {
[email protected]0b33f80b2008-12-17 21:34:36128 DCHECK(!locked_);
129
130 PrefService* pref = g_browser_process->local_state();
131 DCHECK(pref);
132
[email protected]147bbc0b2009-01-06 19:37:40133 OPEN_ELEMENT_FOR_SCOPE("profile");
134 WriteCommonEventAttributes();
135
[email protected]9958a322011-03-08 20:04:17136 WriteInstallElement();
[email protected]147bbc0b2009-01-06 19:37:40137
138 {
139 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
140 WriteRequiredStabilityAttributes(pref);
141 WriteRealtimeStabilityAttributes(pref);
142
[email protected]e324f6b2012-02-28 05:43:37143 WritePluginStabilityElements(pref);
[email protected]147bbc0b2009-01-06 19:37:40144 }
[email protected]0b33f80b2008-12-17 21:34:36145}
146
[email protected]e324f6b2012-02-28 05:43:37147void MetricsLog::WriteStabilityElement(PrefService* pref) {
initial.commit09911bf2008-07-26 23:55:29148 DCHECK(!locked_);
149
[email protected]e324f6b2012-02-28 05:43:37150 DCHECK(pref);
151
initial.commit09911bf2008-07-26 23:55:29152 // Get stability attributes out of Local State, zeroing out stored values.
153 // NOTE: This could lead to some data loss if this report isn't successfully
154 // sent, but that's true for all the metrics.
155
[email protected]ffaf78a2008-11-12 17:38:33156 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40157 WriteRequiredStabilityAttributes(pref);
158 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29159
[email protected]b2a4812d2012-02-28 05:31:31160 // TODO(jar): The following are all optional, so we *could* optimize them for
161 // values of zero (and not include them).
[email protected]e324f6b2012-02-28 05:43:37162 WriteIntAttribute("incompleteshutdowncount",
163 pref->GetInteger(
164 prefs::kStabilityIncompleteSessionEndCount));
165 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]b2a4812d2012-02-28 05:31:31166
[email protected]e324f6b2012-02-28 05:43:37167
[email protected]b2a4812d2012-02-28 05:31:31168 WriteIntAttribute("breakpadregistrationok",
[email protected]e324f6b2012-02-28 05:43:37169 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
170 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
[email protected]b2a4812d2012-02-28 05:31:31171 WriteIntAttribute("breakpadregistrationfail",
[email protected]e324f6b2012-02-28 05:43:37172 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
173 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
174 WriteIntAttribute("debuggerpresent",
175 pref->GetInteger(prefs::kStabilityDebuggerPresent));
176 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
177 WriteIntAttribute("debuggernotpresent",
178 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
179 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
[email protected]b2a4812d2012-02-28 05:31:31180
[email protected]e324f6b2012-02-28 05:43:37181 WritePluginStabilityElements(pref);
[email protected]147bbc0b2009-01-06 19:37:40182}
183
[email protected]e324f6b2012-02-28 05:43:37184void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
[email protected]147bbc0b2009-01-06 19:37:40185 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29186 const ListValue* plugin_stats_list = pref->GetList(
187 prefs::kStabilityPluginStats);
[email protected]1df44b72012-01-19 05:20:34188 if (!plugin_stats_list)
189 return;
initial.commit09911bf2008-07-26 23:55:29190
[email protected]1df44b72012-01-19 05:20:34191 OPEN_ELEMENT_FOR_SCOPE("plugins");
192 for (ListValue::const_iterator iter = plugin_stats_list->begin();
193 iter != plugin_stats_list->end(); ++iter) {
194 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
195 NOTREACHED();
196 continue;
initial.commit09911bf2008-07-26 23:55:29197 }
[email protected]1df44b72012-01-19 05:20:34198 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29199
[email protected]1df44b72012-01-19 05:20:34200 std::string plugin_name;
201 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
202
203 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
204 // Use "filename" instead of "name", otherwise we need to update the
205 // UMA servers.
[email protected]e324f6b2012-02-28 05:43:37206 WriteAttribute("filename", CreateBase64Hash(plugin_name));
[email protected]1df44b72012-01-19 05:20:34207
208 int launches = 0;
209 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
210 WriteIntAttribute("launchcount", launches);
211
212 int instances = 0;
213 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
214 WriteIntAttribute("instancecount", instances);
215
216 int crashes = 0;
217 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
218 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29219 }
[email protected]1df44b72012-01-19 05:20:34220
221 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29222}
223
[email protected]147bbc0b2009-01-06 19:37:40224void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]e324f6b2012-02-28 05:43:37225 // The server refuses data that doesn't have certain values. crashcount and
226 // launchcount are currently "required" in the "stability" group.
227 WriteIntAttribute("launchcount",
228 pref->GetInteger(prefs::kStabilityLaunchCount));
[email protected]0b33f80b2008-12-17 21:34:36229 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
[email protected]e324f6b2012-02-28 05:43:37230 WriteIntAttribute("crashcount",
231 pref->GetInteger(prefs::kStabilityCrashCount));
[email protected]0b33f80b2008-12-17 21:34:36232 pref->SetInteger(prefs::kStabilityCrashCount, 0);
233}
234
[email protected]147bbc0b2009-01-06 19:37:40235void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36236 // Update the stats which are critical for real-time stability monitoring.
237 // Since these are "optional," only list ones that are non-zero, as the counts
238 // are aggergated (summed) server side.
239
240 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
241 if (count) {
242 WriteIntAttribute("pageloadcount", count);
243 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
244 }
245
246 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
247 if (count) {
248 WriteIntAttribute("renderercrashcount", count);
249 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
250 }
251
[email protected]1f085622009-12-04 05:33:45252 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
253 if (count) {
254 WriteIntAttribute("extensionrenderercrashcount", count);
255 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
256 }
257
[email protected]0b33f80b2008-12-17 21:34:36258 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
259 if (count) {
260 WriteIntAttribute("rendererhangcount", count);
261 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
262 }
[email protected]1f085622009-12-04 05:33:45263
264 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
265 if (count) {
266 WriteIntAttribute("childprocesscrashcount", count);
267 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
268 }
[email protected]9165f742010-03-10 22:55:01269
[email protected]c1834a92011-01-21 18:21:03270#if defined(OS_CHROMEOS)
271 count = pref->GetInteger(prefs::kStabilityOtherUserCrashCount);
272 if (count) {
[email protected]e324f6b2012-02-28 05:43:37273 // TODO(kmixter): Write attribute once log server supports it
274 // and remove warning log.
275 // WriteIntAttribute("otherusercrashcount", count);
276 LOG(WARNING) << "Not yet able to send otherusercrashcount="
277 << count;
[email protected]c1834a92011-01-21 18:21:03278 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 0);
279 }
280
281 count = pref->GetInteger(prefs::kStabilityKernelCrashCount);
282 if (count) {
[email protected]e324f6b2012-02-28 05:43:37283 // TODO(kmixter): Write attribute once log server supports it
284 // and remove warning log.
285 // WriteIntAttribute("kernelcrashcount", count);
286 LOG(WARNING) << "Not yet able to send kernelcrashcount="
287 << count;
[email protected]c1834a92011-01-21 18:21:03288 pref->SetInteger(prefs::kStabilityKernelCrashCount, 0);
289 }
290
291 count = pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount);
292 if (count) {
[email protected]e324f6b2012-02-28 05:43:37293 // TODO(kmixter): Write attribute once log server supports it
294 // and remove warning log.
295 // WriteIntAttribute("systemuncleanshutdowns", count);
296 LOG(WARNING) << "Not yet able to send systemuncleanshutdowns="
297 << count;
[email protected]c1834a92011-01-21 18:21:03298 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 0);
299 }
300#endif // OS_CHROMEOS
301
[email protected]9165f742010-03-10 22:55:01302 int64 recent_duration = GetIncrementalUptime(pref);
[email protected]e324f6b2012-02-28 05:43:37303 if (recent_duration)
[email protected]9165f742010-03-10 22:55:01304 WriteInt64Attribute("uptimesec", recent_duration);
[email protected]0b33f80b2008-12-17 21:34:36305}
306
initial.commit09911bf2008-07-26 23:55:29307void MetricsLog::WritePluginList(
[email protected]91d9f3d2011-08-14 05:24:44308 const std::vector<webkit::WebPluginInfo>& plugin_list) {
initial.commit09911bf2008-07-26 23:55:29309 DCHECK(!locked_);
310
[email protected]1df44b72012-01-19 05:20:34311 PluginPrefs* plugin_prefs = GetPluginPrefs();
[email protected]10084982011-08-19 17:56:56312
[email protected]ffaf78a2008-11-12 17:38:33313 OPEN_ELEMENT_FOR_SCOPE("plugins");
[email protected]e324f6b2012-02-28 05:43:37314
[email protected]91d9f3d2011-08-14 05:24:44315 for (std::vector<webkit::WebPluginInfo>::const_iterator iter =
[email protected]191eb3f72010-12-21 06:27:50316 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29317 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33318 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29319
320 // Plugin name and filename are hashed for the privacy of those
321 // testing unreleased new extensions.
[email protected]e324f6b2012-02-28 05:43:37322 WriteAttribute("name", CreateBase64Hash(UTF16ToUTF8(iter->name)));
323 std::string filename_bytes =
324#if defined(OS_WIN)
325 UTF16ToUTF8(iter->path.BaseName().value());
326#else
327 iter->path.BaseName().value();
328#endif
329 WriteAttribute("filename", CreateBase64Hash(filename_bytes));
[email protected]c9d811372010-06-23 21:44:57330 WriteAttribute("version", UTF16ToUTF8(iter->version));
[email protected]24c935e52011-09-01 12:06:26331 if (plugin_prefs)
332 WriteIntAttribute("disabled", !plugin_prefs->IsPluginEnabled(*iter));
initial.commit09911bf2008-07-26 23:55:29333 }
initial.commit09911bf2008-07-26 23:55:29334}
335
[email protected]147bbc0b2009-01-06 19:37:40336void MetricsLog::WriteInstallElement() {
337 OPEN_ELEMENT_FOR_SCOPE("install");
[email protected]e324f6b2012-02-28 05:43:37338 WriteAttribute("installdate", GetInstallDate());
[email protected]147bbc0b2009-01-06 19:37:40339 WriteIntAttribute("buildid", 0); // We're using appversion instead.
[email protected]147bbc0b2009-01-06 19:37:40340}
341
initial.commit09911bf2008-07-26 23:55:29342void MetricsLog::RecordEnvironment(
[email protected]91d9f3d2011-08-14 05:24:44343 const std::vector<webkit::WebPluginInfo>& plugin_list,
initial.commit09911bf2008-07-26 23:55:29344 const DictionaryValue* profile_metrics) {
345 DCHECK(!locked_);
346
347 PrefService* pref = g_browser_process->local_state();
348
[email protected]ffaf78a2008-11-12 17:38:33349 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29350 WriteCommonEventAttributes();
351
[email protected]147bbc0b2009-01-06 19:37:40352 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29353
354 WritePluginList(plugin_list);
355
[email protected]e324f6b2012-02-28 05:43:37356 WriteStabilityElement(pref);
initial.commit09911bf2008-07-26 23:55:29357
358 {
359 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]e324f6b2012-02-28 05:43:37360 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29361 }
362
363 {
initial.commit09911bf2008-07-26 23:55:29364 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]e324f6b2012-02-28 05:43:37365 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38366#if defined(OS_WIN)
367 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
368#endif
initial.commit09911bf2008-07-26 23:55:29369 }
370
371 {
372 OPEN_ELEMENT_FOR_SCOPE("os");
[email protected]e324f6b2012-02-28 05:43:37373 WriteAttribute("name",
374 base::SysInfo::OperatingSystemName());
375 WriteAttribute("version",
376 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29377 }
378
379 {
[email protected]e8c287c872010-07-20 00:49:42380 OPEN_ELEMENT_FOR_SCOPE("gpu");
[email protected]e324f6b2012-02-28 05:43:37381 content::GPUInfo gpu_info = GpuDataManager::GetInstance()->GetGPUInfo();
[email protected]79078df2012-02-16 01:22:32382 WriteIntAttribute("vendorid", gpu_info.vendor_id);
383 WriteIntAttribute("deviceid", gpu_info.device_id);
[email protected]e8c287c872010-07-20 00:49:42384 }
385
386 {
[email protected]b2a4812d2012-02-28 05:31:31387 OPEN_ELEMENT_FOR_SCOPE("display");
[email protected]e324f6b2012-02-28 05:43:37388 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) {
[email protected]57ecc4b2010-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) {
[email protected]e7b418b2010-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;
[email protected]4dad9ad82009-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
[email protected]e7b418b2010-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");
[email protected]e7b418b2010-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;
[email protected]4dad9ad82009-11-25 20:47:52452 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]e7b418b2010-07-30 19:47:47453 DCHECK(*i != "id");
initial.commit09911bf2008-07-26 23:55:29454 switch (value->GetType()) {
455 case Value::TYPE_STRING: {
[email protected]5e324b72008-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");
[email protected]e7b418b2010-07-30 19:47:47459 WriteAttribute("name", *i);
[email protected]5e324b72008-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");
[email protected]e7b418b2010-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");
[email protected]e7b418b2010-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
[email protected]ffaf78a2008-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);
[email protected]6ebc3162011-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
[email protected]ffaf78a2008-11-12 17:38:33507 {
508 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29509
[email protected]ffaf78a2008-11-12 17:38:33510 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
[email protected]0fdc15d2012-01-24 22:32:08511 std::vector<string16> terms;
512 WriteIntAttribute("numterms",
513 static_cast<int>(Tokenize(log.text, kWhitespaceUTF16, &terms)));
[email protected]ffaf78a2008-11-12 17:38:33514 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
515 WriteIntAttribute("completedlength",
516 static_cast<int>(log.inline_autocompleted_length));
[email protected]9e349762012-01-31 03:24:36517 if (log.elapsed_time_since_user_first_modified_omnibox !=
518 base::TimeDelta::FromMilliseconds(-1)) {
519 // Only upload the typing duration if it is set/valid.
520 WriteInt64Attribute("typingduration",
521 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
522 }
[email protected]381e2992008-12-16 01:41:00523 const std::string input_type(
524 AutocompleteInput::TypeToString(log.input_type));
525 if (!input_type.empty())
526 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29527
[email protected]ffaf78a2008-11-12 17:38:33528 for (AutocompleteResult::const_iterator i(log.result.begin());
529 i != log.result.end(); ++i) {
530 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
531 if (i->provider)
532 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00533 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
534 if (!result_type.empty())
535 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33536 WriteIntAttribute("relevance", i->relevance);
537 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
538 }
initial.commit09911bf2008-07-26 23:55:29539 }
initial.commit09911bf2008-07-26 23:55:29540
initial.commit09911bf2008-07-26 23:55:29541 ++num_events_;
542}