blob: 1801066d1995691d3e5877c7011268b2f1cec982 [file] [log] [blame]
isherman@chromium.org1df44b72012-01-19 05:20:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
ben@chromium.orgcd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
girard@chromium.org76869ff2013-01-15 16:13:477#include <algorithm>
thestig@chromium.org1eeb5e02010-07-20 23:02:118#include <string>
9#include <vector>
10
asvitkine@chromium.org0f2f7792013-11-28 16:09:1411#include "base/base64.h"
deanm@chromium.orgd1be67b2008-11-19 20:28:3812#include "base/basictypes.h"
keybuk@chromium.org611ae29a2013-04-29 21:32:1913#include "base/bind.h"
simonjam@chromium.org5c8f89f692013-07-18 11:13:2814#include "base/cpu.h"
thakis@chromium.org054c8012011-11-16 00:12:4215#include "base/lazy_instance.h"
levin@chromium.org3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
joi@chromium.org3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_registry_simple.h"
18#include "base/prefs/pref_service.h"
isherman@chromium.orged0fd002012-04-25 23:10:3419#include "base/profiler/alternate_timer.h"
asvitkine@chromium.org0f2f7792013-11-28 16:09:1420#include "base/sha1.h"
brettw@chromium.org3ea1b182013-02-08 22:38:4121#include "base/strings/string_number_conversions.h"
avi@chromium.orgf9b294362013-06-10 20:22:3122#include "base/strings/string_util.h"
avi@chromium.org112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
deanm@chromium.orgfadf97f2008-09-18 12:18:1424#include "base/sys_info.h"
jar@chromium.org37f39e42010-01-06 17:35:1725#include "base/third_party/nspr/prtime.h"
avi@chromium.org84813472013-06-28 00:25:1926#include "base/time/time.h"
isherman@chromium.orged0fd002012-04-25 23:10:3427#include "base/tracked_objects.h"
tfarina@chromium.org9f8bc2f2012-07-03 16:26:5328#include "chrome/browser/autocomplete/autocomplete_input.h"
scr@chromium.org9ac40092010-10-27 23:05:2629#include "chrome/browser/autocomplete/autocomplete_match.h"
tfarina@chromium.org30f5bc92012-06-26 04:14:5530#include "chrome/browser/autocomplete/autocomplete_provider.h"
tfarina@chromium.org73c2b1632012-07-02 22:51:3831#include "chrome/browser/autocomplete/autocomplete_result.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/browser/browser_process.h"
mpearson@chromium.org24b9bb392013-01-29 20:29:2933#include "chrome/browser/google/google_util.h"
tfarina@chromium.org0fafc8d2013-06-01 00:09:5034#include "chrome/browser/omnibox/omnibox_log.h"
ibraaaa@google.com0f5e57f52012-09-20 20:53:1835#include "chrome/browser/plugins/plugin_prefs.h"
bauerb@chromium.org10084982011-08-19 17:56:5636#include "chrome/browser/profiles/profile_manager.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1137#include "chrome/common/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2938#include "chrome/common/logging_chrome.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5839#include "chrome/common/metrics/proto/omnibox_event.pb.h"
isherman@chromium.orged0fd002012-04-25 23:10:3440#include "chrome/common/metrics/proto/profiler_event.pb.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5841#include "chrome/common/metrics/proto/system_profile.pb.h"
stevet@chromium.orgcf265dc02012-08-15 01:01:1642#include "chrome/common/metrics/variations/variations_util.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/common/pref_names.h"
ryanmyers@chromium.org197c0772012-05-14 23:50:5144#include "chrome/installer/util/google_update_settings.h"
yael.aharon@intel.comd5d383252013-07-04 14:44:3245#include "components/nacl/common/nacl_process_type.h"
jam@chromium.org79078df2012-02-16 01:22:3246#include "content/public/browser/gpu_data_manager.h"
isherman@chromium.org1a838cc2012-04-24 22:06:4947#include "content/public/common/content_client.h"
jam@chromium.orgd7bd3e52013-07-21 04:29:2048#include "content/public/common/webplugininfo.h"
zmo@chromium.orgd7b5cc72013-05-23 20:05:0049#include "gpu/config/gpu_info.h"
derat@chromium.org6b7d954ff2011-10-25 00:39:3550#include "ui/gfx/screen.h"
tfarina@chromium.org761fa4702013-07-02 15:25:1551#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2952
dfalcantara@chromium.org5106b3a2012-10-03 20:10:4453#if defined(OS_ANDROID)
54#include "base/android/build_info.h"
55#endif
56
deanm@chromium.orgd1be67b2008-11-19 20:28:3857#if defined(OS_WIN)
isherman@chromium.org1bb25e02012-08-03 22:39:3958#include "base/win/metro.h"
59
60// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
deanm@chromium.orgd1be67b2008-11-19 20:28:3861extern "C" IMAGE_DOS_HEADER __ImageBase;
62#endif
63
tengs@chromium.orgafc03f02013-10-11 06:01:3564#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:2965#include "chrome/browser/metrics/metrics_log_chromeos.h"
66#endif // OS_CHROMEOS
tengs@chromium.orgafc03f02013-10-11 06:01:3567
jam@chromium.org79078df2012-02-16 01:22:3268using content::GpuDataManager;
isherman@chromium.orgfe58acc22012-02-29 01:29:5869using metrics::OmniboxEventProto;
isherman@chromium.orged0fd002012-04-25 23:10:3470using metrics::ProfilerEventProto;
isherman@chromium.orgfe58acc22012-02-29 01:29:5871using metrics::SystemProfileProto;
isherman@chromium.orged0fd002012-04-25 23:10:3472using tracked_objects::ProcessDataSnapshot;
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:1473typedef chrome_variations::ActiveGroupId ActiveGroupId;
ryanmyers@chromium.org197c0772012-05-14 23:50:5174typedef SystemProfileProto::GoogleUpdate::ProductInfo ProductInfo;
jam@chromium.org79078df2012-02-16 01:22:3275
isherman@chromium.org1df44b72012-01-19 05:20:3476namespace {
77
78// Returns the date at which the current metrics client ID was created as
isherman@chromium.orgca5ac4b2012-12-14 07:46:4079// a string containing seconds since the epoch, or "0" if none was found.
isherman@chromium.orgcc5d7f42012-10-30 00:30:0980std::string GetMetricsEnabledDate(PrefService* pref) {
isherman@chromium.org767c9d92012-03-02 16:04:3481 if (!pref) {
isherman@chromium.org1df44b72012-01-19 05:20:3482 NOTREACHED();
83 return "0";
84 }
isherman@chromium.org767c9d92012-03-02 16:04:3485
86 return pref->GetString(prefs::kMetricsClientIDTimestamp);
isherman@chromium.org1df44b72012-01-19 05:20:3487}
88
isherman@chromium.orgfe58acc22012-02-29 01:29:5889OmniboxEventProto::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;
isherman@chromium.orgfe58acc22012-02-29 01:29:5896 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
isherman@chromium.orgfe58acc22012-02-29 01:29:58108OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
109 AutocompleteMatch::Type type) {
110 switch (type) {
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04111 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
isherman@chromium.orgfe58acc22012-02-29 01:29:58112 return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04113 case AutocompleteMatchType::HISTORY_URL:
isherman@chromium.orgfe58acc22012-02-29 01:29:58114 return OmniboxEventProto::Suggestion::HISTORY_URL;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04115 case AutocompleteMatchType::HISTORY_TITLE:
isherman@chromium.orgfe58acc22012-02-29 01:29:58116 return OmniboxEventProto::Suggestion::HISTORY_TITLE;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04117 case AutocompleteMatchType::HISTORY_BODY:
isherman@chromium.orgfe58acc22012-02-29 01:29:58118 return OmniboxEventProto::Suggestion::HISTORY_BODY;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04119 case AutocompleteMatchType::HISTORY_KEYWORD:
isherman@chromium.orgfe58acc22012-02-29 01:29:58120 return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04121 case AutocompleteMatchType::NAVSUGGEST:
isherman@chromium.orgfe58acc22012-02-29 01:29:58122 return OmniboxEventProto::Suggestion::NAVSUGGEST;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04123 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
isherman@chromium.orgfe58acc22012-02-29 01:29:58124 return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04125 case AutocompleteMatchType::SEARCH_HISTORY:
isherman@chromium.orgfe58acc22012-02-29 01:29:58126 return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04127 case AutocompleteMatchType::SEARCH_SUGGEST:
isherman@chromium.orgfe58acc22012-02-29 01:29:58128 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
skanuj@chromium.org2c03c06b2013-12-11 20:45:02129 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;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04137 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
isherman@chromium.orgfe58acc22012-02-29 01:29:58138 return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04139 case AutocompleteMatchType::EXTENSION_APP:
isherman@chromium.orgfe58acc22012-02-29 01:29:58140 return OmniboxEventProto::Suggestion::EXTENSION_APP;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04141 case AutocompleteMatchType::CONTACT:
derat@chromium.orgdbacefb2012-09-12 03:32:06142 return OmniboxEventProto::Suggestion::CONTACT;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04143 case AutocompleteMatchType::BOOKMARK_TITLE:
mrossetti@chromium.org25320602012-10-18 22:05:56144 return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
isherman@chromium.orgfe58acc22012-02-29 01:29:58145 default:
146 NOTREACHED();
147 return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
148 }
149}
150
mpearson@chromium.org3c919d6a2013-07-28 18:35:52151OmniboxEventProto::PageClassification AsOmniboxEventPageClassification(
152 AutocompleteInput::PageClassification page_classification) {
153 switch (page_classification) {
154 case AutocompleteInput::INVALID_SPEC:
155 return OmniboxEventProto::INVALID_SPEC;
hfung@chromium.org12b5cd052013-11-08 20:59:37156 case AutocompleteInput::NTP:
157 return OmniboxEventProto::NTP;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52158 case AutocompleteInput::BLANK:
159 return OmniboxEventProto::BLANK;
mpearson@chromium.org0b1a1112013-08-28 05:23:31160 case AutocompleteInput::HOME_PAGE:
161 return OmniboxEventProto::HOME_PAGE;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52162 case AutocompleteInput::OTHER:
163 return OmniboxEventProto::OTHER;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52164 case AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT:
165 return OmniboxEventProto::
166 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
hfung@chromium.org12b5cd052013-11-08 20:59:37167 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;
rlp@chromium.org2328ee22013-08-08 23:00:19171 case AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT:
172 return OmniboxEventProto::
173 SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52174 }
175 return OmniboxEventProto::INVALID_SPEC;
176}
177
isherman@chromium.orged0fd002012-04-25 23:10:34178ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
jam@chromium.orgf3b357692013-03-22 05:16:13179 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:34180 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;
isherman@chromium.orged0fd002012-04-25 23:10:34189 case content::PROCESS_TYPE_UTILITY:
190 return ProfilerEventProto::TrackedObject::UTILITY;
isherman@chromium.orged0fd002012-04-25 23:10:34191 case content::PROCESS_TYPE_ZYGOTE:
192 return ProfilerEventProto::TrackedObject::ZYGOTE;
193 case content::PROCESS_TYPE_SANDBOX_HELPER:
194 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
isherman@chromium.orged0fd002012-04-25 23:10:34195 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;
jam@chromium.orgf3b357692013-03-22 05:16:13201 case PROCESS_TYPE_NACL_LOADER:
202 return ProfilerEventProto::TrackedObject::NACL_LOADER;
203 case PROCESS_TYPE_NACL_BROKER:
204 return ProfilerEventProto::TrackedObject::NACL_BROKER;
isherman@chromium.orged0fd002012-04-25 23:10:34205 default:
206 NOTREACHED();
207 return ProfilerEventProto::TrackedObject::UNKNOWN;
208 }
209}
210
asvitkine@chromium.org0f2f7792013-11-28 16:09:14211// Computes a SHA-1 hash of |data| and returns it as a hex string.
212std::string ComputeSHA1(const std::string& data) {
213 const std::string sha1 = base::SHA1HashString(data);
214 return base::HexEncode(sha1.data(), sha1.size());
215}
216
hans@chromium.org1d51882f2013-11-12 01:59:02217#if defined(ENABLE_PLUGINS)
isherman@chromium.org1df44b72012-01-19 05:20:34218// 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.
221PluginPrefs* GetPluginPrefs() {
222 ProfileManager* profile_manager = g_browser_process->profile_manager();
isherman@chromium.orgfe58acc22012-02-29 01:29:58223
224 if (!profile_manager) {
225 // The profile manager can be NULL when testing.
226 return NULL;
227 }
228
isherman@chromium.org1df44b72012-01-19 05:20:34229 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
230 if (profiles.empty())
231 return NULL;
232
rsleevi@chromium.orgcadac622013-06-11 16:46:36233 return PluginPrefs::GetForProfile(profiles.front()).get();
isherman@chromium.org1df44b72012-01-19 05:20:34234}
235
isherman@chromium.orgfe58acc22012-02-29 01:29:58236// Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|.
jam@chromium.orgd7bd3e52013-07-21 04:29:20237void SetPluginInfo(const content::WebPluginInfo& plugin_info,
isherman@chromium.orgfe58acc22012-02-29 01:29:58238 const PluginPrefs* plugin_prefs,
239 SystemProfileProto::Plugin* plugin) {
avi@chromium.org6778fed2013-12-24 20:09:37240 plugin->set_name(base::UTF16ToUTF8(plugin_info.name));
isherman@chromium.orgfe58acc22012-02-29 01:29:58241 plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe());
avi@chromium.org6778fed2013-12-24 20:09:37242 plugin->set_version(base::UTF16ToUTF8(plugin_info.version));
meacer@chromium.org4e3ca1c292013-09-10 01:59:19243 plugin->set_is_pepper(plugin_info.is_pepper_plugin());
isherman@chromium.orgfe58acc22012-02-29 01:29:58244 if (plugin_prefs)
245 plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info));
246}
hans@chromium.org1d51882f2013-11-12 01:59:02247#endif // defined(ENABLE_PLUGINS)
isherman@chromium.orgfe58acc22012-02-29 01:29:58248
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14249void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
isherman@chromium.org767c9d92012-03-02 16:04:34250 SystemProfileProto* system_profile) {
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14251 for (std::vector<ActiveGroupId>::const_iterator it =
stevet@chromium.orgad2461c2012-04-27 21:11:03252 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
isherman@chromium.org767c9d92012-03-02 16:04:34253 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
isherman@chromium.orged0fd002012-04-25 23:10:34260void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
jam@chromium.orgf3b357692013-03-22 05:16:13261 int process_type,
isherman@chromium.orged0fd002012-04-25 23:10:34262 ProfilerEventProto* performance_profile) {
263 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
264 profiler_data.tasks.begin();
265 it != profiler_data.tasks.end(); ++it) {
isherman@chromium.orged0fd002012-04-25 23:10:34266 const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
267 ProfilerEventProto::TrackedObject* tracked_object =
268 performance_profile->add_tracked_object();
isherman@chromium.orgdb7fec832013-07-18 09:41:41269 tracked_object->set_birth_thread_name_hash(
270 MetricsLogBase::Hash(it->birth.thread_name));
271 tracked_object->set_exec_thread_name_hash(
272 MetricsLogBase::Hash(it->death_thread_name));
273 tracked_object->set_source_file_name_hash(
274 MetricsLogBase::Hash(it->birth.location.file_name));
275 tracked_object->set_source_function_name_hash(
276 MetricsLogBase::Hash(it->birth.location.function_name));
isherman@chromium.orged0fd002012-04-25 23:10:34277 tracked_object->set_source_line_number(it->birth.location.line_number);
278 tracked_object->set_exec_count(death_data.count);
279 tracked_object->set_exec_time_total(death_data.run_duration_sum);
280 tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
281 tracked_object->set_queue_time_total(death_data.queue_duration_sum);
282 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
283 tracked_object->set_process_type(AsProtobufProcessType(process_type));
284 tracked_object->set_process_id(profiler_data.process_id);
285 }
286}
287
thakis@chromium.org59a7ae4e2012-10-01 23:54:44288#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
ryanmyers@chromium.org197c0772012-05-14 23:50:51289void ProductDataToProto(const GoogleUpdateSettings::ProductData& product_data,
290 ProductInfo* product_info) {
291 product_info->set_version(product_data.version);
292 product_info->set_last_update_success_timestamp(
293 product_data.last_success.ToTimeT());
294 product_info->set_last_error(product_data.last_error_code);
295 product_info->set_last_extra_error(product_data.last_extra_code);
296 if (ProductInfo::InstallResult_IsValid(product_data.last_result)) {
297 product_info->set_last_result(
298 static_cast<ProductInfo::InstallResult>(product_data.last_result));
299 }
300}
thakis@chromium.org59a7ae4e2012-10-01 23:54:44301#endif
ryanmyers@chromium.org197c0772012-05-14 23:50:51302
girard@chromium.org76869ff2013-01-15 16:13:47303#if defined(OS_WIN)
thestig@chromium.orgffaf7322013-05-15 00:26:11304struct ScreenDPIInformation {
girard@chromium.org76869ff2013-01-15 16:13:47305 double max_dpi_x;
306 double max_dpi_y;
307};
308
309// Called once for each connected monitor.
310BOOL CALLBACK GetMonitorDPICallback(HMONITOR, HDC hdc, LPRECT, LPARAM dwData) {
311 const double kMillimetersPerInch = 25.4;
312 ScreenDPIInformation* screen_info =
313 reinterpret_cast<ScreenDPIInformation*>(dwData);
314 // Size of screen, in mm.
315 DWORD size_x = GetDeviceCaps(hdc, HORZSIZE);
316 DWORD size_y = GetDeviceCaps(hdc, VERTSIZE);
317 double dpi_x = (size_x > 0) ?
318 GetDeviceCaps(hdc, HORZRES) / (size_x / kMillimetersPerInch) : 0;
319 double dpi_y = (size_y > 0) ?
320 GetDeviceCaps(hdc, VERTRES) / (size_y / kMillimetersPerInch) : 0;
321 screen_info->max_dpi_x = std::max(dpi_x, screen_info->max_dpi_x);
322 screen_info->max_dpi_y = std::max(dpi_y, screen_info->max_dpi_y);
323 return TRUE;
324}
325
326void WriteScreenDPIInformationProto(SystemProfileProto::Hardware* hardware) {
327 HDC desktop_dc = GetDC(NULL);
328 if (desktop_dc) {
thestig@chromium.orgffaf7322013-05-15 00:26:11329 ScreenDPIInformation si = {0, 0};
girard@chromium.org76869ff2013-01-15 16:13:47330 if (EnumDisplayMonitors(desktop_dc, NULL, GetMonitorDPICallback,
331 reinterpret_cast<LPARAM>(&si))) {
332 hardware->set_max_dpi_x(si.max_dpi_x);
333 hardware->set_max_dpi_y(si.max_dpi_y);
334 }
335 ReleaseDC(GetDesktopWindow(), desktop_dc);
336 }
337}
338
339#endif // defined(OS_WIN)
340
rkaplow@chromium.org86573d12013-07-11 19:48:32341// Round a timestamp measured in seconds since epoch to one with a granularity
342// of an hour. This can be used before uploaded potentially sensitive
343// timestamps.
344int64 RoundSecondsToHour(int64 time_in_seconds) {
345 return 3600 * (time_in_seconds / 3600);
346}
347
isherman@chromium.org1df44b72012-01-19 05:20:34348} // namespace
349
ryanmyers@chromium.org197c0772012-05-14 23:50:51350GoogleUpdateMetrics::GoogleUpdateMetrics() : is_system_install(false) {}
351
352GoogleUpdateMetrics::~GoogleUpdateMetrics() {}
353
fischman@chromium.org67f92bc32012-01-26 01:56:19354static base::LazyInstance<std::string>::Leaky
asvitkine@chromium.org0f2f7792013-11-28 16:09:14355 g_version_extension = LAZY_INSTANCE_INITIALIZER;
thakis@chromium.org054c8012011-11-16 00:12:42356
ananta@chromium.org1226abb2010-06-10 18:01:28357MetricsLog::MetricsLog(const std::string& client_id, int session_id)
bengr@chromium.org60677562013-11-17 15:52:55358 : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()),
359 creation_time_(base::TimeTicks::Now()) {
tengs@chromium.orgafc03f02013-10-11 06:01:35360#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29361 metrics_log_chromeos_.reset(new MetricsLogChromeOS(uma_proto()));
362#endif // OS_CHROMEOS
tengs@chromium.orgafc03f02013-10-11 06:01:35363}
jar@chromium.org5ed7d4572009-12-23 17:42:41364
ananta@chromium.org1226abb2010-06-10 18:01:28365MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:29366
367// static
joi@chromium.orgb1de2c72013-02-06 02:45:47368void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
369 registry->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29370}
371
isherman@chromium.org1df44b72012-01-19 05:20:34372// static
ananta@chromium.org1226abb2010-06-10 18:01:28373std::string MetricsLog::GetVersionString() {
evan@chromium.org0211f57e2010-08-27 20:28:42374 chrome::VersionInfo version_info;
dmaclach@chromium.org30c91802010-12-18 00:40:17375 if (!version_info.is_valid()) {
376 NOTREACHED() << "Unable to retrieve version info.";
377 return std::string();
378 }
379
evan@chromium.org0211f57e2010-08-27 20:28:42380 std::string version = version_info.Version();
thakis@chromium.org054c8012011-11-16 00:12:42381 if (!version_extension().empty())
382 version += version_extension();
evan@chromium.org0211f57e2010-08-27 20:28:42383 if (!version_info.IsOfficialBuild())
384 version.append("-devel");
385 return version;
ananta@chromium.org1226abb2010-06-10 18:01:28386}
387
thakis@chromium.org054c8012011-11-16 00:12:42388// static
389void MetricsLog::set_version_extension(const std::string& extension) {
390 g_version_extension.Get() = extension;
391}
392
393// static
394const std::string& MetricsLog::version_extension() {
395 return g_version_extension.Get();
396}
397
asvitkine@chromium.org0edf8762013-11-21 18:33:30398void MetricsLog::RecordStabilityMetrics(
asvitkine@chromium.org0edf8762013-11-21 18:33:30399 base::TimeDelta incremental_uptime,
400 LogType log_type) {
401 DCHECK_NE(NO_LOG, log_type);
isherman@chromium.org767c9d92012-03-02 16:04:34402 DCHECK(!locked());
asvitkine@chromium.org0f2f7792013-11-28 16:09:14403 DCHECK(HasEnvironment());
404 DCHECK(!HasStabilityMetrics());
jar@google.com0b33f80b2008-12-17 21:34:36405
isherman@chromium.org767c9d92012-03-02 16:04:34406 PrefService* pref = GetPrefService();
jar@google.com0b33f80b2008-12-17 21:34:36407 DCHECK(pref);
408
initial.commit09911bf2008-07-26 23:55:29409 // Get stability attributes out of Local State, zeroing out stored values.
410 // NOTE: This could lead to some data loss if this report isn't successfully
411 // sent, but that's true for all the metrics.
412
jar@google.com147bbc0b2009-01-06 19:37:40413 WriteRequiredStabilityAttributes(pref);
asvitkine@chromium.org250f7b662013-11-23 02:36:51414 WritePluginStabilityElements(pref);
asvitkine@chromium.org0edf8762013-11-21 18:33:30415
416 // Record recent delta for critical stability metrics. We can't wait for a
417 // restart to gather these, as that delay biases our observation away from
418 // users that run happily for a looooong time. We send increments with each
419 // uma log upload, just as we send histogram data.
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36420 WriteRealtimeStabilityAttributes(pref, incremental_uptime);
initial.commit09911bf2008-07-26 23:55:29421
asvitkine@chromium.org0edf8762013-11-21 18:33:30422 // Omit some stats unless this is the initial stability log.
423 if (log_type != INITIAL_LOG)
424 return;
425
isherman@chromium.orgfe58acc22012-02-29 01:29:58426 int incomplete_shutdown_count =
427 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
cpu@chromium.orge324f6b2012-02-28 05:43:37428 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58429 int breakpad_registration_success_count =
430 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
cpu@chromium.orge324f6b2012-02-28 05:43:37431 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58432 int breakpad_registration_failure_count =
433 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
cpu@chromium.orge324f6b2012-02-28 05:43:37434 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58435 int debugger_present_count =
436 pref->GetInteger(prefs::kStabilityDebuggerPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37437 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58438 int debugger_not_present_count =
439 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37440 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31441
isherman@chromium.orgfe58acc22012-02-29 01:29:58442 // TODO(jar): The following are all optional, so we *could* optimize them for
443 // values of zero (and not include them).
isherman@chromium.orgfe58acc22012-02-29 01:29:58444 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34445 uma_proto()->mutable_system_profile()->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58446 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
447 stability->set_breakpad_registration_success_count(
448 breakpad_registration_success_count);
449 stability->set_breakpad_registration_failure_count(
450 breakpad_registration_failure_count);
451 stability->set_debugger_present_count(debugger_present_count);
452 stability->set_debugger_not_present_count(debugger_not_present_count);
asvitkine@chromium.org0edf8762013-11-21 18:33:30453}
isherman@chromium.orgfe58acc22012-02-29 01:29:58454
asvitkine@chromium.org0edf8762013-11-21 18:33:30455PrefService* MetricsLog::GetPrefService() {
456 return g_browser_process->local_state();
457}
458
459gfx::Size MetricsLog::GetScreenSize() const {
460 return gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
461}
462
463float MetricsLog::GetScreenDeviceScaleFactor() const {
464 return gfx::Screen::GetNativeScreen()->
465 GetPrimaryDisplay().device_scale_factor();
466}
467
468int MetricsLog::GetScreenCount() const {
469 // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312
470 return gfx::Screen::GetNativeScreen()->GetNumDisplays();
471}
472
473void MetricsLog::GetFieldTrialIds(
474 std::vector<ActiveGroupId>* field_trial_ids) const {
475 chrome_variations::GetFieldTrialActiveGroupIds(field_trial_ids);
jar@google.com147bbc0b2009-01-06 19:37:40476}
477
asvitkine@chromium.org0f2f7792013-11-28 16:09:14478bool MetricsLog::HasEnvironment() const {
479 return uma_proto()->system_profile().has_uma_enabled_date();
480}
481
482bool MetricsLog::HasStabilityMetrics() const {
483 return uma_proto()->system_profile().stability().has_launch_count();
484}
485
asvitkine@chromium.org250f7b662013-11-23 02:36:51486void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
jar@google.com147bbc0b2009-01-06 19:37:40487 // Now log plugin stability info.
avi@chromium.orgcb1078de2013-12-23 20:04:22488 const base::ListValue* plugin_stats_list = pref->GetList(
initial.commit09911bf2008-07-26 23:55:29489 prefs::kStabilityPluginStats);
isherman@chromium.org1df44b72012-01-19 05:20:34490 if (!plugin_stats_list)
491 return;
initial.commit09911bf2008-07-26 23:55:29492
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55493#if defined(ENABLE_PLUGINS)
isherman@chromium.orgfe58acc22012-02-29 01:29:58494 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34495 uma_proto()->mutable_system_profile()->mutable_stability();
avi@chromium.orgcb1078de2013-12-23 20:04:22496 for (base::ListValue::const_iterator iter = plugin_stats_list->begin();
isherman@chromium.org1df44b72012-01-19 05:20:34497 iter != plugin_stats_list->end(); ++iter) {
avi@chromium.orgcb1078de2013-12-23 20:04:22498 if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
isherman@chromium.org1df44b72012-01-19 05:20:34499 NOTREACHED();
500 continue;
initial.commit09911bf2008-07-26 23:55:29501 }
avi@chromium.orgcb1078de2013-12-23 20:04:22502 base::DictionaryValue* plugin_dict =
503 static_cast<base::DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29504
isherman@chromium.orgfe58acc22012-02-29 01:29:58505 // Note that this search is potentially a quadratic operation, but given the
506 // low number of plugins installed on a "reasonable" setup, this should be
507 // fine.
508 // TODO(isherman): Verify that this does not show up as a hotspot in
509 // profiler runs.
asvitkine@chromium.org250f7b662013-11-23 02:36:51510 const SystemProfileProto::Plugin* system_profile_plugin = NULL;
isherman@chromium.orgf31a01a2013-07-13 02:51:11511 std::string plugin_name;
512 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
asvitkine@chromium.org250f7b662013-11-23 02:36:51513 const SystemProfileProto& system_profile = uma_proto()->system_profile();
514 for (int i = 0; i < system_profile.plugin_size(); ++i) {
515 if (system_profile.plugin(i).name() == plugin_name) {
516 system_profile_plugin = &system_profile.plugin(i);
isherman@chromium.orgfe58acc22012-02-29 01:29:58517 break;
518 }
519 }
520
asvitkine@chromium.org250f7b662013-11-23 02:36:51521 if (!system_profile_plugin) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58522 NOTREACHED();
523 continue;
524 }
525
526 SystemProfileProto::Stability::PluginStability* plugin_stability =
527 stability->add_plugin_stability();
asvitkine@chromium.org250f7b662013-11-23 02:36:51528 *plugin_stability->mutable_plugin() = *system_profile_plugin;
isherman@chromium.orgf31a01a2013-07-13 02:51:11529
530 int launches = 0;
531 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
532 if (launches > 0)
533 plugin_stability->set_launch_count(launches);
534
535 int instances = 0;
536 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
537 if (instances > 0)
538 plugin_stability->set_instance_count(instances);
539
540 int crashes = 0;
541 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
542 if (crashes > 0)
543 plugin_stability->set_crash_count(crashes);
544
545 int loading_errors = 0;
546 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
547 &loading_errors);
548 if (loading_errors > 0)
549 plugin_stability->set_loading_error_count(loading_errors);
initial.commit09911bf2008-07-26 23:55:29550 }
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55551#endif // defined(ENABLE_PLUGINS)
isherman@chromium.org1df44b72012-01-19 05:20:34552
553 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29554}
555
isherman@chromium.orgfe58acc22012-02-29 01:29:58556// The server refuses data that doesn't have certain values. crashcount and
557// launchcount are currently "required" in the "stability" group.
558// TODO(isherman): Stop writing these attributes specially once the migration to
559// protobufs is complete.
jar@google.com147bbc0b2009-01-06 19:37:40560void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58561 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
jar@google.com0b33f80b2008-12-17 21:34:36562 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58563 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
jar@google.com0b33f80b2008-12-17 21:34:36564 pref->SetInteger(prefs::kStabilityCrashCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58565
isherman@chromium.orgfe58acc22012-02-29 01:29:58566 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34567 uma_proto()->mutable_system_profile()->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58568 stability->set_launch_count(launch_count);
569 stability->set_crash_count(crash_count);
jar@google.com0b33f80b2008-12-17 21:34:36570}
571
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36572void MetricsLog::WriteRealtimeStabilityAttributes(
573 PrefService* pref,
574 base::TimeDelta incremental_uptime) {
jar@google.com0b33f80b2008-12-17 21:34:36575 // Update the stats which are critical for real-time stability monitoring.
576 // Since these are "optional," only list ones that are non-zero, as the counts
asvitkine@chromium.org250f7b662013-11-23 02:36:51577 // are aggregated (summed) server side.
jar@google.com0b33f80b2008-12-17 21:34:36578
isherman@chromium.orgfe58acc22012-02-29 01:29:58579 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34580 uma_proto()->mutable_system_profile()->mutable_stability();
jar@google.com0b33f80b2008-12-17 21:34:36581 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
582 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58583 stability->set_page_load_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36584 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
585 }
586
587 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
588 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58589 stability->set_renderer_crash_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36590 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
591 }
592
asargent@chromium.org1f085622009-12-04 05:33:45593 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
594 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58595 stability->set_extension_renderer_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45596 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
597 }
598
jar@google.com0b33f80b2008-12-17 21:34:36599 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
600 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58601 stability->set_renderer_hang_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36602 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
603 }
asargent@chromium.org1f085622009-12-04 05:33:45604
605 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
606 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58607 stability->set_child_process_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45608 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
609 }
jar@chromium.org9165f742010-03-10 22:55:01610
petkov@chromium.orgc1834a92011-01-21 18:21:03611#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29612 metrics_log_chromeos_->WriteRealtimeStabilityAttributes(pref);
petkov@chromium.orgc1834a92011-01-21 18:21:03613#endif // OS_CHROMEOS
614
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36615 const uint64 uptime_sec = incremental_uptime.InSeconds();
616 if (uptime_sec)
617 stability->set_uptime_sec(uptime_sec);
jar@google.com0b33f80b2008-12-17 21:34:36618}
619
initial.commit09911bf2008-07-26 23:55:29620void MetricsLog::WritePluginList(
jam@chromium.orgd7bd3e52013-07-21 04:29:20621 const std::vector<content::WebPluginInfo>& plugin_list) {
isherman@chromium.org767c9d92012-03-02 16:04:34622 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29623
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55624#if defined(ENABLE_PLUGINS)
625 PluginPrefs* plugin_prefs = GetPluginPrefs();
isherman@chromium.org767c9d92012-03-02 16:04:34626 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
jam@chromium.orgd7bd3e52013-07-21 04:29:20627 for (std::vector<content::WebPluginInfo>::const_iterator iter =
brettw@chromium.org191eb3f72010-12-21 06:27:50628 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29629 iter != plugin_list.end(); ++iter) {
isherman@chromium.org0eecc2542013-06-23 04:22:02630 SystemProfileProto::Plugin* plugin = system_profile->add_plugin();
631 SetPluginInfo(*iter, plugin_prefs, plugin);
initial.commit09911bf2008-07-26 23:55:29632 }
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55633#endif // defined(ENABLE_PLUGINS)
initial.commit09911bf2008-07-26 23:55:29634}
635
636void MetricsLog::RecordEnvironment(
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36637 const std::vector<content::WebPluginInfo>& plugin_list,
638 const GoogleUpdateMetrics& google_update_metrics,
bengr@chromium.org60677562013-11-17 15:52:55639 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials) {
asvitkine@chromium.org0f2f7792013-11-28 16:09:14640 DCHECK(!HasEnvironment());
641
isherman@chromium.orgbc66d532012-03-23 01:57:05642 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
mpearson@chromium.org24b9bb392013-01-29 20:29:29643
644 std::string brand_code;
645 if (google_util::GetBrand(&brand_code))
646 system_profile->set_brand_code(brand_code);
647
isherman@chromium.orgcc5d7f42012-10-30 00:30:09648 int enabled_date;
649 bool success = base::StringToInt(GetMetricsEnabledDate(GetPrefService()),
650 &enabled_date);
isherman@chromium.orgbc66d532012-03-23 01:57:05651 DCHECK(success);
rkaplow@chromium.org86573d12013-07-11 19:48:32652
653 // Reduce granularity of the enabled_date field to nearest hour.
654 system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
655
656 int64 install_date = GetPrefService()->GetInt64(prefs::kInstallDate);
657
658 // Reduce granularity of the install_date field to nearest hour.
659 system_profile->set_install_date(RoundSecondsToHour(install_date));
isherman@chromium.orgbc66d532012-03-23 01:57:05660
661 system_profile->set_application_locale(
jam@chromium.orgeabbfb12013-04-05 23:28:35662 g_browser_process->GetApplicationLocale());
isherman@chromium.orgbc66d532012-03-23 01:57:05663
664 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
thestig@chromium.org0b6a4fb2012-10-16 01:58:21665 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
isherman@chromium.orgbc66d532012-03-23 01:57:05666 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
667#if defined(OS_WIN)
668 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
669#endif
670
szym@chromium.org74b299e2013-01-29 01:24:42671 SystemProfileProto::Network* network = system_profile->mutable_network();
672 network->set_connection_type_is_ambiguous(
szym@chromium.org5eae204c2013-02-13 15:53:42673 network_observer_.connection_type_is_ambiguous());
674 network->set_connection_type(network_observer_.connection_type());
675 network->set_wifi_phy_layer_protocol_is_ambiguous(
676 network_observer_.wifi_phy_layer_protocol_is_ambiguous());
677 network->set_wifi_phy_layer_protocol(
678 network_observer_.wifi_phy_layer_protocol());
679 network_observer_.Reset();
szym@chromium.org74b299e2013-01-29 01:24:42680
isherman@chromium.orgbc66d532012-03-23 01:57:05681 SystemProfileProto::OS* os = system_profile->mutable_os();
isherman@chromium.org1bb25e02012-08-03 22:39:39682 std::string os_name = base::SysInfo::OperatingSystemName();
683#if defined(OS_WIN)
684 // TODO(mad): This only checks whether the main process is a Metro process at
685 // upload time; not whether the collected metrics were all gathered from
686 // Metro. This is ok as an approximation for now, since users will rarely be
687 // switching from Metro to Desktop mode; but we should re-evaluate whether we
688 // can distinguish metrics more cleanly in the future: http://crbug.com/140568
689 if (base::win::IsMetroProcess())
690 os_name += " (Metro)";
691#endif
692 os->set_name(os_name);
isherman@chromium.orgbc66d532012-03-23 01:57:05693 os->set_version(base::SysInfo::OperatingSystemVersion());
dfalcantara@chromium.org5106b3a2012-10-03 20:10:44694#if defined(OS_ANDROID)
695 os->set_fingerprint(
696 base::android::BuildInfo::GetInstance()->android_build_fp());
697#endif
isherman@chromium.orgbc66d532012-03-23 01:57:05698
simonjam@chromium.org5c8f89f692013-07-18 11:13:28699 base::CPU cpu_info;
700 SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
701 cpu->set_vendor_name(cpu_info.vendor_name());
702 cpu->set_signature(cpu_info.signature());
703
zmo@chromium.orgd7b5cc72013-05-23 20:05:00704 const gpu::GPUInfo& gpu_info =
isherman@chromium.orgbc66d532012-03-23 01:57:05705 GpuDataManager::GetInstance()->GetGPUInfo();
706 SystemProfileProto::Hardware::Graphics* gpu = hardware->mutable_gpu();
zmo@chromium.orga094e2c2012-05-10 23:02:42707 gpu->set_vendor_id(gpu_info.gpu.vendor_id);
708 gpu->set_device_id(gpu_info.gpu.device_id);
isherman@chromium.orgbc66d532012-03-23 01:57:05709 gpu->set_driver_version(gpu_info.driver_version);
710 gpu->set_driver_date(gpu_info.driver_date);
711 SystemProfileProto::Hardware::Graphics::PerformanceStatistics*
712 gpu_performance = gpu->mutable_performance_statistics();
713 gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics);
714 gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming);
715 gpu_performance->set_overall_score(gpu_info.performance_stats.overall);
cramya@chromium.orgd6bdfae2013-01-18 20:23:01716 gpu->set_gl_vendor(gpu_info.gl_vendor);
717 gpu->set_gl_renderer(gpu_info.gl_renderer);
isherman@chromium.orgbc66d532012-03-23 01:57:05718
719 const gfx::Size display_size = GetScreenSize();
720 hardware->set_primary_screen_width(display_size.width());
721 hardware->set_primary_screen_height(display_size.height());
thakis@chromium.orgaa96417972012-08-22 03:16:44722 hardware->set_primary_screen_scale_factor(GetScreenDeviceScaleFactor());
isherman@chromium.orgbc66d532012-03-23 01:57:05723 hardware->set_screen_count(GetScreenCount());
724
girard@chromium.org76869ff2013-01-15 16:13:47725#if defined(OS_WIN)
726 WriteScreenDPIInformationProto(hardware);
727#endif
728
ryanmyers@chromium.org197c0772012-05-14 23:50:51729 WriteGoogleUpdateProto(google_update_metrics);
730
isherman@chromium.org0eecc2542013-06-23 04:22:02731 WritePluginList(plugin_list);
isherman@chromium.orgbc66d532012-03-23 01:57:05732
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14733 std::vector<ActiveGroupId> field_trial_ids;
isherman@chromium.org767c9d92012-03-02 16:04:34734 GetFieldTrialIds(&field_trial_ids);
735 WriteFieldTrials(field_trial_ids, system_profile);
bengr@chromium.org60677562013-11-17 15:52:55736 WriteFieldTrials(synthetic_trials, system_profile);
asharif@chromium.orgf65859e2013-02-04 20:00:25737
738#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29739 metrics_log_chromeos_->LogChromeOSMetrics();
740#endif // OS_CHROMEOS
asvitkine@chromium.org0f2f7792013-11-28 16:09:14741
742 std::string serialied_system_profile;
743 std::string base64_system_profile;
vadimt@chromium.org33fca122013-12-11 01:48:50744 if (system_profile->SerializeToString(&serialied_system_profile)) {
745 base::Base64Encode(serialied_system_profile, &base64_system_profile);
asvitkine@chromium.org0f2f7792013-11-28 16:09:14746 PrefService* local_state = GetPrefService();
747 local_state->SetString(prefs::kStabilitySavedSystemProfile,
748 base64_system_profile);
749 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
750 ComputeSHA1(serialied_system_profile));
751 }
752}
753
754bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
755 PrefService* local_state = GetPrefService();
756 const std::string base64_system_profile =
757 local_state->GetString(prefs::kStabilitySavedSystemProfile);
758 if (base64_system_profile.empty())
759 return false;
760
761 const std::string system_profile_hash =
762 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
763 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
764 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
765
766 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
767 std::string serialied_system_profile;
768 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
769 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
770 system_profile->ParseFromString(serialied_system_profile);
initial.commit09911bf2008-07-26 23:55:29771}
772
isherman@chromium.orged0fd002012-04-25 23:10:34773void MetricsLog::RecordProfilerData(
774 const tracked_objects::ProcessDataSnapshot& process_data,
jam@chromium.orgf3b357692013-03-22 05:16:13775 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:34776 DCHECK(!locked());
777
isherman@chromium.org8f829682012-04-27 00:36:49778 if (tracked_objects::GetTimeSourceType() !=
779 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
isherman@chromium.orged0fd002012-04-25 23:10:34780 // We currently only support the default time source, wall clock time.
781 return;
782 }
783
784 ProfilerEventProto* profile;
785 if (!uma_proto()->profiler_event_size()) {
786 // For the first process's data, add a new field to the protocol buffer.
787 profile = uma_proto()->add_profiler_event();
788 profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
789 profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
790 } else {
791 // For the remaining calls, re-use the existing field.
792 profile = uma_proto()->mutable_profiler_event(0);
793 }
794
795 WriteProfilerData(process_data, process_type, profile);
796}
797
tfarina@chromium.org0fafc8d2013-06-01 00:09:50798void MetricsLog::RecordOmniboxOpenedURL(const OmniboxLog& log) {
isherman@chromium.org767c9d92012-03-02 16:04:34799 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29800
brettw@chromium.org8790210c2013-12-02 05:29:53801 std::vector<base::string16> terms;
isherman@chromium.org65956312012-04-19 21:28:12802 const int num_terms =
brettw@chromium.org8790210c2013-12-02 05:29:53803 static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));
initial.commit09911bf2008-07-26 23:55:29804
isherman@chromium.org767c9d92012-03-02 16:04:34805 OmniboxEventProto* omnibox_event = uma_proto()->add_omnibox_event();
isherman@chromium.orgfe58acc22012-02-29 01:29:58806 omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
807 if (log.tab_id != -1) {
808 // If we know what tab the autocomplete URL was opened in, log it.
809 omnibox_event->set_tab_id(log.tab_id);
810 }
811 omnibox_event->set_typed_length(log.text.length());
mpearson@chromium.org660fe8f32012-05-03 20:01:08812 omnibox_event->set_just_deleted_text(log.just_deleted_text);
isherman@chromium.org65956312012-04-19 21:28:12813 omnibox_event->set_num_typed_terms(num_terms);
isherman@chromium.orgfe58acc22012-02-29 01:29:58814 omnibox_event->set_selected_index(log.selected_index);
brettw@chromium.org439f1e32013-12-09 20:09:09815 if (log.completed_length != base::string16::npos)
bartn@chromium.org62b3ef52013-01-18 00:13:02816 omnibox_event->set_completed_length(log.completed_length);
isherman@chromium.org65956312012-04-19 21:28:12817 if (log.elapsed_time_since_user_first_modified_omnibox !=
818 base::TimeDelta::FromMilliseconds(-1)) {
819 // Only upload the typing duration if it is set/valid.
820 omnibox_event->set_typing_duration_ms(
821 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
822 }
mpearson@chromium.org35d55602012-11-20 23:49:03823 omnibox_event->set_duration_since_last_default_match_update_ms(
824 log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
mpearson@chromium.orgee902532012-05-01 23:54:49825 omnibox_event->set_current_page_classification(
mpearson@chromium.org3c919d6a2013-07-28 18:35:52826 AsOmniboxEventPageClassification(log.current_page_classification));
isherman@chromium.orgfe58acc22012-02-29 01:29:58827 omnibox_event->set_input_type(AsOmniboxEventInputType(log.input_type));
dcblack@chromium.org1fce7dd42013-08-06 02:29:17828 omnibox_event->set_is_top_result_hidden_in_dropdown(
829 log.result.ShouldHideTopMatch());
dcblack@chromium.org1fce7dd42013-08-06 02:29:17830
isherman@chromium.orgfe58acc22012-02-29 01:29:58831 for (AutocompleteResult::const_iterator i(log.result.begin());
832 i != log.result.end(); ++i) {
833 OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
mpearson@chromium.org0e9e8782012-05-15 23:01:51834 suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
isherman@chromium.orgfe58acc22012-02-29 01:29:58835 suggestion->set_result_type(AsOmniboxEventResultType(i->type));
836 suggestion->set_relevance(i->relevance);
mpearson@chromium.orgcf6256f2012-06-12 23:36:01837 if (i->typed_count != -1)
838 suggestion->set_typed_count(i->typed_count);
isherman@chromium.orgfe58acc22012-02-29 01:29:58839 suggestion->set_is_starred(i->starred);
840 }
mpearson@chromium.org0e9e8782012-05-15 23:01:51841 for (ProvidersInfo::const_iterator i(log.providers_info.begin());
842 i != log.providers_info.end(); ++i) {
843 OmniboxEventProto::ProviderInfo* provider_info =
844 omnibox_event->add_provider_info();
845 provider_info->CopyFrom(*i);
846 }
isherman@chromium.orgfe58acc22012-02-29 01:29:58847
initial.commit09911bf2008-07-26 23:55:29848 ++num_events_;
849}
ryanmyers@chromium.org197c0772012-05-14 23:50:51850
851void MetricsLog::WriteGoogleUpdateProto(
852 const GoogleUpdateMetrics& google_update_metrics) {
853#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
854 SystemProfileProto::GoogleUpdate* google_update =
855 uma_proto()->mutable_system_profile()->mutable_google_update();
856
857 google_update->set_is_system_install(google_update_metrics.is_system_install);
858
859 if (!google_update_metrics.last_started_au.is_null()) {
860 google_update->set_last_automatic_start_timestamp(
861 google_update_metrics.last_started_au.ToTimeT());
862 }
863
864 if (!google_update_metrics.last_checked.is_null()) {
865 google_update->set_last_update_check_timestamp(
866 google_update_metrics.last_checked.ToTimeT());
867 }
868
869 if (!google_update_metrics.google_update_data.version.empty()) {
870 ProductDataToProto(google_update_metrics.google_update_data,
871 google_update->mutable_google_update_status());
872 }
873
874 if (!google_update_metrics.product_data.version.empty()) {
875 ProductDataToProto(google_update_metrics.product_data,
876 google_update->mutable_client_status());
877 }
878#endif // defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
879}