blob: 03dd92559759ee121f2532b477ff26c2efab430b [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"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/autocomplete/autocomplete.h"
19#include "chrome/browser/browser_process.h"
[email protected]052313b2010-02-19 09:43:0820#include "chrome/browser/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/common/logging_chrome.h"
22#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3523#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2924
25#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
26
[email protected]e1acf6f2008-10-27 20:43:3327using base::Time;
28using base::TimeDelta;
29
[email protected]d1be67b2008-11-19 20:28:3830// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
31#if defined(OS_WIN)
32extern "C" IMAGE_DOS_HEADER __ImageBase;
33#endif
34
[email protected]5ed7d4572009-12-23 17:42:4135// static
36std::string MetricsLog::version_extension_;
37
initial.commit09911bf2008-07-26 23:55:2938// libxml take xmlChar*, which is unsigned char*
39inline const unsigned char* UnsignedChar(const char* input) {
40 return reinterpret_cast<const unsigned char*>(input);
41}
42
43// 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());
[email protected]efc607b2010-02-19 21:31:5769 WriteAttribute("appversion", GetVersionString());
initial.commit09911bf2008-07-26 23:55:2970
71 DCHECK_GE(result, 0);
72}
73
74MetricsLog::~MetricsLog() {
75 if (writer_)
76 xmlFreeTextWriter(writer_);
77
78 if (buffer_)
79 xmlBufferFree(buffer_);
80}
81
82void MetricsLog::CloseLog() {
83 DCHECK(!locked_);
84 locked_ = true;
85
86 int result = xmlTextWriterEndDocument(writer_);
[email protected]5ed7d4572009-12-23 17:42:4187 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2988
89 result = xmlTextWriterFlush(writer_);
[email protected]5ed7d4572009-12-23 17:42:4190 DCHECK_GE(result, 0);
initial.commit09911bf2008-07-26 23:55:2991}
92
93int MetricsLog::GetEncodedLogSize() {
94 DCHECK(locked_);
95 return buffer_->use;
96}
97
98bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
99 DCHECK(locked_);
100 if (buffer_size < GetEncodedLogSize())
101 return false;
102
103 memcpy(buffer, buffer_->content, GetEncodedLogSize());
104 return true;
105}
106
107int MetricsLog::GetElapsedSeconds() {
108 return static_cast<int>((Time::Now() - start_time_).InSeconds());
109}
110
111std::string MetricsLog::CreateHash(const std::string& value) {
112 MD5Context ctx;
113 MD5Init(&ctx);
114 MD5Update(&ctx, value.data(), value.length());
115
116 MD5Digest digest;
117 MD5Final(&digest, &ctx);
118
[email protected]be6bf5e2009-06-16 13:14:08119 uint64 reverse_uint64;
120 // UMA only uses first 8 chars of hash. We use the above uint64 instead
121 // of a unsigned char[8] so that we don't run into strict aliasing issues
122 // in the LOG statement below when trying to interpret reverse as a uint64.
123 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
124 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
125 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
126 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10127 // The following log is VERY helpful when folks add some named histogram into
128 // the code, but forgot to update the descriptive list of histograms. When
129 // that happens, all we get to see (server side) is a hash of the histogram
130 // name. We can then use this logging to find out what histogram name was
131 // being hashed to a given MD5 value by just running the version of Chromium
132 // in question with --enable-logging.
133 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08134 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29135 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
136}
137
138std::string MetricsLog::CreateBase64Hash(const std::string& string) {
139 std::string encoded_digest;
[email protected]978df342009-11-24 06:21:53140 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29141 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
142 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29143 }
[email protected]a9bb6f692008-07-30 16:40:10144 return std::string();
initial.commit09911bf2008-07-26 23:55:29145}
146
[email protected]afe3a1672009-11-17 19:04:12147void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29148 DCHECK(!locked_);
149
[email protected]afe3a1672009-11-17 19:04:12150 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29151 if (command_hash.empty()) {
152 NOTREACHED() << "Unable generate encoded hash of command: " << key;
153 return;
154 }
155
[email protected]ffaf78a2008-11-12 17:38:33156 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29157 WriteAttribute("action", "command");
158 WriteAttribute("targetidhash", command_hash);
159
160 // TODO(jhughes): Properly track windows.
161 WriteIntAttribute("window", 0);
162 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29163
164 ++num_events_;
165}
166
167void MetricsLog::RecordLoadEvent(int window_id,
168 const GURL& url,
169 PageTransition::Type origin,
170 int session_index,
171 TimeDelta load_time) {
172 DCHECK(!locked_);
173
[email protected]ffaf78a2008-11-12 17:38:33174 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29175 WriteAttribute("action", "load");
176 WriteIntAttribute("docid", session_index);
177 WriteIntAttribute("window", window_id);
178 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
179
180 std::string origin_string;
181
182 switch (PageTransition::StripQualifier(origin)) {
183 // TODO(jhughes): Some of these mappings aren't right... we need to add
184 // some values to the server's enum.
185 case PageTransition::LINK:
186 case PageTransition::MANUAL_SUBFRAME:
187 origin_string = "link";
188 break;
189
190 case PageTransition::TYPED:
191 origin_string = "typed";
192 break;
193
194 case PageTransition::AUTO_BOOKMARK:
195 origin_string = "bookmark";
196 break;
197
198 case PageTransition::AUTO_SUBFRAME:
199 case PageTransition::RELOAD:
200 origin_string = "refresh";
201 break;
202
203 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44204 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29205 origin_string = "global-history";
206 break;
207
208 case PageTransition::START_PAGE:
209 origin_string = "start-page";
210 break;
211
212 case PageTransition::FORM_SUBMIT:
213 origin_string = "form-submit";
214 break;
215
216 default:
217 NOTREACHED() << "Received an unknown page transition type: " <<
218 PageTransition::StripQualifier(origin);
219 }
220 if (!origin_string.empty())
221 WriteAttribute("origin", origin_string);
222
223 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29224
225 ++num_events_;
226}
227
initial.commit09911bf2008-07-26 23:55:29228void MetricsLog::RecordWindowEvent(WindowEventType type,
229 int window_id,
230 int parent_id) {
231 DCHECK(!locked_);
232
[email protected]ffaf78a2008-11-12 17:38:33233 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29234 WriteAttribute("action", WindowEventTypeToString(type));
235 WriteAttribute("windowid", IntToString(window_id));
236 if (parent_id >= 0)
237 WriteAttribute("parent", IntToString(parent_id));
238 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29239
240 ++num_events_;
241}
242
243std::string MetricsLog::GetCurrentTimeString() {
244 return Uint64ToString(Time::Now().ToTimeT());
245}
246
247// These are the attributes that are common to every event.
248void MetricsLog::WriteCommonEventAttributes() {
249 WriteAttribute("session", session_id_);
250 WriteAttribute("time", GetCurrentTimeString());
251}
252
253void MetricsLog::WriteAttribute(const std::string& name,
254 const std::string& value) {
255 DCHECK(!locked_);
256 DCHECK(!name.empty());
257
258 int result = xmlTextWriterWriteAttribute(writer_,
259 UnsignedChar(name.c_str()),
260 UnsignedChar(value.c_str()));
261 DCHECK_GE(result, 0);
262}
263
264void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
265 WriteAttribute(name, IntToString(value));
266}
267
268void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
269 WriteAttribute(name, Int64ToString(value));
270}
271
[email protected]ffaf78a2008-11-12 17:38:33272// static
273const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
274 switch (type) {
275 case WINDOW_CREATE: return "create";
276 case WINDOW_OPEN: return "open";
277 case WINDOW_CLOSE: return "close";
278 case WINDOW_DESTROY: return "destroy";
279
280 default:
281 NOTREACHED();
282 return "unknown"; // Can't return NULL as this is used in a required
283 // attribute.
284 }
285}
286
initial.commit09911bf2008-07-26 23:55:29287void MetricsLog::StartElement(const char* name) {
288 DCHECK(!locked_);
289 DCHECK(name);
290
291 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
292 DCHECK_GE(result, 0);
293}
294
295void MetricsLog::EndElement() {
296 DCHECK(!locked_);
297
298 int result = xmlTextWriterEndElement(writer_);
299 DCHECK_GE(result, 0);
300}
301
[email protected]541f77922009-02-23 21:14:38302// static
303std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29304 scoped_ptr<FileVersionInfo> version_info(
305 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
306 if (version_info.get()) {
307 std::string version = WideToUTF8(version_info->product_version());
[email protected]5ed7d4572009-12-23 17:42:41308 if (!version_extension_.empty())
309 version += version_extension_;
initial.commit09911bf2008-07-26 23:55:29310 if (!version_info->is_official_build())
311 version.append("-devel");
312 return version;
313 } else {
314 NOTREACHED() << "Unable to retrieve version string.";
315 }
316
317 return std::string();
318}
319
[email protected]225c50842010-01-19 21:19:13320// static
321int64 MetricsLog::GetBuildTime() {
322 static int64 integral_build_time = 0;
323 if (!integral_build_time) {
324 Time time;
325 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
326 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &time);
327 DCHECK(result);
328 integral_build_time = static_cast<int64>(time.ToTimeT());
329 }
330 return integral_build_time;
331}
332
[email protected]9165f742010-03-10 22:55:01333// static
334int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
335 base::TimeTicks now = base::TimeTicks::Now();
336 static base::TimeTicks last_updated_time(now);
337 int64 incremental_time = (now - last_updated_time).InSeconds();
338 last_updated_time = now;
339
340 if (incremental_time > 0) {
341 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
342 metrics_uptime += incremental_time;
343 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
344 }
345
346 return incremental_time;
347}
348
initial.commit09911bf2008-07-26 23:55:29349std::string MetricsLog::GetInstallDate() const {
350 PrefService* pref = g_browser_process->local_state();
351 if (pref) {
352 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
353 } else {
354 NOTREACHED();
355 return "0";
356 }
357}
358
[email protected]0b33f80b2008-12-17 21:34:36359void MetricsLog::RecordIncrementalStabilityElements() {
360 DCHECK(!locked_);
361
362 PrefService* pref = g_browser_process->local_state();
363 DCHECK(pref);
364
[email protected]147bbc0b2009-01-06 19:37:40365 OPEN_ELEMENT_FOR_SCOPE("profile");
366 WriteCommonEventAttributes();
367
368 WriteInstallElement(); // Supply appversion.
369
370 {
371 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
372 WriteRequiredStabilityAttributes(pref);
373 WriteRealtimeStabilityAttributes(pref);
374
375 WritePluginStabilityElements(pref);
376 }
[email protected]0b33f80b2008-12-17 21:34:36377}
378
initial.commit09911bf2008-07-26 23:55:29379void MetricsLog::WriteStabilityElement() {
380 DCHECK(!locked_);
381
382 PrefService* pref = g_browser_process->local_state();
383 DCHECK(pref);
384
385 // Get stability attributes out of Local State, zeroing out stored values.
386 // NOTE: This could lead to some data loss if this report isn't successfully
387 // sent, but that's true for all the metrics.
388
[email protected]ffaf78a2008-11-12 17:38:33389 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40390 WriteRequiredStabilityAttributes(pref);
391 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29392
[email protected]0b33f80b2008-12-17 21:34:36393 // TODO(jar): The following are all optional, so we *could* optimize them for
394 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48395 WriteIntAttribute("incompleteshutdowncount",
396 pref->GetInteger(
397 prefs::kStabilityIncompleteSessionEndCount));
398 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36399
400
[email protected]e73c01972008-08-13 00:18:24401 WriteIntAttribute("breakpadregistrationok",
402 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
403 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
404 WriteIntAttribute("breakpadregistrationfail",
405 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
406 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
407 WriteIntAttribute("debuggerpresent",
408 pref->GetInteger(prefs::kStabilityDebuggerPresent));
409 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
410 WriteIntAttribute("debuggernotpresent",
411 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
412 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29413
[email protected]147bbc0b2009-01-06 19:37:40414 WritePluginStabilityElements(pref);
415}
416
417void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
418 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29419 const ListValue* plugin_stats_list = pref->GetList(
420 prefs::kStabilityPluginStats);
421 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33422 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29423 for (ListValue::const_iterator iter = plugin_stats_list->begin();
424 iter != plugin_stats_list->end(); ++iter) {
425 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
426 NOTREACHED();
427 continue;
428 }
429 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
430
[email protected]8e50b602009-03-03 22:59:43431 std::wstring plugin_name;
432 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29433
[email protected]ffaf78a2008-11-12 17:38:33434 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10435 // Use "filename" instead of "name", otherwise we need to update the
436 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43437 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29438
439 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43440 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29441 WriteIntAttribute("launchcount", launches);
442
443 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43444 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29445 WriteIntAttribute("instancecount", instances);
446
447 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43448 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29449 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29450 }
451
452 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29453 }
initial.commit09911bf2008-07-26 23:55:29454}
455
[email protected]147bbc0b2009-01-06 19:37:40456void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36457 // The server refuses data that doesn't have certain values. crashcount and
458 // launchcount are currently "required" in the "stability" group.
459 WriteIntAttribute("launchcount",
460 pref->GetInteger(prefs::kStabilityLaunchCount));
461 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
462 WriteIntAttribute("crashcount",
463 pref->GetInteger(prefs::kStabilityCrashCount));
464 pref->SetInteger(prefs::kStabilityCrashCount, 0);
465}
466
[email protected]147bbc0b2009-01-06 19:37:40467void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36468 // Update the stats which are critical for real-time stability monitoring.
469 // Since these are "optional," only list ones that are non-zero, as the counts
470 // are aggergated (summed) server side.
471
472 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
473 if (count) {
474 WriteIntAttribute("pageloadcount", count);
475 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
476 }
477
478 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
479 if (count) {
480 WriteIntAttribute("renderercrashcount", count);
481 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
482 }
483
[email protected]1f085622009-12-04 05:33:45484 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
485 if (count) {
486 WriteIntAttribute("extensionrenderercrashcount", count);
487 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
488 }
489
[email protected]0b33f80b2008-12-17 21:34:36490 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
491 if (count) {
492 WriteIntAttribute("rendererhangcount", count);
493 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
494 }
[email protected]1f085622009-12-04 05:33:45495
496 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
497 if (count) {
498 WriteIntAttribute("childprocesscrashcount", count);
499 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
500 }
[email protected]9165f742010-03-10 22:55:01501
502 int64 recent_duration = GetIncrementalUptime(pref);
503 if (recent_duration)
504 WriteInt64Attribute("uptimesec", recent_duration);
[email protected]0b33f80b2008-12-17 21:34:36505}
506
initial.commit09911bf2008-07-26 23:55:29507void MetricsLog::WritePluginList(
508 const std::vector<WebPluginInfo>& plugin_list) {
509 DCHECK(!locked_);
510
[email protected]ffaf78a2008-11-12 17:38:33511 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29512
513 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
514 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33515 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29516
517 // Plugin name and filename are hashed for the privacy of those
518 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44519 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21520 WriteAttribute("filename",
521 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44522 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29523 }
initial.commit09911bf2008-07-26 23:55:29524}
525
[email protected]147bbc0b2009-01-06 19:37:40526void MetricsLog::WriteInstallElement() {
527 OPEN_ELEMENT_FOR_SCOPE("install");
528 WriteAttribute("installdate", GetInstallDate());
529 WriteIntAttribute("buildid", 0); // We're using appversion instead.
[email protected]147bbc0b2009-01-06 19:37:40530}
531
initial.commit09911bf2008-07-26 23:55:29532void MetricsLog::RecordEnvironment(
533 const std::vector<WebPluginInfo>& plugin_list,
534 const DictionaryValue* profile_metrics) {
535 DCHECK(!locked_);
536
537 PrefService* pref = g_browser_process->local_state();
538
[email protected]ffaf78a2008-11-12 17:38:33539 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29540 WriteCommonEventAttributes();
541
[email protected]147bbc0b2009-01-06 19:37:40542 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29543
544 WritePluginList(plugin_list);
545
546 WriteStabilityElement();
547
548 {
549 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01550 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29551 }
552
553 {
initial.commit09911bf2008-07-26 23:55:29554 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40555 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38556#if defined(OS_WIN)
557 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
558#endif
initial.commit09911bf2008-07-26 23:55:29559 }
560
561 {
562 OPEN_ELEMENT_FOR_SCOPE("os");
563 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01564 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29565 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01566 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29567 }
568
569 {
570 OPEN_ELEMENT_FOR_SCOPE("display");
571 int width = 0;
572 int height = 0;
[email protected]05f9b682008-09-29 22:18:01573 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29574 WriteIntAttribute("xsize", width);
575 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01576 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29577 }
578
579 {
580 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
581 int num_bookmarks_on_bookmark_bar =
582 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
583 int num_folders_on_bookmark_bar =
584 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
585 int num_bookmarks_in_other_bookmarks_folder =
586 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
587 int num_folders_in_other_bookmarks_folder =
588 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
589 {
590 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
591 WriteAttribute("name", "full-tree");
592 WriteIntAttribute("foldercount",
593 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
594 WriteIntAttribute("itemcount",
595 num_bookmarks_on_bookmark_bar +
596 num_bookmarks_in_other_bookmarks_folder);
597 }
598 {
599 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
600 WriteAttribute("name", "toolbar");
601 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
602 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
603 }
604 }
605
606 {
607 OPEN_ELEMENT_FOR_SCOPE("keywords");
608 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
609 }
610
611 if (profile_metrics)
612 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29613}
614
615void MetricsLog::WriteAllProfilesMetrics(
616 const DictionaryValue& all_profiles_metrics) {
617 const std::wstring profile_prefix(prefs::kProfilePrefix);
618 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
619 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43620 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29621 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
622 DictionaryValue* profile;
[email protected]4dad9ad82009-11-25 20:47:52623 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
624 &profile))
initial.commit09911bf2008-07-26 23:55:29625 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
626 }
627 }
628}
629
630void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
631 const DictionaryValue& profile_metrics) {
632 OPEN_ELEMENT_FOR_SCOPE("userprofile");
633 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
634 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
635 i != profile_metrics.end_keys(); ++i) {
636 Value* value;
[email protected]4dad9ad82009-11-25 20:47:52637 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43638 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29639 switch (value->GetType()) {
640 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59641 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29642 if (value->GetAsString(&string_value)) {
643 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43644 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59645 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29646 }
647 break;
648 }
649
650 case Value::TYPE_BOOLEAN: {
651 bool bool_value;
652 if (value->GetAsBoolean(&bool_value)) {
653 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43654 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29655 WriteIntAttribute("value", bool_value ? 1 : 0);
656 }
657 break;
658 }
659
660 case Value::TYPE_INTEGER: {
661 int int_value;
662 if (value->GetAsInteger(&int_value)) {
663 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43664 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29665 WriteIntAttribute("value", int_value);
666 }
667 break;
668 }
669
670 default:
671 NOTREACHED();
672 break;
673 }
674 }
675 }
676}
677
678void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
679 DCHECK(!locked_);
680
[email protected]ffaf78a2008-11-12 17:38:33681 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29682 WriteAttribute("action", "autocomplete");
683 WriteAttribute("targetidhash", "");
684 // TODO(kochi): Properly track windows.
685 WriteIntAttribute("window", 0);
686 WriteCommonEventAttributes();
687
[email protected]ffaf78a2008-11-12 17:38:33688 {
689 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29690
[email protected]ffaf78a2008-11-12 17:38:33691 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
692 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
693 WriteIntAttribute("completedlength",
694 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00695 const std::string input_type(
696 AutocompleteInput::TypeToString(log.input_type));
697 if (!input_type.empty())
698 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29699
[email protected]ffaf78a2008-11-12 17:38:33700 for (AutocompleteResult::const_iterator i(log.result.begin());
701 i != log.result.end(); ++i) {
702 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
703 if (i->provider)
704 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00705 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
706 if (!result_type.empty())
707 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33708 WriteIntAttribute("relevance", i->relevance);
709 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
710 }
initial.commit09911bf2008-07-26 23:55:29711 }
initial.commit09911bf2008-07-26 23:55:29712
713 ++num_events_;
714}
715
716// TODO(JAR): A The following should really be part of the histogram class.
717// Internal state is being needlessly exposed, and it would be hard to reuse
718// this code. If we moved this into the Histogram class, then we could use
719// the same infrastructure for logging StatsCounters, RatesCounters, etc.
720void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
721 const Histogram::SampleSet& snapshot) {
722 DCHECK(!locked_);
[email protected]5ed7d4572009-12-23 17:42:41723 DCHECK_NE(0, snapshot.TotalCount());
initial.commit09911bf2008-07-26 23:55:29724 snapshot.CheckSize(histogram);
725
726 // We will ignore the MAX_INT/infinite value in the last element of range[].
727
728 OPEN_ELEMENT_FOR_SCOPE("histogram");
729
730 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
731
732 WriteInt64Attribute("sum", snapshot.sum());
733 WriteInt64Attribute("sumsquares", snapshot.square_sum());
734
735 for (size_t i = 0; i < histogram.bucket_count(); i++) {
736 if (snapshot.counts(i)) {
737 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
738 WriteIntAttribute("min", histogram.ranges(i));
739 WriteIntAttribute("max", histogram.ranges(i + 1));
740 WriteIntAttribute("count", snapshot.counts(i));
741 }
742 }
743}