blob: 2c7c6177e9b50575ed8749f4b6d8216542f26bd3 [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
[email protected]37f39e42010-01-06 17:35:1741static int64 GetBuildTime();
42
initial.commit09911bf2008-07-26 23:55:2943// static
44void MetricsLog::RegisterPrefs(PrefService* local_state) {
45 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2946}
47
48MetricsLog::MetricsLog(const std::string& client_id, int session_id)
49 : start_time_(Time::Now()),
[email protected]29d02e52008-12-16 16:58:2750 client_id_(client_id),
51 session_id_(IntToString(session_id)),
initial.commit09911bf2008-07-26 23:55:2952 locked_(false),
53 buffer_(NULL),
54 writer_(NULL),
[email protected]29d02e52008-12-16 16:58:2755 num_events_(0) {
initial.commit09911bf2008-07-26 23:55:2956
57 buffer_ = xmlBufferCreate();
58 DCHECK(buffer_);
59
60 writer_ = xmlNewTextWriterMemory(buffer_, 0);
61 DCHECK(writer_);
62
63 int result = xmlTextWriterSetIndent(writer_, 2);
64 DCHECK_EQ(0, result);
65
66 StartElement("log");
67 WriteAttribute("clientid", client_id_);
[email protected]37f39e42010-01-06 17:35:1768 WriteInt64Attribute("buildtime", GetBuildTime());
initial.commit09911bf2008-07-26 23:55:2969
70 DCHECK_GE(result, 0);
71}
72
73MetricsLog::~MetricsLog() {
74 if (writer_)
75 xmlFreeTextWriter(writer_);
76
77 if (buffer_)
78 xmlBufferFree(buffer_);
79}
80
81void MetricsLog::CloseLog() {
82 DCHECK(!locked_);
83 locked_ = true;
84
85 int result = xmlTextWriterEndDocument(writer_);
[email protected]5ed7d4572009-12-23 17:42:4186 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2987
88 result = xmlTextWriterFlush(writer_);
[email protected]5ed7d4572009-12-23 17:42:4189 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2990}
91
92int MetricsLog::GetEncodedLogSize() {
93 DCHECK(locked_);
94 return buffer_->use;
95}
96
97bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
98 DCHECK(locked_);
99 if (buffer_size < GetEncodedLogSize())
100 return false;
101
102 memcpy(buffer, buffer_->content, GetEncodedLogSize());
103 return true;
104}
105
106int MetricsLog::GetElapsedSeconds() {
107 return static_cast<int>((Time::Now() - start_time_).InSeconds());
108}
109
110std::string MetricsLog::CreateHash(const std::string& value) {
111 MD5Context ctx;
112 MD5Init(&ctx);
113 MD5Update(&ctx, value.data(), value.length());
114
115 MD5Digest digest;
116 MD5Final(&digest, &ctx);
117
[email protected]be6bf5e2009-06-16 13:14:08118 uint64 reverse_uint64;
119 // UMA only uses first 8 chars of hash. We use the above uint64 instead
120 // of a unsigned char[8] so that we don't run into strict aliasing issues
121 // in the LOG statement below when trying to interpret reverse as a uint64.
122 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
123 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
124 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
125 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10126 // The following log is VERY helpful when folks add some named histogram into
127 // the code, but forgot to update the descriptive list of histograms. When
128 // that happens, all we get to see (server side) is a hash of the histogram
129 // name. We can then use this logging to find out what histogram name was
130 // being hashed to a given MD5 value by just running the version of Chromium
131 // in question with --enable-logging.
132 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08133 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29134 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
135}
136
137std::string MetricsLog::CreateBase64Hash(const std::string& string) {
138 std::string encoded_digest;
[email protected]978df342009-11-24 06:21:53139 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29140 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
141 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29142 }
[email protected]a9bb6f692008-07-30 16:40:10143 return std::string();
initial.commit09911bf2008-07-26 23:55:29144}
145
[email protected]afe3a1672009-11-17 19:04:12146void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29147 DCHECK(!locked_);
148
[email protected]afe3a1672009-11-17 19:04:12149 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29150 if (command_hash.empty()) {
151 NOTREACHED() << "Unable generate encoded hash of command: " << key;
152 return;
153 }
154
[email protected]ffaf78a2008-11-12 17:38:33155 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29156 WriteAttribute("action", "command");
157 WriteAttribute("targetidhash", command_hash);
158
159 // TODO(jhughes): Properly track windows.
160 WriteIntAttribute("window", 0);
161 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29162
163 ++num_events_;
164}
165
166void MetricsLog::RecordLoadEvent(int window_id,
167 const GURL& url,
168 PageTransition::Type origin,
169 int session_index,
170 TimeDelta load_time) {
171 DCHECK(!locked_);
172
[email protected]ffaf78a2008-11-12 17:38:33173 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29174 WriteAttribute("action", "load");
175 WriteIntAttribute("docid", session_index);
176 WriteIntAttribute("window", window_id);
177 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
178
179 std::string origin_string;
180
181 switch (PageTransition::StripQualifier(origin)) {
182 // TODO(jhughes): Some of these mappings aren't right... we need to add
183 // some values to the server's enum.
184 case PageTransition::LINK:
185 case PageTransition::MANUAL_SUBFRAME:
186 origin_string = "link";
187 break;
188
189 case PageTransition::TYPED:
190 origin_string = "typed";
191 break;
192
193 case PageTransition::AUTO_BOOKMARK:
194 origin_string = "bookmark";
195 break;
196
197 case PageTransition::AUTO_SUBFRAME:
198 case PageTransition::RELOAD:
199 origin_string = "refresh";
200 break;
201
202 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44203 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29204 origin_string = "global-history";
205 break;
206
207 case PageTransition::START_PAGE:
208 origin_string = "start-page";
209 break;
210
211 case PageTransition::FORM_SUBMIT:
212 origin_string = "form-submit";
213 break;
214
215 default:
216 NOTREACHED() << "Received an unknown page transition type: " <<
217 PageTransition::StripQualifier(origin);
218 }
219 if (!origin_string.empty())
220 WriteAttribute("origin", origin_string);
221
222 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29223
224 ++num_events_;
225}
226
initial.commit09911bf2008-07-26 23:55:29227void MetricsLog::RecordWindowEvent(WindowEventType type,
228 int window_id,
229 int parent_id) {
230 DCHECK(!locked_);
231
[email protected]ffaf78a2008-11-12 17:38:33232 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29233 WriteAttribute("action", WindowEventTypeToString(type));
234 WriteAttribute("windowid", IntToString(window_id));
235 if (parent_id >= 0)
236 WriteAttribute("parent", IntToString(parent_id));
237 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29238
239 ++num_events_;
240}
241
242std::string MetricsLog::GetCurrentTimeString() {
243 return Uint64ToString(Time::Now().ToTimeT());
244}
245
246// These are the attributes that are common to every event.
247void MetricsLog::WriteCommonEventAttributes() {
248 WriteAttribute("session", session_id_);
249 WriteAttribute("time", GetCurrentTimeString());
250}
251
252void MetricsLog::WriteAttribute(const std::string& name,
253 const std::string& value) {
254 DCHECK(!locked_);
255 DCHECK(!name.empty());
256
257 int result = xmlTextWriterWriteAttribute(writer_,
258 UnsignedChar(name.c_str()),
259 UnsignedChar(value.c_str()));
260 DCHECK_GE(result, 0);
261}
262
263void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
264 WriteAttribute(name, IntToString(value));
265}
266
267void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
268 WriteAttribute(name, Int64ToString(value));
269}
270
[email protected]ffaf78a2008-11-12 17:38:33271// static
272const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
273 switch (type) {
274 case WINDOW_CREATE: return "create";
275 case WINDOW_OPEN: return "open";
276 case WINDOW_CLOSE: return "close";
277 case WINDOW_DESTROY: return "destroy";
278
279 default:
280 NOTREACHED();
281 return "unknown"; // Can't return NULL as this is used in a required
282 // attribute.
283 }
284}
285
initial.commit09911bf2008-07-26 23:55:29286void MetricsLog::StartElement(const char* name) {
287 DCHECK(!locked_);
288 DCHECK(name);
289
290 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
291 DCHECK_GE(result, 0);
292}
293
294void MetricsLog::EndElement() {
295 DCHECK(!locked_);
296
297 int result = xmlTextWriterEndElement(writer_);
298 DCHECK_GE(result, 0);
299}
300
[email protected]541f77922009-02-23 21:14:38301// static
302std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29303 scoped_ptr<FileVersionInfo> version_info(
304 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
305 if (version_info.get()) {
306 std::string version = WideToUTF8(version_info->product_version());
[email protected]5ed7d4572009-12-23 17:42:41307 if (!version_extension_.empty())
308 version += version_extension_;
initial.commit09911bf2008-07-26 23:55:29309 if (!version_info->is_official_build())
310 version.append("-devel");
311 return version;
312 } else {
313 NOTREACHED() << "Unable to retrieve version string.";
314 }
315
316 return std::string();
317}
318
319std::string MetricsLog::GetInstallDate() const {
320 PrefService* pref = g_browser_process->local_state();
321 if (pref) {
322 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
323 } else {
324 NOTREACHED();
325 return "0";
326 }
327}
328
[email protected]0b33f80b2008-12-17 21:34:36329void MetricsLog::RecordIncrementalStabilityElements() {
330 DCHECK(!locked_);
331
332 PrefService* pref = g_browser_process->local_state();
333 DCHECK(pref);
334
[email protected]147bbc0b2009-01-06 19:37:40335 OPEN_ELEMENT_FOR_SCOPE("profile");
336 WriteCommonEventAttributes();
337
338 WriteInstallElement(); // Supply appversion.
339
340 {
341 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
342 WriteRequiredStabilityAttributes(pref);
343 WriteRealtimeStabilityAttributes(pref);
344
345 WritePluginStabilityElements(pref);
346 }
[email protected]0b33f80b2008-12-17 21:34:36347}
348
initial.commit09911bf2008-07-26 23:55:29349void MetricsLog::WriteStabilityElement() {
350 DCHECK(!locked_);
351
352 PrefService* pref = g_browser_process->local_state();
353 DCHECK(pref);
354
355 // Get stability attributes out of Local State, zeroing out stored values.
356 // NOTE: This could lead to some data loss if this report isn't successfully
357 // sent, but that's true for all the metrics.
358
[email protected]ffaf78a2008-11-12 17:38:33359 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40360 WriteRequiredStabilityAttributes(pref);
361 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29362
[email protected]0b33f80b2008-12-17 21:34:36363 // TODO(jar): The following are all optional, so we *could* optimize them for
364 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48365 WriteIntAttribute("incompleteshutdowncount",
366 pref->GetInteger(
367 prefs::kStabilityIncompleteSessionEndCount));
368 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36369
370
[email protected]e73c01972008-08-13 00:18:24371 WriteIntAttribute("breakpadregistrationok",
372 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
373 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
374 WriteIntAttribute("breakpadregistrationfail",
375 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
376 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
377 WriteIntAttribute("debuggerpresent",
378 pref->GetInteger(prefs::kStabilityDebuggerPresent));
379 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
380 WriteIntAttribute("debuggernotpresent",
381 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
382 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29383
384 // Uptime is stored as a string, since there's no int64 in Value/JSON.
385 WriteAttribute("uptimesec",
386 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
387 pref->SetString(prefs::kStabilityUptimeSec, L"0");
388
[email protected]147bbc0b2009-01-06 19:37:40389 WritePluginStabilityElements(pref);
390}
391
392void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
393 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29394 const ListValue* plugin_stats_list = pref->GetList(
395 prefs::kStabilityPluginStats);
396 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33397 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29398 for (ListValue::const_iterator iter = plugin_stats_list->begin();
399 iter != plugin_stats_list->end(); ++iter) {
400 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
401 NOTREACHED();
402 continue;
403 }
404 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
405
[email protected]8e50b602009-03-03 22:59:43406 std::wstring plugin_name;
407 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29408
[email protected]ffaf78a2008-11-12 17:38:33409 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10410 // Use "filename" instead of "name", otherwise we need to update the
411 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43412 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29413
414 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43415 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29416 WriteIntAttribute("launchcount", launches);
417
418 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43419 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29420 WriteIntAttribute("instancecount", instances);
421
422 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43423 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29424 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29425 }
426
427 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29428 }
initial.commit09911bf2008-07-26 23:55:29429}
430
[email protected]147bbc0b2009-01-06 19:37:40431void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36432 // The server refuses data that doesn't have certain values. crashcount and
433 // launchcount are currently "required" in the "stability" group.
434 WriteIntAttribute("launchcount",
435 pref->GetInteger(prefs::kStabilityLaunchCount));
436 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
437 WriteIntAttribute("crashcount",
438 pref->GetInteger(prefs::kStabilityCrashCount));
439 pref->SetInteger(prefs::kStabilityCrashCount, 0);
440}
441
[email protected]147bbc0b2009-01-06 19:37:40442void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36443 // Update the stats which are critical for real-time stability monitoring.
444 // Since these are "optional," only list ones that are non-zero, as the counts
445 // are aggergated (summed) server side.
446
447 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
448 if (count) {
449 WriteIntAttribute("pageloadcount", count);
450 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
451 }
452
453 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
454 if (count) {
455 WriteIntAttribute("renderercrashcount", count);
456 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
457 }
458
[email protected]1f085622009-12-04 05:33:45459 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
460 if (count) {
461 WriteIntAttribute("extensionrenderercrashcount", count);
462 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
463 }
464
[email protected]0b33f80b2008-12-17 21:34:36465 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
466 if (count) {
467 WriteIntAttribute("rendererhangcount", count);
468 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
469 }
[email protected]1f085622009-12-04 05:33:45470
471 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
472 if (count) {
473 WriteIntAttribute("childprocesscrashcount", count);
474 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
475 }
[email protected]0b33f80b2008-12-17 21:34:36476}
477
initial.commit09911bf2008-07-26 23:55:29478void MetricsLog::WritePluginList(
479 const std::vector<WebPluginInfo>& plugin_list) {
480 DCHECK(!locked_);
481
[email protected]ffaf78a2008-11-12 17:38:33482 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29483
484 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
485 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33486 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29487
488 // Plugin name and filename are hashed for the privacy of those
489 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44490 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21491 WriteAttribute("filename",
492 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44493 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29494 }
initial.commit09911bf2008-07-26 23:55:29495}
496
[email protected]147bbc0b2009-01-06 19:37:40497void MetricsLog::WriteInstallElement() {
498 OPEN_ELEMENT_FOR_SCOPE("install");
499 WriteAttribute("installdate", GetInstallDate());
500 WriteIntAttribute("buildid", 0); // We're using appversion instead.
501 WriteAttribute("appversion", GetVersionString());
502}
503
initial.commit09911bf2008-07-26 23:55:29504void MetricsLog::RecordEnvironment(
505 const std::vector<WebPluginInfo>& plugin_list,
506 const DictionaryValue* profile_metrics) {
507 DCHECK(!locked_);
508
509 PrefService* pref = g_browser_process->local_state();
510
[email protected]ffaf78a2008-11-12 17:38:33511 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29512 WriteCommonEventAttributes();
513
[email protected]147bbc0b2009-01-06 19:37:40514 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29515
516 WritePluginList(plugin_list);
517
518 WriteStabilityElement();
519
520 {
521 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01522 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29523 }
524
525 {
initial.commit09911bf2008-07-26 23:55:29526 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40527 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38528#if defined(OS_WIN)
529 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
530#endif
initial.commit09911bf2008-07-26 23:55:29531 }
532
533 {
534 OPEN_ELEMENT_FOR_SCOPE("os");
535 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01536 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29537 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01538 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29539 }
540
541 {
542 OPEN_ELEMENT_FOR_SCOPE("display");
543 int width = 0;
544 int height = 0;
[email protected]05f9b682008-09-29 22:18:01545 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29546 WriteIntAttribute("xsize", width);
547 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01548 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29549 }
550
551 {
552 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
553 int num_bookmarks_on_bookmark_bar =
554 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
555 int num_folders_on_bookmark_bar =
556 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
557 int num_bookmarks_in_other_bookmarks_folder =
558 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
559 int num_folders_in_other_bookmarks_folder =
560 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
561 {
562 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
563 WriteAttribute("name", "full-tree");
564 WriteIntAttribute("foldercount",
565 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
566 WriteIntAttribute("itemcount",
567 num_bookmarks_on_bookmark_bar +
568 num_bookmarks_in_other_bookmarks_folder);
569 }
570 {
571 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
572 WriteAttribute("name", "toolbar");
573 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
574 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
575 }
576 }
577
578 {
579 OPEN_ELEMENT_FOR_SCOPE("keywords");
580 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
581 }
582
583 if (profile_metrics)
584 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29585}
586
587void MetricsLog::WriteAllProfilesMetrics(
588 const DictionaryValue& all_profiles_metrics) {
589 const std::wstring profile_prefix(prefs::kProfilePrefix);
590 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
591 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43592 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29593 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
594 DictionaryValue* profile;
[email protected]4dad9ad82009-11-25 20:47:52595 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
596 &profile))
initial.commit09911bf2008-07-26 23:55:29597 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
598 }
599 }
600}
601
602void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
603 const DictionaryValue& profile_metrics) {
604 OPEN_ELEMENT_FOR_SCOPE("userprofile");
605 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
606 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
607 i != profile_metrics.end_keys(); ++i) {
608 Value* value;
[email protected]4dad9ad82009-11-25 20:47:52609 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43610 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29611 switch (value->GetType()) {
612 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59613 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29614 if (value->GetAsString(&string_value)) {
615 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43616 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59617 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29618 }
619 break;
620 }
621
622 case Value::TYPE_BOOLEAN: {
623 bool bool_value;
624 if (value->GetAsBoolean(&bool_value)) {
625 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43626 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29627 WriteIntAttribute("value", bool_value ? 1 : 0);
628 }
629 break;
630 }
631
632 case Value::TYPE_INTEGER: {
633 int int_value;
634 if (value->GetAsInteger(&int_value)) {
635 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43636 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29637 WriteIntAttribute("value", int_value);
638 }
639 break;
640 }
641
642 default:
643 NOTREACHED();
644 break;
645 }
646 }
647 }
648}
649
650void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
651 DCHECK(!locked_);
652
[email protected]ffaf78a2008-11-12 17:38:33653 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29654 WriteAttribute("action", "autocomplete");
655 WriteAttribute("targetidhash", "");
656 // TODO(kochi): Properly track windows.
657 WriteIntAttribute("window", 0);
658 WriteCommonEventAttributes();
659
[email protected]ffaf78a2008-11-12 17:38:33660 {
661 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29662
[email protected]ffaf78a2008-11-12 17:38:33663 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
664 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
665 WriteIntAttribute("completedlength",
666 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00667 const std::string input_type(
668 AutocompleteInput::TypeToString(log.input_type));
669 if (!input_type.empty())
670 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29671
[email protected]ffaf78a2008-11-12 17:38:33672 for (AutocompleteResult::const_iterator i(log.result.begin());
673 i != log.result.end(); ++i) {
674 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
675 if (i->provider)
676 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00677 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
678 if (!result_type.empty())
679 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33680 WriteIntAttribute("relevance", i->relevance);
681 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
682 }
initial.commit09911bf2008-07-26 23:55:29683 }
initial.commit09911bf2008-07-26 23:55:29684
685 ++num_events_;
686}
687
688// TODO(JAR): A The following should really be part of the histogram class.
689// Internal state is being needlessly exposed, and it would be hard to reuse
690// this code. If we moved this into the Histogram class, then we could use
691// the same infrastructure for logging StatsCounters, RatesCounters, etc.
692void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
693 const Histogram::SampleSet& snapshot) {
694 DCHECK(!locked_);
[email protected]5ed7d4572009-12-23 17:42:41695 DCHECK_NE(0, snapshot.TotalCount());
initial.commit09911bf2008-07-26 23:55:29696 snapshot.CheckSize(histogram);
697
698 // We will ignore the MAX_INT/infinite value in the last element of range[].
699
700 OPEN_ELEMENT_FOR_SCOPE("histogram");
701
702 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
703
704 WriteInt64Attribute("sum", snapshot.sum());
705 WriteInt64Attribute("sumsquares", snapshot.square_sum());
706
707 for (size_t i = 0; i < histogram.bucket_count(); i++) {
708 if (snapshot.counts(i)) {
709 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
710 WriteIntAttribute("min", histogram.ranges(i));
711 WriteIntAttribute("max", histogram.ranges(i + 1));
712 WriteIntAttribute("count", snapshot.counts(i));
713 }
714 }
715}
[email protected]37f39e42010-01-06 17:35:17716
717static int64 GetBuildTime() {
718 Time parsed_time;
719 const char* kDateTime = __DATE__ " " __TIME__;
720 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &parsed_time);
721 DCHECK(result);
722 return static_cast<int64>(parsed_time.ToTimeT());
723}