blob: 27cb88e584ad3d6b1d3de153a28a1ebb1c18e95d [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:
186 origin_string = "global-history";
187 break;
188
189 case PageTransition::START_PAGE:
190 origin_string = "start-page";
191 break;
192
193 case PageTransition::FORM_SUBMIT:
194 origin_string = "form-submit";
195 break;
196
197 default:
198 NOTREACHED() << "Received an unknown page transition type: " <<
199 PageTransition::StripQualifier(origin);
200 }
201 if (!origin_string.empty())
202 WriteAttribute("origin", origin_string);
203
204 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29205
206 ++num_events_;
207}
208
initial.commit09911bf2008-07-26 23:55:29209void MetricsLog::RecordWindowEvent(WindowEventType type,
210 int window_id,
211 int parent_id) {
212 DCHECK(!locked_);
213
[email protected]ffaf78a2008-11-12 17:38:33214 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29215 WriteAttribute("action", WindowEventTypeToString(type));
216 WriteAttribute("windowid", IntToString(window_id));
217 if (parent_id >= 0)
218 WriteAttribute("parent", IntToString(parent_id));
219 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29220
221 ++num_events_;
222}
223
224std::string MetricsLog::GetCurrentTimeString() {
225 return Uint64ToString(Time::Now().ToTimeT());
226}
227
228// These are the attributes that are common to every event.
229void MetricsLog::WriteCommonEventAttributes() {
230 WriteAttribute("session", session_id_);
231 WriteAttribute("time", GetCurrentTimeString());
232}
233
234void MetricsLog::WriteAttribute(const std::string& name,
235 const std::string& value) {
236 DCHECK(!locked_);
237 DCHECK(!name.empty());
238
239 int result = xmlTextWriterWriteAttribute(writer_,
240 UnsignedChar(name.c_str()),
241 UnsignedChar(value.c_str()));
242 DCHECK_GE(result, 0);
243}
244
245void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
246 WriteAttribute(name, IntToString(value));
247}
248
249void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
250 WriteAttribute(name, Int64ToString(value));
251}
252
[email protected]ffaf78a2008-11-12 17:38:33253// static
254const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
255 switch (type) {
256 case WINDOW_CREATE: return "create";
257 case WINDOW_OPEN: return "open";
258 case WINDOW_CLOSE: return "close";
259 case WINDOW_DESTROY: return "destroy";
260
261 default:
262 NOTREACHED();
263 return "unknown"; // Can't return NULL as this is used in a required
264 // attribute.
265 }
266}
267
initial.commit09911bf2008-07-26 23:55:29268void MetricsLog::StartElement(const char* name) {
269 DCHECK(!locked_);
270 DCHECK(name);
271
272 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
273 DCHECK_GE(result, 0);
274}
275
276void MetricsLog::EndElement() {
277 DCHECK(!locked_);
278
279 int result = xmlTextWriterEndElement(writer_);
280 DCHECK_GE(result, 0);
281}
282
283std::string MetricsLog::GetVersionString() const {
284 scoped_ptr<FileVersionInfo> version_info(
285 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
286 if (version_info.get()) {
287 std::string version = WideToUTF8(version_info->product_version());
288 if (!version_info->is_official_build())
289 version.append("-devel");
290 return version;
291 } else {
292 NOTREACHED() << "Unable to retrieve version string.";
293 }
294
295 return std::string();
296}
297
298std::string MetricsLog::GetInstallDate() const {
299 PrefService* pref = g_browser_process->local_state();
300 if (pref) {
301 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
302 } else {
303 NOTREACHED();
304 return "0";
305 }
306}
307
[email protected]0b33f80b2008-12-17 21:34:36308void MetricsLog::RecordIncrementalStabilityElements() {
309 DCHECK(!locked_);
310
311 PrefService* pref = g_browser_process->local_state();
312 DCHECK(pref);
313
[email protected]147bbc0b2009-01-06 19:37:40314 OPEN_ELEMENT_FOR_SCOPE("profile");
315 WriteCommonEventAttributes();
316
317 WriteInstallElement(); // Supply appversion.
318
319 {
320 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
321 WriteRequiredStabilityAttributes(pref);
322 WriteRealtimeStabilityAttributes(pref);
323
324 WritePluginStabilityElements(pref);
325 }
[email protected]0b33f80b2008-12-17 21:34:36326}
327
initial.commit09911bf2008-07-26 23:55:29328void MetricsLog::WriteStabilityElement() {
329 DCHECK(!locked_);
330
331 PrefService* pref = g_browser_process->local_state();
332 DCHECK(pref);
333
334 // Get stability attributes out of Local State, zeroing out stored values.
335 // NOTE: This could lead to some data loss if this report isn't successfully
336 // sent, but that's true for all the metrics.
337
[email protected]ffaf78a2008-11-12 17:38:33338 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40339 WriteRequiredStabilityAttributes(pref);
340 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29341
[email protected]0b33f80b2008-12-17 21:34:36342 // TODO(jar): The following are all optional, so we *could* optimize them for
343 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48344 WriteIntAttribute("incompleteshutdowncount",
345 pref->GetInteger(
346 prefs::kStabilityIncompleteSessionEndCount));
347 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36348
349
[email protected]e73c01972008-08-13 00:18:24350 WriteIntAttribute("breakpadregistrationok",
351 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
352 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
353 WriteIntAttribute("breakpadregistrationfail",
354 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
355 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
356 WriteIntAttribute("debuggerpresent",
357 pref->GetInteger(prefs::kStabilityDebuggerPresent));
358 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
359 WriteIntAttribute("debuggernotpresent",
360 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
361 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29362
363 // Uptime is stored as a string, since there's no int64 in Value/JSON.
364 WriteAttribute("uptimesec",
365 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
366 pref->SetString(prefs::kStabilityUptimeSec, L"0");
367
[email protected]147bbc0b2009-01-06 19:37:40368 WritePluginStabilityElements(pref);
369}
370
371void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
372 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29373 const ListValue* plugin_stats_list = pref->GetList(
374 prefs::kStabilityPluginStats);
375 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33376 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29377 for (ListValue::const_iterator iter = plugin_stats_list->begin();
378 iter != plugin_stats_list->end(); ++iter) {
379 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
380 NOTREACHED();
381 continue;
382 }
383 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
384
[email protected]a27a9382009-02-11 23:55:10385 std::wstring plugin_name;
386 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29387
[email protected]ffaf78a2008-11-12 17:38:33388 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10389 // Use "filename" instead of "name", otherwise we need to update the
390 // UMA servers.
391 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29392
393 int launches = 0;
394 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
395 WriteIntAttribute("launchcount", launches);
396
397 int instances = 0;
398 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
399 WriteIntAttribute("instancecount", instances);
400
401 int crashes = 0;
402 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
403 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29404 }
405
406 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29407 }
initial.commit09911bf2008-07-26 23:55:29408}
409
[email protected]147bbc0b2009-01-06 19:37:40410void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36411 // The server refuses data that doesn't have certain values. crashcount and
412 // launchcount are currently "required" in the "stability" group.
413 WriteIntAttribute("launchcount",
414 pref->GetInteger(prefs::kStabilityLaunchCount));
415 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
416 WriteIntAttribute("crashcount",
417 pref->GetInteger(prefs::kStabilityCrashCount));
418 pref->SetInteger(prefs::kStabilityCrashCount, 0);
419}
420
[email protected]147bbc0b2009-01-06 19:37:40421void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36422 // Update the stats which are critical for real-time stability monitoring.
423 // Since these are "optional," only list ones that are non-zero, as the counts
424 // are aggergated (summed) server side.
425
426 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
427 if (count) {
428 WriteIntAttribute("pageloadcount", count);
429 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
430 }
431
432 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
433 if (count) {
434 WriteIntAttribute("renderercrashcount", count);
435 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
436 }
437
438 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
439 if (count) {
440 WriteIntAttribute("rendererhangcount", count);
441 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
442 }
443}
444
initial.commit09911bf2008-07-26 23:55:29445void MetricsLog::WritePluginList(
446 const std::vector<WebPluginInfo>& plugin_list) {
447 DCHECK(!locked_);
448
[email protected]ffaf78a2008-11-12 17:38:33449 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29450
451 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
452 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33453 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29454
455 // Plugin name and filename are hashed for the privacy of those
456 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44457 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21458 WriteAttribute("filename",
459 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44460 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29461 }
initial.commit09911bf2008-07-26 23:55:29462}
463
[email protected]147bbc0b2009-01-06 19:37:40464void MetricsLog::WriteInstallElement() {
465 OPEN_ELEMENT_FOR_SCOPE("install");
466 WriteAttribute("installdate", GetInstallDate());
467 WriteIntAttribute("buildid", 0); // We're using appversion instead.
468 WriteAttribute("appversion", GetVersionString());
469}
470
initial.commit09911bf2008-07-26 23:55:29471void MetricsLog::RecordEnvironment(
472 const std::vector<WebPluginInfo>& plugin_list,
473 const DictionaryValue* profile_metrics) {
474 DCHECK(!locked_);
475
476 PrefService* pref = g_browser_process->local_state();
477
[email protected]ffaf78a2008-11-12 17:38:33478 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29479 WriteCommonEventAttributes();
480
[email protected]147bbc0b2009-01-06 19:37:40481 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29482
483 WritePluginList(plugin_list);
484
485 WriteStabilityElement();
486
487 {
488 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01489 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29490 }
491
492 {
493 OPEN_ELEMENT_FOR_SCOPE("security");
494 WriteIntAttribute("rendereronsboxdesktop",
495 pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop));
496 pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0);
497
498 WriteIntAttribute("rendererondefaultdesktop",
499 pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop));
500 pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0);
501 }
502
503 {
504 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40505 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38506#if defined(OS_WIN)
507 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
508#endif
initial.commit09911bf2008-07-26 23:55:29509 }
510
511 {
512 OPEN_ELEMENT_FOR_SCOPE("os");
513 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01514 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29515 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01516 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29517 }
518
519 {
520 OPEN_ELEMENT_FOR_SCOPE("display");
521 int width = 0;
522 int height = 0;
[email protected]05f9b682008-09-29 22:18:01523 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29524 WriteIntAttribute("xsize", width);
525 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01526 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29527 }
528
529 {
530 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
531 int num_bookmarks_on_bookmark_bar =
532 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
533 int num_folders_on_bookmark_bar =
534 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
535 int num_bookmarks_in_other_bookmarks_folder =
536 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
537 int num_folders_in_other_bookmarks_folder =
538 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
539 {
540 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
541 WriteAttribute("name", "full-tree");
542 WriteIntAttribute("foldercount",
543 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
544 WriteIntAttribute("itemcount",
545 num_bookmarks_on_bookmark_bar +
546 num_bookmarks_in_other_bookmarks_folder);
547 }
548 {
549 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
550 WriteAttribute("name", "toolbar");
551 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
552 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
553 }
554 }
555
556 {
557 OPEN_ELEMENT_FOR_SCOPE("keywords");
558 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
559 }
560
561 if (profile_metrics)
562 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29563}
564
565void MetricsLog::WriteAllProfilesMetrics(
566 const DictionaryValue& all_profiles_metrics) {
567 const std::wstring profile_prefix(prefs::kProfilePrefix);
568 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
569 i != all_profiles_metrics.end_keys(); ++i) {
570 const std::wstring& key_name = *i;
571 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
572 DictionaryValue* profile;
573 if (all_profiles_metrics.GetDictionary(key_name, &profile))
574 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
575 }
576 }
577}
578
579void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
580 const DictionaryValue& profile_metrics) {
581 OPEN_ELEMENT_FOR_SCOPE("userprofile");
582 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
583 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
584 i != profile_metrics.end_keys(); ++i) {
585 Value* value;
586 if (profile_metrics.Get(*i, &value)) {
587 DCHECK(*i != L"id");
588 switch (value->GetType()) {
589 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59590 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29591 if (value->GetAsString(&string_value)) {
592 OPEN_ELEMENT_FOR_SCOPE("profileparam");
593 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59594 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29595 }
596 break;
597 }
598
599 case Value::TYPE_BOOLEAN: {
600 bool bool_value;
601 if (value->GetAsBoolean(&bool_value)) {
602 OPEN_ELEMENT_FOR_SCOPE("profileparam");
603 WriteAttribute("name", WideToUTF8(*i));
604 WriteIntAttribute("value", bool_value ? 1 : 0);
605 }
606 break;
607 }
608
609 case Value::TYPE_INTEGER: {
610 int int_value;
611 if (value->GetAsInteger(&int_value)) {
612 OPEN_ELEMENT_FOR_SCOPE("profileparam");
613 WriteAttribute("name", WideToUTF8(*i));
614 WriteIntAttribute("value", int_value);
615 }
616 break;
617 }
618
619 default:
620 NOTREACHED();
621 break;
622 }
623 }
624 }
625}
626
627void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
628 DCHECK(!locked_);
629
[email protected]ffaf78a2008-11-12 17:38:33630 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29631 WriteAttribute("action", "autocomplete");
632 WriteAttribute("targetidhash", "");
633 // TODO(kochi): Properly track windows.
634 WriteIntAttribute("window", 0);
635 WriteCommonEventAttributes();
636
[email protected]ffaf78a2008-11-12 17:38:33637 {
638 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29639
[email protected]ffaf78a2008-11-12 17:38:33640 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
641 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
642 WriteIntAttribute("completedlength",
643 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00644 const std::string input_type(
645 AutocompleteInput::TypeToString(log.input_type));
646 if (!input_type.empty())
647 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29648
[email protected]ffaf78a2008-11-12 17:38:33649 for (AutocompleteResult::const_iterator i(log.result.begin());
650 i != log.result.end(); ++i) {
651 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
652 if (i->provider)
653 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00654 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
655 if (!result_type.empty())
656 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33657 WriteIntAttribute("relevance", i->relevance);
658 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
659 }
initial.commit09911bf2008-07-26 23:55:29660 }
initial.commit09911bf2008-07-26 23:55:29661
662 ++num_events_;
663}
664
665// TODO(JAR): A The following should really be part of the histogram class.
666// Internal state is being needlessly exposed, and it would be hard to reuse
667// this code. If we moved this into the Histogram class, then we could use
668// the same infrastructure for logging StatsCounters, RatesCounters, etc.
669void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
670 const Histogram::SampleSet& snapshot) {
671 DCHECK(!locked_);
672 DCHECK(0 != snapshot.TotalCount());
673 snapshot.CheckSize(histogram);
674
675 // We will ignore the MAX_INT/infinite value in the last element of range[].
676
677 OPEN_ELEMENT_FOR_SCOPE("histogram");
678
679 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
680
681 WriteInt64Attribute("sum", snapshot.sum());
682 WriteInt64Attribute("sumsquares", snapshot.square_sum());
683
684 for (size_t i = 0; i < histogram.bucket_count(); i++) {
685 if (snapshot.counts(i)) {
686 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
687 WriteIntAttribute("min", histogram.ranges(i));
688 WriteIntAttribute("max", histogram.ranges(i + 1));
689 WriteIntAttribute("count", snapshot.counts(i));
690 }
691 }
692}