blob: 4ce52737cf3921012a43153c89e0ba78cd41eb9c [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]d1be67b2008-11-19 20:28:388#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/file_util.h"
10#include "base/file_version_info.h"
11#include "base/md5.h"
12#include "base/scoped_ptr.h"
13#include "base/string_util.h"
[email protected]fadf97f2008-09-18 12:18:1414#include "base/sys_info.h"
[email protected]37f39e42010-01-06 17:35:1715#include "base/third_party/nspr/prtime.h"
initial.commit09911bf2008-07-26 23:55:2916#include "chrome/browser/autocomplete/autocomplete.h"
17#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/common/logging_chrome.h"
19#include "chrome/common/pref_names.h"
20#include "chrome/common/pref_service.h"
[email protected]46072d42008-07-28 14:49:3521#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2922
23#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
24
[email protected]e1acf6f2008-10-27 20:43:3325using base::Time;
26using base::TimeDelta;
27
[email protected]d1be67b2008-11-19 20:28:3828// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
29#if defined(OS_WIN)
30extern "C" IMAGE_DOS_HEADER __ImageBase;
31#endif
32
[email protected]5ed7d4572009-12-23 17:42:4133// static
34std::string MetricsLog::version_extension_;
35
initial.commit09911bf2008-07-26 23:55:2936// libxml take xmlChar*, which is unsigned char*
37inline const unsigned char* UnsignedChar(const char* input) {
38 return reinterpret_cast<const unsigned char*>(input);
39}
40
41// static
42void MetricsLog::RegisterPrefs(PrefService* local_state) {
43 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2944}
45
46MetricsLog::MetricsLog(const std::string& client_id, int session_id)
47 : start_time_(Time::Now()),
[email protected]29d02e52008-12-16 16:58:2748 client_id_(client_id),
49 session_id_(IntToString(session_id)),
initial.commit09911bf2008-07-26 23:55:2950 locked_(false),
51 buffer_(NULL),
52 writer_(NULL),
[email protected]29d02e52008-12-16 16:58:2753 num_events_(0) {
initial.commit09911bf2008-07-26 23:55:2954
55 buffer_ = xmlBufferCreate();
56 DCHECK(buffer_);
57
58 writer_ = xmlNewTextWriterMemory(buffer_, 0);
59 DCHECK(writer_);
60
61 int result = xmlTextWriterSetIndent(writer_, 2);
62 DCHECK_EQ(0, result);
63
64 StartElement("log");
65 WriteAttribute("clientid", client_id_);
[email protected]37f39e42010-01-06 17:35:1766 WriteInt64Attribute("buildtime", GetBuildTime());
initial.commit09911bf2008-07-26 23:55:2967
68 DCHECK_GE(result, 0);
69}
70
71MetricsLog::~MetricsLog() {
72 if (writer_)
73 xmlFreeTextWriter(writer_);
74
75 if (buffer_)
76 xmlBufferFree(buffer_);
77}
78
79void MetricsLog::CloseLog() {
80 DCHECK(!locked_);
81 locked_ = true;
82
83 int result = xmlTextWriterEndDocument(writer_);
[email protected]5ed7d4572009-12-23 17:42:4184 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2985
86 result = xmlTextWriterFlush(writer_);
[email protected]5ed7d4572009-12-23 17:42:4187 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2988}
89
90int MetricsLog::GetEncodedLogSize() {
91 DCHECK(locked_);
92 return buffer_->use;
93}
94
95bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
96 DCHECK(locked_);
97 if (buffer_size < GetEncodedLogSize())
98 return false;
99
100 memcpy(buffer, buffer_->content, GetEncodedLogSize());
101 return true;
102}
103
104int MetricsLog::GetElapsedSeconds() {
105 return static_cast<int>((Time::Now() - start_time_).InSeconds());
106}
107
108std::string MetricsLog::CreateHash(const std::string& value) {
109 MD5Context ctx;
110 MD5Init(&ctx);
111 MD5Update(&ctx, value.data(), value.length());
112
113 MD5Digest digest;
114 MD5Final(&digest, &ctx);
115
[email protected]be6bf5e2009-06-16 13:14:08116 uint64 reverse_uint64;
117 // UMA only uses first 8 chars of hash. We use the above uint64 instead
118 // of a unsigned char[8] so that we don't run into strict aliasing issues
119 // in the LOG statement below when trying to interpret reverse as a uint64.
120 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
121 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
122 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
123 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10124 // The following log is VERY helpful when folks add some named histogram into
125 // the code, but forgot to update the descriptive list of histograms. When
126 // that happens, all we get to see (server side) is a hash of the histogram
127 // name. We can then use this logging to find out what histogram name was
128 // being hashed to a given MD5 value by just running the version of Chromium
129 // in question with --enable-logging.
130 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08131 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29132 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
133}
134
135std::string MetricsLog::CreateBase64Hash(const std::string& string) {
136 std::string encoded_digest;
[email protected]978df342009-11-24 06:21:53137 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29138 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
139 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29140 }
[email protected]a9bb6f692008-07-30 16:40:10141 return std::string();
initial.commit09911bf2008-07-26 23:55:29142}
143
[email protected]afe3a1672009-11-17 19:04:12144void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29145 DCHECK(!locked_);
146
[email protected]afe3a1672009-11-17 19:04:12147 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29148 if (command_hash.empty()) {
149 NOTREACHED() << "Unable generate encoded hash of command: " << key;
150 return;
151 }
152
[email protected]ffaf78a2008-11-12 17:38:33153 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29154 WriteAttribute("action", "command");
155 WriteAttribute("targetidhash", command_hash);
156
157 // TODO(jhughes): Properly track windows.
158 WriteIntAttribute("window", 0);
159 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29160
161 ++num_events_;
162}
163
164void MetricsLog::RecordLoadEvent(int window_id,
165 const GURL& url,
166 PageTransition::Type origin,
167 int session_index,
168 TimeDelta load_time) {
169 DCHECK(!locked_);
170
[email protected]ffaf78a2008-11-12 17:38:33171 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29172 WriteAttribute("action", "load");
173 WriteIntAttribute("docid", session_index);
174 WriteIntAttribute("window", window_id);
175 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
176
177 std::string origin_string;
178
179 switch (PageTransition::StripQualifier(origin)) {
180 // TODO(jhughes): Some of these mappings aren't right... we need to add
181 // some values to the server's enum.
182 case PageTransition::LINK:
183 case PageTransition::MANUAL_SUBFRAME:
184 origin_string = "link";
185 break;
186
187 case PageTransition::TYPED:
188 origin_string = "typed";
189 break;
190
191 case PageTransition::AUTO_BOOKMARK:
192 origin_string = "bookmark";
193 break;
194
195 case PageTransition::AUTO_SUBFRAME:
196 case PageTransition::RELOAD:
197 origin_string = "refresh";
198 break;
199
200 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44201 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29202 origin_string = "global-history";
203 break;
204
205 case PageTransition::START_PAGE:
206 origin_string = "start-page";
207 break;
208
209 case PageTransition::FORM_SUBMIT:
210 origin_string = "form-submit";
211 break;
212
213 default:
214 NOTREACHED() << "Received an unknown page transition type: " <<
215 PageTransition::StripQualifier(origin);
216 }
217 if (!origin_string.empty())
218 WriteAttribute("origin", origin_string);
219
220 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29221
222 ++num_events_;
223}
224
initial.commit09911bf2008-07-26 23:55:29225void MetricsLog::RecordWindowEvent(WindowEventType type,
226 int window_id,
227 int parent_id) {
228 DCHECK(!locked_);
229
[email protected]ffaf78a2008-11-12 17:38:33230 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29231 WriteAttribute("action", WindowEventTypeToString(type));
232 WriteAttribute("windowid", IntToString(window_id));
233 if (parent_id >= 0)
234 WriteAttribute("parent", IntToString(parent_id));
235 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29236
237 ++num_events_;
238}
239
240std::string MetricsLog::GetCurrentTimeString() {
241 return Uint64ToString(Time::Now().ToTimeT());
242}
243
244// These are the attributes that are common to every event.
245void MetricsLog::WriteCommonEventAttributes() {
246 WriteAttribute("session", session_id_);
247 WriteAttribute("time", GetCurrentTimeString());
248}
249
250void MetricsLog::WriteAttribute(const std::string& name,
251 const std::string& value) {
252 DCHECK(!locked_);
253 DCHECK(!name.empty());
254
255 int result = xmlTextWriterWriteAttribute(writer_,
256 UnsignedChar(name.c_str()),
257 UnsignedChar(value.c_str()));
258 DCHECK_GE(result, 0);
259}
260
261void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
262 WriteAttribute(name, IntToString(value));
263}
264
265void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
266 WriteAttribute(name, Int64ToString(value));
267}
268
[email protected]ffaf78a2008-11-12 17:38:33269// static
270const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
271 switch (type) {
272 case WINDOW_CREATE: return "create";
273 case WINDOW_OPEN: return "open";
274 case WINDOW_CLOSE: return "close";
275 case WINDOW_DESTROY: return "destroy";
276
277 default:
278 NOTREACHED();
279 return "unknown"; // Can't return NULL as this is used in a required
280 // attribute.
281 }
282}
283
initial.commit09911bf2008-07-26 23:55:29284void MetricsLog::StartElement(const char* name) {
285 DCHECK(!locked_);
286 DCHECK(name);
287
288 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
289 DCHECK_GE(result, 0);
290}
291
292void MetricsLog::EndElement() {
293 DCHECK(!locked_);
294
295 int result = xmlTextWriterEndElement(writer_);
296 DCHECK_GE(result, 0);
297}
298
[email protected]541f77922009-02-23 21:14:38299// static
300std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29301 scoped_ptr<FileVersionInfo> version_info(
302 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
303 if (version_info.get()) {
304 std::string version = WideToUTF8(version_info->product_version());
[email protected]5ed7d4572009-12-23 17:42:41305 if (!version_extension_.empty())
306 version += version_extension_;
initial.commit09911bf2008-07-26 23:55:29307 if (!version_info->is_official_build())
308 version.append("-devel");
309 return version;
310 } else {
311 NOTREACHED() << "Unable to retrieve version string.";
312 }
313
314 return std::string();
315}
316
[email protected]225c50842010-01-19 21:19:13317// static
318int64 MetricsLog::GetBuildTime() {
319 static int64 integral_build_time = 0;
320 if (!integral_build_time) {
321 Time time;
322 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
323 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &time);
324 DCHECK(result);
325 integral_build_time = static_cast<int64>(time.ToTimeT());
326 }
327 return integral_build_time;
328}
329
initial.commit09911bf2008-07-26 23:55:29330std::string MetricsLog::GetInstallDate() const {
331 PrefService* pref = g_browser_process->local_state();
332 if (pref) {
333 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
334 } else {
335 NOTREACHED();
336 return "0";
337 }
338}
339
[email protected]0b33f80b2008-12-17 21:34:36340void MetricsLog::RecordIncrementalStabilityElements() {
341 DCHECK(!locked_);
342
343 PrefService* pref = g_browser_process->local_state();
344 DCHECK(pref);
345
[email protected]147bbc0b2009-01-06 19:37:40346 OPEN_ELEMENT_FOR_SCOPE("profile");
347 WriteCommonEventAttributes();
348
349 WriteInstallElement(); // Supply appversion.
350
351 {
352 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
353 WriteRequiredStabilityAttributes(pref);
354 WriteRealtimeStabilityAttributes(pref);
355
356 WritePluginStabilityElements(pref);
357 }
[email protected]0b33f80b2008-12-17 21:34:36358}
359
initial.commit09911bf2008-07-26 23:55:29360void MetricsLog::WriteStabilityElement() {
361 DCHECK(!locked_);
362
363 PrefService* pref = g_browser_process->local_state();
364 DCHECK(pref);
365
366 // Get stability attributes out of Local State, zeroing out stored values.
367 // NOTE: This could lead to some data loss if this report isn't successfully
368 // sent, but that's true for all the metrics.
369
[email protected]ffaf78a2008-11-12 17:38:33370 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40371 WriteRequiredStabilityAttributes(pref);
372 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29373
[email protected]0b33f80b2008-12-17 21:34:36374 // TODO(jar): The following are all optional, so we *could* optimize them for
375 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48376 WriteIntAttribute("incompleteshutdowncount",
377 pref->GetInteger(
378 prefs::kStabilityIncompleteSessionEndCount));
379 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36380
381
[email protected]e73c01972008-08-13 00:18:24382 WriteIntAttribute("breakpadregistrationok",
383 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
384 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
385 WriteIntAttribute("breakpadregistrationfail",
386 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
387 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
388 WriteIntAttribute("debuggerpresent",
389 pref->GetInteger(prefs::kStabilityDebuggerPresent));
390 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
391 WriteIntAttribute("debuggernotpresent",
392 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
393 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29394
395 // Uptime is stored as a string, since there's no int64 in Value/JSON.
396 WriteAttribute("uptimesec",
397 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
398 pref->SetString(prefs::kStabilityUptimeSec, L"0");
399
[email protected]147bbc0b2009-01-06 19:37:40400 WritePluginStabilityElements(pref);
401}
402
403void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
404 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29405 const ListValue* plugin_stats_list = pref->GetList(
406 prefs::kStabilityPluginStats);
407 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33408 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29409 for (ListValue::const_iterator iter = plugin_stats_list->begin();
410 iter != plugin_stats_list->end(); ++iter) {
411 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
412 NOTREACHED();
413 continue;
414 }
415 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
416
[email protected]8e50b602009-03-03 22:59:43417 std::wstring plugin_name;
418 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29419
[email protected]ffaf78a2008-11-12 17:38:33420 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10421 // Use "filename" instead of "name", otherwise we need to update the
422 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43423 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29424
425 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43426 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29427 WriteIntAttribute("launchcount", launches);
428
429 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43430 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29431 WriteIntAttribute("instancecount", instances);
432
433 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43434 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29435 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29436 }
437
438 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29439 }
initial.commit09911bf2008-07-26 23:55:29440}
441
[email protected]147bbc0b2009-01-06 19:37:40442void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36443 // The server refuses data that doesn't have certain values. crashcount and
444 // launchcount are currently "required" in the "stability" group.
445 WriteIntAttribute("launchcount",
446 pref->GetInteger(prefs::kStabilityLaunchCount));
447 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
448 WriteIntAttribute("crashcount",
449 pref->GetInteger(prefs::kStabilityCrashCount));
450 pref->SetInteger(prefs::kStabilityCrashCount, 0);
451}
452
[email protected]147bbc0b2009-01-06 19:37:40453void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36454 // Update the stats which are critical for real-time stability monitoring.
455 // Since these are "optional," only list ones that are non-zero, as the counts
456 // are aggergated (summed) server side.
457
458 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
459 if (count) {
460 WriteIntAttribute("pageloadcount", count);
461 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
462 }
463
464 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
465 if (count) {
466 WriteIntAttribute("renderercrashcount", count);
467 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
468 }
469
[email protected]1f085622009-12-04 05:33:45470 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
471 if (count) {
472 WriteIntAttribute("extensionrenderercrashcount", count);
473 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
474 }
475
[email protected]0b33f80b2008-12-17 21:34:36476 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
477 if (count) {
478 WriteIntAttribute("rendererhangcount", count);
479 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
480 }
[email protected]1f085622009-12-04 05:33:45481
482 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
483 if (count) {
484 WriteIntAttribute("childprocesscrashcount", count);
485 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
486 }
[email protected]0b33f80b2008-12-17 21:34:36487}
488
initial.commit09911bf2008-07-26 23:55:29489void MetricsLog::WritePluginList(
490 const std::vector<WebPluginInfo>& plugin_list) {
491 DCHECK(!locked_);
492
[email protected]ffaf78a2008-11-12 17:38:33493 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29494
495 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
496 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33497 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29498
499 // Plugin name and filename are hashed for the privacy of those
500 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44501 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21502 WriteAttribute("filename",
503 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44504 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29505 }
initial.commit09911bf2008-07-26 23:55:29506}
507
[email protected]147bbc0b2009-01-06 19:37:40508void MetricsLog::WriteInstallElement() {
509 OPEN_ELEMENT_FOR_SCOPE("install");
510 WriteAttribute("installdate", GetInstallDate());
511 WriteIntAttribute("buildid", 0); // We're using appversion instead.
512 WriteAttribute("appversion", GetVersionString());
513}
514
initial.commit09911bf2008-07-26 23:55:29515void MetricsLog::RecordEnvironment(
516 const std::vector<WebPluginInfo>& plugin_list,
517 const DictionaryValue* profile_metrics) {
518 DCHECK(!locked_);
519
520 PrefService* pref = g_browser_process->local_state();
521
[email protected]ffaf78a2008-11-12 17:38:33522 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29523 WriteCommonEventAttributes();
524
[email protected]147bbc0b2009-01-06 19:37:40525 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29526
527 WritePluginList(plugin_list);
528
529 WriteStabilityElement();
530
531 {
532 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01533 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29534 }
535
536 {
initial.commit09911bf2008-07-26 23:55:29537 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40538 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38539#if defined(OS_WIN)
540 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
541#endif
initial.commit09911bf2008-07-26 23:55:29542 }
543
544 {
545 OPEN_ELEMENT_FOR_SCOPE("os");
546 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01547 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29548 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01549 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29550 }
551
552 {
553 OPEN_ELEMENT_FOR_SCOPE("display");
554 int width = 0;
555 int height = 0;
[email protected]05f9b682008-09-29 22:18:01556 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29557 WriteIntAttribute("xsize", width);
558 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01559 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29560 }
561
562 {
563 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
564 int num_bookmarks_on_bookmark_bar =
565 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
566 int num_folders_on_bookmark_bar =
567 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
568 int num_bookmarks_in_other_bookmarks_folder =
569 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
570 int num_folders_in_other_bookmarks_folder =
571 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
572 {
573 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
574 WriteAttribute("name", "full-tree");
575 WriteIntAttribute("foldercount",
576 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
577 WriteIntAttribute("itemcount",
578 num_bookmarks_on_bookmark_bar +
579 num_bookmarks_in_other_bookmarks_folder);
580 }
581 {
582 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
583 WriteAttribute("name", "toolbar");
584 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
585 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
586 }
587 }
588
589 {
590 OPEN_ELEMENT_FOR_SCOPE("keywords");
591 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
592 }
593
594 if (profile_metrics)
595 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29596}
597
598void MetricsLog::WriteAllProfilesMetrics(
599 const DictionaryValue& all_profiles_metrics) {
600 const std::wstring profile_prefix(prefs::kProfilePrefix);
601 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
602 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43603 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29604 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
605 DictionaryValue* profile;
[email protected]4dad9ad82009-11-25 20:47:52606 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
607 &profile))
initial.commit09911bf2008-07-26 23:55:29608 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
609 }
610 }
611}
612
613void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
614 const DictionaryValue& profile_metrics) {
615 OPEN_ELEMENT_FOR_SCOPE("userprofile");
616 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
617 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
618 i != profile_metrics.end_keys(); ++i) {
619 Value* value;
[email protected]4dad9ad82009-11-25 20:47:52620 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43621 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29622 switch (value->GetType()) {
623 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59624 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29625 if (value->GetAsString(&string_value)) {
626 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43627 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59628 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29629 }
630 break;
631 }
632
633 case Value::TYPE_BOOLEAN: {
634 bool bool_value;
635 if (value->GetAsBoolean(&bool_value)) {
636 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43637 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29638 WriteIntAttribute("value", bool_value ? 1 : 0);
639 }
640 break;
641 }
642
643 case Value::TYPE_INTEGER: {
644 int int_value;
645 if (value->GetAsInteger(&int_value)) {
646 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43647 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29648 WriteIntAttribute("value", int_value);
649 }
650 break;
651 }
652
653 default:
654 NOTREACHED();
655 break;
656 }
657 }
658 }
659}
660
661void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
662 DCHECK(!locked_);
663
[email protected]ffaf78a2008-11-12 17:38:33664 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29665 WriteAttribute("action", "autocomplete");
666 WriteAttribute("targetidhash", "");
667 // TODO(kochi): Properly track windows.
668 WriteIntAttribute("window", 0);
669 WriteCommonEventAttributes();
670
[email protected]ffaf78a2008-11-12 17:38:33671 {
672 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29673
[email protected]ffaf78a2008-11-12 17:38:33674 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
675 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
676 WriteIntAttribute("completedlength",
677 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00678 const std::string input_type(
679 AutocompleteInput::TypeToString(log.input_type));
680 if (!input_type.empty())
681 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29682
[email protected]ffaf78a2008-11-12 17:38:33683 for (AutocompleteResult::const_iterator i(log.result.begin());
684 i != log.result.end(); ++i) {
685 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
686 if (i->provider)
687 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00688 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
689 if (!result_type.empty())
690 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33691 WriteIntAttribute("relevance", i->relevance);
692 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
693 }
initial.commit09911bf2008-07-26 23:55:29694 }
initial.commit09911bf2008-07-26 23:55:29695
696 ++num_events_;
697}
698
699// TODO(JAR): A The following should really be part of the histogram class.
700// Internal state is being needlessly exposed, and it would be hard to reuse
701// this code. If we moved this into the Histogram class, then we could use
702// the same infrastructure for logging StatsCounters, RatesCounters, etc.
703void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
704 const Histogram::SampleSet& snapshot) {
705 DCHECK(!locked_);
[email protected]5ed7d4572009-12-23 17:42:41706 DCHECK_NE(0, snapshot.TotalCount());
initial.commit09911bf2008-07-26 23:55:29707 snapshot.CheckSize(histogram);
708
709 // We will ignore the MAX_INT/infinite value in the last element of range[].
710
711 OPEN_ELEMENT_FOR_SCOPE("histogram");
712
713 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
714
715 WriteInt64Attribute("sum", snapshot.sum());
716 WriteInt64Attribute("sumsquares", snapshot.square_sum());
717
718 for (size_t i = 0; i < histogram.bucket_count(); i++) {
719 if (snapshot.counts(i)) {
720 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
721 WriteIntAttribute("min", histogram.ranges(i));
722 WriteIntAttribute("max", histogram.ranges(i + 1));
723 WriteIntAttribute("count", snapshot.counts(i));
724 }
725 }
726}