license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/metrics_log.h" |
| 6 | |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/file_version_info.h" |
| 9 | #include "base/md5.h" |
| 10 | #include "base/scoped_ptr.h" |
| 11 | #include "base/string_util.h" |
[email protected] | fadf97f | 2008-09-18 12:18:14 | [diff] [blame] | 12 | #include "base/sys_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "chrome/browser/autocomplete/autocomplete.h" |
| 14 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "chrome/common/logging_chrome.h" |
| 16 | #include "chrome/common/pref_names.h" |
| 17 | #include "chrome/common/pref_service.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "net/base/base64.h" |
| 20 | |
| 21 | #define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name) |
| 22 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 23 | using base::Time; |
| 24 | using base::TimeDelta; |
| 25 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | // libxml take xmlChar*, which is unsigned char* |
| 27 | inline const unsigned char* UnsignedChar(const char* input) { |
| 28 | return reinterpret_cast<const unsigned char*>(input); |
| 29 | } |
| 30 | |
| 31 | // static |
| 32 | void MetricsLog::RegisterPrefs(PrefService* local_state) { |
| 33 | local_state->RegisterListPref(prefs::kStabilityPluginStats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | MetricsLog::MetricsLog(const std::string& client_id, int session_id) |
| 37 | : start_time_(Time::Now()), |
| 38 | num_events_(0), |
| 39 | locked_(false), |
| 40 | buffer_(NULL), |
| 41 | writer_(NULL), |
| 42 | client_id_(client_id), |
| 43 | session_id_(IntToString(session_id)) { |
| 44 | |
| 45 | buffer_ = xmlBufferCreate(); |
| 46 | DCHECK(buffer_); |
| 47 | |
| 48 | writer_ = xmlNewTextWriterMemory(buffer_, 0); |
| 49 | DCHECK(writer_); |
| 50 | |
| 51 | int result = xmlTextWriterSetIndent(writer_, 2); |
| 52 | DCHECK_EQ(0, result); |
| 53 | |
| 54 | StartElement("log"); |
| 55 | WriteAttribute("clientid", client_id_); |
| 56 | |
| 57 | DCHECK_GE(result, 0); |
| 58 | } |
| 59 | |
| 60 | MetricsLog::~MetricsLog() { |
| 61 | if (writer_) |
| 62 | xmlFreeTextWriter(writer_); |
| 63 | |
| 64 | if (buffer_) |
| 65 | xmlBufferFree(buffer_); |
| 66 | } |
| 67 | |
| 68 | void MetricsLog::CloseLog() { |
| 69 | DCHECK(!locked_); |
| 70 | locked_ = true; |
| 71 | |
| 72 | int result = xmlTextWriterEndDocument(writer_); |
| 73 | DCHECK(result >= 0); |
| 74 | |
| 75 | result = xmlTextWriterFlush(writer_); |
| 76 | DCHECK(result >= 0); |
| 77 | } |
| 78 | |
| 79 | int MetricsLog::GetEncodedLogSize() { |
| 80 | DCHECK(locked_); |
| 81 | return buffer_->use; |
| 82 | } |
| 83 | |
| 84 | bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) { |
| 85 | DCHECK(locked_); |
| 86 | if (buffer_size < GetEncodedLogSize()) |
| 87 | return false; |
| 88 | |
| 89 | memcpy(buffer, buffer_->content, GetEncodedLogSize()); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | int MetricsLog::GetElapsedSeconds() { |
| 94 | return static_cast<int>((Time::Now() - start_time_).InSeconds()); |
| 95 | } |
| 96 | |
| 97 | std::string MetricsLog::CreateHash(const std::string& value) { |
| 98 | MD5Context ctx; |
| 99 | MD5Init(&ctx); |
| 100 | MD5Update(&ctx, value.data(), value.length()); |
| 101 | |
| 102 | MD5Digest digest; |
| 103 | MD5Final(&digest, &ctx); |
| 104 | |
| 105 | unsigned char reverse[8]; // UMA only uses first 8 chars of hash. |
| 106 | DCHECK(arraysize(digest.a) >= arraysize(reverse)); |
| 107 | for (int i = 0; i < arraysize(reverse); ++i) |
| 108 | reverse[i] = digest.a[arraysize(reverse) - i - 1]; |
| 109 | LOG(INFO) << "Metrics: Hash numeric [" << value << "]=[" |
| 110 | << *reinterpret_cast<const uint64*>(&reverse[0]) << "]"; |
| 111 | return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a)); |
| 112 | } |
| 113 | |
| 114 | std::string MetricsLog::CreateBase64Hash(const std::string& string) { |
| 115 | std::string encoded_digest; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 116 | if (net::Base64Encode(CreateHash(string), &encoded_digest)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]"; |
| 118 | return encoded_digest; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | } |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 120 | return std::string(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void MetricsLog::RecordUserAction(const wchar_t* key) { |
| 124 | DCHECK(!locked_); |
| 125 | |
| 126 | std::string command_hash = CreateBase64Hash(WideToUTF8(key)); |
| 127 | if (command_hash.empty()) { |
| 128 | NOTREACHED() << "Unable generate encoded hash of command: " << key; |
| 129 | return; |
| 130 | } |
| 131 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 132 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | WriteAttribute("action", "command"); |
| 134 | WriteAttribute("targetidhash", command_hash); |
| 135 | |
| 136 | // TODO(jhughes): Properly track windows. |
| 137 | WriteIntAttribute("window", 0); |
| 138 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | |
| 140 | ++num_events_; |
| 141 | } |
| 142 | |
| 143 | void MetricsLog::RecordLoadEvent(int window_id, |
| 144 | const GURL& url, |
| 145 | PageTransition::Type origin, |
| 146 | int session_index, |
| 147 | TimeDelta load_time) { |
| 148 | DCHECK(!locked_); |
| 149 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 150 | OPEN_ELEMENT_FOR_SCOPE("document"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | WriteAttribute("action", "load"); |
| 152 | WriteIntAttribute("docid", session_index); |
| 153 | WriteIntAttribute("window", window_id); |
| 154 | WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds())); |
| 155 | |
| 156 | std::string origin_string; |
| 157 | |
| 158 | switch (PageTransition::StripQualifier(origin)) { |
| 159 | // TODO(jhughes): Some of these mappings aren't right... we need to add |
| 160 | // some values to the server's enum. |
| 161 | case PageTransition::LINK: |
| 162 | case PageTransition::MANUAL_SUBFRAME: |
| 163 | origin_string = "link"; |
| 164 | break; |
| 165 | |
| 166 | case PageTransition::TYPED: |
| 167 | origin_string = "typed"; |
| 168 | break; |
| 169 | |
| 170 | case PageTransition::AUTO_BOOKMARK: |
| 171 | origin_string = "bookmark"; |
| 172 | break; |
| 173 | |
| 174 | case PageTransition::AUTO_SUBFRAME: |
| 175 | case PageTransition::RELOAD: |
| 176 | origin_string = "refresh"; |
| 177 | break; |
| 178 | |
| 179 | case PageTransition::GENERATED: |
| 180 | origin_string = "global-history"; |
| 181 | break; |
| 182 | |
| 183 | case PageTransition::START_PAGE: |
| 184 | origin_string = "start-page"; |
| 185 | break; |
| 186 | |
| 187 | case PageTransition::FORM_SUBMIT: |
| 188 | origin_string = "form-submit"; |
| 189 | break; |
| 190 | |
| 191 | default: |
| 192 | NOTREACHED() << "Received an unknown page transition type: " << |
| 193 | PageTransition::StripQualifier(origin); |
| 194 | } |
| 195 | if (!origin_string.empty()) |
| 196 | WriteAttribute("origin", origin_string); |
| 197 | |
| 198 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
| 200 | ++num_events_; |
| 201 | } |
| 202 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | void MetricsLog::RecordWindowEvent(WindowEventType type, |
| 204 | int window_id, |
| 205 | int parent_id) { |
| 206 | DCHECK(!locked_); |
| 207 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 208 | OPEN_ELEMENT_FOR_SCOPE("window"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | WriteAttribute("action", WindowEventTypeToString(type)); |
| 210 | WriteAttribute("windowid", IntToString(window_id)); |
| 211 | if (parent_id >= 0) |
| 212 | WriteAttribute("parent", IntToString(parent_id)); |
| 213 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
| 215 | ++num_events_; |
| 216 | } |
| 217 | |
| 218 | std::string MetricsLog::GetCurrentTimeString() { |
| 219 | return Uint64ToString(Time::Now().ToTimeT()); |
| 220 | } |
| 221 | |
| 222 | // These are the attributes that are common to every event. |
| 223 | void MetricsLog::WriteCommonEventAttributes() { |
| 224 | WriteAttribute("session", session_id_); |
| 225 | WriteAttribute("time", GetCurrentTimeString()); |
| 226 | } |
| 227 | |
| 228 | void MetricsLog::WriteAttribute(const std::string& name, |
| 229 | const std::string& value) { |
| 230 | DCHECK(!locked_); |
| 231 | DCHECK(!name.empty()); |
| 232 | |
| 233 | int result = xmlTextWriterWriteAttribute(writer_, |
| 234 | UnsignedChar(name.c_str()), |
| 235 | UnsignedChar(value.c_str())); |
| 236 | DCHECK_GE(result, 0); |
| 237 | } |
| 238 | |
| 239 | void MetricsLog::WriteIntAttribute(const std::string& name, int value) { |
| 240 | WriteAttribute(name, IntToString(value)); |
| 241 | } |
| 242 | |
| 243 | void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) { |
| 244 | WriteAttribute(name, Int64ToString(value)); |
| 245 | } |
| 246 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 247 | // static |
| 248 | const char* MetricsLog::WindowEventTypeToString(WindowEventType type) { |
| 249 | switch (type) { |
| 250 | case WINDOW_CREATE: return "create"; |
| 251 | case WINDOW_OPEN: return "open"; |
| 252 | case WINDOW_CLOSE: return "close"; |
| 253 | case WINDOW_DESTROY: return "destroy"; |
| 254 | |
| 255 | default: |
| 256 | NOTREACHED(); |
| 257 | return "unknown"; // Can't return NULL as this is used in a required |
| 258 | // attribute. |
| 259 | } |
| 260 | } |
| 261 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | void MetricsLog::StartElement(const char* name) { |
| 263 | DCHECK(!locked_); |
| 264 | DCHECK(name); |
| 265 | |
| 266 | int result = xmlTextWriterStartElement(writer_, UnsignedChar(name)); |
| 267 | DCHECK_GE(result, 0); |
| 268 | } |
| 269 | |
| 270 | void MetricsLog::EndElement() { |
| 271 | DCHECK(!locked_); |
| 272 | |
| 273 | int result = xmlTextWriterEndElement(writer_); |
| 274 | DCHECK_GE(result, 0); |
| 275 | } |
| 276 | |
| 277 | std::string MetricsLog::GetVersionString() const { |
| 278 | scoped_ptr<FileVersionInfo> version_info( |
| 279 | FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 280 | if (version_info.get()) { |
| 281 | std::string version = WideToUTF8(version_info->product_version()); |
| 282 | if (!version_info->is_official_build()) |
| 283 | version.append("-devel"); |
| 284 | return version; |
| 285 | } else { |
| 286 | NOTREACHED() << "Unable to retrieve version string."; |
| 287 | } |
| 288 | |
| 289 | return std::string(); |
| 290 | } |
| 291 | |
| 292 | std::string MetricsLog::GetInstallDate() const { |
| 293 | PrefService* pref = g_browser_process->local_state(); |
| 294 | if (pref) { |
| 295 | return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp)); |
| 296 | } else { |
| 297 | NOTREACHED(); |
| 298 | return "0"; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void MetricsLog::WriteStabilityElement() { |
| 303 | DCHECK(!locked_); |
| 304 | |
| 305 | PrefService* pref = g_browser_process->local_state(); |
| 306 | DCHECK(pref); |
| 307 | |
| 308 | // Get stability attributes out of Local State, zeroing out stored values. |
| 309 | // NOTE: This could lead to some data loss if this report isn't successfully |
| 310 | // sent, but that's true for all the metrics. |
| 311 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 312 | OPEN_ELEMENT_FOR_SCOPE("stability"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | |
| 314 | WriteIntAttribute("launchcount", |
| 315 | pref->GetInteger(prefs::kStabilityLaunchCount)); |
| 316 | pref->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 317 | WriteIntAttribute("crashcount", |
| 318 | pref->GetInteger(prefs::kStabilityCrashCount)); |
| 319 | pref->SetInteger(prefs::kStabilityCrashCount, 0); |
[email protected] | 8e674e4 | 2008-07-30 16:05:48 | [diff] [blame] | 320 | WriteIntAttribute("incompleteshutdowncount", |
| 321 | pref->GetInteger( |
| 322 | prefs::kStabilityIncompleteSessionEndCount)); |
| 323 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | WriteIntAttribute("pageloadcount", |
| 325 | pref->GetInteger(prefs::kStabilityPageLoadCount)); |
| 326 | pref->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 327 | WriteIntAttribute("renderercrashcount", |
| 328 | pref->GetInteger(prefs::kStabilityRendererCrashCount)); |
| 329 | pref->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 330 | WriteIntAttribute("rendererhangcount", |
| 331 | pref->GetInteger(prefs::kStabilityRendererHangCount)); |
| 332 | pref->SetInteger(prefs::kStabilityRendererHangCount, 0); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 333 | WriteIntAttribute("breakpadregistrationok", |
| 334 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess)); |
| 335 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 336 | WriteIntAttribute("breakpadregistrationfail", |
| 337 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail)); |
| 338 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 339 | WriteIntAttribute("debuggerpresent", |
| 340 | pref->GetInteger(prefs::kStabilityDebuggerPresent)); |
| 341 | pref->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 342 | WriteIntAttribute("debuggernotpresent", |
| 343 | pref->GetInteger(prefs::kStabilityDebuggerNotPresent)); |
| 344 | pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | |
| 346 | // Uptime is stored as a string, since there's no int64 in Value/JSON. |
| 347 | WriteAttribute("uptimesec", |
| 348 | WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec))); |
| 349 | pref->SetString(prefs::kStabilityUptimeSec, L"0"); |
| 350 | |
| 351 | // Now log plugin stability info |
| 352 | const ListValue* plugin_stats_list = pref->GetList( |
| 353 | prefs::kStabilityPluginStats); |
| 354 | if (plugin_stats_list) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 355 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | for (ListValue::const_iterator iter = plugin_stats_list->begin(); |
| 357 | iter != plugin_stats_list->end(); ++iter) { |
| 358 | if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) { |
| 359 | NOTREACHED(); |
| 360 | continue; |
| 361 | } |
| 362 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter); |
| 363 | |
| 364 | std::wstring plugin_path; |
| 365 | plugin_dict->GetString(prefs::kStabilityPluginPath, &plugin_path); |
| 366 | plugin_path = file_util::GetFilenameFromPath(plugin_path); |
| 367 | if (plugin_path.empty()) { |
| 368 | NOTREACHED(); |
| 369 | continue; |
| 370 | } |
| 371 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 372 | OPEN_ELEMENT_FOR_SCOPE("pluginstability"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_path))); |
| 374 | |
| 375 | int launches = 0; |
| 376 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
| 377 | WriteIntAttribute("launchcount", launches); |
| 378 | |
| 379 | int instances = 0; |
| 380 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
| 381 | WriteIntAttribute("instancecount", instances); |
| 382 | |
| 383 | int crashes = 0; |
| 384 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
| 385 | WriteIntAttribute("crashcount", crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | pref->ClearPref(prefs::kStabilityPluginStats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void MetricsLog::WritePluginList( |
| 393 | const std::vector<WebPluginInfo>& plugin_list) { |
| 394 | DCHECK(!locked_); |
| 395 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 396 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | |
| 398 | for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin(); |
| 399 | iter != plugin_list.end(); ++iter) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 400 | OPEN_ELEMENT_FOR_SCOPE("plugin"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | |
| 402 | // Plugin name and filename are hashed for the privacy of those |
| 403 | // testing unreleased new extensions. |
| 404 | WriteAttribute("name", CreateBase64Hash(WideToUTF8((*iter).name))); |
| 405 | std::wstring filename = file_util::GetFilenameFromPath((*iter).file); |
| 406 | WriteAttribute("filename", CreateBase64Hash(WideToUTF8(filename))); |
| 407 | |
| 408 | WriteAttribute("version", WideToUTF8((*iter).version)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void MetricsLog::RecordEnvironment( |
| 413 | const std::vector<WebPluginInfo>& plugin_list, |
| 414 | const DictionaryValue* profile_metrics) { |
| 415 | DCHECK(!locked_); |
| 416 | |
| 417 | PrefService* pref = g_browser_process->local_state(); |
| 418 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 419 | OPEN_ELEMENT_FOR_SCOPE("profile"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | WriteCommonEventAttributes(); |
| 421 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 422 | { |
| 423 | OPEN_ELEMENT_FOR_SCOPE("install"); |
| 424 | WriteAttribute("installdate", GetInstallDate()); |
| 425 | WriteIntAttribute("buildid", 0); // We're using appversion instead. |
| 426 | WriteAttribute("appversion", GetVersionString()); |
| 427 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | |
| 429 | WritePluginList(plugin_list); |
| 430 | |
| 431 | WriteStabilityElement(); |
| 432 | |
| 433 | { |
| 434 | OPEN_ELEMENT_FOR_SCOPE("cpu"); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 435 | WriteAttribute("arch", base::SysInfo::CPUArchitecture()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | { |
| 439 | OPEN_ELEMENT_FOR_SCOPE("security"); |
| 440 | WriteIntAttribute("rendereronsboxdesktop", |
| 441 | pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop)); |
| 442 | pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0); |
| 443 | |
| 444 | WriteIntAttribute("rendererondefaultdesktop", |
| 445 | pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop)); |
| 446 | pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0); |
| 447 | } |
| 448 | |
| 449 | { |
| 450 | OPEN_ELEMENT_FOR_SCOPE("memory"); |
[email protected] | ed6fc35 | 2008-09-18 12:44:40 | [diff] [blame] | 451 | WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | { |
| 455 | OPEN_ELEMENT_FOR_SCOPE("os"); |
| 456 | WriteAttribute("name", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 457 | base::SysInfo::OperatingSystemName()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | WriteAttribute("version", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 459 | base::SysInfo::OperatingSystemVersion()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | { |
| 463 | OPEN_ELEMENT_FOR_SCOPE("display"); |
| 464 | int width = 0; |
| 465 | int height = 0; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 466 | base::SysInfo::GetPrimaryDisplayDimensions(&width, &height); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | WriteIntAttribute("xsize", width); |
| 468 | WriteIntAttribute("ysize", height); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 469 | WriteIntAttribute("screens", base::SysInfo::DisplayCount()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | { |
| 473 | OPEN_ELEMENT_FOR_SCOPE("bookmarks"); |
| 474 | int num_bookmarks_on_bookmark_bar = |
| 475 | pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar); |
| 476 | int num_folders_on_bookmark_bar = |
| 477 | pref->GetInteger(prefs::kNumFoldersOnBookmarkBar); |
| 478 | int num_bookmarks_in_other_bookmarks_folder = |
| 479 | pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder); |
| 480 | int num_folders_in_other_bookmarks_folder = |
| 481 | pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder); |
| 482 | { |
| 483 | OPEN_ELEMENT_FOR_SCOPE("bookmarklocation"); |
| 484 | WriteAttribute("name", "full-tree"); |
| 485 | WriteIntAttribute("foldercount", |
| 486 | num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder); |
| 487 | WriteIntAttribute("itemcount", |
| 488 | num_bookmarks_on_bookmark_bar + |
| 489 | num_bookmarks_in_other_bookmarks_folder); |
| 490 | } |
| 491 | { |
| 492 | OPEN_ELEMENT_FOR_SCOPE("bookmarklocation"); |
| 493 | WriteAttribute("name", "toolbar"); |
| 494 | WriteIntAttribute("foldercount", num_folders_on_bookmark_bar); |
| 495 | WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | { |
| 500 | OPEN_ELEMENT_FOR_SCOPE("keywords"); |
| 501 | WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords)); |
| 502 | } |
| 503 | |
| 504 | if (profile_metrics) |
| 505 | WriteAllProfilesMetrics(*profile_metrics); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void MetricsLog::WriteAllProfilesMetrics( |
| 509 | const DictionaryValue& all_profiles_metrics) { |
| 510 | const std::wstring profile_prefix(prefs::kProfilePrefix); |
| 511 | for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys(); |
| 512 | i != all_profiles_metrics.end_keys(); ++i) { |
| 513 | const std::wstring& key_name = *i; |
| 514 | if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) { |
| 515 | DictionaryValue* profile; |
| 516 | if (all_profiles_metrics.GetDictionary(key_name, &profile)) |
| 517 | WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash, |
| 523 | const DictionaryValue& profile_metrics) { |
| 524 | OPEN_ELEMENT_FOR_SCOPE("userprofile"); |
| 525 | WriteAttribute("profileidhash", WideToUTF8(profileidhash)); |
| 526 | for (DictionaryValue::key_iterator i = profile_metrics.begin_keys(); |
| 527 | i != profile_metrics.end_keys(); ++i) { |
| 528 | Value* value; |
| 529 | if (profile_metrics.Get(*i, &value)) { |
| 530 | DCHECK(*i != L"id"); |
| 531 | switch (value->GetType()) { |
| 532 | case Value::TYPE_STRING: { |
| 533 | std::wstring string_value; |
| 534 | if (value->GetAsString(&string_value)) { |
| 535 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 536 | WriteAttribute("name", WideToUTF8(*i)); |
| 537 | WriteAttribute("value", WideToUTF8(string_value)); |
| 538 | } |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | case Value::TYPE_BOOLEAN: { |
| 543 | bool bool_value; |
| 544 | if (value->GetAsBoolean(&bool_value)) { |
| 545 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 546 | WriteAttribute("name", WideToUTF8(*i)); |
| 547 | WriteIntAttribute("value", bool_value ? 1 : 0); |
| 548 | } |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | case Value::TYPE_INTEGER: { |
| 553 | int int_value; |
| 554 | if (value->GetAsInteger(&int_value)) { |
| 555 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 556 | WriteAttribute("name", WideToUTF8(*i)); |
| 557 | WriteIntAttribute("value", int_value); |
| 558 | } |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | default: |
| 563 | NOTREACHED(); |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) { |
| 571 | DCHECK(!locked_); |
| 572 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 573 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | WriteAttribute("action", "autocomplete"); |
| 575 | WriteAttribute("targetidhash", ""); |
| 576 | // TODO(kochi): Properly track windows. |
| 577 | WriteIntAttribute("window", 0); |
| 578 | WriteCommonEventAttributes(); |
| 579 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 580 | { |
| 581 | OPEN_ELEMENT_FOR_SCOPE("autocomplete"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 583 | WriteIntAttribute("typedlength", static_cast<int>(log.text.length())); |
| 584 | WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index)); |
| 585 | WriteIntAttribute("completedlength", |
| 586 | static_cast<int>(log.inline_autocompleted_length)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 587 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame^] | 588 | for (AutocompleteResult::const_iterator i(log.result.begin()); |
| 589 | i != log.result.end(); ++i) { |
| 590 | OPEN_ELEMENT_FOR_SCOPE("autocompleteitem"); |
| 591 | if (i->provider) |
| 592 | WriteAttribute("provider", i->provider->name()); |
| 593 | WriteIntAttribute("relevance", i->relevance); |
| 594 | WriteIntAttribute("isstarred", i->starred ? 1 : 0); |
| 595 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 596 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | |
| 598 | ++num_events_; |
| 599 | } |
| 600 | |
| 601 | // TODO(JAR): A The following should really be part of the histogram class. |
| 602 | // Internal state is being needlessly exposed, and it would be hard to reuse |
| 603 | // this code. If we moved this into the Histogram class, then we could use |
| 604 | // the same infrastructure for logging StatsCounters, RatesCounters, etc. |
| 605 | void MetricsLog::RecordHistogramDelta(const Histogram& histogram, |
| 606 | const Histogram::SampleSet& snapshot) { |
| 607 | DCHECK(!locked_); |
| 608 | DCHECK(0 != snapshot.TotalCount()); |
| 609 | snapshot.CheckSize(histogram); |
| 610 | |
| 611 | // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| 612 | |
| 613 | OPEN_ELEMENT_FOR_SCOPE("histogram"); |
| 614 | |
| 615 | WriteAttribute("name", CreateBase64Hash(histogram.histogram_name())); |
| 616 | |
| 617 | WriteInt64Attribute("sum", snapshot.sum()); |
| 618 | WriteInt64Attribute("sumsquares", snapshot.square_sum()); |
| 619 | |
| 620 | for (size_t i = 0; i < histogram.bucket_count(); i++) { |
| 621 | if (snapshot.counts(i)) { |
| 622 | OPEN_ELEMENT_FOR_SCOPE("histogrambucket"); |
| 623 | WriteIntAttribute("min", histogram.ranges(i)); |
| 624 | WriteIntAttribute("max", histogram.ranges(i + 1)); |
| 625 | WriteIntAttribute("count", snapshot.counts(i)); |
| 626 | } |
| 627 | } |
| 628 | } |