blob: dd4aa3f47d7ab35cfd778686c24dffbf58eaaf5e [file] [log] [blame]
pkasting@chromium.org4dad9ad82009-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
ben@chromium.orgcd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
hayato@chromium.org978df342009-11-24 06:21:537#include "base/base64.h"
deanm@chromium.orgd1be67b2008-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"
deanm@chromium.orgfadf97f2008-09-18 12:18:1414#include "base/sys_info.h"
jar@chromium.org37f39e42010-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"
phajdan.jr@chromium.org052313b2010-02-19 09:43:0818#include "chrome/browser/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/common/logging_chrome.h"
20#include "chrome/common/pref_names.h"
deanm@google.com46072d42008-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
dsh@google.come1acf6f2008-10-27 20:43:3325using base::Time;
26using base::TimeDelta;
27
deanm@chromium.orgd1be67b2008-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
jar@chromium.org5ed7d4572009-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()),
phajdan.jr@chromium.org29d02e52008-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),
phajdan.jr@chromium.org29d02e52008-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_);
jar@chromium.org37f39e42010-01-06 17:35:1766 WriteInt64Attribute("buildtime", GetBuildTime());
jar@chromium.orgefc607b2010-02-19 21:31:5767 WriteAttribute("appversion", GetVersionString());
initial.commit09911bf2008-07-26 23:55:2968
69 DCHECK_GE(result, 0);
70}
71
72MetricsLog::~MetricsLog() {
73 if (writer_)
74 xmlFreeTextWriter(writer_);
75
76 if (buffer_)
77 xmlBufferFree(buffer_);
78}
79
80void MetricsLog::CloseLog() {
81 DCHECK(!locked_);
82 locked_ = true;
83
84 int result = xmlTextWriterEndDocument(writer_);
jar@chromium.org5ed7d4572009-12-23 17:42:4185 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2986
87 result = xmlTextWriterFlush(writer_);
jar@chromium.org5ed7d4572009-12-23 17:42:4188 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2989}
90
91int MetricsLog::GetEncodedLogSize() {
92 DCHECK(locked_);
93 return buffer_->use;
94}
95
96bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
97 DCHECK(locked_);
98 if (buffer_size < GetEncodedLogSize())
99 return false;
100
101 memcpy(buffer, buffer_->content, GetEncodedLogSize());
102 return true;
103}
104
105int MetricsLog::GetElapsedSeconds() {
106 return static_cast<int>((Time::Now() - start_time_).InSeconds());
107}
108
109std::string MetricsLog::CreateHash(const std::string& value) {
110 MD5Context ctx;
111 MD5Init(&ctx);
112 MD5Update(&ctx, value.data(), value.length());
113
114 MD5Digest digest;
115 MD5Final(&digest, &ctx);
116
craig.schlenter@chromium.orgbe6bf5e2009-06-16 13:14:08117 uint64 reverse_uint64;
118 // UMA only uses first 8 chars of hash. We use the above uint64 instead
119 // of a unsigned char[8] so that we don't run into strict aliasing issues
120 // in the LOG statement below when trying to interpret reverse as a uint64.
121 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
122 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
123 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
124 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
jar@chromium.orgf88ba61a2009-06-12 16:52:10125 // The following log is VERY helpful when folks add some named histogram into
126 // the code, but forgot to update the descriptive list of histograms. When
127 // that happens, all we get to see (server side) is a hash of the histogram
128 // name. We can then use this logging to find out what histogram name was
129 // being hashed to a given MD5 value by just running the version of Chromium
130 // in question with --enable-logging.
131 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
craig.schlenter@chromium.orgbe6bf5e2009-06-16 13:14:08132 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29133 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
134}
135
136std::string MetricsLog::CreateBase64Hash(const std::string& string) {
137 std::string encoded_digest;
hayato@chromium.org978df342009-11-24 06:21:53138 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29139 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
140 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29141 }
darin@google.coma9bb6f692008-07-30 16:40:10142 return std::string();
initial.commit09911bf2008-07-26 23:55:29143}
144
evan@chromium.orgafe3a1672009-11-17 19:04:12145void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29146 DCHECK(!locked_);
147
evan@chromium.orgafe3a1672009-11-17 19:04:12148 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29149 if (command_hash.empty()) {
150 NOTREACHED() << "Unable generate encoded hash of command: " << key;
151 return;
152 }
153
pkasting@chromium.orgffaf78a2008-11-12 17:38:33154 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29155 WriteAttribute("action", "command");
156 WriteAttribute("targetidhash", command_hash);
157
158 // TODO(jhughes): Properly track windows.
159 WriteIntAttribute("window", 0);
160 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29161
162 ++num_events_;
163}
164
165void MetricsLog::RecordLoadEvent(int window_id,
166 const GURL& url,
167 PageTransition::Type origin,
168 int session_index,
169 TimeDelta load_time) {
170 DCHECK(!locked_);
171
pkasting@chromium.orgffaf78a2008-11-12 17:38:33172 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29173 WriteAttribute("action", "load");
174 WriteIntAttribute("docid", session_index);
175 WriteIntAttribute("window", window_id);
176 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
177
178 std::string origin_string;
179
180 switch (PageTransition::StripQualifier(origin)) {
181 // TODO(jhughes): Some of these mappings aren't right... we need to add
182 // some values to the server's enum.
183 case PageTransition::LINK:
184 case PageTransition::MANUAL_SUBFRAME:
185 origin_string = "link";
186 break;
187
188 case PageTransition::TYPED:
189 origin_string = "typed";
190 break;
191
192 case PageTransition::AUTO_BOOKMARK:
193 origin_string = "bookmark";
194 break;
195
196 case PageTransition::AUTO_SUBFRAME:
197 case PageTransition::RELOAD:
198 origin_string = "refresh";
199 break;
200
201 case PageTransition::GENERATED:
sky@chromium.org0bfc29a2009-04-27 16:15:44202 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29203 origin_string = "global-history";
204 break;
205
206 case PageTransition::START_PAGE:
207 origin_string = "start-page";
208 break;
209
210 case PageTransition::FORM_SUBMIT:
211 origin_string = "form-submit";
212 break;
213
214 default:
215 NOTREACHED() << "Received an unknown page transition type: " <<
216 PageTransition::StripQualifier(origin);
217 }
218 if (!origin_string.empty())
219 WriteAttribute("origin", origin_string);
220
221 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29222
223 ++num_events_;
224}
225
initial.commit09911bf2008-07-26 23:55:29226void MetricsLog::RecordWindowEvent(WindowEventType type,
227 int window_id,
228 int parent_id) {
229 DCHECK(!locked_);
230
pkasting@chromium.orgffaf78a2008-11-12 17:38:33231 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29232 WriteAttribute("action", WindowEventTypeToString(type));
233 WriteAttribute("windowid", IntToString(window_id));
234 if (parent_id >= 0)
235 WriteAttribute("parent", IntToString(parent_id));
236 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29237
238 ++num_events_;
239}
240
241std::string MetricsLog::GetCurrentTimeString() {
242 return Uint64ToString(Time::Now().ToTimeT());
243}
244
245// These are the attributes that are common to every event.
246void MetricsLog::WriteCommonEventAttributes() {
247 WriteAttribute("session", session_id_);
248 WriteAttribute("time", GetCurrentTimeString());
249}
250
251void MetricsLog::WriteAttribute(const std::string& name,
252 const std::string& value) {
253 DCHECK(!locked_);
254 DCHECK(!name.empty());
255
256 int result = xmlTextWriterWriteAttribute(writer_,
257 UnsignedChar(name.c_str()),
258 UnsignedChar(value.c_str()));
259 DCHECK_GE(result, 0);
260}
261
262void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
263 WriteAttribute(name, IntToString(value));
264}
265
266void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
267 WriteAttribute(name, Int64ToString(value));
268}
269
pkasting@chromium.orgffaf78a2008-11-12 17:38:33270// static
271const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
272 switch (type) {
273 case WINDOW_CREATE: return "create";
274 case WINDOW_OPEN: return "open";
275 case WINDOW_CLOSE: return "close";
276 case WINDOW_DESTROY: return "destroy";
277
278 default:
279 NOTREACHED();
280 return "unknown"; // Can't return NULL as this is used in a required
281 // attribute.
282 }
283}
284
initial.commit09911bf2008-07-26 23:55:29285void MetricsLog::StartElement(const char* name) {
286 DCHECK(!locked_);
287 DCHECK(name);
288
289 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
290 DCHECK_GE(result, 0);
291}
292
293void MetricsLog::EndElement() {
294 DCHECK(!locked_);
295
296 int result = xmlTextWriterEndElement(writer_);
297 DCHECK_GE(result, 0);
298}
299
jar@chromium.org541f77922009-02-23 21:14:38300// static
301std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29302 scoped_ptr<FileVersionInfo> version_info(
303 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
304 if (version_info.get()) {
305 std::string version = WideToUTF8(version_info->product_version());
jar@chromium.org5ed7d4572009-12-23 17:42:41306 if (!version_extension_.empty())
307 version += version_extension_;
initial.commit09911bf2008-07-26 23:55:29308 if (!version_info->is_official_build())
309 version.append("-devel");
310 return version;
311 } else {
312 NOTREACHED() << "Unable to retrieve version string.";
313 }
314
315 return std::string();
316}
317
jar@chromium.org225c50842010-01-19 21:19:13318// static
319int64 MetricsLog::GetBuildTime() {
320 static int64 integral_build_time = 0;
321 if (!integral_build_time) {
322 Time time;
323 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
324 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &time);
325 DCHECK(result);
326 integral_build_time = static_cast<int64>(time.ToTimeT());
327 }
328 return integral_build_time;
329}
330
initial.commit09911bf2008-07-26 23:55:29331std::string MetricsLog::GetInstallDate() const {
332 PrefService* pref = g_browser_process->local_state();
333 if (pref) {
334 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
335 } else {
336 NOTREACHED();
337 return "0";
338 }
339}
340
jar@google.com0b33f80b2008-12-17 21:34:36341void MetricsLog::RecordIncrementalStabilityElements() {
342 DCHECK(!locked_);
343
344 PrefService* pref = g_browser_process->local_state();
345 DCHECK(pref);
346
jar@google.com147bbc0b2009-01-06 19:37:40347 OPEN_ELEMENT_FOR_SCOPE("profile");
348 WriteCommonEventAttributes();
349
350 WriteInstallElement(); // Supply appversion.
351
352 {
353 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
354 WriteRequiredStabilityAttributes(pref);
355 WriteRealtimeStabilityAttributes(pref);
356
357 WritePluginStabilityElements(pref);
358 }
jar@google.com0b33f80b2008-12-17 21:34:36359}
360
initial.commit09911bf2008-07-26 23:55:29361void MetricsLog::WriteStabilityElement() {
362 DCHECK(!locked_);
363
364 PrefService* pref = g_browser_process->local_state();
365 DCHECK(pref);
366
367 // Get stability attributes out of Local State, zeroing out stored values.
368 // NOTE: This could lead to some data loss if this report isn't successfully
369 // sent, but that's true for all the metrics.
370
pkasting@chromium.orgffaf78a2008-11-12 17:38:33371 OPEN_ELEMENT_FOR_SCOPE("stability");
jar@google.com147bbc0b2009-01-06 19:37:40372 WriteRequiredStabilityAttributes(pref);
373 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29374
jar@google.com0b33f80b2008-12-17 21:34:36375 // TODO(jar): The following are all optional, so we *could* optimize them for
376 // values of zero (and not include them).
evanm@google.com8e674e42008-07-30 16:05:48377 WriteIntAttribute("incompleteshutdowncount",
378 pref->GetInteger(
379 prefs::kStabilityIncompleteSessionEndCount));
380 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
jar@google.com0b33f80b2008-12-17 21:34:36381
382
cpu@google.come73c01972008-08-13 00:18:24383 WriteIntAttribute("breakpadregistrationok",
384 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
385 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
386 WriteIntAttribute("breakpadregistrationfail",
387 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
388 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
389 WriteIntAttribute("debuggerpresent",
390 pref->GetInteger(prefs::kStabilityDebuggerPresent));
391 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
392 WriteIntAttribute("debuggernotpresent",
393 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
394 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29395
396 // Uptime is stored as a string, since there's no int64 in Value/JSON.
397 WriteAttribute("uptimesec",
398 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
399 pref->SetString(prefs::kStabilityUptimeSec, L"0");
400
jar@google.com147bbc0b2009-01-06 19:37:40401 WritePluginStabilityElements(pref);
402}
403
404void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
405 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29406 const ListValue* plugin_stats_list = pref->GetList(
407 prefs::kStabilityPluginStats);
408 if (plugin_stats_list) {
pkasting@chromium.orgffaf78a2008-11-12 17:38:33409 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29410 for (ListValue::const_iterator iter = plugin_stats_list->begin();
411 iter != plugin_stats_list->end(); ++iter) {
412 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
413 NOTREACHED();
414 continue;
415 }
416 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
417
nsylvain@chromium.org8e50b602009-03-03 22:59:43418 std::wstring plugin_name;
419 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29420
pkasting@chromium.orgffaf78a2008-11-12 17:38:33421 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
jam@chromium.orga27a9382009-02-11 23:55:10422 // Use "filename" instead of "name", otherwise we need to update the
423 // UMA servers.
nsylvain@chromium.org8e50b602009-03-03 22:59:43424 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29425
426 int launches = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:43427 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29428 WriteIntAttribute("launchcount", launches);
429
430 int instances = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:43431 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29432 WriteIntAttribute("instancecount", instances);
433
434 int crashes = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:43435 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29436 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29437 }
438
439 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29440 }
initial.commit09911bf2008-07-26 23:55:29441}
442
jar@google.com147bbc0b2009-01-06 19:37:40443void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
jar@google.com0b33f80b2008-12-17 21:34:36444 // The server refuses data that doesn't have certain values. crashcount and
445 // launchcount are currently "required" in the "stability" group.
446 WriteIntAttribute("launchcount",
447 pref->GetInteger(prefs::kStabilityLaunchCount));
448 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
449 WriteIntAttribute("crashcount",
450 pref->GetInteger(prefs::kStabilityCrashCount));
451 pref->SetInteger(prefs::kStabilityCrashCount, 0);
452}
453
jar@google.com147bbc0b2009-01-06 19:37:40454void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
jar@google.com0b33f80b2008-12-17 21:34:36455 // Update the stats which are critical for real-time stability monitoring.
456 // Since these are "optional," only list ones that are non-zero, as the counts
457 // are aggergated (summed) server side.
458
459 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
460 if (count) {
461 WriteIntAttribute("pageloadcount", count);
462 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
463 }
464
465 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
466 if (count) {
467 WriteIntAttribute("renderercrashcount", count);
468 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
469 }
470
asargent@chromium.org1f085622009-12-04 05:33:45471 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
472 if (count) {
473 WriteIntAttribute("extensionrenderercrashcount", count);
474 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
475 }
476
jar@google.com0b33f80b2008-12-17 21:34:36477 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
478 if (count) {
479 WriteIntAttribute("rendererhangcount", count);
480 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
481 }
asargent@chromium.org1f085622009-12-04 05:33:45482
483 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
484 if (count) {
485 WriteIntAttribute("childprocesscrashcount", count);
486 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
487 }
jar@google.com0b33f80b2008-12-17 21:34:36488}
489
initial.commit09911bf2008-07-26 23:55:29490void MetricsLog::WritePluginList(
491 const std::vector<WebPluginInfo>& plugin_list) {
492 DCHECK(!locked_);
493
pkasting@chromium.orgffaf78a2008-11-12 17:38:33494 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29495
496 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
497 iter != plugin_list.end(); ++iter) {
pkasting@chromium.orgffaf78a2008-11-12 17:38:33498 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29499
500 // Plugin name and filename are hashed for the privacy of those
501 // testing unreleased new extensions.
jam@chromium.orgb7344502009-01-12 19:43:44502 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
jam@chromium.org046344c2009-01-13 00:54:21503 WriteAttribute("filename",
504 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
jam@chromium.orgb7344502009-01-12 19:43:44505 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29506 }
initial.commit09911bf2008-07-26 23:55:29507}
508
jar@google.com147bbc0b2009-01-06 19:37:40509void MetricsLog::WriteInstallElement() {
510 OPEN_ELEMENT_FOR_SCOPE("install");
511 WriteAttribute("installdate", GetInstallDate());
512 WriteIntAttribute("buildid", 0); // We're using appversion instead.
jar@google.com147bbc0b2009-01-06 19:37:40513}
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
pkasting@chromium.orgffaf78a2008-11-12 17:38:33522 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29523 WriteCommonEventAttributes();
524
jar@google.com147bbc0b2009-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");
mark@chromium.org05f9b682008-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");
deanm@chromium.orged6fc352008-09-18 12:44:40538 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
deanm@chromium.orgd1be67b2008-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",
mark@chromium.org05f9b682008-09-29 22:18:01547 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29548 WriteAttribute("version",
mark@chromium.org05f9b682008-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;
mark@chromium.org05f9b682008-09-29 22:18:01556 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29557 WriteIntAttribute("xsize", width);
558 WriteIntAttribute("ysize", height);
mark@chromium.org05f9b682008-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) {
nsylvain@chromium.org8e50b602009-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;
pkasting@chromium.org4dad9ad82009-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;
pkasting@chromium.org4dad9ad82009-11-25 20:47:52620 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
nsylvain@chromium.org8e50b602009-03-03 22:59:43621 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29622 switch (value->GetType()) {
623 case Value::TYPE_STRING: {
scherkus@chromium.org5e324b72008-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");
nsylvain@chromium.org8e50b602009-03-03 22:59:43627 WriteAttribute("name", WideToUTF8(*i));
scherkus@chromium.org5e324b72008-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");
nsylvain@chromium.org8e50b602009-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");
nsylvain@chromium.org8e50b602009-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
pkasting@chromium.orgffaf78a2008-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
pkasting@chromium.orgffaf78a2008-11-12 17:38:33671 {
672 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29673
pkasting@chromium.orgffaf78a2008-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));
pkasting@chromium.org381e2992008-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
pkasting@chromium.orgffaf78a2008-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());
pkasting@chromium.org381e2992008-12-16 01:41:00688 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
689 if (!result_type.empty())
690 WriteAttribute("resulttype", result_type);
pkasting@chromium.orgffaf78a2008-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_);
jar@chromium.org5ed7d4572009-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}