blob: baa7ec33482df35994159329529264b9ee499f4d [file] [log] [blame]
[email protected]4dad9ad82009-11-25 20:47:521// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]978df342009-11-24 06:21:537#include "base/base64.h"
[email protected]9165f742010-03-10 22:55:018#include "base/time.h"
[email protected]d1be67b2008-11-19 20:28:389#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/file_util.h"
11#include "base/file_version_info.h"
12#include "base/md5.h"
13#include "base/scoped_ptr.h"
14#include "base/string_util.h"
[email protected]fadf97f2008-09-18 12:18:1415#include "base/sys_info.h"
[email protected]1cb92b82010-03-08 23:12:1516#include "base/utf_string_conversions.h"
[email protected]37f39e42010-01-06 17:35:1717#include "base/third_party/nspr/prtime.h"
[email protected]bcff05a2010-04-14 01:46:4318#include "chrome/app/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/autocomplete/autocomplete.h"
20#include "chrome/browser/browser_process.h"
[email protected]052313b2010-02-19 09:43:0821#include "chrome/browser/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/common/logging_chrome.h"
23#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3524#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2925
26#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
27
[email protected]e1acf6f2008-10-27 20:43:3328using base::Time;
29using base::TimeDelta;
30
[email protected]d1be67b2008-11-19 20:28:3831// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
32#if defined(OS_WIN)
33extern "C" IMAGE_DOS_HEADER __ImageBase;
34#endif
35
[email protected]5ed7d4572009-12-23 17:42:4136// static
37std::string MetricsLog::version_extension_;
38
initial.commit09911bf2008-07-26 23:55:2939// libxml take xmlChar*, which is unsigned char*
40inline const unsigned char* UnsignedChar(const char* input) {
41 return reinterpret_cast<const unsigned char*>(input);
42}
43
44// static
45void MetricsLog::RegisterPrefs(PrefService* local_state) {
46 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2947}
48
49MetricsLog::MetricsLog(const std::string& client_id, int session_id)
50 : start_time_(Time::Now()),
[email protected]29d02e52008-12-16 16:58:2751 client_id_(client_id),
52 session_id_(IntToString(session_id)),
initial.commit09911bf2008-07-26 23:55:2953 locked_(false),
54 buffer_(NULL),
55 writer_(NULL),
[email protected]29d02e52008-12-16 16:58:2756 num_events_(0) {
initial.commit09911bf2008-07-26 23:55:2957
58 buffer_ = xmlBufferCreate();
59 DCHECK(buffer_);
60
61 writer_ = xmlNewTextWriterMemory(buffer_, 0);
62 DCHECK(writer_);
63
64 int result = xmlTextWriterSetIndent(writer_, 2);
65 DCHECK_EQ(0, result);
66
67 StartElement("log");
68 WriteAttribute("clientid", client_id_);
[email protected]37f39e42010-01-06 17:35:1769 WriteInt64Attribute("buildtime", GetBuildTime());
[email protected]efc607b2010-02-19 21:31:5770 WriteAttribute("appversion", GetVersionString());
initial.commit09911bf2008-07-26 23:55:2971
72 DCHECK_GE(result, 0);
73}
74
75MetricsLog::~MetricsLog() {
76 if (writer_)
77 xmlFreeTextWriter(writer_);
78
79 if (buffer_)
80 xmlBufferFree(buffer_);
81}
82
83void MetricsLog::CloseLog() {
84 DCHECK(!locked_);
85 locked_ = true;
86
87 int result = xmlTextWriterEndDocument(writer_);
[email protected]5ed7d4572009-12-23 17:42:4188 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2989
90 result = xmlTextWriterFlush(writer_);
[email protected]5ed7d4572009-12-23 17:42:4191 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2992}
93
94int MetricsLog::GetEncodedLogSize() {
95 DCHECK(locked_);
96 return buffer_->use;
97}
98
99bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
100 DCHECK(locked_);
101 if (buffer_size < GetEncodedLogSize())
102 return false;
103
104 memcpy(buffer, buffer_->content, GetEncodedLogSize());
105 return true;
106}
107
108int MetricsLog::GetElapsedSeconds() {
109 return static_cast<int>((Time::Now() - start_time_).InSeconds());
110}
111
112std::string MetricsLog::CreateHash(const std::string& value) {
113 MD5Context ctx;
114 MD5Init(&ctx);
115 MD5Update(&ctx, value.data(), value.length());
116
117 MD5Digest digest;
118 MD5Final(&digest, &ctx);
119
[email protected]be6bf5e2009-06-16 13:14:08120 uint64 reverse_uint64;
121 // UMA only uses first 8 chars of hash. We use the above uint64 instead
122 // of a unsigned char[8] so that we don't run into strict aliasing issues
123 // in the LOG statement below when trying to interpret reverse as a uint64.
124 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
125 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
126 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
127 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10128 // The following log is VERY helpful when folks add some named histogram into
129 // the code, but forgot to update the descriptive list of histograms. When
130 // that happens, all we get to see (server side) is a hash of the histogram
131 // name. We can then use this logging to find out what histogram name was
132 // being hashed to a given MD5 value by just running the version of Chromium
133 // in question with --enable-logging.
134 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08135 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29136 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
137}
138
139std::string MetricsLog::CreateBase64Hash(const std::string& string) {
140 std::string encoded_digest;
[email protected]978df342009-11-24 06:21:53141 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29142 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
143 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29144 }
[email protected]a9bb6f692008-07-30 16:40:10145 return std::string();
initial.commit09911bf2008-07-26 23:55:29146}
147
[email protected]afe3a1672009-11-17 19:04:12148void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29149 DCHECK(!locked_);
150
[email protected]afe3a1672009-11-17 19:04:12151 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29152 if (command_hash.empty()) {
153 NOTREACHED() << "Unable generate encoded hash of command: " << key;
154 return;
155 }
156
[email protected]ffaf78a2008-11-12 17:38:33157 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29158 WriteAttribute("action", "command");
159 WriteAttribute("targetidhash", command_hash);
160
161 // TODO(jhughes): Properly track windows.
162 WriteIntAttribute("window", 0);
163 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29164
165 ++num_events_;
166}
167
168void MetricsLog::RecordLoadEvent(int window_id,
169 const GURL& url,
170 PageTransition::Type origin,
171 int session_index,
172 TimeDelta load_time) {
173 DCHECK(!locked_);
174
[email protected]ffaf78a2008-11-12 17:38:33175 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29176 WriteAttribute("action", "load");
177 WriteIntAttribute("docid", session_index);
178 WriteIntAttribute("window", window_id);
179 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
180
181 std::string origin_string;
182
183 switch (PageTransition::StripQualifier(origin)) {
184 // TODO(jhughes): Some of these mappings aren't right... we need to add
185 // some values to the server's enum.
186 case PageTransition::LINK:
187 case PageTransition::MANUAL_SUBFRAME:
188 origin_string = "link";
189 break;
190
191 case PageTransition::TYPED:
192 origin_string = "typed";
193 break;
194
195 case PageTransition::AUTO_BOOKMARK:
196 origin_string = "bookmark";
197 break;
198
199 case PageTransition::AUTO_SUBFRAME:
200 case PageTransition::RELOAD:
201 origin_string = "refresh";
202 break;
203
204 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44205 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29206 origin_string = "global-history";
207 break;
208
209 case PageTransition::START_PAGE:
210 origin_string = "start-page";
211 break;
212
213 case PageTransition::FORM_SUBMIT:
214 origin_string = "form-submit";
215 break;
216
217 default:
218 NOTREACHED() << "Received an unknown page transition type: " <<
219 PageTransition::StripQualifier(origin);
220 }
221 if (!origin_string.empty())
222 WriteAttribute("origin", origin_string);
223
224 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29225
226 ++num_events_;
227}
228
initial.commit09911bf2008-07-26 23:55:29229void MetricsLog::RecordWindowEvent(WindowEventType type,
230 int window_id,
231 int parent_id) {
232 DCHECK(!locked_);
233
[email protected]ffaf78a2008-11-12 17:38:33234 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29235 WriteAttribute("action", WindowEventTypeToString(type));
236 WriteAttribute("windowid", IntToString(window_id));
237 if (parent_id >= 0)
238 WriteAttribute("parent", IntToString(parent_id));
239 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29240
241 ++num_events_;
242}
243
244std::string MetricsLog::GetCurrentTimeString() {
245 return Uint64ToString(Time::Now().ToTimeT());
246}
247
248// These are the attributes that are common to every event.
249void MetricsLog::WriteCommonEventAttributes() {
250 WriteAttribute("session", session_id_);
251 WriteAttribute("time", GetCurrentTimeString());
252}
253
254void MetricsLog::WriteAttribute(const std::string& name,
255 const std::string& value) {
256 DCHECK(!locked_);
257 DCHECK(!name.empty());
258
259 int result = xmlTextWriterWriteAttribute(writer_,
260 UnsignedChar(name.c_str()),
261 UnsignedChar(value.c_str()));
262 DCHECK_GE(result, 0);
263}
264
265void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
266 WriteAttribute(name, IntToString(value));
267}
268
269void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
270 WriteAttribute(name, Int64ToString(value));
271}
272
[email protected]ffaf78a2008-11-12 17:38:33273// static
274const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
275 switch (type) {
276 case WINDOW_CREATE: return "create";
277 case WINDOW_OPEN: return "open";
278 case WINDOW_CLOSE: return "close";
279 case WINDOW_DESTROY: return "destroy";
280
281 default:
282 NOTREACHED();
283 return "unknown"; // Can't return NULL as this is used in a required
284 // attribute.
285 }
286}
287
initial.commit09911bf2008-07-26 23:55:29288void MetricsLog::StartElement(const char* name) {
289 DCHECK(!locked_);
290 DCHECK(name);
291
292 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
293 DCHECK_GE(result, 0);
294}
295
296void MetricsLog::EndElement() {
297 DCHECK(!locked_);
298
299 int result = xmlTextWriterEndElement(writer_);
300 DCHECK_GE(result, 0);
301}
302
[email protected]541f77922009-02-23 21:14:38303// static
304std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29305 scoped_ptr<FileVersionInfo> version_info(
[email protected]bcff05a2010-04-14 01:46:43306 chrome_app::GetChromeVersionInfo());
initial.commit09911bf2008-07-26 23:55:29307 if (version_info.get()) {
308 std::string version = WideToUTF8(version_info->product_version());
[email protected]5ed7d4572009-12-23 17:42:41309 if (!version_extension_.empty())
310 version += version_extension_;
initial.commit09911bf2008-07-26 23:55:29311 if (!version_info->is_official_build())
312 version.append("-devel");
313 return version;
314 } else {
315 NOTREACHED() << "Unable to retrieve version string.";
316 }
317
318 return std::string();
319}
320
[email protected]225c50842010-01-19 21:19:13321// static
322int64 MetricsLog::GetBuildTime() {
323 static int64 integral_build_time = 0;
324 if (!integral_build_time) {
325 Time time;
326 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
327 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &time);
328 DCHECK(result);
329 integral_build_time = static_cast<int64>(time.ToTimeT());
330 }
331 return integral_build_time;
332}
333
[email protected]9165f742010-03-10 22:55:01334// static
335int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
336 base::TimeTicks now = base::TimeTicks::Now();
337 static base::TimeTicks last_updated_time(now);
338 int64 incremental_time = (now - last_updated_time).InSeconds();
339 last_updated_time = now;
340
341 if (incremental_time > 0) {
342 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
343 metrics_uptime += incremental_time;
344 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
345 }
346
347 return incremental_time;
348}
349
initial.commit09911bf2008-07-26 23:55:29350std::string MetricsLog::GetInstallDate() const {
351 PrefService* pref = g_browser_process->local_state();
352 if (pref) {
353 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
354 } else {
355 NOTREACHED();
356 return "0";
357 }
358}
359
[email protected]0b33f80b2008-12-17 21:34:36360void MetricsLog::RecordIncrementalStabilityElements() {
361 DCHECK(!locked_);
362
363 PrefService* pref = g_browser_process->local_state();
364 DCHECK(pref);
365
[email protected]147bbc0b2009-01-06 19:37:40366 OPEN_ELEMENT_FOR_SCOPE("profile");
367 WriteCommonEventAttributes();
368
369 WriteInstallElement(); // Supply appversion.
370
371 {
372 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
373 WriteRequiredStabilityAttributes(pref);
374 WriteRealtimeStabilityAttributes(pref);
375
376 WritePluginStabilityElements(pref);
377 }
[email protected]0b33f80b2008-12-17 21:34:36378}
379
initial.commit09911bf2008-07-26 23:55:29380void MetricsLog::WriteStabilityElement() {
381 DCHECK(!locked_);
382
383 PrefService* pref = g_browser_process->local_state();
384 DCHECK(pref);
385
386 // Get stability attributes out of Local State, zeroing out stored values.
387 // NOTE: This could lead to some data loss if this report isn't successfully
388 // sent, but that's true for all the metrics.
389
[email protected]ffaf78a2008-11-12 17:38:33390 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40391 WriteRequiredStabilityAttributes(pref);
392 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29393
[email protected]0b33f80b2008-12-17 21:34:36394 // TODO(jar): The following are all optional, so we *could* optimize them for
395 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48396 WriteIntAttribute("incompleteshutdowncount",
397 pref->GetInteger(
398 prefs::kStabilityIncompleteSessionEndCount));
399 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36400
401
[email protected]e73c01972008-08-13 00:18:24402 WriteIntAttribute("breakpadregistrationok",
403 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
404 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
405 WriteIntAttribute("breakpadregistrationfail",
406 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
407 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
408 WriteIntAttribute("debuggerpresent",
409 pref->GetInteger(prefs::kStabilityDebuggerPresent));
410 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
411 WriteIntAttribute("debuggernotpresent",
412 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
413 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29414
[email protected]147bbc0b2009-01-06 19:37:40415 WritePluginStabilityElements(pref);
416}
417
418void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
419 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29420 const ListValue* plugin_stats_list = pref->GetList(
421 prefs::kStabilityPluginStats);
422 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33423 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29424 for (ListValue::const_iterator iter = plugin_stats_list->begin();
425 iter != plugin_stats_list->end(); ++iter) {
426 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
427 NOTREACHED();
428 continue;
429 }
430 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
431
[email protected]8e50b602009-03-03 22:59:43432 std::wstring plugin_name;
433 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29434
[email protected]ffaf78a2008-11-12 17:38:33435 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10436 // Use "filename" instead of "name", otherwise we need to update the
437 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43438 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29439
440 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43441 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29442 WriteIntAttribute("launchcount", launches);
443
444 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43445 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29446 WriteIntAttribute("instancecount", instances);
447
448 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43449 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29450 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29451 }
452
453 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29454 }
initial.commit09911bf2008-07-26 23:55:29455}
456
[email protected]147bbc0b2009-01-06 19:37:40457void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36458 // The server refuses data that doesn't have certain values. crashcount and
459 // launchcount are currently "required" in the "stability" group.
460 WriteIntAttribute("launchcount",
461 pref->GetInteger(prefs::kStabilityLaunchCount));
462 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
463 WriteIntAttribute("crashcount",
464 pref->GetInteger(prefs::kStabilityCrashCount));
465 pref->SetInteger(prefs::kStabilityCrashCount, 0);
466}
467
[email protected]147bbc0b2009-01-06 19:37:40468void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36469 // Update the stats which are critical for real-time stability monitoring.
470 // Since these are "optional," only list ones that are non-zero, as the counts
471 // are aggergated (summed) server side.
472
473 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
474 if (count) {
475 WriteIntAttribute("pageloadcount", count);
476 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
477 }
478
479 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
480 if (count) {
481 WriteIntAttribute("renderercrashcount", count);
482 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
483 }
484
[email protected]1f085622009-12-04 05:33:45485 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
486 if (count) {
487 WriteIntAttribute("extensionrenderercrashcount", count);
488 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
489 }
490
[email protected]0b33f80b2008-12-17 21:34:36491 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
492 if (count) {
493 WriteIntAttribute("rendererhangcount", count);
494 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
495 }
[email protected]1f085622009-12-04 05:33:45496
497 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
498 if (count) {
499 WriteIntAttribute("childprocesscrashcount", count);
500 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
501 }
[email protected]9165f742010-03-10 22:55:01502
503 int64 recent_duration = GetIncrementalUptime(pref);
504 if (recent_duration)
505 WriteInt64Attribute("uptimesec", recent_duration);
[email protected]0b33f80b2008-12-17 21:34:36506}
507
initial.commit09911bf2008-07-26 23:55:29508void MetricsLog::WritePluginList(
509 const std::vector<WebPluginInfo>& plugin_list) {
510 DCHECK(!locked_);
511
[email protected]ffaf78a2008-11-12 17:38:33512 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29513
514 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
515 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33516 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29517
518 // Plugin name and filename are hashed for the privacy of those
519 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44520 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21521 WriteAttribute("filename",
522 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44523 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29524 }
initial.commit09911bf2008-07-26 23:55:29525}
526
[email protected]147bbc0b2009-01-06 19:37:40527void MetricsLog::WriteInstallElement() {
528 OPEN_ELEMENT_FOR_SCOPE("install");
529 WriteAttribute("installdate", GetInstallDate());
530 WriteIntAttribute("buildid", 0); // We're using appversion instead.
[email protected]147bbc0b2009-01-06 19:37:40531}
532
initial.commit09911bf2008-07-26 23:55:29533void MetricsLog::RecordEnvironment(
534 const std::vector<WebPluginInfo>& plugin_list,
535 const DictionaryValue* profile_metrics) {
536 DCHECK(!locked_);
537
538 PrefService* pref = g_browser_process->local_state();
539
[email protected]ffaf78a2008-11-12 17:38:33540 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29541 WriteCommonEventAttributes();
542
[email protected]147bbc0b2009-01-06 19:37:40543 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29544
545 WritePluginList(plugin_list);
546
547 WriteStabilityElement();
548
549 {
550 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01551 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29552 }
553
554 {
initial.commit09911bf2008-07-26 23:55:29555 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40556 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38557#if defined(OS_WIN)
558 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
559#endif
initial.commit09911bf2008-07-26 23:55:29560 }
561
562 {
563 OPEN_ELEMENT_FOR_SCOPE("os");
564 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01565 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29566 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01567 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29568 }
569
570 {
571 OPEN_ELEMENT_FOR_SCOPE("display");
572 int width = 0;
573 int height = 0;
[email protected]05f9b682008-09-29 22:18:01574 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29575 WriteIntAttribute("xsize", width);
576 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01577 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29578 }
579
580 {
581 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
582 int num_bookmarks_on_bookmark_bar =
583 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
584 int num_folders_on_bookmark_bar =
585 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
586 int num_bookmarks_in_other_bookmarks_folder =
587 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
588 int num_folders_in_other_bookmarks_folder =
589 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
590 {
591 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
592 WriteAttribute("name", "full-tree");
593 WriteIntAttribute("foldercount",
594 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
595 WriteIntAttribute("itemcount",
596 num_bookmarks_on_bookmark_bar +
597 num_bookmarks_in_other_bookmarks_folder);
598 }
599 {
600 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
601 WriteAttribute("name", "toolbar");
602 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
603 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
604 }
605 }
606
607 {
608 OPEN_ELEMENT_FOR_SCOPE("keywords");
609 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
610 }
611
612 if (profile_metrics)
613 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29614}
615
616void MetricsLog::WriteAllProfilesMetrics(
617 const DictionaryValue& all_profiles_metrics) {
618 const std::wstring profile_prefix(prefs::kProfilePrefix);
619 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
620 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43621 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29622 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
623 DictionaryValue* profile;
[email protected]4dad9ad82009-11-25 20:47:52624 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
625 &profile))
initial.commit09911bf2008-07-26 23:55:29626 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
627 }
628 }
629}
630
631void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
632 const DictionaryValue& profile_metrics) {
633 OPEN_ELEMENT_FOR_SCOPE("userprofile");
634 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
635 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
636 i != profile_metrics.end_keys(); ++i) {
637 Value* value;
[email protected]4dad9ad82009-11-25 20:47:52638 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43639 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29640 switch (value->GetType()) {
641 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59642 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29643 if (value->GetAsString(&string_value)) {
644 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43645 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59646 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29647 }
648 break;
649 }
650
651 case Value::TYPE_BOOLEAN: {
652 bool bool_value;
653 if (value->GetAsBoolean(&bool_value)) {
654 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43655 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29656 WriteIntAttribute("value", bool_value ? 1 : 0);
657 }
658 break;
659 }
660
661 case Value::TYPE_INTEGER: {
662 int int_value;
663 if (value->GetAsInteger(&int_value)) {
664 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43665 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29666 WriteIntAttribute("value", int_value);
667 }
668 break;
669 }
670
671 default:
672 NOTREACHED();
673 break;
674 }
675 }
676 }
677}
678
679void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
680 DCHECK(!locked_);
681
[email protected]ffaf78a2008-11-12 17:38:33682 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29683 WriteAttribute("action", "autocomplete");
684 WriteAttribute("targetidhash", "");
685 // TODO(kochi): Properly track windows.
686 WriteIntAttribute("window", 0);
687 WriteCommonEventAttributes();
688
[email protected]ffaf78a2008-11-12 17:38:33689 {
690 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29691
[email protected]ffaf78a2008-11-12 17:38:33692 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
693 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
694 WriteIntAttribute("completedlength",
695 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00696 const std::string input_type(
697 AutocompleteInput::TypeToString(log.input_type));
698 if (!input_type.empty())
699 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29700
[email protected]ffaf78a2008-11-12 17:38:33701 for (AutocompleteResult::const_iterator i(log.result.begin());
702 i != log.result.end(); ++i) {
703 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
704 if (i->provider)
705 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00706 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
707 if (!result_type.empty())
708 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33709 WriteIntAttribute("relevance", i->relevance);
710 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
711 }
initial.commit09911bf2008-07-26 23:55:29712 }
initial.commit09911bf2008-07-26 23:55:29713
714 ++num_events_;
715}
716
717// TODO(JAR): A The following should really be part of the histogram class.
718// Internal state is being needlessly exposed, and it would be hard to reuse
719// this code. If we moved this into the Histogram class, then we could use
720// the same infrastructure for logging StatsCounters, RatesCounters, etc.
721void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
722 const Histogram::SampleSet& snapshot) {
723 DCHECK(!locked_);
[email protected]5ed7d4572009-12-23 17:42:41724 DCHECK_NE(0, snapshot.TotalCount());
initial.commit09911bf2008-07-26 23:55:29725 snapshot.CheckSize(histogram);
726
727 // We will ignore the MAX_INT/infinite value in the last element of range[].
728
729 OPEN_ELEMENT_FOR_SCOPE("histogram");
730
731 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
732
733 WriteInt64Attribute("sum", snapshot.sum());
734 WriteInt64Attribute("sumsquares", snapshot.square_sum());
735
736 for (size_t i = 0; i < histogram.bucket_count(); i++) {
737 if (snapshot.counts(i)) {
738 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
739 WriteIntAttribute("min", histogram.ranges(i));
740 WriteIntAttribute("max", histogram.ranges(i + 1));
741 WriteIntAttribute("count", snapshot.counts(i));
742 }
743 }
744}