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