blob: e4079466a77acab649581b20d7ba2c1817d51013 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
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]d1be67b2008-11-19 20:28:387#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/file_util.h"
9#include "base/file_version_info.h"
10#include "base/md5.h"
11#include "base/scoped_ptr.h"
12#include "base/string_util.h"
[email protected]fadf97f2008-09-18 12:18:1413#include "base/sys_info.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/autocomplete/autocomplete.h"
15#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2916#include "chrome/common/logging_chrome.h"
17#include "chrome/common/pref_names.h"
18#include "chrome/common/pref_service.h"
[email protected]46072d42008-07-28 14:49:3519#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2920#include "net/base/base64.h"
21
22#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
23
[email protected]e1acf6f2008-10-27 20:43:3324using base::Time;
25using base::TimeDelta;
26
[email protected]d1be67b2008-11-19 20:28:3827// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
28#if defined(OS_WIN)
29extern "C" IMAGE_DOS_HEADER __ImageBase;
30#endif
31
initial.commit09911bf2008-07-26 23:55:2932// libxml take xmlChar*, which is unsigned char*
33inline const unsigned char* UnsignedChar(const char* input) {
34 return reinterpret_cast<const unsigned char*>(input);
35}
36
37// static
38void MetricsLog::RegisterPrefs(PrefService* local_state) {
39 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2940}
41
42MetricsLog::MetricsLog(const std::string& client_id, int session_id)
43 : start_time_(Time::Now()),
[email protected]29d02e52008-12-16 16:58:2744 client_id_(client_id),
45 session_id_(IntToString(session_id)),
initial.commit09911bf2008-07-26 23:55:2946 locked_(false),
47 buffer_(NULL),
48 writer_(NULL),
[email protected]29d02e52008-12-16 16:58:2749 num_events_(0) {
initial.commit09911bf2008-07-26 23:55:2950
51 buffer_ = xmlBufferCreate();
52 DCHECK(buffer_);
53
54 writer_ = xmlNewTextWriterMemory(buffer_, 0);
55 DCHECK(writer_);
56
57 int result = xmlTextWriterSetIndent(writer_, 2);
58 DCHECK_EQ(0, result);
59
60 StartElement("log");
61 WriteAttribute("clientid", client_id_);
62
63 DCHECK_GE(result, 0);
64}
65
66MetricsLog::~MetricsLog() {
67 if (writer_)
68 xmlFreeTextWriter(writer_);
69
70 if (buffer_)
71 xmlBufferFree(buffer_);
72}
73
74void MetricsLog::CloseLog() {
75 DCHECK(!locked_);
76 locked_ = true;
77
78 int result = xmlTextWriterEndDocument(writer_);
79 DCHECK(result >= 0);
80
81 result = xmlTextWriterFlush(writer_);
82 DCHECK(result >= 0);
83}
84
85int MetricsLog::GetEncodedLogSize() {
86 DCHECK(locked_);
87 return buffer_->use;
88}
89
90bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
91 DCHECK(locked_);
92 if (buffer_size < GetEncodedLogSize())
93 return false;
94
95 memcpy(buffer, buffer_->content, GetEncodedLogSize());
96 return true;
97}
98
99int MetricsLog::GetElapsedSeconds() {
100 return static_cast<int>((Time::Now() - start_time_).InSeconds());
101}
102
103std::string MetricsLog::CreateHash(const std::string& value) {
104 MD5Context ctx;
105 MD5Init(&ctx);
106 MD5Update(&ctx, value.data(), value.length());
107
108 MD5Digest digest;
109 MD5Final(&digest, &ctx);
110
111 unsigned char reverse[8]; // UMA only uses first 8 chars of hash.
112 DCHECK(arraysize(digest.a) >= arraysize(reverse));
[email protected]29d02e52008-12-16 16:58:27113 for (size_t i = 0; i < arraysize(reverse); ++i)
initial.commit09911bf2008-07-26 23:55:29114 reverse[i] = digest.a[arraysize(reverse) - i - 1];
115 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
116 << *reinterpret_cast<const uint64*>(&reverse[0]) << "]";
117 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
118}
119
120std::string MetricsLog::CreateBase64Hash(const std::string& string) {
121 std::string encoded_digest;
[email protected]a9bb6f692008-07-30 16:40:10122 if (net::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29123 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
124 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29125 }
[email protected]a9bb6f692008-07-30 16:40:10126 return std::string();
initial.commit09911bf2008-07-26 23:55:29127}
128
129void MetricsLog::RecordUserAction(const wchar_t* key) {
130 DCHECK(!locked_);
131
132 std::string command_hash = CreateBase64Hash(WideToUTF8(key));
133 if (command_hash.empty()) {
134 NOTREACHED() << "Unable generate encoded hash of command: " << key;
135 return;
136 }
137
[email protected]ffaf78a2008-11-12 17:38:33138 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29139 WriteAttribute("action", "command");
140 WriteAttribute("targetidhash", command_hash);
141
142 // TODO(jhughes): Properly track windows.
143 WriteIntAttribute("window", 0);
144 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29145
146 ++num_events_;
147}
148
149void MetricsLog::RecordLoadEvent(int window_id,
150 const GURL& url,
151 PageTransition::Type origin,
152 int session_index,
153 TimeDelta load_time) {
154 DCHECK(!locked_);
155
[email protected]ffaf78a2008-11-12 17:38:33156 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29157 WriteAttribute("action", "load");
158 WriteIntAttribute("docid", session_index);
159 WriteIntAttribute("window", window_id);
160 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
161
162 std::string origin_string;
163
164 switch (PageTransition::StripQualifier(origin)) {
165 // TODO(jhughes): Some of these mappings aren't right... we need to add
166 // some values to the server's enum.
167 case PageTransition::LINK:
168 case PageTransition::MANUAL_SUBFRAME:
169 origin_string = "link";
170 break;
171
172 case PageTransition::TYPED:
173 origin_string = "typed";
174 break;
175
176 case PageTransition::AUTO_BOOKMARK:
177 origin_string = "bookmark";
178 break;
179
180 case PageTransition::AUTO_SUBFRAME:
181 case PageTransition::RELOAD:
182 origin_string = "refresh";
183 break;
184
185 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44186 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29187 origin_string = "global-history";
188 break;
189
190 case PageTransition::START_PAGE:
191 origin_string = "start-page";
192 break;
193
194 case PageTransition::FORM_SUBMIT:
195 origin_string = "form-submit";
196 break;
197
198 default:
199 NOTREACHED() << "Received an unknown page transition type: " <<
200 PageTransition::StripQualifier(origin);
201 }
202 if (!origin_string.empty())
203 WriteAttribute("origin", origin_string);
204
205 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29206
207 ++num_events_;
208}
209
initial.commit09911bf2008-07-26 23:55:29210void MetricsLog::RecordWindowEvent(WindowEventType type,
211 int window_id,
212 int parent_id) {
213 DCHECK(!locked_);
214
[email protected]ffaf78a2008-11-12 17:38:33215 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29216 WriteAttribute("action", WindowEventTypeToString(type));
217 WriteAttribute("windowid", IntToString(window_id));
218 if (parent_id >= 0)
219 WriteAttribute("parent", IntToString(parent_id));
220 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29221
222 ++num_events_;
223}
224
225std::string MetricsLog::GetCurrentTimeString() {
226 return Uint64ToString(Time::Now().ToTimeT());
227}
228
229// These are the attributes that are common to every event.
230void MetricsLog::WriteCommonEventAttributes() {
231 WriteAttribute("session", session_id_);
232 WriteAttribute("time", GetCurrentTimeString());
233}
234
235void MetricsLog::WriteAttribute(const std::string& name,
236 const std::string& value) {
237 DCHECK(!locked_);
238 DCHECK(!name.empty());
239
240 int result = xmlTextWriterWriteAttribute(writer_,
241 UnsignedChar(name.c_str()),
242 UnsignedChar(value.c_str()));
243 DCHECK_GE(result, 0);
244}
245
246void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
247 WriteAttribute(name, IntToString(value));
248}
249
250void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
251 WriteAttribute(name, Int64ToString(value));
252}
253
[email protected]ffaf78a2008-11-12 17:38:33254// static
255const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
256 switch (type) {
257 case WINDOW_CREATE: return "create";
258 case WINDOW_OPEN: return "open";
259 case WINDOW_CLOSE: return "close";
260 case WINDOW_DESTROY: return "destroy";
261
262 default:
263 NOTREACHED();
264 return "unknown"; // Can't return NULL as this is used in a required
265 // attribute.
266 }
267}
268
initial.commit09911bf2008-07-26 23:55:29269void MetricsLog::StartElement(const char* name) {
270 DCHECK(!locked_);
271 DCHECK(name);
272
273 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
274 DCHECK_GE(result, 0);
275}
276
277void MetricsLog::EndElement() {
278 DCHECK(!locked_);
279
280 int result = xmlTextWriterEndElement(writer_);
281 DCHECK_GE(result, 0);
282}
283
[email protected]541f77922009-02-23 21:14:38284// static
285std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29286 scoped_ptr<FileVersionInfo> version_info(
287 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
288 if (version_info.get()) {
289 std::string version = WideToUTF8(version_info->product_version());
290 if (!version_info->is_official_build())
291 version.append("-devel");
292 return version;
293 } else {
294 NOTREACHED() << "Unable to retrieve version string.";
295 }
296
297 return std::string();
298}
299
300std::string MetricsLog::GetInstallDate() const {
301 PrefService* pref = g_browser_process->local_state();
302 if (pref) {
303 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
304 } else {
305 NOTREACHED();
306 return "0";
307 }
308}
309
[email protected]0b33f80b2008-12-17 21:34:36310void MetricsLog::RecordIncrementalStabilityElements() {
311 DCHECK(!locked_);
312
313 PrefService* pref = g_browser_process->local_state();
314 DCHECK(pref);
315
[email protected]147bbc0b2009-01-06 19:37:40316 OPEN_ELEMENT_FOR_SCOPE("profile");
317 WriteCommonEventAttributes();
318
319 WriteInstallElement(); // Supply appversion.
320
321 {
322 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
323 WriteRequiredStabilityAttributes(pref);
324 WriteRealtimeStabilityAttributes(pref);
325
326 WritePluginStabilityElements(pref);
327 }
[email protected]0b33f80b2008-12-17 21:34:36328}
329
initial.commit09911bf2008-07-26 23:55:29330void MetricsLog::WriteStabilityElement() {
331 DCHECK(!locked_);
332
333 PrefService* pref = g_browser_process->local_state();
334 DCHECK(pref);
335
336 // Get stability attributes out of Local State, zeroing out stored values.
337 // NOTE: This could lead to some data loss if this report isn't successfully
338 // sent, but that's true for all the metrics.
339
[email protected]ffaf78a2008-11-12 17:38:33340 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40341 WriteRequiredStabilityAttributes(pref);
342 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29343
[email protected]0b33f80b2008-12-17 21:34:36344 // TODO(jar): The following are all optional, so we *could* optimize them for
345 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48346 WriteIntAttribute("incompleteshutdowncount",
347 pref->GetInteger(
348 prefs::kStabilityIncompleteSessionEndCount));
349 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36350
351
[email protected]e73c01972008-08-13 00:18:24352 WriteIntAttribute("breakpadregistrationok",
353 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
354 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
355 WriteIntAttribute("breakpadregistrationfail",
356 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
357 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
358 WriteIntAttribute("debuggerpresent",
359 pref->GetInteger(prefs::kStabilityDebuggerPresent));
360 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
361 WriteIntAttribute("debuggernotpresent",
362 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
363 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29364
365 // Uptime is stored as a string, since there's no int64 in Value/JSON.
366 WriteAttribute("uptimesec",
367 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
368 pref->SetString(prefs::kStabilityUptimeSec, L"0");
369
[email protected]147bbc0b2009-01-06 19:37:40370 WritePluginStabilityElements(pref);
371}
372
373void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
374 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29375 const ListValue* plugin_stats_list = pref->GetList(
376 prefs::kStabilityPluginStats);
377 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33378 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29379 for (ListValue::const_iterator iter = plugin_stats_list->begin();
380 iter != plugin_stats_list->end(); ++iter) {
381 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
382 NOTREACHED();
383 continue;
384 }
385 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
386
[email protected]8e50b602009-03-03 22:59:43387 std::wstring plugin_name;
388 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29389
[email protected]ffaf78a2008-11-12 17:38:33390 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10391 // Use "filename" instead of "name", otherwise we need to update the
392 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43393 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29394
395 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43396 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29397 WriteIntAttribute("launchcount", launches);
398
399 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43400 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29401 WriteIntAttribute("instancecount", instances);
402
403 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43404 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29405 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29406 }
407
408 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29409 }
initial.commit09911bf2008-07-26 23:55:29410}
411
[email protected]147bbc0b2009-01-06 19:37:40412void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36413 // The server refuses data that doesn't have certain values. crashcount and
414 // launchcount are currently "required" in the "stability" group.
415 WriteIntAttribute("launchcount",
416 pref->GetInteger(prefs::kStabilityLaunchCount));
417 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
418 WriteIntAttribute("crashcount",
419 pref->GetInteger(prefs::kStabilityCrashCount));
420 pref->SetInteger(prefs::kStabilityCrashCount, 0);
421}
422
[email protected]147bbc0b2009-01-06 19:37:40423void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36424 // Update the stats which are critical for real-time stability monitoring.
425 // Since these are "optional," only list ones that are non-zero, as the counts
426 // are aggergated (summed) server side.
427
428 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
429 if (count) {
430 WriteIntAttribute("pageloadcount", count);
431 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
432 }
433
434 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
435 if (count) {
436 WriteIntAttribute("renderercrashcount", count);
437 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
438 }
439
440 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
441 if (count) {
442 WriteIntAttribute("rendererhangcount", count);
443 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
444 }
445}
446
initial.commit09911bf2008-07-26 23:55:29447void MetricsLog::WritePluginList(
448 const std::vector<WebPluginInfo>& plugin_list) {
449 DCHECK(!locked_);
450
[email protected]ffaf78a2008-11-12 17:38:33451 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29452
453 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
454 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33455 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29456
457 // Plugin name and filename are hashed for the privacy of those
458 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44459 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21460 WriteAttribute("filename",
461 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44462 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29463 }
initial.commit09911bf2008-07-26 23:55:29464}
465
[email protected]147bbc0b2009-01-06 19:37:40466void MetricsLog::WriteInstallElement() {
467 OPEN_ELEMENT_FOR_SCOPE("install");
468 WriteAttribute("installdate", GetInstallDate());
469 WriteIntAttribute("buildid", 0); // We're using appversion instead.
470 WriteAttribute("appversion", GetVersionString());
471}
472
initial.commit09911bf2008-07-26 23:55:29473void MetricsLog::RecordEnvironment(
474 const std::vector<WebPluginInfo>& plugin_list,
475 const DictionaryValue* profile_metrics) {
476 DCHECK(!locked_);
477
478 PrefService* pref = g_browser_process->local_state();
479
[email protected]ffaf78a2008-11-12 17:38:33480 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29481 WriteCommonEventAttributes();
482
[email protected]147bbc0b2009-01-06 19:37:40483 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29484
485 WritePluginList(plugin_list);
486
487 WriteStabilityElement();
488
489 {
490 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01491 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29492 }
493
494 {
495 OPEN_ELEMENT_FOR_SCOPE("security");
496 WriteIntAttribute("rendereronsboxdesktop",
497 pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop));
498 pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0);
499
500 WriteIntAttribute("rendererondefaultdesktop",
501 pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop));
502 pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0);
503 }
504
505 {
506 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40507 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38508#if defined(OS_WIN)
509 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
510#endif
initial.commit09911bf2008-07-26 23:55:29511 }
512
513 {
514 OPEN_ELEMENT_FOR_SCOPE("os");
515 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01516 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29517 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01518 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29519 }
520
521 {
522 OPEN_ELEMENT_FOR_SCOPE("display");
523 int width = 0;
524 int height = 0;
[email protected]05f9b682008-09-29 22:18:01525 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29526 WriteIntAttribute("xsize", width);
527 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01528 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29529 }
530
531 {
532 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
533 int num_bookmarks_on_bookmark_bar =
534 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
535 int num_folders_on_bookmark_bar =
536 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
537 int num_bookmarks_in_other_bookmarks_folder =
538 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
539 int num_folders_in_other_bookmarks_folder =
540 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
541 {
542 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
543 WriteAttribute("name", "full-tree");
544 WriteIntAttribute("foldercount",
545 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
546 WriteIntAttribute("itemcount",
547 num_bookmarks_on_bookmark_bar +
548 num_bookmarks_in_other_bookmarks_folder);
549 }
550 {
551 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
552 WriteAttribute("name", "toolbar");
553 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
554 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
555 }
556 }
557
558 {
559 OPEN_ELEMENT_FOR_SCOPE("keywords");
560 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
561 }
562
563 if (profile_metrics)
564 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29565}
566
567void MetricsLog::WriteAllProfilesMetrics(
568 const DictionaryValue& all_profiles_metrics) {
569 const std::wstring profile_prefix(prefs::kProfilePrefix);
570 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
571 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43572 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29573 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
574 DictionaryValue* profile;
[email protected]8e50b602009-03-03 22:59:43575 if (all_profiles_metrics.GetDictionary(key_name, &profile))
initial.commit09911bf2008-07-26 23:55:29576 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
577 }
578 }
579}
580
581void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
582 const DictionaryValue& profile_metrics) {
583 OPEN_ELEMENT_FOR_SCOPE("userprofile");
584 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
585 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
586 i != profile_metrics.end_keys(); ++i) {
587 Value* value;
588 if (profile_metrics.Get(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43589 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29590 switch (value->GetType()) {
591 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59592 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29593 if (value->GetAsString(&string_value)) {
594 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43595 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59596 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29597 }
598 break;
599 }
600
601 case Value::TYPE_BOOLEAN: {
602 bool bool_value;
603 if (value->GetAsBoolean(&bool_value)) {
604 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43605 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29606 WriteIntAttribute("value", bool_value ? 1 : 0);
607 }
608 break;
609 }
610
611 case Value::TYPE_INTEGER: {
612 int int_value;
613 if (value->GetAsInteger(&int_value)) {
614 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43615 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29616 WriteIntAttribute("value", int_value);
617 }
618 break;
619 }
620
621 default:
622 NOTREACHED();
623 break;
624 }
625 }
626 }
627}
628
629void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
630 DCHECK(!locked_);
631
[email protected]ffaf78a2008-11-12 17:38:33632 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29633 WriteAttribute("action", "autocomplete");
634 WriteAttribute("targetidhash", "");
635 // TODO(kochi): Properly track windows.
636 WriteIntAttribute("window", 0);
637 WriteCommonEventAttributes();
638
[email protected]ffaf78a2008-11-12 17:38:33639 {
640 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29641
[email protected]ffaf78a2008-11-12 17:38:33642 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
643 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
644 WriteIntAttribute("completedlength",
645 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00646 const std::string input_type(
647 AutocompleteInput::TypeToString(log.input_type));
648 if (!input_type.empty())
649 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29650
[email protected]ffaf78a2008-11-12 17:38:33651 for (AutocompleteResult::const_iterator i(log.result.begin());
652 i != log.result.end(); ++i) {
653 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
654 if (i->provider)
655 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00656 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
657 if (!result_type.empty())
658 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33659 WriteIntAttribute("relevance", i->relevance);
660 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
661 }
initial.commit09911bf2008-07-26 23:55:29662 }
initial.commit09911bf2008-07-26 23:55:29663
664 ++num_events_;
665}
666
667// TODO(JAR): A The following should really be part of the histogram class.
668// Internal state is being needlessly exposed, and it would be hard to reuse
669// this code. If we moved this into the Histogram class, then we could use
670// the same infrastructure for logging StatsCounters, RatesCounters, etc.
671void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
672 const Histogram::SampleSet& snapshot) {
673 DCHECK(!locked_);
674 DCHECK(0 != snapshot.TotalCount());
675 snapshot.CheckSize(histogram);
676
677 // We will ignore the MAX_INT/infinite value in the last element of range[].
678
679 OPEN_ELEMENT_FOR_SCOPE("histogram");
680
681 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
682
683 WriteInt64Attribute("sum", snapshot.sum());
684 WriteInt64Attribute("sumsquares", snapshot.square_sum());
685
686 for (size_t i = 0; i < histogram.bucket_count(); i++) {
687 if (snapshot.counts(i)) {
688 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
689 WriteIntAttribute("min", histogram.ranges(i));
690 WriteIntAttribute("max", histogram.ranges(i + 1));
691 WriteIntAttribute("count", snapshot.counts(i));
692 }
693 }
694}