blob: dbf3e97ee518c9404fdf90c3d9a5610ad82d44fc [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
[email protected]be6bf5e2009-06-16 13:14:08111 uint64 reverse_uint64;
112 // UMA only uses first 8 chars of hash. We use the above uint64 instead
113 // of a unsigned char[8] so that we don't run into strict aliasing issues
114 // in the LOG statement below when trying to interpret reverse as a uint64.
115 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
116 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
117 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
118 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10119 // The following log is VERY helpful when folks add some named histogram into
120 // the code, but forgot to update the descriptive list of histograms. When
121 // that happens, all we get to see (server side) is a hash of the histogram
122 // name. We can then use this logging to find out what histogram name was
123 // being hashed to a given MD5 value by just running the version of Chromium
124 // in question with --enable-logging.
125 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08126 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29127 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
128}
129
130std::string MetricsLog::CreateBase64Hash(const std::string& string) {
131 std::string encoded_digest;
[email protected]a9bb6f692008-07-30 16:40:10132 if (net::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29133 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
134 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29135 }
[email protected]a9bb6f692008-07-30 16:40:10136 return std::string();
initial.commit09911bf2008-07-26 23:55:29137}
138
[email protected]afe3a1672009-11-17 19:04:12139void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29140 DCHECK(!locked_);
141
[email protected]afe3a1672009-11-17 19:04:12142 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29143 if (command_hash.empty()) {
144 NOTREACHED() << "Unable generate encoded hash of command: " << key;
145 return;
146 }
147
[email protected]ffaf78a2008-11-12 17:38:33148 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29149 WriteAttribute("action", "command");
150 WriteAttribute("targetidhash", command_hash);
151
152 // TODO(jhughes): Properly track windows.
153 WriteIntAttribute("window", 0);
154 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29155
156 ++num_events_;
157}
158
159void MetricsLog::RecordLoadEvent(int window_id,
160 const GURL& url,
161 PageTransition::Type origin,
162 int session_index,
163 TimeDelta load_time) {
164 DCHECK(!locked_);
165
[email protected]ffaf78a2008-11-12 17:38:33166 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29167 WriteAttribute("action", "load");
168 WriteIntAttribute("docid", session_index);
169 WriteIntAttribute("window", window_id);
170 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
171
172 std::string origin_string;
173
174 switch (PageTransition::StripQualifier(origin)) {
175 // TODO(jhughes): Some of these mappings aren't right... we need to add
176 // some values to the server's enum.
177 case PageTransition::LINK:
178 case PageTransition::MANUAL_SUBFRAME:
179 origin_string = "link";
180 break;
181
182 case PageTransition::TYPED:
183 origin_string = "typed";
184 break;
185
186 case PageTransition::AUTO_BOOKMARK:
187 origin_string = "bookmark";
188 break;
189
190 case PageTransition::AUTO_SUBFRAME:
191 case PageTransition::RELOAD:
192 origin_string = "refresh";
193 break;
194
195 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44196 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29197 origin_string = "global-history";
198 break;
199
200 case PageTransition::START_PAGE:
201 origin_string = "start-page";
202 break;
203
204 case PageTransition::FORM_SUBMIT:
205 origin_string = "form-submit";
206 break;
207
208 default:
209 NOTREACHED() << "Received an unknown page transition type: " <<
210 PageTransition::StripQualifier(origin);
211 }
212 if (!origin_string.empty())
213 WriteAttribute("origin", origin_string);
214
215 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29216
217 ++num_events_;
218}
219
initial.commit09911bf2008-07-26 23:55:29220void MetricsLog::RecordWindowEvent(WindowEventType type,
221 int window_id,
222 int parent_id) {
223 DCHECK(!locked_);
224
[email protected]ffaf78a2008-11-12 17:38:33225 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29226 WriteAttribute("action", WindowEventTypeToString(type));
227 WriteAttribute("windowid", IntToString(window_id));
228 if (parent_id >= 0)
229 WriteAttribute("parent", IntToString(parent_id));
230 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29231
232 ++num_events_;
233}
234
235std::string MetricsLog::GetCurrentTimeString() {
236 return Uint64ToString(Time::Now().ToTimeT());
237}
238
239// These are the attributes that are common to every event.
240void MetricsLog::WriteCommonEventAttributes() {
241 WriteAttribute("session", session_id_);
242 WriteAttribute("time", GetCurrentTimeString());
243}
244
245void MetricsLog::WriteAttribute(const std::string& name,
246 const std::string& value) {
247 DCHECK(!locked_);
248 DCHECK(!name.empty());
249
250 int result = xmlTextWriterWriteAttribute(writer_,
251 UnsignedChar(name.c_str()),
252 UnsignedChar(value.c_str()));
253 DCHECK_GE(result, 0);
254}
255
256void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
257 WriteAttribute(name, IntToString(value));
258}
259
260void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
261 WriteAttribute(name, Int64ToString(value));
262}
263
[email protected]ffaf78a2008-11-12 17:38:33264// static
265const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
266 switch (type) {
267 case WINDOW_CREATE: return "create";
268 case WINDOW_OPEN: return "open";
269 case WINDOW_CLOSE: return "close";
270 case WINDOW_DESTROY: return "destroy";
271
272 default:
273 NOTREACHED();
274 return "unknown"; // Can't return NULL as this is used in a required
275 // attribute.
276 }
277}
278
initial.commit09911bf2008-07-26 23:55:29279void MetricsLog::StartElement(const char* name) {
280 DCHECK(!locked_);
281 DCHECK(name);
282
283 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
284 DCHECK_GE(result, 0);
285}
286
287void MetricsLog::EndElement() {
288 DCHECK(!locked_);
289
290 int result = xmlTextWriterEndElement(writer_);
291 DCHECK_GE(result, 0);
292}
293
[email protected]541f77922009-02-23 21:14:38294// static
295std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29296 scoped_ptr<FileVersionInfo> version_info(
297 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
298 if (version_info.get()) {
299 std::string version = WideToUTF8(version_info->product_version());
300 if (!version_info->is_official_build())
301 version.append("-devel");
302 return version;
303 } else {
304 NOTREACHED() << "Unable to retrieve version string.";
305 }
306
307 return std::string();
308}
309
310std::string MetricsLog::GetInstallDate() const {
311 PrefService* pref = g_browser_process->local_state();
312 if (pref) {
313 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
314 } else {
315 NOTREACHED();
316 return "0";
317 }
318}
319
[email protected]0b33f80b2008-12-17 21:34:36320void MetricsLog::RecordIncrementalStabilityElements() {
321 DCHECK(!locked_);
322
323 PrefService* pref = g_browser_process->local_state();
324 DCHECK(pref);
325
[email protected]147bbc0b2009-01-06 19:37:40326 OPEN_ELEMENT_FOR_SCOPE("profile");
327 WriteCommonEventAttributes();
328
329 WriteInstallElement(); // Supply appversion.
330
331 {
332 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
333 WriteRequiredStabilityAttributes(pref);
334 WriteRealtimeStabilityAttributes(pref);
335
336 WritePluginStabilityElements(pref);
337 }
[email protected]0b33f80b2008-12-17 21:34:36338}
339
initial.commit09911bf2008-07-26 23:55:29340void MetricsLog::WriteStabilityElement() {
341 DCHECK(!locked_);
342
343 PrefService* pref = g_browser_process->local_state();
344 DCHECK(pref);
345
346 // Get stability attributes out of Local State, zeroing out stored values.
347 // NOTE: This could lead to some data loss if this report isn't successfully
348 // sent, but that's true for all the metrics.
349
[email protected]ffaf78a2008-11-12 17:38:33350 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40351 WriteRequiredStabilityAttributes(pref);
352 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29353
[email protected]0b33f80b2008-12-17 21:34:36354 // TODO(jar): The following are all optional, so we *could* optimize them for
355 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48356 WriteIntAttribute("incompleteshutdowncount",
357 pref->GetInteger(
358 prefs::kStabilityIncompleteSessionEndCount));
359 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36360
361
[email protected]e73c01972008-08-13 00:18:24362 WriteIntAttribute("breakpadregistrationok",
363 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
364 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
365 WriteIntAttribute("breakpadregistrationfail",
366 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
367 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
368 WriteIntAttribute("debuggerpresent",
369 pref->GetInteger(prefs::kStabilityDebuggerPresent));
370 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
371 WriteIntAttribute("debuggernotpresent",
372 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
373 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29374
375 // Uptime is stored as a string, since there's no int64 in Value/JSON.
376 WriteAttribute("uptimesec",
377 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
378 pref->SetString(prefs::kStabilityUptimeSec, L"0");
379
[email protected]147bbc0b2009-01-06 19:37:40380 WritePluginStabilityElements(pref);
381}
382
383void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
384 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29385 const ListValue* plugin_stats_list = pref->GetList(
386 prefs::kStabilityPluginStats);
387 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33388 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29389 for (ListValue::const_iterator iter = plugin_stats_list->begin();
390 iter != plugin_stats_list->end(); ++iter) {
391 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
392 NOTREACHED();
393 continue;
394 }
395 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
396
[email protected]8e50b602009-03-03 22:59:43397 std::wstring plugin_name;
398 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29399
[email protected]ffaf78a2008-11-12 17:38:33400 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10401 // Use "filename" instead of "name", otherwise we need to update the
402 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43403 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29404
405 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43406 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29407 WriteIntAttribute("launchcount", launches);
408
409 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43410 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29411 WriteIntAttribute("instancecount", instances);
412
413 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43414 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29415 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29416 }
417
418 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29419 }
initial.commit09911bf2008-07-26 23:55:29420}
421
[email protected]147bbc0b2009-01-06 19:37:40422void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36423 // The server refuses data that doesn't have certain values. crashcount and
424 // launchcount are currently "required" in the "stability" group.
425 WriteIntAttribute("launchcount",
426 pref->GetInteger(prefs::kStabilityLaunchCount));
427 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
428 WriteIntAttribute("crashcount",
429 pref->GetInteger(prefs::kStabilityCrashCount));
430 pref->SetInteger(prefs::kStabilityCrashCount, 0);
431}
432
[email protected]147bbc0b2009-01-06 19:37:40433void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36434 // Update the stats which are critical for real-time stability monitoring.
435 // Since these are "optional," only list ones that are non-zero, as the counts
436 // are aggergated (summed) server side.
437
438 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
439 if (count) {
440 WriteIntAttribute("pageloadcount", count);
441 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
442 }
443
444 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
445 if (count) {
446 WriteIntAttribute("renderercrashcount", count);
447 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
448 }
449
450 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
451 if (count) {
452 WriteIntAttribute("rendererhangcount", count);
453 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
454 }
455}
456
initial.commit09911bf2008-07-26 23:55:29457void MetricsLog::WritePluginList(
458 const std::vector<WebPluginInfo>& plugin_list) {
459 DCHECK(!locked_);
460
[email protected]ffaf78a2008-11-12 17:38:33461 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29462
463 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
464 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33465 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29466
467 // Plugin name and filename are hashed for the privacy of those
468 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44469 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21470 WriteAttribute("filename",
471 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44472 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29473 }
initial.commit09911bf2008-07-26 23:55:29474}
475
[email protected]147bbc0b2009-01-06 19:37:40476void MetricsLog::WriteInstallElement() {
477 OPEN_ELEMENT_FOR_SCOPE("install");
478 WriteAttribute("installdate", GetInstallDate());
479 WriteIntAttribute("buildid", 0); // We're using appversion instead.
480 WriteAttribute("appversion", GetVersionString());
481}
482
initial.commit09911bf2008-07-26 23:55:29483void MetricsLog::RecordEnvironment(
484 const std::vector<WebPluginInfo>& plugin_list,
485 const DictionaryValue* profile_metrics) {
486 DCHECK(!locked_);
487
488 PrefService* pref = g_browser_process->local_state();
489
[email protected]ffaf78a2008-11-12 17:38:33490 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29491 WriteCommonEventAttributes();
492
[email protected]147bbc0b2009-01-06 19:37:40493 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29494
495 WritePluginList(plugin_list);
496
497 WriteStabilityElement();
498
499 {
500 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01501 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29502 }
503
504 {
initial.commit09911bf2008-07-26 23:55:29505 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40506 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38507#if defined(OS_WIN)
508 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
509#endif
initial.commit09911bf2008-07-26 23:55:29510 }
511
512 {
513 OPEN_ELEMENT_FOR_SCOPE("os");
514 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01515 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29516 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01517 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29518 }
519
520 {
521 OPEN_ELEMENT_FOR_SCOPE("display");
522 int width = 0;
523 int height = 0;
[email protected]05f9b682008-09-29 22:18:01524 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29525 WriteIntAttribute("xsize", width);
526 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01527 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29528 }
529
530 {
531 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
532 int num_bookmarks_on_bookmark_bar =
533 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
534 int num_folders_on_bookmark_bar =
535 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
536 int num_bookmarks_in_other_bookmarks_folder =
537 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
538 int num_folders_in_other_bookmarks_folder =
539 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
540 {
541 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
542 WriteAttribute("name", "full-tree");
543 WriteIntAttribute("foldercount",
544 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
545 WriteIntAttribute("itemcount",
546 num_bookmarks_on_bookmark_bar +
547 num_bookmarks_in_other_bookmarks_folder);
548 }
549 {
550 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
551 WriteAttribute("name", "toolbar");
552 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
553 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
554 }
555 }
556
557 {
558 OPEN_ELEMENT_FOR_SCOPE("keywords");
559 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
560 }
561
562 if (profile_metrics)
563 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29564}
565
566void MetricsLog::WriteAllProfilesMetrics(
567 const DictionaryValue& all_profiles_metrics) {
568 const std::wstring profile_prefix(prefs::kProfilePrefix);
569 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
570 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43571 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29572 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
573 DictionaryValue* profile;
[email protected]8e50b602009-03-03 22:59:43574 if (all_profiles_metrics.GetDictionary(key_name, &profile))
initial.commit09911bf2008-07-26 23:55:29575 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
576 }
577 }
578}
579
580void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
581 const DictionaryValue& profile_metrics) {
582 OPEN_ELEMENT_FOR_SCOPE("userprofile");
583 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
584 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
585 i != profile_metrics.end_keys(); ++i) {
586 Value* value;
587 if (profile_metrics.Get(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43588 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29589 switch (value->GetType()) {
590 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59591 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29592 if (value->GetAsString(&string_value)) {
593 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43594 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59595 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29596 }
597 break;
598 }
599
600 case Value::TYPE_BOOLEAN: {
601 bool bool_value;
602 if (value->GetAsBoolean(&bool_value)) {
603 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43604 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29605 WriteIntAttribute("value", bool_value ? 1 : 0);
606 }
607 break;
608 }
609
610 case Value::TYPE_INTEGER: {
611 int int_value;
612 if (value->GetAsInteger(&int_value)) {
613 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43614 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29615 WriteIntAttribute("value", int_value);
616 }
617 break;
618 }
619
620 default:
621 NOTREACHED();
622 break;
623 }
624 }
625 }
626}
627
628void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
629 DCHECK(!locked_);
630
[email protected]ffaf78a2008-11-12 17:38:33631 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29632 WriteAttribute("action", "autocomplete");
633 WriteAttribute("targetidhash", "");
634 // TODO(kochi): Properly track windows.
635 WriteIntAttribute("window", 0);
636 WriteCommonEventAttributes();
637
[email protected]ffaf78a2008-11-12 17:38:33638 {
639 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29640
[email protected]ffaf78a2008-11-12 17:38:33641 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
642 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
643 WriteIntAttribute("completedlength",
644 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00645 const std::string input_type(
646 AutocompleteInput::TypeToString(log.input_type));
647 if (!input_type.empty())
648 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29649
[email protected]ffaf78a2008-11-12 17:38:33650 for (AutocompleteResult::const_iterator i(log.result.begin());
651 i != log.result.end(); ++i) {
652 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
653 if (i->provider)
654 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00655 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
656 if (!result_type.empty())
657 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33658 WriteIntAttribute("relevance", i->relevance);
659 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
660 }
initial.commit09911bf2008-07-26 23:55:29661 }
initial.commit09911bf2008-07-26 23:55:29662
663 ++num_events_;
664}
665
666// TODO(JAR): A The following should really be part of the histogram class.
667// Internal state is being needlessly exposed, and it would be hard to reuse
668// this code. If we moved this into the Histogram class, then we could use
669// the same infrastructure for logging StatsCounters, RatesCounters, etc.
670void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
671 const Histogram::SampleSet& snapshot) {
672 DCHECK(!locked_);
673 DCHECK(0 != snapshot.TotalCount());
674 snapshot.CheckSize(histogram);
675
676 // We will ignore the MAX_INT/infinite value in the last element of range[].
677
678 OPEN_ELEMENT_FOR_SCOPE("histogram");
679
680 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
681
682 WriteInt64Attribute("sum", snapshot.sum());
683 WriteInt64Attribute("sumsquares", snapshot.square_sum());
684
685 for (size_t i = 0; i < histogram.bucket_count(); i++) {
686 if (snapshot.counts(i)) {
687 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
688 WriteIntAttribute("min", histogram.ranges(i));
689 WriteIntAttribute("max", histogram.ranges(i + 1));
690 WriteIntAttribute("count", snapshot.counts(i));
691 }
692 }
693}