blob: f4b1188819a6dc107abb77fbc694fc259ac967e9 [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"
mvrable@chromium.orgb84ee6e2014-03-15 22:51:5434#include "chrome/browser/metrics/extension_metrics.h"
tfarina@chromium.org0fafc8d2013-06-01 00:09:5035#include "chrome/browser/omnibox/omnibox_log.h"
ibraaaa@google.com0f5e57f52012-09-20 20:53:1836#include "chrome/browser/plugins/plugin_prefs.h"
bauerb@chromium.org10084982011-08-19 17:56:5637#include "chrome/browser/profiles/profile_manager.h"
thestig@chromium.org1eeb5e02010-07-20 23:02:1138#include "chrome/common/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/common/logging_chrome.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5840#include "chrome/common/metrics/proto/omnibox_event.pb.h"
isherman@chromium.orged0fd002012-04-25 23:10:3441#include "chrome/common/metrics/proto/profiler_event.pb.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5842#include "chrome/common/metrics/proto/system_profile.pb.h"
stevet@chromium.orgcf265dc02012-08-15 01:01:1643#include "chrome/common/metrics/variations/variations_util.h"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/common/pref_names.h"
ryanmyers@chromium.org197c0772012-05-14 23:50:5145#include "chrome/installer/util/google_update_settings.h"
yael.aharon@intel.comd5d383252013-07-04 14:44:3246#include "components/nacl/common/nacl_process_type.h"
jam@chromium.org79078df2012-02-16 01:22:3247#include "content/public/browser/gpu_data_manager.h"
isherman@chromium.org1a838cc2012-04-24 22:06:4948#include "content/public/common/content_client.h"
jam@chromium.orgd7bd3e52013-07-21 04:29:2049#include "content/public/common/webplugininfo.h"
zmo@chromium.orgd7b5cc72013-05-23 20:05:0050#include "gpu/config/gpu_info.h"
derat@chromium.org6b7d954ff2011-10-25 00:39:3551#include "ui/gfx/screen.h"
tfarina@chromium.org761fa4702013-07-02 15:25:1552#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2953
dfalcantara@chromium.org5106b3a2012-10-03 20:10:4454#if defined(OS_ANDROID)
55#include "base/android/build_info.h"
56#endif
57
deanm@chromium.orgd1be67b2008-11-19 20:28:3858#if defined(OS_WIN)
isherman@chromium.org1bb25e02012-08-03 22:39:3959#include "base/win/metro.h"
60
61// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
deanm@chromium.orgd1be67b2008-11-19 20:28:3862extern "C" IMAGE_DOS_HEADER __ImageBase;
63#endif
64
tengs@chromium.orgafc03f02013-10-11 06:01:3565#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:2966#include "chrome/browser/metrics/metrics_log_chromeos.h"
67#endif // OS_CHROMEOS
tengs@chromium.orgafc03f02013-10-11 06:01:3568
jam@chromium.org79078df2012-02-16 01:22:3269using content::GpuDataManager;
isherman@chromium.orgfe58acc22012-02-29 01:29:5870using metrics::OmniboxEventProto;
isherman@chromium.orged0fd002012-04-25 23:10:3471using metrics::ProfilerEventProto;
isherman@chromium.orgfe58acc22012-02-29 01:29:5872using metrics::SystemProfileProto;
isherman@chromium.orged0fd002012-04-25 23:10:3473using tracked_objects::ProcessDataSnapshot;
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:1474typedef chrome_variations::ActiveGroupId ActiveGroupId;
ryanmyers@chromium.org197c0772012-05-14 23:50:5175typedef SystemProfileProto::GoogleUpdate::ProductInfo ProductInfo;
jam@chromium.org79078df2012-02-16 01:22:3276
isherman@chromium.org1df44b72012-01-19 05:20:3477namespace {
78
79// Returns the date at which the current metrics client ID was created as
isherman@chromium.orgca5ac4b2012-12-14 07:46:4080// a string containing seconds since the epoch, or "0" if none was found.
isherman@chromium.orgcc5d7f42012-10-30 00:30:0981std::string GetMetricsEnabledDate(PrefService* pref) {
isherman@chromium.org767c9d92012-03-02 16:04:3482 if (!pref) {
isherman@chromium.org1df44b72012-01-19 05:20:3483 NOTREACHED();
84 return "0";
85 }
isherman@chromium.org767c9d92012-03-02 16:04:3486
87 return pref->GetString(prefs::kMetricsClientIDTimestamp);
isherman@chromium.org1df44b72012-01-19 05:20:3488}
89
isherman@chromium.orgfe58acc22012-02-29 01:29:5890OmniboxEventProto::InputType AsOmniboxEventInputType(
91 AutocompleteInput::Type type) {
92 switch (type) {
93 case AutocompleteInput::INVALID:
94 return OmniboxEventProto::INVALID;
95 case AutocompleteInput::UNKNOWN:
96 return OmniboxEventProto::UNKNOWN;
isherman@chromium.orgfe58acc22012-02-29 01:29:5897 case AutocompleteInput::URL:
98 return OmniboxEventProto::URL;
99 case AutocompleteInput::QUERY:
100 return OmniboxEventProto::QUERY;
101 case AutocompleteInput::FORCED_QUERY:
102 return OmniboxEventProto::FORCED_QUERY;
103 default:
104 NOTREACHED();
105 return OmniboxEventProto::INVALID;
106 }
107}
108
isherman@chromium.orgfe58acc22012-02-29 01:29:58109OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
110 AutocompleteMatch::Type type) {
111 switch (type) {
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04112 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
isherman@chromium.orgfe58acc22012-02-29 01:29:58113 return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04114 case AutocompleteMatchType::HISTORY_URL:
isherman@chromium.orgfe58acc22012-02-29 01:29:58115 return OmniboxEventProto::Suggestion::HISTORY_URL;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04116 case AutocompleteMatchType::HISTORY_TITLE:
isherman@chromium.orgfe58acc22012-02-29 01:29:58117 return OmniboxEventProto::Suggestion::HISTORY_TITLE;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04118 case AutocompleteMatchType::HISTORY_BODY:
isherman@chromium.orgfe58acc22012-02-29 01:29:58119 return OmniboxEventProto::Suggestion::HISTORY_BODY;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04120 case AutocompleteMatchType::HISTORY_KEYWORD:
isherman@chromium.orgfe58acc22012-02-29 01:29:58121 return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04122 case AutocompleteMatchType::NAVSUGGEST:
isherman@chromium.orgfe58acc22012-02-29 01:29:58123 return OmniboxEventProto::Suggestion::NAVSUGGEST;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04124 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
isherman@chromium.orgfe58acc22012-02-29 01:29:58125 return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04126 case AutocompleteMatchType::SEARCH_HISTORY:
isherman@chromium.orgfe58acc22012-02-29 01:29:58127 return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04128 case AutocompleteMatchType::SEARCH_SUGGEST:
isherman@chromium.orgfe58acc22012-02-29 01:29:58129 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
skanuj@chromium.org2c03c06b2013-12-11 20:45:02130 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
131 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ENTITY;
132 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE:
133 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_INFINITE;
134 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
135 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PERSONALIZED;
136 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
137 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PROFILE;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04138 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
isherman@chromium.orgfe58acc22012-02-29 01:29:58139 return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04140 case AutocompleteMatchType::EXTENSION_APP:
isherman@chromium.orgfe58acc22012-02-29 01:29:58141 return OmniboxEventProto::Suggestion::EXTENSION_APP;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04142 case AutocompleteMatchType::CONTACT:
derat@chromium.orgdbacefb2012-09-12 03:32:06143 return OmniboxEventProto::Suggestion::CONTACT;
kmadhusu@chromium.orgb7f64d742013-05-21 04:04:04144 case AutocompleteMatchType::BOOKMARK_TITLE:
mrossetti@chromium.org25320602012-10-18 22:05:56145 return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
isherman@chromium.orgfe58acc22012-02-29 01:29:58146 default:
147 NOTREACHED();
148 return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
149 }
150}
151
mpearson@chromium.org3c919d6a2013-07-28 18:35:52152OmniboxEventProto::PageClassification AsOmniboxEventPageClassification(
153 AutocompleteInput::PageClassification page_classification) {
154 switch (page_classification) {
155 case AutocompleteInput::INVALID_SPEC:
156 return OmniboxEventProto::INVALID_SPEC;
hfung@chromium.org12b5cd052013-11-08 20:59:37157 case AutocompleteInput::NTP:
158 return OmniboxEventProto::NTP;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52159 case AutocompleteInput::BLANK:
160 return OmniboxEventProto::BLANK;
mpearson@chromium.org0b1a1112013-08-28 05:23:31161 case AutocompleteInput::HOME_PAGE:
162 return OmniboxEventProto::HOME_PAGE;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52163 case AutocompleteInput::OTHER:
164 return OmniboxEventProto::OTHER;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52165 case AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT:
166 return OmniboxEventProto::
167 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
hfung@chromium.org12b5cd052013-11-08 20:59:37168 case AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS:
169 return OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
170 case AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS:
171 return OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS;
rlp@chromium.org2328ee22013-08-08 23:00:19172 case AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT:
173 return OmniboxEventProto::
174 SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
mpearson@chromium.org3c919d6a2013-07-28 18:35:52175 }
176 return OmniboxEventProto::INVALID_SPEC;
177}
178
isherman@chromium.orged0fd002012-04-25 23:10:34179ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
jam@chromium.orgf3b357692013-03-22 05:16:13180 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:34181 switch (process_type) {
182 case content::PROCESS_TYPE_BROWSER:
183 return ProfilerEventProto::TrackedObject::BROWSER;
184 case content::PROCESS_TYPE_RENDERER:
185 return ProfilerEventProto::TrackedObject::RENDERER;
186 case content::PROCESS_TYPE_PLUGIN:
187 return ProfilerEventProto::TrackedObject::PLUGIN;
188 case content::PROCESS_TYPE_WORKER:
189 return ProfilerEventProto::TrackedObject::WORKER;
isherman@chromium.orged0fd002012-04-25 23:10:34190 case content::PROCESS_TYPE_UTILITY:
191 return ProfilerEventProto::TrackedObject::UTILITY;
isherman@chromium.orged0fd002012-04-25 23:10:34192 case content::PROCESS_TYPE_ZYGOTE:
193 return ProfilerEventProto::TrackedObject::ZYGOTE;
194 case content::PROCESS_TYPE_SANDBOX_HELPER:
195 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
isherman@chromium.orged0fd002012-04-25 23:10:34196 case content::PROCESS_TYPE_GPU:
197 return ProfilerEventProto::TrackedObject::GPU;
198 case content::PROCESS_TYPE_PPAPI_PLUGIN:
199 return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN;
200 case content::PROCESS_TYPE_PPAPI_BROKER:
201 return ProfilerEventProto::TrackedObject::PPAPI_BROKER;
jam@chromium.orgf3b357692013-03-22 05:16:13202 case PROCESS_TYPE_NACL_LOADER:
203 return ProfilerEventProto::TrackedObject::NACL_LOADER;
204 case PROCESS_TYPE_NACL_BROKER:
205 return ProfilerEventProto::TrackedObject::NACL_BROKER;
isherman@chromium.orged0fd002012-04-25 23:10:34206 default:
207 NOTREACHED();
208 return ProfilerEventProto::TrackedObject::UNKNOWN;
209 }
210}
211
asvitkine@chromium.org0f2f7792013-11-28 16:09:14212// Computes a SHA-1 hash of |data| and returns it as a hex string.
213std::string ComputeSHA1(const std::string& data) {
214 const std::string sha1 = base::SHA1HashString(data);
215 return base::HexEncode(sha1.data(), sha1.size());
216}
217
hans@chromium.org1d51882f2013-11-12 01:59:02218#if defined(ENABLE_PLUGINS)
isherman@chromium.org1df44b72012-01-19 05:20:34219// Returns the plugin preferences corresponding for this user, if available.
220// If multiple user profiles are loaded, returns the preferences corresponding
221// to an arbitrary one of the profiles.
222PluginPrefs* GetPluginPrefs() {
223 ProfileManager* profile_manager = g_browser_process->profile_manager();
isherman@chromium.orgfe58acc22012-02-29 01:29:58224
225 if (!profile_manager) {
226 // The profile manager can be NULL when testing.
227 return NULL;
228 }
229
isherman@chromium.org1df44b72012-01-19 05:20:34230 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
231 if (profiles.empty())
232 return NULL;
233
rsleevi@chromium.orgcadac622013-06-11 16:46:36234 return PluginPrefs::GetForProfile(profiles.front()).get();
isherman@chromium.org1df44b72012-01-19 05:20:34235}
236
isherman@chromium.orgfe58acc22012-02-29 01:29:58237// Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|.
jam@chromium.orgd7bd3e52013-07-21 04:29:20238void SetPluginInfo(const content::WebPluginInfo& plugin_info,
isherman@chromium.orgfe58acc22012-02-29 01:29:58239 const PluginPrefs* plugin_prefs,
240 SystemProfileProto::Plugin* plugin) {
avi@chromium.org6778fed2013-12-24 20:09:37241 plugin->set_name(base::UTF16ToUTF8(plugin_info.name));
isherman@chromium.orgfe58acc22012-02-29 01:29:58242 plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe());
avi@chromium.org6778fed2013-12-24 20:09:37243 plugin->set_version(base::UTF16ToUTF8(plugin_info.version));
meacer@chromium.org4e3ca1c292013-09-10 01:59:19244 plugin->set_is_pepper(plugin_info.is_pepper_plugin());
isherman@chromium.orgfe58acc22012-02-29 01:29:58245 if (plugin_prefs)
246 plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info));
247}
hans@chromium.org1d51882f2013-11-12 01:59:02248#endif // defined(ENABLE_PLUGINS)
isherman@chromium.orgfe58acc22012-02-29 01:29:58249
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14250void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
isherman@chromium.org767c9d92012-03-02 16:04:34251 SystemProfileProto* system_profile) {
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14252 for (std::vector<ActiveGroupId>::const_iterator it =
stevet@chromium.orgad2461c2012-04-27 21:11:03253 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
isherman@chromium.org767c9d92012-03-02 16:04:34254 SystemProfileProto::FieldTrial* field_trial =
255 system_profile->add_field_trial();
256 field_trial->set_name_id(it->name);
257 field_trial->set_group_id(it->group);
258 }
259}
260
vadimt@chromium.org0018f722014-02-08 03:08:51261// Maps a thread name by replacing trailing sequence of digits with "*".
262// Examples:
263// 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*"
264// 2. "Chrome_IOThread" => "Chrome_IOThread"
265std::string MapThreadName(const std::string& thread_name) {
266 size_t i = thread_name.length();
267
268 while (i > 0 && isdigit(thread_name[i - 1])) {
269 --i;
270 }
271
272 if (i == thread_name.length())
273 return thread_name;
274
275 return thread_name.substr(0, i) + '*';
276}
277
isherman@chromium.orged0fd002012-04-25 23:10:34278void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
jam@chromium.orgf3b357692013-03-22 05:16:13279 int process_type,
isherman@chromium.orged0fd002012-04-25 23:10:34280 ProfilerEventProto* performance_profile) {
281 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
282 profiler_data.tasks.begin();
283 it != profiler_data.tasks.end(); ++it) {
isherman@chromium.orged0fd002012-04-25 23:10:34284 const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
285 ProfilerEventProto::TrackedObject* tracked_object =
286 performance_profile->add_tracked_object();
isherman@chromium.orgdb7fec832013-07-18 09:41:41287 tracked_object->set_birth_thread_name_hash(
vadimt@chromium.org0018f722014-02-08 03:08:51288 MetricsLogBase::Hash(MapThreadName(it->birth.thread_name)));
isherman@chromium.orgdb7fec832013-07-18 09:41:41289 tracked_object->set_exec_thread_name_hash(
vadimt@chromium.org0018f722014-02-08 03:08:51290 MetricsLogBase::Hash(MapThreadName(it->death_thread_name)));
isherman@chromium.orgdb7fec832013-07-18 09:41:41291 tracked_object->set_source_file_name_hash(
292 MetricsLogBase::Hash(it->birth.location.file_name));
293 tracked_object->set_source_function_name_hash(
294 MetricsLogBase::Hash(it->birth.location.function_name));
isherman@chromium.orged0fd002012-04-25 23:10:34295 tracked_object->set_source_line_number(it->birth.location.line_number);
296 tracked_object->set_exec_count(death_data.count);
297 tracked_object->set_exec_time_total(death_data.run_duration_sum);
298 tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
299 tracked_object->set_queue_time_total(death_data.queue_duration_sum);
300 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
301 tracked_object->set_process_type(AsProtobufProcessType(process_type));
302 tracked_object->set_process_id(profiler_data.process_id);
303 }
304}
305
thakis@chromium.org59a7ae4e2012-10-01 23:54:44306#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
ryanmyers@chromium.org197c0772012-05-14 23:50:51307void ProductDataToProto(const GoogleUpdateSettings::ProductData& product_data,
308 ProductInfo* product_info) {
309 product_info->set_version(product_data.version);
310 product_info->set_last_update_success_timestamp(
311 product_data.last_success.ToTimeT());
312 product_info->set_last_error(product_data.last_error_code);
313 product_info->set_last_extra_error(product_data.last_extra_code);
314 if (ProductInfo::InstallResult_IsValid(product_data.last_result)) {
315 product_info->set_last_result(
316 static_cast<ProductInfo::InstallResult>(product_data.last_result));
317 }
318}
thakis@chromium.org59a7ae4e2012-10-01 23:54:44319#endif
ryanmyers@chromium.org197c0772012-05-14 23:50:51320
girard@chromium.org76869ff2013-01-15 16:13:47321#if defined(OS_WIN)
thestig@chromium.orgffaf7322013-05-15 00:26:11322struct ScreenDPIInformation {
girard@chromium.org76869ff2013-01-15 16:13:47323 double max_dpi_x;
324 double max_dpi_y;
325};
326
327// Called once for each connected monitor.
328BOOL CALLBACK GetMonitorDPICallback(HMONITOR, HDC hdc, LPRECT, LPARAM dwData) {
329 const double kMillimetersPerInch = 25.4;
330 ScreenDPIInformation* screen_info =
331 reinterpret_cast<ScreenDPIInformation*>(dwData);
332 // Size of screen, in mm.
333 DWORD size_x = GetDeviceCaps(hdc, HORZSIZE);
334 DWORD size_y = GetDeviceCaps(hdc, VERTSIZE);
335 double dpi_x = (size_x > 0) ?
336 GetDeviceCaps(hdc, HORZRES) / (size_x / kMillimetersPerInch) : 0;
337 double dpi_y = (size_y > 0) ?
338 GetDeviceCaps(hdc, VERTRES) / (size_y / kMillimetersPerInch) : 0;
339 screen_info->max_dpi_x = std::max(dpi_x, screen_info->max_dpi_x);
340 screen_info->max_dpi_y = std::max(dpi_y, screen_info->max_dpi_y);
341 return TRUE;
342}
343
344void WriteScreenDPIInformationProto(SystemProfileProto::Hardware* hardware) {
345 HDC desktop_dc = GetDC(NULL);
346 if (desktop_dc) {
thestig@chromium.orgffaf7322013-05-15 00:26:11347 ScreenDPIInformation si = {0, 0};
girard@chromium.org76869ff2013-01-15 16:13:47348 if (EnumDisplayMonitors(desktop_dc, NULL, GetMonitorDPICallback,
349 reinterpret_cast<LPARAM>(&si))) {
350 hardware->set_max_dpi_x(si.max_dpi_x);
351 hardware->set_max_dpi_y(si.max_dpi_y);
352 }
353 ReleaseDC(GetDesktopWindow(), desktop_dc);
354 }
355}
356
357#endif // defined(OS_WIN)
358
rkaplow@chromium.org86573d12013-07-11 19:48:32359// Round a timestamp measured in seconds since epoch to one with a granularity
360// of an hour. This can be used before uploaded potentially sensitive
361// timestamps.
362int64 RoundSecondsToHour(int64 time_in_seconds) {
363 return 3600 * (time_in_seconds / 3600);
364}
365
isherman@chromium.org1df44b72012-01-19 05:20:34366} // namespace
367
ryanmyers@chromium.org197c0772012-05-14 23:50:51368GoogleUpdateMetrics::GoogleUpdateMetrics() : is_system_install(false) {}
369
370GoogleUpdateMetrics::~GoogleUpdateMetrics() {}
371
fischman@chromium.org67f92bc32012-01-26 01:56:19372static base::LazyInstance<std::string>::Leaky
asvitkine@chromium.org0f2f7792013-11-28 16:09:14373 g_version_extension = LAZY_INSTANCE_INITIALIZER;
thakis@chromium.org054c8012011-11-16 00:12:42374
ananta@chromium.org1226abb2010-06-10 18:01:28375MetricsLog::MetricsLog(const std::string& client_id, int session_id)
bengr@chromium.org60677562013-11-17 15:52:55376 : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()),
mvrable@chromium.orgb84ee6e2014-03-15 22:51:54377 creation_time_(base::TimeTicks::Now()),
378 extension_metrics_(uma_proto()->client_id()) {
tengs@chromium.orgafc03f02013-10-11 06:01:35379#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29380 metrics_log_chromeos_.reset(new MetricsLogChromeOS(uma_proto()));
381#endif // OS_CHROMEOS
tengs@chromium.orgafc03f02013-10-11 06:01:35382}
jar@chromium.org5ed7d4572009-12-23 17:42:41383
ananta@chromium.org1226abb2010-06-10 18:01:28384MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:29385
386// static
joi@chromium.orgb1de2c72013-02-06 02:45:47387void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
388 registry->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29389}
390
isherman@chromium.org1df44b72012-01-19 05:20:34391// static
ananta@chromium.org1226abb2010-06-10 18:01:28392std::string MetricsLog::GetVersionString() {
evan@chromium.org0211f57e2010-08-27 20:28:42393 chrome::VersionInfo version_info;
dmaclach@chromium.org30c91802010-12-18 00:40:17394 if (!version_info.is_valid()) {
395 NOTREACHED() << "Unable to retrieve version info.";
396 return std::string();
397 }
398
evan@chromium.org0211f57e2010-08-27 20:28:42399 std::string version = version_info.Version();
thakis@chromium.org054c8012011-11-16 00:12:42400 if (!version_extension().empty())
401 version += version_extension();
evan@chromium.org0211f57e2010-08-27 20:28:42402 if (!version_info.IsOfficialBuild())
403 version.append("-devel");
404 return version;
ananta@chromium.org1226abb2010-06-10 18:01:28405}
406
thakis@chromium.org054c8012011-11-16 00:12:42407// static
408void MetricsLog::set_version_extension(const std::string& extension) {
409 g_version_extension.Get() = extension;
410}
411
412// static
413const std::string& MetricsLog::version_extension() {
414 return g_version_extension.Get();
415}
416
mpearson@chromium.org076961c2014-03-12 22:23:56417void MetricsLog::RecordStabilityMetrics(base::TimeDelta incremental_uptime,
418 base::TimeDelta uptime,
419 LogType log_type) {
asvitkine@chromium.org0edf8762013-11-21 18:33:30420 DCHECK_NE(NO_LOG, log_type);
isherman@chromium.org767c9d92012-03-02 16:04:34421 DCHECK(!locked());
asvitkine@chromium.org0f2f7792013-11-28 16:09:14422 DCHECK(HasEnvironment());
423 DCHECK(!HasStabilityMetrics());
jar@google.com0b33f80b2008-12-17 21:34:36424
isherman@chromium.org767c9d92012-03-02 16:04:34425 PrefService* pref = GetPrefService();
jar@google.com0b33f80b2008-12-17 21:34:36426 DCHECK(pref);
427
initial.commit09911bf2008-07-26 23:55:29428 // Get stability attributes out of Local State, zeroing out stored values.
429 // NOTE: This could lead to some data loss if this report isn't successfully
430 // sent, but that's true for all the metrics.
431
jar@google.com147bbc0b2009-01-06 19:37:40432 WriteRequiredStabilityAttributes(pref);
asvitkine@chromium.org250f7b662013-11-23 02:36:51433 WritePluginStabilityElements(pref);
asvitkine@chromium.org0edf8762013-11-21 18:33:30434
435 // Record recent delta for critical stability metrics. We can't wait for a
436 // restart to gather these, as that delay biases our observation away from
437 // users that run happily for a looooong time. We send increments with each
438 // uma log upload, just as we send histogram data.
mpearson@chromium.org076961c2014-03-12 22:23:56439 WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
initial.commit09911bf2008-07-26 23:55:29440
asvitkine@chromium.org0edf8762013-11-21 18:33:30441 // Omit some stats unless this is the initial stability log.
442 if (log_type != INITIAL_LOG)
443 return;
444
isherman@chromium.orgfe58acc22012-02-29 01:29:58445 int incomplete_shutdown_count =
446 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
cpu@chromium.orge324f6b2012-02-28 05:43:37447 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58448 int breakpad_registration_success_count =
449 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
cpu@chromium.orge324f6b2012-02-28 05:43:37450 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58451 int breakpad_registration_failure_count =
452 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
cpu@chromium.orge324f6b2012-02-28 05:43:37453 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58454 int debugger_present_count =
455 pref->GetInteger(prefs::kStabilityDebuggerPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37456 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58457 int debugger_not_present_count =
458 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37459 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31460
isherman@chromium.orgfe58acc22012-02-29 01:29:58461 // TODO(jar): The following are all optional, so we *could* optimize them for
462 // values of zero (and not include them).
isherman@chromium.orgfe58acc22012-02-29 01:29:58463 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34464 uma_proto()->mutable_system_profile()->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58465 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
466 stability->set_breakpad_registration_success_count(
467 breakpad_registration_success_count);
468 stability->set_breakpad_registration_failure_count(
469 breakpad_registration_failure_count);
470 stability->set_debugger_present_count(debugger_present_count);
471 stability->set_debugger_not_present_count(debugger_not_present_count);
asvitkine@chromium.org0edf8762013-11-21 18:33:30472}
isherman@chromium.orgfe58acc22012-02-29 01:29:58473
asvitkine@chromium.org0edf8762013-11-21 18:33:30474PrefService* MetricsLog::GetPrefService() {
475 return g_browser_process->local_state();
476}
477
478gfx::Size MetricsLog::GetScreenSize() const {
479 return gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
480}
481
482float MetricsLog::GetScreenDeviceScaleFactor() const {
483 return gfx::Screen::GetNativeScreen()->
484 GetPrimaryDisplay().device_scale_factor();
485}
486
487int MetricsLog::GetScreenCount() const {
488 // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312
489 return gfx::Screen::GetNativeScreen()->GetNumDisplays();
490}
491
492void MetricsLog::GetFieldTrialIds(
493 std::vector<ActiveGroupId>* field_trial_ids) const {
494 chrome_variations::GetFieldTrialActiveGroupIds(field_trial_ids);
jar@google.com147bbc0b2009-01-06 19:37:40495}
496
asvitkine@chromium.org0f2f7792013-11-28 16:09:14497bool MetricsLog::HasEnvironment() const {
498 return uma_proto()->system_profile().has_uma_enabled_date();
499}
500
501bool MetricsLog::HasStabilityMetrics() const {
502 return uma_proto()->system_profile().stability().has_launch_count();
503}
504
asvitkine@chromium.org250f7b662013-11-23 02:36:51505void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
jar@google.com147bbc0b2009-01-06 19:37:40506 // Now log plugin stability info.
avi@chromium.orgcb1078de2013-12-23 20:04:22507 const base::ListValue* plugin_stats_list = pref->GetList(
initial.commit09911bf2008-07-26 23:55:29508 prefs::kStabilityPluginStats);
isherman@chromium.org1df44b72012-01-19 05:20:34509 if (!plugin_stats_list)
510 return;
initial.commit09911bf2008-07-26 23:55:29511
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55512#if defined(ENABLE_PLUGINS)
isherman@chromium.orgfe58acc22012-02-29 01:29:58513 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34514 uma_proto()->mutable_system_profile()->mutable_stability();
avi@chromium.orgcb1078de2013-12-23 20:04:22515 for (base::ListValue::const_iterator iter = plugin_stats_list->begin();
isherman@chromium.org1df44b72012-01-19 05:20:34516 iter != plugin_stats_list->end(); ++iter) {
avi@chromium.orgcb1078de2013-12-23 20:04:22517 if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
isherman@chromium.org1df44b72012-01-19 05:20:34518 NOTREACHED();
519 continue;
initial.commit09911bf2008-07-26 23:55:29520 }
avi@chromium.orgcb1078de2013-12-23 20:04:22521 base::DictionaryValue* plugin_dict =
522 static_cast<base::DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29523
isherman@chromium.orgfe58acc22012-02-29 01:29:58524 // Note that this search is potentially a quadratic operation, but given the
525 // low number of plugins installed on a "reasonable" setup, this should be
526 // fine.
527 // TODO(isherman): Verify that this does not show up as a hotspot in
528 // profiler runs.
asvitkine@chromium.org250f7b662013-11-23 02:36:51529 const SystemProfileProto::Plugin* system_profile_plugin = NULL;
isherman@chromium.orgf31a01a2013-07-13 02:51:11530 std::string plugin_name;
531 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
asvitkine@chromium.org250f7b662013-11-23 02:36:51532 const SystemProfileProto& system_profile = uma_proto()->system_profile();
533 for (int i = 0; i < system_profile.plugin_size(); ++i) {
534 if (system_profile.plugin(i).name() == plugin_name) {
535 system_profile_plugin = &system_profile.plugin(i);
isherman@chromium.orgfe58acc22012-02-29 01:29:58536 break;
537 }
538 }
539
asvitkine@chromium.org250f7b662013-11-23 02:36:51540 if (!system_profile_plugin) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58541 NOTREACHED();
542 continue;
543 }
544
545 SystemProfileProto::Stability::PluginStability* plugin_stability =
546 stability->add_plugin_stability();
asvitkine@chromium.org250f7b662013-11-23 02:36:51547 *plugin_stability->mutable_plugin() = *system_profile_plugin;
isherman@chromium.orgf31a01a2013-07-13 02:51:11548
549 int launches = 0;
550 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
551 if (launches > 0)
552 plugin_stability->set_launch_count(launches);
553
554 int instances = 0;
555 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
556 if (instances > 0)
557 plugin_stability->set_instance_count(instances);
558
559 int crashes = 0;
560 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
561 if (crashes > 0)
562 plugin_stability->set_crash_count(crashes);
563
564 int loading_errors = 0;
565 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
566 &loading_errors);
567 if (loading_errors > 0)
568 plugin_stability->set_loading_error_count(loading_errors);
initial.commit09911bf2008-07-26 23:55:29569 }
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55570#endif // defined(ENABLE_PLUGINS)
isherman@chromium.org1df44b72012-01-19 05:20:34571
572 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29573}
574
isherman@chromium.orgfe58acc22012-02-29 01:29:58575// The server refuses data that doesn't have certain values. crashcount and
576// launchcount are currently "required" in the "stability" group.
577// TODO(isherman): Stop writing these attributes specially once the migration to
578// protobufs is complete.
jar@google.com147bbc0b2009-01-06 19:37:40579void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58580 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
jar@google.com0b33f80b2008-12-17 21:34:36581 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58582 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
jar@google.com0b33f80b2008-12-17 21:34:36583 pref->SetInteger(prefs::kStabilityCrashCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58584
isherman@chromium.orgfe58acc22012-02-29 01:29:58585 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34586 uma_proto()->mutable_system_profile()->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58587 stability->set_launch_count(launch_count);
588 stability->set_crash_count(crash_count);
jar@google.com0b33f80b2008-12-17 21:34:36589}
590
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36591void MetricsLog::WriteRealtimeStabilityAttributes(
592 PrefService* pref,
mpearson@chromium.org076961c2014-03-12 22:23:56593 base::TimeDelta incremental_uptime,
594 base::TimeDelta uptime) {
jar@google.com0b33f80b2008-12-17 21:34:36595 // Update the stats which are critical for real-time stability monitoring.
596 // Since these are "optional," only list ones that are non-zero, as the counts
asvitkine@chromium.org250f7b662013-11-23 02:36:51597 // are aggregated (summed) server side.
jar@google.com0b33f80b2008-12-17 21:34:36598
isherman@chromium.orgfe58acc22012-02-29 01:29:58599 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34600 uma_proto()->mutable_system_profile()->mutable_stability();
jar@google.com0b33f80b2008-12-17 21:34:36601 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
602 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58603 stability->set_page_load_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36604 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
605 }
606
607 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
608 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58609 stability->set_renderer_crash_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36610 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
611 }
612
asargent@chromium.org1f085622009-12-04 05:33:45613 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
614 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58615 stability->set_extension_renderer_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45616 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
617 }
618
jar@google.com0b33f80b2008-12-17 21:34:36619 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
620 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58621 stability->set_renderer_hang_count(count);
jar@google.com0b33f80b2008-12-17 21:34:36622 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
623 }
asargent@chromium.org1f085622009-12-04 05:33:45624
625 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
626 if (count) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58627 stability->set_child_process_crash_count(count);
asargent@chromium.org1f085622009-12-04 05:33:45628 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
629 }
jar@chromium.org9165f742010-03-10 22:55:01630
petkov@chromium.orgc1834a92011-01-21 18:21:03631#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29632 metrics_log_chromeos_->WriteRealtimeStabilityAttributes(pref);
petkov@chromium.orgc1834a92011-01-21 18:21:03633#endif // OS_CHROMEOS
634
mpearson@chromium.org076961c2014-03-12 22:23:56635 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
636 if (incremental_uptime_sec)
637 stability->set_incremental_uptime_sec(incremental_uptime_sec);
638 const uint64 uptime_sec = uptime.InSeconds();
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36639 if (uptime_sec)
640 stability->set_uptime_sec(uptime_sec);
jar@google.com0b33f80b2008-12-17 21:34:36641}
642
initial.commit09911bf2008-07-26 23:55:29643void MetricsLog::WritePluginList(
jam@chromium.orgd7bd3e52013-07-21 04:29:20644 const std::vector<content::WebPluginInfo>& plugin_list) {
isherman@chromium.org767c9d92012-03-02 16:04:34645 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29646
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55647#if defined(ENABLE_PLUGINS)
648 PluginPrefs* plugin_prefs = GetPluginPrefs();
isherman@chromium.org767c9d92012-03-02 16:04:34649 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
jam@chromium.orgd7bd3e52013-07-21 04:29:20650 for (std::vector<content::WebPluginInfo>::const_iterator iter =
brettw@chromium.org191eb3f72010-12-21 06:27:50651 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29652 iter != plugin_list.end(); ++iter) {
isherman@chromium.org0eecc2542013-06-23 04:22:02653 SystemProfileProto::Plugin* plugin = system_profile->add_plugin();
654 SetPluginInfo(*iter, plugin_prefs, plugin);
initial.commit09911bf2008-07-26 23:55:29655 }
nileshagrawal@chromium.orgebd71962012-12-20 02:56:55656#endif // defined(ENABLE_PLUGINS)
initial.commit09911bf2008-07-26 23:55:29657}
658
659void MetricsLog::RecordEnvironment(
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36660 const std::vector<content::WebPluginInfo>& plugin_list,
661 const GoogleUpdateMetrics& google_update_metrics,
bengr@chromium.org60677562013-11-17 15:52:55662 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials) {
asvitkine@chromium.org0f2f7792013-11-28 16:09:14663 DCHECK(!HasEnvironment());
664
isherman@chromium.orgbc66d532012-03-23 01:57:05665 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
mpearson@chromium.org24b9bb392013-01-29 20:29:29666
667 std::string brand_code;
668 if (google_util::GetBrand(&brand_code))
669 system_profile->set_brand_code(brand_code);
670
isherman@chromium.orgcc5d7f42012-10-30 00:30:09671 int enabled_date;
672 bool success = base::StringToInt(GetMetricsEnabledDate(GetPrefService()),
673 &enabled_date);
isherman@chromium.orgbc66d532012-03-23 01:57:05674 DCHECK(success);
rkaplow@chromium.org86573d12013-07-11 19:48:32675
676 // Reduce granularity of the enabled_date field to nearest hour.
677 system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
678
679 int64 install_date = GetPrefService()->GetInt64(prefs::kInstallDate);
680
681 // Reduce granularity of the install_date field to nearest hour.
682 system_profile->set_install_date(RoundSecondsToHour(install_date));
isherman@chromium.orgbc66d532012-03-23 01:57:05683
684 system_profile->set_application_locale(
jam@chromium.orgeabbfb12013-04-05 23:28:35685 g_browser_process->GetApplicationLocale());
isherman@chromium.orgbc66d532012-03-23 01:57:05686
687 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
thestig@chromium.org0b6a4fb2012-10-16 01:58:21688 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
isherman@chromium.orgbc66d532012-03-23 01:57:05689 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
690#if defined(OS_WIN)
691 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
692#endif
693
szym@chromium.org74b299e2013-01-29 01:24:42694 SystemProfileProto::Network* network = system_profile->mutable_network();
695 network->set_connection_type_is_ambiguous(
szym@chromium.org5eae204c2013-02-13 15:53:42696 network_observer_.connection_type_is_ambiguous());
697 network->set_connection_type(network_observer_.connection_type());
698 network->set_wifi_phy_layer_protocol_is_ambiguous(
699 network_observer_.wifi_phy_layer_protocol_is_ambiguous());
700 network->set_wifi_phy_layer_protocol(
701 network_observer_.wifi_phy_layer_protocol());
702 network_observer_.Reset();
szym@chromium.org74b299e2013-01-29 01:24:42703
isherman@chromium.orgbc66d532012-03-23 01:57:05704 SystemProfileProto::OS* os = system_profile->mutable_os();
isherman@chromium.org1bb25e02012-08-03 22:39:39705 std::string os_name = base::SysInfo::OperatingSystemName();
706#if defined(OS_WIN)
707 // TODO(mad): This only checks whether the main process is a Metro process at
708 // upload time; not whether the collected metrics were all gathered from
709 // Metro. This is ok as an approximation for now, since users will rarely be
710 // switching from Metro to Desktop mode; but we should re-evaluate whether we
711 // can distinguish metrics more cleanly in the future: http://crbug.com/140568
712 if (base::win::IsMetroProcess())
713 os_name += " (Metro)";
714#endif
715 os->set_name(os_name);
isherman@chromium.orgbc66d532012-03-23 01:57:05716 os->set_version(base::SysInfo::OperatingSystemVersion());
dfalcantara@chromium.org5106b3a2012-10-03 20:10:44717#if defined(OS_ANDROID)
718 os->set_fingerprint(
719 base::android::BuildInfo::GetInstance()->android_build_fp());
720#endif
isherman@chromium.orgbc66d532012-03-23 01:57:05721
simonjam@chromium.org5c8f89f692013-07-18 11:13:28722 base::CPU cpu_info;
723 SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
724 cpu->set_vendor_name(cpu_info.vendor_name());
725 cpu->set_signature(cpu_info.signature());
726
zmo@chromium.orgd7b5cc72013-05-23 20:05:00727 const gpu::GPUInfo& gpu_info =
isherman@chromium.orgbc66d532012-03-23 01:57:05728 GpuDataManager::GetInstance()->GetGPUInfo();
729 SystemProfileProto::Hardware::Graphics* gpu = hardware->mutable_gpu();
zmo@chromium.orga094e2c2012-05-10 23:02:42730 gpu->set_vendor_id(gpu_info.gpu.vendor_id);
731 gpu->set_device_id(gpu_info.gpu.device_id);
isherman@chromium.orgbc66d532012-03-23 01:57:05732 gpu->set_driver_version(gpu_info.driver_version);
733 gpu->set_driver_date(gpu_info.driver_date);
734 SystemProfileProto::Hardware::Graphics::PerformanceStatistics*
735 gpu_performance = gpu->mutable_performance_statistics();
736 gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics);
737 gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming);
738 gpu_performance->set_overall_score(gpu_info.performance_stats.overall);
cramya@chromium.orgd6bdfae2013-01-18 20:23:01739 gpu->set_gl_vendor(gpu_info.gl_vendor);
740 gpu->set_gl_renderer(gpu_info.gl_renderer);
isherman@chromium.orgbc66d532012-03-23 01:57:05741
742 const gfx::Size display_size = GetScreenSize();
743 hardware->set_primary_screen_width(display_size.width());
744 hardware->set_primary_screen_height(display_size.height());
thakis@chromium.orgaa96417972012-08-22 03:16:44745 hardware->set_primary_screen_scale_factor(GetScreenDeviceScaleFactor());
isherman@chromium.orgbc66d532012-03-23 01:57:05746 hardware->set_screen_count(GetScreenCount());
747
girard@chromium.org76869ff2013-01-15 16:13:47748#if defined(OS_WIN)
749 WriteScreenDPIInformationProto(hardware);
750#endif
751
ryanmyers@chromium.org197c0772012-05-14 23:50:51752 WriteGoogleUpdateProto(google_update_metrics);
753
isherman@chromium.org0eecc2542013-06-23 04:22:02754 WritePluginList(plugin_list);
mvrable@chromium.orgb84ee6e2014-03-15 22:51:54755 extension_metrics_.WriteExtensionList(uma_proto()->mutable_system_profile());
isherman@chromium.orgbc66d532012-03-23 01:57:05756
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14757 std::vector<ActiveGroupId> field_trial_ids;
isherman@chromium.org767c9d92012-03-02 16:04:34758 GetFieldTrialIds(&field_trial_ids);
759 WriteFieldTrials(field_trial_ids, system_profile);
bengr@chromium.org60677562013-11-17 15:52:55760 WriteFieldTrials(synthetic_trials, system_profile);
asharif@chromium.orgf65859e2013-02-04 20:00:25761
762#if defined(OS_CHROMEOS)
tdresser@chromium.orgc9afed6e2014-02-03 22:12:29763 metrics_log_chromeos_->LogChromeOSMetrics();
764#endif // OS_CHROMEOS
asvitkine@chromium.org0f2f7792013-11-28 16:09:14765
766 std::string serialied_system_profile;
767 std::string base64_system_profile;
vadimt@chromium.org33fca122013-12-11 01:48:50768 if (system_profile->SerializeToString(&serialied_system_profile)) {
769 base::Base64Encode(serialied_system_profile, &base64_system_profile);
asvitkine@chromium.org0f2f7792013-11-28 16:09:14770 PrefService* local_state = GetPrefService();
771 local_state->SetString(prefs::kStabilitySavedSystemProfile,
772 base64_system_profile);
773 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
774 ComputeSHA1(serialied_system_profile));
775 }
776}
777
778bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
779 PrefService* local_state = GetPrefService();
780 const std::string base64_system_profile =
781 local_state->GetString(prefs::kStabilitySavedSystemProfile);
782 if (base64_system_profile.empty())
783 return false;
784
785 const std::string system_profile_hash =
786 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
787 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
788 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
789
790 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
791 std::string serialied_system_profile;
792 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
793 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
794 system_profile->ParseFromString(serialied_system_profile);
initial.commit09911bf2008-07-26 23:55:29795}
796
isherman@chromium.orged0fd002012-04-25 23:10:34797void MetricsLog::RecordProfilerData(
798 const tracked_objects::ProcessDataSnapshot& process_data,
jam@chromium.orgf3b357692013-03-22 05:16:13799 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:34800 DCHECK(!locked());
801
isherman@chromium.org8f829682012-04-27 00:36:49802 if (tracked_objects::GetTimeSourceType() !=
803 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
isherman@chromium.orged0fd002012-04-25 23:10:34804 // We currently only support the default time source, wall clock time.
805 return;
806 }
807
808 ProfilerEventProto* profile;
809 if (!uma_proto()->profiler_event_size()) {
810 // For the first process's data, add a new field to the protocol buffer.
811 profile = uma_proto()->add_profiler_event();
812 profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
813 profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
814 } else {
815 // For the remaining calls, re-use the existing field.
816 profile = uma_proto()->mutable_profiler_event(0);
817 }
818
819 WriteProfilerData(process_data, process_type, profile);
820}
821
tfarina@chromium.org0fafc8d2013-06-01 00:09:50822void MetricsLog::RecordOmniboxOpenedURL(const OmniboxLog& log) {
isherman@chromium.org767c9d92012-03-02 16:04:34823 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29824
brettw@chromium.org8790210c2013-12-02 05:29:53825 std::vector<base::string16> terms;
isherman@chromium.org65956312012-04-19 21:28:12826 const int num_terms =
brettw@chromium.org8790210c2013-12-02 05:29:53827 static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));
initial.commit09911bf2008-07-26 23:55:29828
isherman@chromium.org767c9d92012-03-02 16:04:34829 OmniboxEventProto* omnibox_event = uma_proto()->add_omnibox_event();
isherman@chromium.orgfe58acc22012-02-29 01:29:58830 omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
831 if (log.tab_id != -1) {
832 // If we know what tab the autocomplete URL was opened in, log it.
833 omnibox_event->set_tab_id(log.tab_id);
834 }
835 omnibox_event->set_typed_length(log.text.length());
mpearson@chromium.org660fe8f32012-05-03 20:01:08836 omnibox_event->set_just_deleted_text(log.just_deleted_text);
isherman@chromium.org65956312012-04-19 21:28:12837 omnibox_event->set_num_typed_terms(num_terms);
isherman@chromium.orgfe58acc22012-02-29 01:29:58838 omnibox_event->set_selected_index(log.selected_index);
brettw@chromium.org439f1e32013-12-09 20:09:09839 if (log.completed_length != base::string16::npos)
bartn@chromium.org62b3ef52013-01-18 00:13:02840 omnibox_event->set_completed_length(log.completed_length);
isherman@chromium.org65956312012-04-19 21:28:12841 if (log.elapsed_time_since_user_first_modified_omnibox !=
842 base::TimeDelta::FromMilliseconds(-1)) {
843 // Only upload the typing duration if it is set/valid.
844 omnibox_event->set_typing_duration_ms(
845 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
846 }
mpearson@chromium.org35d55602012-11-20 23:49:03847 omnibox_event->set_duration_since_last_default_match_update_ms(
848 log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
mpearson@chromium.orgee902532012-05-01 23:54:49849 omnibox_event->set_current_page_classification(
mpearson@chromium.org3c919d6a2013-07-28 18:35:52850 AsOmniboxEventPageClassification(log.current_page_classification));
isherman@chromium.orgfe58acc22012-02-29 01:29:58851 omnibox_event->set_input_type(AsOmniboxEventInputType(log.input_type));
dcblack@chromium.org1fce7dd42013-08-06 02:29:17852 omnibox_event->set_is_top_result_hidden_in_dropdown(
853 log.result.ShouldHideTopMatch());
dcblack@chromium.org1fce7dd42013-08-06 02:29:17854
isherman@chromium.orgfe58acc22012-02-29 01:29:58855 for (AutocompleteResult::const_iterator i(log.result.begin());
856 i != log.result.end(); ++i) {
857 OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
mpearson@chromium.org0e9e8782012-05-15 23:01:51858 suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
isherman@chromium.orgfe58acc22012-02-29 01:29:58859 suggestion->set_result_type(AsOmniboxEventResultType(i->type));
860 suggestion->set_relevance(i->relevance);
mpearson@chromium.orgcf6256f2012-06-12 23:36:01861 if (i->typed_count != -1)
862 suggestion->set_typed_count(i->typed_count);
isherman@chromium.orgfe58acc22012-02-29 01:29:58863 suggestion->set_is_starred(i->starred);
864 }
mpearson@chromium.org0e9e8782012-05-15 23:01:51865 for (ProvidersInfo::const_iterator i(log.providers_info.begin());
866 i != log.providers_info.end(); ++i) {
867 OmniboxEventProto::ProviderInfo* provider_info =
868 omnibox_event->add_provider_info();
869 provider_info->CopyFrom(*i);
870 }
isherman@chromium.orgfe58acc22012-02-29 01:29:58871
initial.commit09911bf2008-07-26 23:55:29872 ++num_events_;
873}
ryanmyers@chromium.org197c0772012-05-14 23:50:51874
875void MetricsLog::WriteGoogleUpdateProto(
876 const GoogleUpdateMetrics& google_update_metrics) {
877#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
878 SystemProfileProto::GoogleUpdate* google_update =
879 uma_proto()->mutable_system_profile()->mutable_google_update();
880
881 google_update->set_is_system_install(google_update_metrics.is_system_install);
882
883 if (!google_update_metrics.last_started_au.is_null()) {
884 google_update->set_last_automatic_start_timestamp(
885 google_update_metrics.last_started_au.ToTimeT());
886 }
887
888 if (!google_update_metrics.last_checked.is_null()) {
889 google_update->set_last_update_check_timestamp(
890 google_update_metrics.last_checked.ToTimeT());
891 }
892
893 if (!google_update_metrics.google_update_data.version.empty()) {
894 ProductDataToProto(google_update_metrics.google_update_data,
895 google_update->mutable_google_update_status());
896 }
897
898 if (!google_update_metrics.product_data.version.empty()) {
899 ProductDataToProto(google_update_metrics.product_data,
900 google_update->mutable_client_status());
901 }
902#endif // defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
903}