blob: 16a24b71785db9ac70366ae161a6e17b261cb51a [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
5#include "chrome/browser/metrics_log.h"
6
[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
308void MetricsLog::WriteStabilityElement() {
309 DCHECK(!locked_);
310
311 PrefService* pref = g_browser_process->local_state();
312 DCHECK(pref);
313
314 // Get stability attributes out of Local State, zeroing out stored values.
315 // NOTE: This could lead to some data loss if this report isn't successfully
316 // sent, but that's true for all the metrics.
317
[email protected]ffaf78a2008-11-12 17:38:33318 OPEN_ELEMENT_FOR_SCOPE("stability");
initial.commit09911bf2008-07-26 23:55:29319
320 WriteIntAttribute("launchcount",
321 pref->GetInteger(prefs::kStabilityLaunchCount));
322 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
323 WriteIntAttribute("crashcount",
324 pref->GetInteger(prefs::kStabilityCrashCount));
325 pref->SetInteger(prefs::kStabilityCrashCount, 0);
[email protected]8e674e42008-07-30 16:05:48326 WriteIntAttribute("incompleteshutdowncount",
327 pref->GetInteger(
328 prefs::kStabilityIncompleteSessionEndCount));
329 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
initial.commit09911bf2008-07-26 23:55:29330 WriteIntAttribute("pageloadcount",
331 pref->GetInteger(prefs::kStabilityPageLoadCount));
332 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
333 WriteIntAttribute("renderercrashcount",
334 pref->GetInteger(prefs::kStabilityRendererCrashCount));
335 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
336 WriteIntAttribute("rendererhangcount",
337 pref->GetInteger(prefs::kStabilityRendererHangCount));
338 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
[email protected]e73c01972008-08-13 00:18:24339 WriteIntAttribute("breakpadregistrationok",
340 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
341 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
342 WriteIntAttribute("breakpadregistrationfail",
343 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
344 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
345 WriteIntAttribute("debuggerpresent",
346 pref->GetInteger(prefs::kStabilityDebuggerPresent));
347 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
348 WriteIntAttribute("debuggernotpresent",
349 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
350 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29351
352 // Uptime is stored as a string, since there's no int64 in Value/JSON.
353 WriteAttribute("uptimesec",
354 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
355 pref->SetString(prefs::kStabilityUptimeSec, L"0");
356
357 // Now log plugin stability info
358 const ListValue* plugin_stats_list = pref->GetList(
359 prefs::kStabilityPluginStats);
360 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33361 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29362 for (ListValue::const_iterator iter = plugin_stats_list->begin();
363 iter != plugin_stats_list->end(); ++iter) {
364 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
365 NOTREACHED();
366 continue;
367 }
368 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
369
370 std::wstring plugin_path;
371 plugin_dict->GetString(prefs::kStabilityPluginPath, &plugin_path);
372 plugin_path = file_util::GetFilenameFromPath(plugin_path);
373 if (plugin_path.empty()) {
374 NOTREACHED();
375 continue;
376 }
377
[email protected]ffaf78a2008-11-12 17:38:33378 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
initial.commit09911bf2008-07-26 23:55:29379 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_path)));
380
381 int launches = 0;
382 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
383 WriteIntAttribute("launchcount", launches);
384
385 int instances = 0;
386 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
387 WriteIntAttribute("instancecount", instances);
388
389 int crashes = 0;
390 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
391 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29392 }
393
394 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29395 }
initial.commit09911bf2008-07-26 23:55:29396}
397
398void MetricsLog::WritePluginList(
399 const std::vector<WebPluginInfo>& plugin_list) {
400 DCHECK(!locked_);
401
[email protected]ffaf78a2008-11-12 17:38:33402 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29403
404 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
405 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33406 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29407
408 // Plugin name and filename are hashed for the privacy of those
409 // testing unreleased new extensions.
410 WriteAttribute("name", CreateBase64Hash(WideToUTF8((*iter).name)));
411 std::wstring filename = file_util::GetFilenameFromPath((*iter).file);
412 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(filename)));
413
414 WriteAttribute("version", WideToUTF8((*iter).version));
initial.commit09911bf2008-07-26 23:55:29415 }
initial.commit09911bf2008-07-26 23:55:29416}
417
418void MetricsLog::RecordEnvironment(
419 const std::vector<WebPluginInfo>& plugin_list,
420 const DictionaryValue* profile_metrics) {
421 DCHECK(!locked_);
422
423 PrefService* pref = g_browser_process->local_state();
424
[email protected]ffaf78a2008-11-12 17:38:33425 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29426 WriteCommonEventAttributes();
427
[email protected]ffaf78a2008-11-12 17:38:33428 {
429 OPEN_ELEMENT_FOR_SCOPE("install");
430 WriteAttribute("installdate", GetInstallDate());
431 WriteIntAttribute("buildid", 0); // We're using appversion instead.
432 WriteAttribute("appversion", GetVersionString());
433 }
initial.commit09911bf2008-07-26 23:55:29434
435 WritePluginList(plugin_list);
436
437 WriteStabilityElement();
438
439 {
440 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01441 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29442 }
443
444 {
445 OPEN_ELEMENT_FOR_SCOPE("security");
446 WriteIntAttribute("rendereronsboxdesktop",
447 pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop));
448 pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0);
449
450 WriteIntAttribute("rendererondefaultdesktop",
451 pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop));
452 pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0);
453 }
454
455 {
456 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40457 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38458#if defined(OS_WIN)
459 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
460#endif
initial.commit09911bf2008-07-26 23:55:29461 }
462
463 {
464 OPEN_ELEMENT_FOR_SCOPE("os");
465 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01466 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29467 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01468 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29469 }
470
471 {
472 OPEN_ELEMENT_FOR_SCOPE("display");
473 int width = 0;
474 int height = 0;
[email protected]05f9b682008-09-29 22:18:01475 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29476 WriteIntAttribute("xsize", width);
477 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01478 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29479 }
480
481 {
482 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
483 int num_bookmarks_on_bookmark_bar =
484 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
485 int num_folders_on_bookmark_bar =
486 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
487 int num_bookmarks_in_other_bookmarks_folder =
488 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
489 int num_folders_in_other_bookmarks_folder =
490 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
491 {
492 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
493 WriteAttribute("name", "full-tree");
494 WriteIntAttribute("foldercount",
495 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
496 WriteIntAttribute("itemcount",
497 num_bookmarks_on_bookmark_bar +
498 num_bookmarks_in_other_bookmarks_folder);
499 }
500 {
501 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
502 WriteAttribute("name", "toolbar");
503 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
504 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
505 }
506 }
507
508 {
509 OPEN_ELEMENT_FOR_SCOPE("keywords");
510 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
511 }
512
513 if (profile_metrics)
514 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29515}
516
517void MetricsLog::WriteAllProfilesMetrics(
518 const DictionaryValue& all_profiles_metrics) {
519 const std::wstring profile_prefix(prefs::kProfilePrefix);
520 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
521 i != all_profiles_metrics.end_keys(); ++i) {
522 const std::wstring& key_name = *i;
523 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
524 DictionaryValue* profile;
525 if (all_profiles_metrics.GetDictionary(key_name, &profile))
526 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
527 }
528 }
529}
530
531void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
532 const DictionaryValue& profile_metrics) {
533 OPEN_ELEMENT_FOR_SCOPE("userprofile");
534 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
535 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
536 i != profile_metrics.end_keys(); ++i) {
537 Value* value;
538 if (profile_metrics.Get(*i, &value)) {
539 DCHECK(*i != L"id");
540 switch (value->GetType()) {
541 case Value::TYPE_STRING: {
542 std::wstring string_value;
543 if (value->GetAsString(&string_value)) {
544 OPEN_ELEMENT_FOR_SCOPE("profileparam");
545 WriteAttribute("name", WideToUTF8(*i));
546 WriteAttribute("value", WideToUTF8(string_value));
547 }
548 break;
549 }
550
551 case Value::TYPE_BOOLEAN: {
552 bool bool_value;
553 if (value->GetAsBoolean(&bool_value)) {
554 OPEN_ELEMENT_FOR_SCOPE("profileparam");
555 WriteAttribute("name", WideToUTF8(*i));
556 WriteIntAttribute("value", bool_value ? 1 : 0);
557 }
558 break;
559 }
560
561 case Value::TYPE_INTEGER: {
562 int int_value;
563 if (value->GetAsInteger(&int_value)) {
564 OPEN_ELEMENT_FOR_SCOPE("profileparam");
565 WriteAttribute("name", WideToUTF8(*i));
566 WriteIntAttribute("value", int_value);
567 }
568 break;
569 }
570
571 default:
572 NOTREACHED();
573 break;
574 }
575 }
576 }
577}
578
579void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
580 DCHECK(!locked_);
581
[email protected]ffaf78a2008-11-12 17:38:33582 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29583 WriteAttribute("action", "autocomplete");
584 WriteAttribute("targetidhash", "");
585 // TODO(kochi): Properly track windows.
586 WriteIntAttribute("window", 0);
587 WriteCommonEventAttributes();
588
[email protected]ffaf78a2008-11-12 17:38:33589 {
590 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29591
[email protected]ffaf78a2008-11-12 17:38:33592 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
593 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
594 WriteIntAttribute("completedlength",
595 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00596 const std::string input_type(
597 AutocompleteInput::TypeToString(log.input_type));
598 if (!input_type.empty())
599 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29600
[email protected]ffaf78a2008-11-12 17:38:33601 for (AutocompleteResult::const_iterator i(log.result.begin());
602 i != log.result.end(); ++i) {
603 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
604 if (i->provider)
605 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00606 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
607 if (!result_type.empty())
608 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33609 WriteIntAttribute("relevance", i->relevance);
610 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
611 }
initial.commit09911bf2008-07-26 23:55:29612 }
initial.commit09911bf2008-07-26 23:55:29613
614 ++num_events_;
615}
616
617// TODO(JAR): A The following should really be part of the histogram class.
618// Internal state is being needlessly exposed, and it would be hard to reuse
619// this code. If we moved this into the Histogram class, then we could use
620// the same infrastructure for logging StatsCounters, RatesCounters, etc.
621void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
622 const Histogram::SampleSet& snapshot) {
623 DCHECK(!locked_);
624 DCHECK(0 != snapshot.TotalCount());
625 snapshot.CheckSize(histogram);
626
627 // We will ignore the MAX_INT/infinite value in the last element of range[].
628
629 OPEN_ELEMENT_FOR_SCOPE("histogram");
630
631 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
632
633 WriteInt64Attribute("sum", snapshot.sum());
634 WriteInt64Attribute("sumsquares", snapshot.square_sum());
635
636 for (size_t i = 0; i < histogram.bucket_count(); i++) {
637 if (snapshot.counts(i)) {
638 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
639 WriteIntAttribute("min", histogram.ranges(i));
640 WriteIntAttribute("max", histogram.ranges(i + 1));
641 WriteIntAttribute("count", snapshot.counts(i));
642 }
643 }
644}