Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / metrics / metrics_log.cc
1 // Copyright (c) 2012 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.
4
5 #include "chrome/browser/metrics/metrics_log.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10
11 #include "base/base64.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/cpu.h"
15 #include "base/lazy_instance.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/profiler/alternate_timer.h"
20 #include "base/sha1.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/sys_info.h"
25 #include "base/third_party/nspr/prtime.h"
26 #include "base/time/time.h"
27 #include "base/tracked_objects.h"
28 #include "chrome/browser/autocomplete/autocomplete_input.h"
29 #include "chrome/browser/autocomplete/autocomplete_match.h"
30 #include "chrome/browser/autocomplete/autocomplete_provider.h"
31 #include "chrome/browser/autocomplete/autocomplete_result.h"
32 #include "chrome/browser/browser_process.h"
33 #include "chrome/browser/google/google_util.h"
34 #include "chrome/browser/omnibox/omnibox_log.h"
35 #include "chrome/browser/plugins/plugin_prefs.h"
36 #include "chrome/browser/profiles/profile_manager.h"
37 #include "chrome/common/chrome_version_info.h"
38 #include "chrome/common/logging_chrome.h"
39 #include "chrome/common/metrics/proto/omnibox_event.pb.h"
40 #include "chrome/common/metrics/proto/profiler_event.pb.h"
41 #include "chrome/common/metrics/proto/system_profile.pb.h"
42 #include "chrome/common/metrics/variations/variations_util.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/installer/util/google_update_settings.h"
45 #include "components/nacl/common/nacl_process_type.h"
46 #include "content/public/browser/gpu_data_manager.h"
47 #include "content/public/common/content_client.h"
48 #include "content/public/common/webplugininfo.h"
49 #include "gpu/config/gpu_info.h"
50 #include "ui/gfx/screen.h"
51 #include "url/gurl.h"
52
53 #if defined(OS_ANDROID)
54 #include "base/android/build_info.h"
55 #endif
56
57 #if defined(OS_WIN)
58 #include "base/win/metro.h"
59
60 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
61 extern "C" IMAGE_DOS_HEADER __ImageBase;
62 #endif
63
64 #if defined(OS_CHROMEOS)
65 #include "chrome/browser/metrics/metrics_log_chromeos.h"
66 #endif  // OS_CHROMEOS
67
68 using content::GpuDataManager;
69 using metrics::OmniboxEventProto;
70 using metrics::ProfilerEventProto;
71 using metrics::SystemProfileProto;
72 using tracked_objects::ProcessDataSnapshot;
73 typedef chrome_variations::ActiveGroupId ActiveGroupId;
74 typedef SystemProfileProto::GoogleUpdate::ProductInfo ProductInfo;
75
76 namespace {
77
78 // Returns the date at which the current metrics client ID was created as
79 // a string containing seconds since the epoch, or "0" if none was found.
80 std::string GetMetricsEnabledDate(PrefService* pref) {
81   if (!pref) {
82     NOTREACHED();
83     return "0";
84   }
85
86   return pref->GetString(prefs::kMetricsClientIDTimestamp);
87 }
88
89 OmniboxEventProto::InputType AsOmniboxEventInputType(
90     AutocompleteInput::Type type) {
91   switch (type) {
92     case AutocompleteInput::INVALID:
93       return OmniboxEventProto::INVALID;
94     case AutocompleteInput::UNKNOWN:
95       return OmniboxEventProto::UNKNOWN;
96     case AutocompleteInput::URL:
97       return OmniboxEventProto::URL;
98     case AutocompleteInput::QUERY:
99       return OmniboxEventProto::QUERY;
100     case AutocompleteInput::FORCED_QUERY:
101       return OmniboxEventProto::FORCED_QUERY;
102     default:
103       NOTREACHED();
104       return OmniboxEventProto::INVALID;
105   }
106 }
107
108 OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
109     AutocompleteMatch::Type type) {
110   switch (type) {
111     case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
112       return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
113     case AutocompleteMatchType::HISTORY_URL:
114       return OmniboxEventProto::Suggestion::HISTORY_URL;
115     case AutocompleteMatchType::HISTORY_TITLE:
116       return OmniboxEventProto::Suggestion::HISTORY_TITLE;
117     case AutocompleteMatchType::HISTORY_BODY:
118       return OmniboxEventProto::Suggestion::HISTORY_BODY;
119     case AutocompleteMatchType::HISTORY_KEYWORD:
120       return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
121     case AutocompleteMatchType::NAVSUGGEST:
122       return OmniboxEventProto::Suggestion::NAVSUGGEST;
123     case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
124       return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
125     case AutocompleteMatchType::SEARCH_HISTORY:
126       return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
127     case AutocompleteMatchType::SEARCH_SUGGEST:
128       return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
129     case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
130       return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ENTITY;
131     case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE:
132       return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_INFINITE;
133     case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
134       return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PERSONALIZED;
135     case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
136       return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PROFILE;
137     case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
138       return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
139     case AutocompleteMatchType::EXTENSION_APP:
140       return OmniboxEventProto::Suggestion::EXTENSION_APP;
141     case AutocompleteMatchType::CONTACT:
142       return OmniboxEventProto::Suggestion::CONTACT;
143     case AutocompleteMatchType::BOOKMARK_TITLE:
144       return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
145     default:
146       NOTREACHED();
147       return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
148   }
149 }
150
151 OmniboxEventProto::PageClassification AsOmniboxEventPageClassification(
152     AutocompleteInput::PageClassification page_classification) {
153   switch (page_classification) {
154     case AutocompleteInput::INVALID_SPEC:
155       return OmniboxEventProto::INVALID_SPEC;
156     case AutocompleteInput::NTP:
157       return OmniboxEventProto::NTP;
158     case AutocompleteInput::BLANK:
159       return OmniboxEventProto::BLANK;
160     case AutocompleteInput::HOME_PAGE:
161       return OmniboxEventProto::HOME_PAGE;
162     case AutocompleteInput::OTHER:
163       return OmniboxEventProto::OTHER;
164     case AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT:
165       return OmniboxEventProto::
166           SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
167     case AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS:
168       return OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
169     case AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS:
170       return OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS;
171     case AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT:
172       return OmniboxEventProto::
173           SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
174   }
175   return OmniboxEventProto::INVALID_SPEC;
176 }
177
178 ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
179     int process_type) {
180   switch (process_type) {
181     case content::PROCESS_TYPE_BROWSER:
182       return ProfilerEventProto::TrackedObject::BROWSER;
183     case content::PROCESS_TYPE_RENDERER:
184       return ProfilerEventProto::TrackedObject::RENDERER;
185     case content::PROCESS_TYPE_PLUGIN:
186       return ProfilerEventProto::TrackedObject::PLUGIN;
187     case content::PROCESS_TYPE_WORKER:
188       return ProfilerEventProto::TrackedObject::WORKER;
189     case content::PROCESS_TYPE_UTILITY:
190       return ProfilerEventProto::TrackedObject::UTILITY;
191     case content::PROCESS_TYPE_ZYGOTE:
192       return ProfilerEventProto::TrackedObject::ZYGOTE;
193     case content::PROCESS_TYPE_SANDBOX_HELPER:
194       return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
195     case content::PROCESS_TYPE_GPU:
196       return ProfilerEventProto::TrackedObject::GPU;
197     case content::PROCESS_TYPE_PPAPI_PLUGIN:
198       return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN;
199     case content::PROCESS_TYPE_PPAPI_BROKER:
200       return ProfilerEventProto::TrackedObject::PPAPI_BROKER;
201     case PROCESS_TYPE_NACL_LOADER:
202       return ProfilerEventProto::TrackedObject::NACL_LOADER;
203     case PROCESS_TYPE_NACL_BROKER:
204       return ProfilerEventProto::TrackedObject::NACL_BROKER;
205     default:
206       NOTREACHED();
207       return ProfilerEventProto::TrackedObject::UNKNOWN;
208   }
209 }
210
211 // Computes a SHA-1 hash of |data| and returns it as a hex string.
212 std::string ComputeSHA1(const std::string& data) {
213   const std::string sha1 = base::SHA1HashString(data);
214   return base::HexEncode(sha1.data(), sha1.size());
215 }
216
217 #if defined(ENABLE_PLUGINS)
218 // Returns the plugin preferences corresponding for this user, if available.
219 // If multiple user profiles are loaded, returns the preferences corresponding
220 // to an arbitrary one of the profiles.
221 PluginPrefs* GetPluginPrefs() {
222   ProfileManager* profile_manager = g_browser_process->profile_manager();
223
224   if (!profile_manager) {
225     // The profile manager can be NULL when testing.
226     return NULL;
227   }
228
229   std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
230   if (profiles.empty())
231     return NULL;
232
233   return PluginPrefs::GetForProfile(profiles.front()).get();
234 }
235
236 // Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|.
237 void SetPluginInfo(const content::WebPluginInfo& plugin_info,
238                    const PluginPrefs* plugin_prefs,
239                    SystemProfileProto::Plugin* plugin) {
240   plugin->set_name(base::UTF16ToUTF8(plugin_info.name));
241   plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe());
242   plugin->set_version(base::UTF16ToUTF8(plugin_info.version));
243   plugin->set_is_pepper(plugin_info.is_pepper_plugin());
244   if (plugin_prefs)
245     plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info));
246 }
247 #endif  // defined(ENABLE_PLUGINS)
248
249 void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
250                       SystemProfileProto* system_profile) {
251   for (std::vector<ActiveGroupId>::const_iterator it =
252        field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
253     SystemProfileProto::FieldTrial* field_trial =
254         system_profile->add_field_trial();
255     field_trial->set_name_id(it->name);
256     field_trial->set_group_id(it->group);
257   }
258 }
259
260 // Maps a thread name by replacing trailing sequence of digits with "*".
261 // Examples:
262 // 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*"
263 // 2. "Chrome_IOThread" => "Chrome_IOThread"
264 std::string MapThreadName(const std::string& thread_name) {
265   size_t i = thread_name.length();
266
267   while (i > 0 && isdigit(thread_name[i - 1])) {
268     --i;
269   }
270
271   if (i == thread_name.length())
272     return thread_name;
273
274   return thread_name.substr(0, i) + '*';
275 }
276
277 void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
278                        int process_type,
279                        ProfilerEventProto* performance_profile) {
280   for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
281            profiler_data.tasks.begin();
282        it != profiler_data.tasks.end(); ++it) {
283     const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
284     ProfilerEventProto::TrackedObject* tracked_object =
285         performance_profile->add_tracked_object();
286     tracked_object->set_birth_thread_name_hash(
287         MetricsLogBase::Hash(MapThreadName(it->birth.thread_name)));
288     tracked_object->set_exec_thread_name_hash(
289         MetricsLogBase::Hash(MapThreadName(it->death_thread_name)));
290     tracked_object->set_source_file_name_hash(
291         MetricsLogBase::Hash(it->birth.location.file_name));
292     tracked_object->set_source_function_name_hash(
293         MetricsLogBase::Hash(it->birth.location.function_name));
294     tracked_object->set_source_line_number(it->birth.location.line_number);
295     tracked_object->set_exec_count(death_data.count);
296     tracked_object->set_exec_time_total(death_data.run_duration_sum);
297     tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
298     tracked_object->set_queue_time_total(death_data.queue_duration_sum);
299     tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
300     tracked_object->set_process_type(AsProtobufProcessType(process_type));
301     tracked_object->set_process_id(profiler_data.process_id);
302   }
303 }
304
305 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
306 void ProductDataToProto(const GoogleUpdateSettings::ProductData& product_data,
307                         ProductInfo* product_info) {
308   product_info->set_version(product_data.version);
309   product_info->set_last_update_success_timestamp(
310       product_data.last_success.ToTimeT());
311   product_info->set_last_error(product_data.last_error_code);
312   product_info->set_last_extra_error(product_data.last_extra_code);
313   if (ProductInfo::InstallResult_IsValid(product_data.last_result)) {
314     product_info->set_last_result(
315         static_cast<ProductInfo::InstallResult>(product_data.last_result));
316   }
317 }
318 #endif
319
320 #if defined(OS_WIN)
321 struct ScreenDPIInformation {
322   double max_dpi_x;
323   double max_dpi_y;
324 };
325
326 // Called once for each connected monitor.
327 BOOL CALLBACK GetMonitorDPICallback(HMONITOR, HDC hdc, LPRECT, LPARAM dwData) {
328   const double kMillimetersPerInch = 25.4;
329   ScreenDPIInformation* screen_info =
330       reinterpret_cast<ScreenDPIInformation*>(dwData);
331   // Size of screen, in mm.
332   DWORD size_x = GetDeviceCaps(hdc, HORZSIZE);
333   DWORD size_y = GetDeviceCaps(hdc, VERTSIZE);
334   double dpi_x = (size_x > 0) ?
335       GetDeviceCaps(hdc, HORZRES) / (size_x / kMillimetersPerInch) : 0;
336   double dpi_y = (size_y > 0) ?
337       GetDeviceCaps(hdc, VERTRES) / (size_y / kMillimetersPerInch) : 0;
338   screen_info->max_dpi_x = std::max(dpi_x, screen_info->max_dpi_x);
339   screen_info->max_dpi_y = std::max(dpi_y, screen_info->max_dpi_y);
340   return TRUE;
341 }
342
343 void WriteScreenDPIInformationProto(SystemProfileProto::Hardware* hardware) {
344   HDC desktop_dc = GetDC(NULL);
345   if (desktop_dc) {
346     ScreenDPIInformation si = {0, 0};
347     if (EnumDisplayMonitors(desktop_dc, NULL, GetMonitorDPICallback,
348             reinterpret_cast<LPARAM>(&si))) {
349       hardware->set_max_dpi_x(si.max_dpi_x);
350       hardware->set_max_dpi_y(si.max_dpi_y);
351     }
352     ReleaseDC(GetDesktopWindow(), desktop_dc);
353   }
354 }
355
356 #endif  // defined(OS_WIN)
357
358 // Round a timestamp measured in seconds since epoch to one with a granularity
359 // of an hour. This can be used before uploaded potentially sensitive
360 // timestamps.
361 int64 RoundSecondsToHour(int64 time_in_seconds) {
362   return 3600 * (time_in_seconds / 3600);
363 }
364
365 }  // namespace
366
367 GoogleUpdateMetrics::GoogleUpdateMetrics() : is_system_install(false) {}
368
369 GoogleUpdateMetrics::~GoogleUpdateMetrics() {}
370
371 static base::LazyInstance<std::string>::Leaky
372     g_version_extension = LAZY_INSTANCE_INITIALIZER;
373
374 MetricsLog::MetricsLog(const std::string& client_id, int session_id)
375     : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()),
376       creation_time_(base::TimeTicks::Now()) {
377 #if defined(OS_CHROMEOS)
378   metrics_log_chromeos_.reset(new MetricsLogChromeOS(uma_proto()));
379 #endif  // OS_CHROMEOS
380 }
381
382 MetricsLog::~MetricsLog() {}
383
384 // static
385 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
386   registry->RegisterListPref(prefs::kStabilityPluginStats);
387 }
388
389 // static
390 std::string MetricsLog::GetVersionString() {
391   chrome::VersionInfo version_info;
392   if (!version_info.is_valid()) {
393     NOTREACHED() << "Unable to retrieve version info.";
394     return std::string();
395   }
396
397   std::string version = version_info.Version();
398   if (!version_extension().empty())
399     version += version_extension();
400   if (!version_info.IsOfficialBuild())
401     version.append("-devel");
402   return version;
403 }
404
405 // static
406 void MetricsLog::set_version_extension(const std::string& extension) {
407   g_version_extension.Get() = extension;
408 }
409
410 // static
411 const std::string& MetricsLog::version_extension() {
412   return g_version_extension.Get();
413 }
414
415 void MetricsLog::RecordStabilityMetrics(
416     base::TimeDelta incremental_uptime,
417     LogType log_type) {
418   DCHECK_NE(NO_LOG, log_type);
419   DCHECK(!locked());
420   DCHECK(HasEnvironment());
421   DCHECK(!HasStabilityMetrics());
422
423   PrefService* pref = GetPrefService();
424   DCHECK(pref);
425
426   // Get stability attributes out of Local State, zeroing out stored values.
427   // NOTE: This could lead to some data loss if this report isn't successfully
428   //       sent, but that's true for all the metrics.
429
430   WriteRequiredStabilityAttributes(pref);
431   WritePluginStabilityElements(pref);
432
433   // Record recent delta for critical stability metrics.  We can't wait for a
434   // restart to gather these, as that delay biases our observation away from
435   // users that run happily for a looooong time.  We send increments with each
436   // uma log upload, just as we send histogram data.
437   WriteRealtimeStabilityAttributes(pref, incremental_uptime);
438
439   // Omit some stats unless this is the initial stability log.
440   if (log_type != INITIAL_LOG)
441     return;
442
443   int incomplete_shutdown_count =
444       pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
445   pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
446   int breakpad_registration_success_count =
447       pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
448   pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
449   int breakpad_registration_failure_count =
450       pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
451   pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
452   int debugger_present_count =
453       pref->GetInteger(prefs::kStabilityDebuggerPresent);
454   pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
455   int debugger_not_present_count =
456       pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
457   pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
458
459   // TODO(jar): The following are all optional, so we *could* optimize them for
460   // values of zero (and not include them).
461   SystemProfileProto::Stability* stability =
462       uma_proto()->mutable_system_profile()->mutable_stability();
463   stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
464   stability->set_breakpad_registration_success_count(
465       breakpad_registration_success_count);
466   stability->set_breakpad_registration_failure_count(
467       breakpad_registration_failure_count);
468   stability->set_debugger_present_count(debugger_present_count);
469   stability->set_debugger_not_present_count(debugger_not_present_count);
470 }
471
472 PrefService* MetricsLog::GetPrefService() {
473   return g_browser_process->local_state();
474 }
475
476 gfx::Size MetricsLog::GetScreenSize() const {
477   return gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
478 }
479
480 float MetricsLog::GetScreenDeviceScaleFactor() const {
481   return gfx::Screen::GetNativeScreen()->
482       GetPrimaryDisplay().device_scale_factor();
483 }
484
485 int MetricsLog::GetScreenCount() const {
486   // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312
487   return gfx::Screen::GetNativeScreen()->GetNumDisplays();
488 }
489
490 void MetricsLog::GetFieldTrialIds(
491     std::vector<ActiveGroupId>* field_trial_ids) const {
492   chrome_variations::GetFieldTrialActiveGroupIds(field_trial_ids);
493 }
494
495 bool MetricsLog::HasEnvironment() const {
496   return uma_proto()->system_profile().has_uma_enabled_date();
497 }
498
499 bool MetricsLog::HasStabilityMetrics() const {
500   return uma_proto()->system_profile().stability().has_launch_count();
501 }
502
503 void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
504   // Now log plugin stability info.
505   const base::ListValue* plugin_stats_list = pref->GetList(
506       prefs::kStabilityPluginStats);
507   if (!plugin_stats_list)
508     return;
509
510 #if defined(ENABLE_PLUGINS)
511   SystemProfileProto::Stability* stability =
512       uma_proto()->mutable_system_profile()->mutable_stability();
513   for (base::ListValue::const_iterator iter = plugin_stats_list->begin();
514        iter != plugin_stats_list->end(); ++iter) {
515     if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
516       NOTREACHED();
517       continue;
518     }
519     base::DictionaryValue* plugin_dict =
520         static_cast<base::DictionaryValue*>(*iter);
521
522     // Note that this search is potentially a quadratic operation, but given the
523     // low number of plugins installed on a "reasonable" setup, this should be
524     // fine.
525     // TODO(isherman): Verify that this does not show up as a hotspot in
526     // profiler runs.
527     const SystemProfileProto::Plugin* system_profile_plugin = NULL;
528     std::string plugin_name;
529     plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
530     const SystemProfileProto& system_profile = uma_proto()->system_profile();
531     for (int i = 0; i < system_profile.plugin_size(); ++i) {
532       if (system_profile.plugin(i).name() == plugin_name) {
533         system_profile_plugin = &system_profile.plugin(i);
534         break;
535       }
536     }
537
538     if (!system_profile_plugin) {
539       NOTREACHED();
540       continue;
541     }
542
543     SystemProfileProto::Stability::PluginStability* plugin_stability =
544         stability->add_plugin_stability();
545     *plugin_stability->mutable_plugin() = *system_profile_plugin;
546
547     int launches = 0;
548     plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
549     if (launches > 0)
550       plugin_stability->set_launch_count(launches);
551
552     int instances = 0;
553     plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
554     if (instances > 0)
555       plugin_stability->set_instance_count(instances);
556
557     int crashes = 0;
558     plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
559     if (crashes > 0)
560       plugin_stability->set_crash_count(crashes);
561
562     int loading_errors = 0;
563     plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
564                             &loading_errors);
565     if (loading_errors > 0)
566       plugin_stability->set_loading_error_count(loading_errors);
567   }
568 #endif  // defined(ENABLE_PLUGINS)
569
570   pref->ClearPref(prefs::kStabilityPluginStats);
571 }
572
573 // The server refuses data that doesn't have certain values.  crashcount and
574 // launchcount are currently "required" in the "stability" group.
575 // TODO(isherman): Stop writing these attributes specially once the migration to
576 // protobufs is complete.
577 void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
578   int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
579   pref->SetInteger(prefs::kStabilityLaunchCount, 0);
580   int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
581   pref->SetInteger(prefs::kStabilityCrashCount, 0);
582
583   SystemProfileProto::Stability* stability =
584       uma_proto()->mutable_system_profile()->mutable_stability();
585   stability->set_launch_count(launch_count);
586   stability->set_crash_count(crash_count);
587 }
588
589 void MetricsLog::WriteRealtimeStabilityAttributes(
590     PrefService* pref,
591     base::TimeDelta incremental_uptime) {
592   // Update the stats which are critical for real-time stability monitoring.
593   // Since these are "optional," only list ones that are non-zero, as the counts
594   // are aggregated (summed) server side.
595
596   SystemProfileProto::Stability* stability =
597       uma_proto()->mutable_system_profile()->mutable_stability();
598   int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
599   if (count) {
600     stability->set_page_load_count(count);
601     pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
602   }
603
604   count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
605   if (count) {
606     stability->set_renderer_crash_count(count);
607     pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
608   }
609
610   count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
611   if (count) {
612     stability->set_extension_renderer_crash_count(count);
613     pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
614   }
615
616   count = pref->GetInteger(prefs::kStabilityRendererHangCount);
617   if (count) {
618     stability->set_renderer_hang_count(count);
619     pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
620   }
621
622   count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
623   if (count) {
624     stability->set_child_process_crash_count(count);
625     pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
626   }
627
628 #if defined(OS_CHROMEOS)
629   metrics_log_chromeos_->WriteRealtimeStabilityAttributes(pref);
630 #endif  // OS_CHROMEOS
631
632   const uint64 uptime_sec = incremental_uptime.InSeconds();
633   if (uptime_sec)
634     stability->set_uptime_sec(uptime_sec);
635 }
636
637 void MetricsLog::WritePluginList(
638     const std::vector<content::WebPluginInfo>& plugin_list) {
639   DCHECK(!locked());
640
641 #if defined(ENABLE_PLUGINS)
642   PluginPrefs* plugin_prefs = GetPluginPrefs();
643   SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
644   for (std::vector<content::WebPluginInfo>::const_iterator iter =
645            plugin_list.begin();
646        iter != plugin_list.end(); ++iter) {
647     SystemProfileProto::Plugin* plugin = system_profile->add_plugin();
648     SetPluginInfo(*iter, plugin_prefs, plugin);
649   }
650 #endif  // defined(ENABLE_PLUGINS)
651 }
652
653 void MetricsLog::RecordEnvironment(
654     const std::vector<content::WebPluginInfo>& plugin_list,
655     const GoogleUpdateMetrics& google_update_metrics,
656     const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials) {
657   DCHECK(!HasEnvironment());
658
659   SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
660
661   std::string brand_code;
662   if (google_util::GetBrand(&brand_code))
663     system_profile->set_brand_code(brand_code);
664
665   int enabled_date;
666   bool success = base::StringToInt(GetMetricsEnabledDate(GetPrefService()),
667                                    &enabled_date);
668   DCHECK(success);
669
670   // Reduce granularity of the enabled_date field to nearest hour.
671   system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
672
673   int64 install_date = GetPrefService()->GetInt64(prefs::kInstallDate);
674
675   // Reduce granularity of the install_date field to nearest hour.
676   system_profile->set_install_date(RoundSecondsToHour(install_date));
677
678   system_profile->set_application_locale(
679       g_browser_process->GetApplicationLocale());
680
681   SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
682   hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
683   hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
684 #if defined(OS_WIN)
685   hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
686 #endif
687
688   SystemProfileProto::Network* network = system_profile->mutable_network();
689   network->set_connection_type_is_ambiguous(
690       network_observer_.connection_type_is_ambiguous());
691   network->set_connection_type(network_observer_.connection_type());
692   network->set_wifi_phy_layer_protocol_is_ambiguous(
693       network_observer_.wifi_phy_layer_protocol_is_ambiguous());
694   network->set_wifi_phy_layer_protocol(
695       network_observer_.wifi_phy_layer_protocol());
696   network_observer_.Reset();
697
698   SystemProfileProto::OS* os = system_profile->mutable_os();
699   std::string os_name = base::SysInfo::OperatingSystemName();
700 #if defined(OS_WIN)
701   // TODO(mad): This only checks whether the main process is a Metro process at
702   // upload time; not whether the collected metrics were all gathered from
703   // Metro.  This is ok as an approximation for now, since users will rarely be
704   // switching from Metro to Desktop mode; but we should re-evaluate whether we
705   // can distinguish metrics more cleanly in the future: http://crbug.com/140568
706   if (base::win::IsMetroProcess())
707     os_name += " (Metro)";
708 #endif
709   os->set_name(os_name);
710   os->set_version(base::SysInfo::OperatingSystemVersion());
711 #if defined(OS_ANDROID)
712   os->set_fingerprint(
713       base::android::BuildInfo::GetInstance()->android_build_fp());
714 #endif
715
716   base::CPU cpu_info;
717   SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
718   cpu->set_vendor_name(cpu_info.vendor_name());
719   cpu->set_signature(cpu_info.signature());
720
721   const gpu::GPUInfo& gpu_info =
722       GpuDataManager::GetInstance()->GetGPUInfo();
723   SystemProfileProto::Hardware::Graphics* gpu = hardware->mutable_gpu();
724   gpu->set_vendor_id(gpu_info.gpu.vendor_id);
725   gpu->set_device_id(gpu_info.gpu.device_id);
726   gpu->set_driver_version(gpu_info.driver_version);
727   gpu->set_driver_date(gpu_info.driver_date);
728   SystemProfileProto::Hardware::Graphics::PerformanceStatistics*
729       gpu_performance = gpu->mutable_performance_statistics();
730   gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics);
731   gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming);
732   gpu_performance->set_overall_score(gpu_info.performance_stats.overall);
733   gpu->set_gl_vendor(gpu_info.gl_vendor);
734   gpu->set_gl_renderer(gpu_info.gl_renderer);
735
736   const gfx::Size display_size = GetScreenSize();
737   hardware->set_primary_screen_width(display_size.width());
738   hardware->set_primary_screen_height(display_size.height());
739   hardware->set_primary_screen_scale_factor(GetScreenDeviceScaleFactor());
740   hardware->set_screen_count(GetScreenCount());
741
742 #if defined(OS_WIN)
743   WriteScreenDPIInformationProto(hardware);
744 #endif
745
746   WriteGoogleUpdateProto(google_update_metrics);
747
748   WritePluginList(plugin_list);
749
750   std::vector<ActiveGroupId> field_trial_ids;
751   GetFieldTrialIds(&field_trial_ids);
752   WriteFieldTrials(field_trial_ids, system_profile);
753   WriteFieldTrials(synthetic_trials, system_profile);
754
755 #if defined(OS_CHROMEOS)
756   metrics_log_chromeos_->LogChromeOSMetrics();
757 #endif  // OS_CHROMEOS
758
759   std::string serialied_system_profile;
760   std::string base64_system_profile;
761   if (system_profile->SerializeToString(&serialied_system_profile)) {
762     base::Base64Encode(serialied_system_profile, &base64_system_profile);
763     PrefService* local_state = GetPrefService();
764     local_state->SetString(prefs::kStabilitySavedSystemProfile,
765                            base64_system_profile);
766     local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
767                            ComputeSHA1(serialied_system_profile));
768   }
769 }
770
771 bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
772   PrefService* local_state = GetPrefService();
773   const std::string base64_system_profile =
774       local_state->GetString(prefs::kStabilitySavedSystemProfile);
775   if (base64_system_profile.empty())
776     return false;
777
778   const std::string system_profile_hash =
779       local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
780   local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
781   local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
782
783   SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
784   std::string serialied_system_profile;
785   return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
786          ComputeSHA1(serialied_system_profile) == system_profile_hash &&
787          system_profile->ParseFromString(serialied_system_profile);
788 }
789
790 void MetricsLog::RecordProfilerData(
791     const tracked_objects::ProcessDataSnapshot& process_data,
792     int process_type) {
793   DCHECK(!locked());
794
795   if (tracked_objects::GetTimeSourceType() !=
796           tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
797     // We currently only support the default time source, wall clock time.
798     return;
799   }
800
801   ProfilerEventProto* profile;
802   if (!uma_proto()->profiler_event_size()) {
803     // For the first process's data, add a new field to the protocol buffer.
804     profile = uma_proto()->add_profiler_event();
805     profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
806     profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
807   } else {
808     // For the remaining calls, re-use the existing field.
809     profile = uma_proto()->mutable_profiler_event(0);
810   }
811
812   WriteProfilerData(process_data, process_type, profile);
813 }
814
815 void MetricsLog::RecordOmniboxOpenedURL(const OmniboxLog& log) {
816   DCHECK(!locked());
817
818   std::vector<base::string16> terms;
819   const int num_terms =
820       static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));
821
822   OmniboxEventProto* omnibox_event = uma_proto()->add_omnibox_event();
823   omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
824   if (log.tab_id != -1) {
825     // If we know what tab the autocomplete URL was opened in, log it.
826     omnibox_event->set_tab_id(log.tab_id);
827   }
828   omnibox_event->set_typed_length(log.text.length());
829   omnibox_event->set_just_deleted_text(log.just_deleted_text);
830   omnibox_event->set_num_typed_terms(num_terms);
831   omnibox_event->set_selected_index(log.selected_index);
832   if (log.completed_length != base::string16::npos)
833     omnibox_event->set_completed_length(log.completed_length);
834   if (log.elapsed_time_since_user_first_modified_omnibox !=
835       base::TimeDelta::FromMilliseconds(-1)) {
836     // Only upload the typing duration if it is set/valid.
837     omnibox_event->set_typing_duration_ms(
838         log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
839   }
840   omnibox_event->set_duration_since_last_default_match_update_ms(
841       log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
842   omnibox_event->set_current_page_classification(
843       AsOmniboxEventPageClassification(log.current_page_classification));
844   omnibox_event->set_input_type(AsOmniboxEventInputType(log.input_type));
845   omnibox_event->set_is_top_result_hidden_in_dropdown(
846       log.result.ShouldHideTopMatch());
847
848   for (AutocompleteResult::const_iterator i(log.result.begin());
849        i != log.result.end(); ++i) {
850     OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
851     suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
852     suggestion->set_result_type(AsOmniboxEventResultType(i->type));
853     suggestion->set_relevance(i->relevance);
854     if (i->typed_count != -1)
855       suggestion->set_typed_count(i->typed_count);
856     suggestion->set_is_starred(i->starred);
857   }
858   for (ProvidersInfo::const_iterator i(log.providers_info.begin());
859        i != log.providers_info.end(); ++i) {
860     OmniboxEventProto::ProviderInfo* provider_info =
861         omnibox_event->add_provider_info();
862     provider_info->CopyFrom(*i);
863   }
864
865   ++num_events_;
866 }
867
868 void MetricsLog::WriteGoogleUpdateProto(
869     const GoogleUpdateMetrics& google_update_metrics) {
870 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
871   SystemProfileProto::GoogleUpdate* google_update =
872       uma_proto()->mutable_system_profile()->mutable_google_update();
873
874   google_update->set_is_system_install(google_update_metrics.is_system_install);
875
876   if (!google_update_metrics.last_started_au.is_null()) {
877     google_update->set_last_automatic_start_timestamp(
878         google_update_metrics.last_started_au.ToTimeT());
879   }
880
881   if (!google_update_metrics.last_checked.is_null()) {
882     google_update->set_last_update_check_timestamp(
883       google_update_metrics.last_checked.ToTimeT());
884   }
885
886   if (!google_update_metrics.google_update_data.version.empty()) {
887     ProductDataToProto(google_update_metrics.google_update_data,
888                        google_update->mutable_google_update_status());
889   }
890
891   if (!google_update_metrics.product_data.version.empty()) {
892     ProductDataToProto(google_update_metrics.product_data,
893                        google_update->mutable_client_status());
894   }
895 #endif  // defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
896 }