blob: 24e94e59d41fa1f117ea4d45054813dd82349887 [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
[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
314 OPEN_ELEMENT_FOR_SCOPE("stability");
315 WriteRequiredStabilityElements(pref);
316 WriteRealtimeStabilityElements(pref);
317}
318
initial.commit09911bf2008-07-26 23:55:29319void MetricsLog::WriteStabilityElement() {
320 DCHECK(!locked_);
321
322 PrefService* pref = g_browser_process->local_state();
323 DCHECK(pref);
324
325 // Get stability attributes out of Local State, zeroing out stored values.
326 // NOTE: This could lead to some data loss if this report isn't successfully
327 // sent, but that's true for all the metrics.
328
[email protected]ffaf78a2008-11-12 17:38:33329 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]0b33f80b2008-12-17 21:34:36330 WriteRequiredStabilityElements(pref);
331 WriteRealtimeStabilityElements(pref);
initial.commit09911bf2008-07-26 23:55:29332
[email protected]0b33f80b2008-12-17 21:34:36333 // TODO(jar): The following are all optional, so we *could* optimize them for
334 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48335 WriteIntAttribute("incompleteshutdowncount",
336 pref->GetInteger(
337 prefs::kStabilityIncompleteSessionEndCount));
338 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36339
340
[email protected]e73c01972008-08-13 00:18:24341 WriteIntAttribute("breakpadregistrationok",
342 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
343 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
344 WriteIntAttribute("breakpadregistrationfail",
345 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
346 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
347 WriteIntAttribute("debuggerpresent",
348 pref->GetInteger(prefs::kStabilityDebuggerPresent));
349 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
350 WriteIntAttribute("debuggernotpresent",
351 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
352 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29353
354 // Uptime is stored as a string, since there's no int64 in Value/JSON.
355 WriteAttribute("uptimesec",
356 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
357 pref->SetString(prefs::kStabilityUptimeSec, L"0");
358
359 // Now log plugin stability info
360 const ListValue* plugin_stats_list = pref->GetList(
361 prefs::kStabilityPluginStats);
362 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33363 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29364 for (ListValue::const_iterator iter = plugin_stats_list->begin();
365 iter != plugin_stats_list->end(); ++iter) {
366 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
367 NOTREACHED();
368 continue;
369 }
370 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
371
372 std::wstring plugin_path;
373 plugin_dict->GetString(prefs::kStabilityPluginPath, &plugin_path);
374 plugin_path = file_util::GetFilenameFromPath(plugin_path);
375 if (plugin_path.empty()) {
376 NOTREACHED();
377 continue;
378 }
379
[email protected]ffaf78a2008-11-12 17:38:33380 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
initial.commit09911bf2008-07-26 23:55:29381 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_path)));
382
383 int launches = 0;
384 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
385 WriteIntAttribute("launchcount", launches);
386
387 int instances = 0;
388 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
389 WriteIntAttribute("instancecount", instances);
390
391 int crashes = 0;
392 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
393 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29394 }
395
396 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29397 }
initial.commit09911bf2008-07-26 23:55:29398}
399
[email protected]0b33f80b2008-12-17 21:34:36400void MetricsLog::WriteRequiredStabilityElements(PrefService* pref) {
401 // The server refuses data that doesn't have certain values. crashcount and
402 // launchcount are currently "required" in the "stability" group.
403 WriteIntAttribute("launchcount",
404 pref->GetInteger(prefs::kStabilityLaunchCount));
405 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
406 WriteIntAttribute("crashcount",
407 pref->GetInteger(prefs::kStabilityCrashCount));
408 pref->SetInteger(prefs::kStabilityCrashCount, 0);
409}
410
411void MetricsLog::WriteRealtimeStabilityElements(PrefService* pref) {
412 // Update the stats which are critical for real-time stability monitoring.
413 // Since these are "optional," only list ones that are non-zero, as the counts
414 // are aggergated (summed) server side.
415
416 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
417 if (count) {
418 WriteIntAttribute("pageloadcount", count);
419 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
420 }
421
422 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
423 if (count) {
424 WriteIntAttribute("renderercrashcount", count);
425 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
426 }
427
428 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
429 if (count) {
430 WriteIntAttribute("rendererhangcount", count);
431 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
432 }
433}
434
initial.commit09911bf2008-07-26 23:55:29435void MetricsLog::WritePluginList(
436 const std::vector<WebPluginInfo>& plugin_list) {
437 DCHECK(!locked_);
438
[email protected]ffaf78a2008-11-12 17:38:33439 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29440
441 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
442 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33443 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29444
445 // Plugin name and filename are hashed for the privacy of those
446 // testing unreleased new extensions.
447 WriteAttribute("name", CreateBase64Hash(WideToUTF8((*iter).name)));
448 std::wstring filename = file_util::GetFilenameFromPath((*iter).file);
449 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(filename)));
450
451 WriteAttribute("version", WideToUTF8((*iter).version));
initial.commit09911bf2008-07-26 23:55:29452 }
initial.commit09911bf2008-07-26 23:55:29453}
454
455void MetricsLog::RecordEnvironment(
456 const std::vector<WebPluginInfo>& plugin_list,
457 const DictionaryValue* profile_metrics) {
458 DCHECK(!locked_);
459
460 PrefService* pref = g_browser_process->local_state();
461
[email protected]ffaf78a2008-11-12 17:38:33462 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29463 WriteCommonEventAttributes();
464
[email protected]ffaf78a2008-11-12 17:38:33465 {
466 OPEN_ELEMENT_FOR_SCOPE("install");
467 WriteAttribute("installdate", GetInstallDate());
468 WriteIntAttribute("buildid", 0); // We're using appversion instead.
469 WriteAttribute("appversion", GetVersionString());
470 }
initial.commit09911bf2008-07-26 23:55:29471
472 WritePluginList(plugin_list);
473
474 WriteStabilityElement();
475
476 {
477 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01478 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29479 }
480
481 {
482 OPEN_ELEMENT_FOR_SCOPE("security");
483 WriteIntAttribute("rendereronsboxdesktop",
484 pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop));
485 pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0);
486
487 WriteIntAttribute("rendererondefaultdesktop",
488 pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop));
489 pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0);
490 }
491
492 {
493 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40494 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38495#if defined(OS_WIN)
496 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
497#endif
initial.commit09911bf2008-07-26 23:55:29498 }
499
500 {
501 OPEN_ELEMENT_FOR_SCOPE("os");
502 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01503 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29504 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01505 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29506 }
507
508 {
509 OPEN_ELEMENT_FOR_SCOPE("display");
510 int width = 0;
511 int height = 0;
[email protected]05f9b682008-09-29 22:18:01512 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29513 WriteIntAttribute("xsize", width);
514 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01515 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29516 }
517
518 {
519 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
520 int num_bookmarks_on_bookmark_bar =
521 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
522 int num_folders_on_bookmark_bar =
523 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
524 int num_bookmarks_in_other_bookmarks_folder =
525 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
526 int num_folders_in_other_bookmarks_folder =
527 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
528 {
529 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
530 WriteAttribute("name", "full-tree");
531 WriteIntAttribute("foldercount",
532 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
533 WriteIntAttribute("itemcount",
534 num_bookmarks_on_bookmark_bar +
535 num_bookmarks_in_other_bookmarks_folder);
536 }
537 {
538 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
539 WriteAttribute("name", "toolbar");
540 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
541 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
542 }
543 }
544
545 {
546 OPEN_ELEMENT_FOR_SCOPE("keywords");
547 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
548 }
549
550 if (profile_metrics)
551 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29552}
553
554void MetricsLog::WriteAllProfilesMetrics(
555 const DictionaryValue& all_profiles_metrics) {
556 const std::wstring profile_prefix(prefs::kProfilePrefix);
557 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
558 i != all_profiles_metrics.end_keys(); ++i) {
559 const std::wstring& key_name = *i;
560 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
561 DictionaryValue* profile;
562 if (all_profiles_metrics.GetDictionary(key_name, &profile))
563 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
564 }
565 }
566}
567
568void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
569 const DictionaryValue& profile_metrics) {
570 OPEN_ELEMENT_FOR_SCOPE("userprofile");
571 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
572 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
573 i != profile_metrics.end_keys(); ++i) {
574 Value* value;
575 if (profile_metrics.Get(*i, &value)) {
576 DCHECK(*i != L"id");
577 switch (value->GetType()) {
578 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59579 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29580 if (value->GetAsString(&string_value)) {
581 OPEN_ELEMENT_FOR_SCOPE("profileparam");
582 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59583 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29584 }
585 break;
586 }
587
588 case Value::TYPE_BOOLEAN: {
589 bool bool_value;
590 if (value->GetAsBoolean(&bool_value)) {
591 OPEN_ELEMENT_FOR_SCOPE("profileparam");
592 WriteAttribute("name", WideToUTF8(*i));
593 WriteIntAttribute("value", bool_value ? 1 : 0);
594 }
595 break;
596 }
597
598 case Value::TYPE_INTEGER: {
599 int int_value;
600 if (value->GetAsInteger(&int_value)) {
601 OPEN_ELEMENT_FOR_SCOPE("profileparam");
602 WriteAttribute("name", WideToUTF8(*i));
603 WriteIntAttribute("value", int_value);
604 }
605 break;
606 }
607
608 default:
609 NOTREACHED();
610 break;
611 }
612 }
613 }
614}
615
616void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
617 DCHECK(!locked_);
618
[email protected]ffaf78a2008-11-12 17:38:33619 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29620 WriteAttribute("action", "autocomplete");
621 WriteAttribute("targetidhash", "");
622 // TODO(kochi): Properly track windows.
623 WriteIntAttribute("window", 0);
624 WriteCommonEventAttributes();
625
[email protected]ffaf78a2008-11-12 17:38:33626 {
627 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29628
[email protected]ffaf78a2008-11-12 17:38:33629 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
630 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
631 WriteIntAttribute("completedlength",
632 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00633 const std::string input_type(
634 AutocompleteInput::TypeToString(log.input_type));
635 if (!input_type.empty())
636 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29637
[email protected]ffaf78a2008-11-12 17:38:33638 for (AutocompleteResult::const_iterator i(log.result.begin());
639 i != log.result.end(); ++i) {
640 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
641 if (i->provider)
642 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00643 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
644 if (!result_type.empty())
645 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33646 WriteIntAttribute("relevance", i->relevance);
647 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
648 }
initial.commit09911bf2008-07-26 23:55:29649 }
initial.commit09911bf2008-07-26 23:55:29650
651 ++num_events_;
652}
653
654// TODO(JAR): A The following should really be part of the histogram class.
655// Internal state is being needlessly exposed, and it would be hard to reuse
656// this code. If we moved this into the Histogram class, then we could use
657// the same infrastructure for logging StatsCounters, RatesCounters, etc.
658void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
659 const Histogram::SampleSet& snapshot) {
660 DCHECK(!locked_);
661 DCHECK(0 != snapshot.TotalCount());
662 snapshot.CheckSize(histogram);
663
664 // We will ignore the MAX_INT/infinite value in the last element of range[].
665
666 OPEN_ELEMENT_FOR_SCOPE("histogram");
667
668 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
669
670 WriteInt64Attribute("sum", snapshot.sum());
671 WriteInt64Attribute("sumsquares", snapshot.square_sum());
672
673 for (size_t i = 0; i < histogram.bucket_count(); i++) {
674 if (snapshot.counts(i)) {
675 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
676 WriteIntAttribute("min", histogram.ranges(i));
677 WriteIntAttribute("max", histogram.ranges(i + 1));
678 WriteIntAttribute("count", snapshot.counts(i));
679 }
680 }
681}