blob: 0041bc4d01417e5dd9e0023b2500ecfbdae2980f [file] [log] [blame]
[email protected]1df44b72012-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
[email protected]cd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]76869ff2013-01-15 16:13:477#include <algorithm>
[email protected]1eeb5e02010-07-20 23:02:118#include <string>
9#include <vector>
10
[email protected]0f2f7792013-11-28 16:09:1411#include "base/base64.h"
[email protected]d1be67b2008-11-19 20:28:3812#include "base/basictypes.h"
[email protected]611ae29a2013-04-29 21:32:1913#include "base/bind.h"
[email protected]5c8f89f692013-07-18 11:13:2814#include "base/cpu.h"
[email protected]054c8012011-11-16 00:12:4215#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_registry_simple.h"
18#include "base/prefs/pref_service.h"
[email protected]ed0fd002012-04-25 23:10:3419#include "base/profiler/alternate_timer.h"
[email protected]0f2f7792013-11-28 16:09:1420#include "base/sha1.h"
[email protected]3ea1b182013-02-08 22:38:4121#include "base/strings/string_number_conversions.h"
[email protected]f9b294362013-06-10 20:22:3122#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]fadf97f2008-09-18 12:18:1424#include "base/sys_info.h"
[email protected]37f39e42010-01-06 17:35:1725#include "base/third_party/nspr/prtime.h"
[email protected]84813472013-06-28 00:25:1926#include "base/time/time.h"
[email protected]ed0fd002012-04-25 23:10:3427#include "base/tracked_objects.h"
[email protected]9f8bc2f2012-07-03 16:26:5328#include "chrome/browser/autocomplete/autocomplete_input.h"
[email protected]9ac40092010-10-27 23:05:2629#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]30f5bc92012-06-26 04:14:5530#include "chrome/browser/autocomplete/autocomplete_provider.h"
[email protected]73c2b1632012-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"
[email protected]24b9bb392013-01-29 20:29:2933#include "chrome/browser/google/google_util.h"
[email protected]b84ee6e2014-03-15 22:51:5434#include "chrome/browser/metrics/extension_metrics.h"
[email protected]0fafc8d2013-06-01 00:09:5035#include "chrome/browser/omnibox/omnibox_log.h"
[email protected]0f5e57f52012-09-20 20:53:1836#include "chrome/browser/plugins/plugin_prefs.h"
[email protected]10084982011-08-19 17:56:5637#include "chrome/browser/profiles/profile_manager.h"
[email protected]1eeb5e02010-07-20 23:02:1138#include "chrome/common/chrome_version_info.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/common/pref_names.h"
[email protected]197c0772012-05-14 23:50:5140#include "chrome/installer/util/google_update_settings.h"
[email protected]85791b0b2014-05-20 15:18:5841#include "components/metrics/metrics_provider.h"
[email protected]064107e2014-05-02 00:59:0642#include "components/metrics/proto/omnibox_event.pb.h"
43#include "components/metrics/proto/profiler_event.pb.h"
44#include "components/metrics/proto/system_profile.pb.h"
[email protected]d5d383252013-07-04 14:44:3245#include "components/nacl/common/nacl_process_type.h"
[email protected]b3610d42014-05-19 18:07:2346#include "components/variations/active_field_trials.h"
[email protected]79078df2012-02-16 01:22:3247#include "content/public/browser/gpu_data_manager.h"
[email protected]1a838cc2012-04-24 22:06:4948#include "content/public/common/content_client.h"
[email protected]d7bd3e52013-07-21 04:29:2049#include "content/public/common/webplugininfo.h"
[email protected]d7b5cc72013-05-23 20:05:0050#include "gpu/config/gpu_info.h"
[email protected]6b7d954ff2011-10-25 00:39:3551#include "ui/gfx/screen.h"
[email protected]761fa4702013-07-02 15:25:1552#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2953
[email protected]5106b3a2012-10-03 20:10:4454#if defined(OS_ANDROID)
55#include "base/android/build_info.h"
56#endif
57
[email protected]d1be67b2008-11-19 20:28:3858#if defined(OS_WIN)
[email protected]1bb25e02012-08-03 22:39:3959#include "base/win/metro.h"
60
61// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
[email protected]d1be67b2008-11-19 20:28:3862extern "C" IMAGE_DOS_HEADER __ImageBase;
63#endif
64
[email protected]afc03f02013-10-11 06:01:3565#if defined(OS_CHROMEOS)
[email protected]c9afed6e2014-02-03 22:12:2966#include "chrome/browser/metrics/metrics_log_chromeos.h"
67#endif // OS_CHROMEOS
[email protected]afc03f02013-10-11 06:01:3568
[email protected]79078df2012-02-16 01:22:3269using content::GpuDataManager;
[email protected]064107e2014-05-02 00:59:0670using metrics::MetricsLogBase;
[email protected]fe58acc22012-02-29 01:29:5871using metrics::OmniboxEventProto;
[email protected]ed0fd002012-04-25 23:10:3472using metrics::ProfilerEventProto;
[email protected]fe58acc22012-02-29 01:29:5873using metrics::SystemProfileProto;
[email protected]ed0fd002012-04-25 23:10:3474using tracked_objects::ProcessDataSnapshot;
[email protected]b3610d42014-05-19 18:07:2375typedef variations::ActiveGroupId ActiveGroupId;
[email protected]197c0772012-05-14 23:50:5176typedef SystemProfileProto::GoogleUpdate::ProductInfo ProductInfo;
[email protected]79078df2012-02-16 01:22:3277
[email protected]1df44b72012-01-19 05:20:3478namespace {
79
80// Returns the date at which the current metrics client ID was created as
[email protected]ca5ac4b2012-12-14 07:46:4081// a string containing seconds since the epoch, or "0" if none was found.
[email protected]cc5d7f42012-10-30 00:30:0982std::string GetMetricsEnabledDate(PrefService* pref) {
[email protected]767c9d92012-03-02 16:04:3483 if (!pref) {
[email protected]1df44b72012-01-19 05:20:3484 NOTREACHED();
85 return "0";
86 }
[email protected]767c9d92012-03-02 16:04:3487
[email protected]1f74e802014-03-29 03:08:3588 return pref->GetString(prefs::kMetricsReportingEnabledTimestamp);
[email protected]1df44b72012-01-19 05:20:3489}
90
[email protected]fe58acc22012-02-29 01:29:5891OmniboxEventProto::InputType AsOmniboxEventInputType(
92 AutocompleteInput::Type type) {
93 switch (type) {
94 case AutocompleteInput::INVALID:
95 return OmniboxEventProto::INVALID;
96 case AutocompleteInput::UNKNOWN:
97 return OmniboxEventProto::UNKNOWN;
[email protected]fe58acc22012-02-29 01:29:5898 case AutocompleteInput::URL:
99 return OmniboxEventProto::URL;
100 case AutocompleteInput::QUERY:
101 return OmniboxEventProto::QUERY;
102 case AutocompleteInput::FORCED_QUERY:
103 return OmniboxEventProto::FORCED_QUERY;
104 default:
105 NOTREACHED();
106 return OmniboxEventProto::INVALID;
107 }
108}
109
[email protected]fe58acc22012-02-29 01:29:58110OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
111 AutocompleteMatch::Type type) {
112 switch (type) {
[email protected]b7f64d742013-05-21 04:04:04113 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
[email protected]fe58acc22012-02-29 01:29:58114 return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
[email protected]b7f64d742013-05-21 04:04:04115 case AutocompleteMatchType::HISTORY_URL:
[email protected]fe58acc22012-02-29 01:29:58116 return OmniboxEventProto::Suggestion::HISTORY_URL;
[email protected]b7f64d742013-05-21 04:04:04117 case AutocompleteMatchType::HISTORY_TITLE:
[email protected]fe58acc22012-02-29 01:29:58118 return OmniboxEventProto::Suggestion::HISTORY_TITLE;
[email protected]b7f64d742013-05-21 04:04:04119 case AutocompleteMatchType::HISTORY_BODY:
[email protected]fe58acc22012-02-29 01:29:58120 return OmniboxEventProto::Suggestion::HISTORY_BODY;
[email protected]b7f64d742013-05-21 04:04:04121 case AutocompleteMatchType::HISTORY_KEYWORD:
[email protected]fe58acc22012-02-29 01:29:58122 return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
[email protected]b7f64d742013-05-21 04:04:04123 case AutocompleteMatchType::NAVSUGGEST:
[email protected]fe58acc22012-02-29 01:29:58124 return OmniboxEventProto::Suggestion::NAVSUGGEST;
[email protected]b7f64d742013-05-21 04:04:04125 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
[email protected]fe58acc22012-02-29 01:29:58126 return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
[email protected]b7f64d742013-05-21 04:04:04127 case AutocompleteMatchType::SEARCH_HISTORY:
[email protected]fe58acc22012-02-29 01:29:58128 return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
[email protected]b7f64d742013-05-21 04:04:04129 case AutocompleteMatchType::SEARCH_SUGGEST:
[email protected]fe58acc22012-02-29 01:29:58130 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
[email protected]2c03c06b2013-12-11 20:45:02131 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
132 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ENTITY;
133 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE:
134 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_INFINITE;
135 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
136 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PERSONALIZED;
137 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
138 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PROFILE;
[email protected]b7f64d742013-05-21 04:04:04139 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
[email protected]fe58acc22012-02-29 01:29:58140 return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
[email protected]b7f64d742013-05-21 04:04:04141 case AutocompleteMatchType::EXTENSION_APP:
[email protected]fe58acc22012-02-29 01:29:58142 return OmniboxEventProto::Suggestion::EXTENSION_APP;
[email protected]b7f64d742013-05-21 04:04:04143 case AutocompleteMatchType::BOOKMARK_TITLE:
[email protected]25320602012-10-18 22:05:56144 return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
[email protected]78981d8c2014-05-09 15:05:47145 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
146 return OmniboxEventProto::Suggestion::NAVSUGGEST_PERSONALIZED;
[email protected]fe58acc22012-02-29 01:29:58147 default:
148 NOTREACHED();
149 return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
150 }
151}
152
[email protected]3c919d6a2013-07-28 18:35:52153OmniboxEventProto::PageClassification AsOmniboxEventPageClassification(
154 AutocompleteInput::PageClassification page_classification) {
155 switch (page_classification) {
156 case AutocompleteInput::INVALID_SPEC:
157 return OmniboxEventProto::INVALID_SPEC;
[email protected]12b5cd052013-11-08 20:59:37158 case AutocompleteInput::NTP:
159 return OmniboxEventProto::NTP;
[email protected]3c919d6a2013-07-28 18:35:52160 case AutocompleteInput::BLANK:
161 return OmniboxEventProto::BLANK;
[email protected]0b1a1112013-08-28 05:23:31162 case AutocompleteInput::HOME_PAGE:
163 return OmniboxEventProto::HOME_PAGE;
[email protected]3c919d6a2013-07-28 18:35:52164 case AutocompleteInput::OTHER:
165 return OmniboxEventProto::OTHER;
[email protected]3c919d6a2013-07-28 18:35:52166 case AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT:
167 return OmniboxEventProto::
168 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
[email protected]12b5cd052013-11-08 20:59:37169 case AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS:
170 return OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
171 case AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS:
172 return OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS;
[email protected]2328ee22013-08-08 23:00:19173 case AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT:
174 return OmniboxEventProto::
175 SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
[email protected]3c919d6a2013-07-28 18:35:52176 }
177 return OmniboxEventProto::INVALID_SPEC;
178}
179
[email protected]ed0fd002012-04-25 23:10:34180ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
[email protected]f3b357692013-03-22 05:16:13181 int process_type) {
[email protected]ed0fd002012-04-25 23:10:34182 switch (process_type) {
183 case content::PROCESS_TYPE_BROWSER:
184 return ProfilerEventProto::TrackedObject::BROWSER;
185 case content::PROCESS_TYPE_RENDERER:
186 return ProfilerEventProto::TrackedObject::RENDERER;
187 case content::PROCESS_TYPE_PLUGIN:
188 return ProfilerEventProto::TrackedObject::PLUGIN;
189 case content::PROCESS_TYPE_WORKER:
190 return ProfilerEventProto::TrackedObject::WORKER;
[email protected]ed0fd002012-04-25 23:10:34191 case content::PROCESS_TYPE_UTILITY:
192 return ProfilerEventProto::TrackedObject::UTILITY;
[email protected]ed0fd002012-04-25 23:10:34193 case content::PROCESS_TYPE_ZYGOTE:
194 return ProfilerEventProto::TrackedObject::ZYGOTE;
195 case content::PROCESS_TYPE_SANDBOX_HELPER:
196 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
[email protected]ed0fd002012-04-25 23:10:34197 case content::PROCESS_TYPE_GPU:
198 return ProfilerEventProto::TrackedObject::GPU;
199 case content::PROCESS_TYPE_PPAPI_PLUGIN:
200 return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN;
201 case content::PROCESS_TYPE_PPAPI_BROKER:
202 return ProfilerEventProto::TrackedObject::PPAPI_BROKER;
[email protected]f3b357692013-03-22 05:16:13203 case PROCESS_TYPE_NACL_LOADER:
204 return ProfilerEventProto::TrackedObject::NACL_LOADER;
205 case PROCESS_TYPE_NACL_BROKER:
206 return ProfilerEventProto::TrackedObject::NACL_BROKER;
[email protected]ed0fd002012-04-25 23:10:34207 default:
208 NOTREACHED();
209 return ProfilerEventProto::TrackedObject::UNKNOWN;
210 }
211}
212
[email protected]064107e2014-05-02 00:59:06213SystemProfileProto::Channel AsProtobufChannel(
214 chrome::VersionInfo::Channel channel) {
215 switch (channel) {
216 case chrome::VersionInfo::CHANNEL_UNKNOWN:
217 return SystemProfileProto::CHANNEL_UNKNOWN;
218 case chrome::VersionInfo::CHANNEL_CANARY:
219 return SystemProfileProto::CHANNEL_CANARY;
220 case chrome::VersionInfo::CHANNEL_DEV:
221 return SystemProfileProto::CHANNEL_DEV;
222 case chrome::VersionInfo::CHANNEL_BETA:
223 return SystemProfileProto::CHANNEL_BETA;
224 case chrome::VersionInfo::CHANNEL_STABLE:
225 return SystemProfileProto::CHANNEL_STABLE;
226 default:
227 NOTREACHED();
228 return SystemProfileProto::CHANNEL_UNKNOWN;
229 }
230}
231
[email protected]0f2f7792013-11-28 16:09:14232// Computes a SHA-1 hash of |data| and returns it as a hex string.
233std::string ComputeSHA1(const std::string& data) {
234 const std::string sha1 = base::SHA1HashString(data);
235 return base::HexEncode(sha1.data(), sha1.size());
236}
237
[email protected]1d51882f2013-11-12 01:59:02238#if defined(ENABLE_PLUGINS)
[email protected]1df44b72012-01-19 05:20:34239// Returns the plugin preferences corresponding for this user, if available.
240// If multiple user profiles are loaded, returns the preferences corresponding
241// to an arbitrary one of the profiles.
242PluginPrefs* GetPluginPrefs() {
243 ProfileManager* profile_manager = g_browser_process->profile_manager();
[email protected]fe58acc22012-02-29 01:29:58244
245 if (!profile_manager) {
246 // The profile manager can be NULL when testing.
247 return NULL;
248 }
249
[email protected]1df44b72012-01-19 05:20:34250 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
251 if (profiles.empty())
252 return NULL;
253
[email protected]cadac622013-06-11 16:46:36254 return PluginPrefs::GetForProfile(profiles.front()).get();
[email protected]1df44b72012-01-19 05:20:34255}
256
[email protected]fe58acc22012-02-29 01:29:58257// Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|.
[email protected]d7bd3e52013-07-21 04:29:20258void SetPluginInfo(const content::WebPluginInfo& plugin_info,
[email protected]fe58acc22012-02-29 01:29:58259 const PluginPrefs* plugin_prefs,
260 SystemProfileProto::Plugin* plugin) {
[email protected]6778fed2013-12-24 20:09:37261 plugin->set_name(base::UTF16ToUTF8(plugin_info.name));
[email protected]fe58acc22012-02-29 01:29:58262 plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe());
[email protected]6778fed2013-12-24 20:09:37263 plugin->set_version(base::UTF16ToUTF8(plugin_info.version));
[email protected]4e3ca1c292013-09-10 01:59:19264 plugin->set_is_pepper(plugin_info.is_pepper_plugin());
[email protected]fe58acc22012-02-29 01:29:58265 if (plugin_prefs)
266 plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info));
267}
[email protected]1d51882f2013-11-12 01:59:02268#endif // defined(ENABLE_PLUGINS)
[email protected]fe58acc22012-02-29 01:29:58269
[email protected]0c8b7ad2012-11-06 07:08:14270void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
[email protected]767c9d92012-03-02 16:04:34271 SystemProfileProto* system_profile) {
[email protected]0c8b7ad2012-11-06 07:08:14272 for (std::vector<ActiveGroupId>::const_iterator it =
[email protected]ad2461c2012-04-27 21:11:03273 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
[email protected]767c9d92012-03-02 16:04:34274 SystemProfileProto::FieldTrial* field_trial =
275 system_profile->add_field_trial();
276 field_trial->set_name_id(it->name);
277 field_trial->set_group_id(it->group);
278 }
279}
280
[email protected]0018f722014-02-08 03:08:51281// Maps a thread name by replacing trailing sequence of digits with "*".
282// Examples:
283// 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*"
284// 2. "Chrome_IOThread" => "Chrome_IOThread"
285std::string MapThreadName(const std::string& thread_name) {
286 size_t i = thread_name.length();
287
288 while (i > 0 && isdigit(thread_name[i - 1])) {
289 --i;
290 }
291
292 if (i == thread_name.length())
293 return thread_name;
294
295 return thread_name.substr(0, i) + '*';
296}
297
[email protected]87b9bf12014-04-03 15:25:00298// Normalizes a source filename (which is platform- and build-method-dependent)
299// by extracting the last component of the full file name.
300// Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" =>
301// "chrome_main.cc".
302std::string NormalizeFileName(const std::string& file_name) {
303 const size_t offset = file_name.find_last_of("\\/");
304 return offset != std::string::npos ? file_name.substr(offset + 1) : file_name;
305}
306
[email protected]ed0fd002012-04-25 23:10:34307void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
[email protected]f3b357692013-03-22 05:16:13308 int process_type,
[email protected]ed0fd002012-04-25 23:10:34309 ProfilerEventProto* performance_profile) {
310 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
311 profiler_data.tasks.begin();
312 it != profiler_data.tasks.end(); ++it) {
[email protected]ed0fd002012-04-25 23:10:34313 const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
314 ProfilerEventProto::TrackedObject* tracked_object =
315 performance_profile->add_tracked_object();
[email protected]db7fec832013-07-18 09:41:41316 tracked_object->set_birth_thread_name_hash(
[email protected]0018f722014-02-08 03:08:51317 MetricsLogBase::Hash(MapThreadName(it->birth.thread_name)));
[email protected]db7fec832013-07-18 09:41:41318 tracked_object->set_exec_thread_name_hash(
[email protected]0018f722014-02-08 03:08:51319 MetricsLogBase::Hash(MapThreadName(it->death_thread_name)));
[email protected]db7fec832013-07-18 09:41:41320 tracked_object->set_source_file_name_hash(
[email protected]87b9bf12014-04-03 15:25:00321 MetricsLogBase::Hash(NormalizeFileName(it->birth.location.file_name)));
[email protected]db7fec832013-07-18 09:41:41322 tracked_object->set_source_function_name_hash(
323 MetricsLogBase::Hash(it->birth.location.function_name));
[email protected]ed0fd002012-04-25 23:10:34324 tracked_object->set_source_line_number(it->birth.location.line_number);
325 tracked_object->set_exec_count(death_data.count);
326 tracked_object->set_exec_time_total(death_data.run_duration_sum);
327 tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
328 tracked_object->set_queue_time_total(death_data.queue_duration_sum);
329 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
330 tracked_object->set_process_type(AsProtobufProcessType(process_type));
331 tracked_object->set_process_id(profiler_data.process_id);
332 }
333}
334
[email protected]59a7ae4e2012-10-01 23:54:44335#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
[email protected]197c0772012-05-14 23:50:51336void ProductDataToProto(const GoogleUpdateSettings::ProductData& product_data,
337 ProductInfo* product_info) {
338 product_info->set_version(product_data.version);
339 product_info->set_last_update_success_timestamp(
340 product_data.last_success.ToTimeT());
341 product_info->set_last_error(product_data.last_error_code);
342 product_info->set_last_extra_error(product_data.last_extra_code);
343 if (ProductInfo::InstallResult_IsValid(product_data.last_result)) {
344 product_info->set_last_result(
345 static_cast<ProductInfo::InstallResult>(product_data.last_result));
346 }
347}
[email protected]59a7ae4e2012-10-01 23:54:44348#endif
[email protected]197c0772012-05-14 23:50:51349
[email protected]76869ff2013-01-15 16:13:47350#if defined(OS_WIN)
[email protected]ffaf7322013-05-15 00:26:11351struct ScreenDPIInformation {
[email protected]76869ff2013-01-15 16:13:47352 double max_dpi_x;
353 double max_dpi_y;
354};
355
356// Called once for each connected monitor.
357BOOL CALLBACK GetMonitorDPICallback(HMONITOR, HDC hdc, LPRECT, LPARAM dwData) {
358 const double kMillimetersPerInch = 25.4;
359 ScreenDPIInformation* screen_info =
360 reinterpret_cast<ScreenDPIInformation*>(dwData);
361 // Size of screen, in mm.
362 DWORD size_x = GetDeviceCaps(hdc, HORZSIZE);
363 DWORD size_y = GetDeviceCaps(hdc, VERTSIZE);
364 double dpi_x = (size_x > 0) ?
365 GetDeviceCaps(hdc, HORZRES) / (size_x / kMillimetersPerInch) : 0;
366 double dpi_y = (size_y > 0) ?
367 GetDeviceCaps(hdc, VERTRES) / (size_y / kMillimetersPerInch) : 0;
368 screen_info->max_dpi_x = std::max(dpi_x, screen_info->max_dpi_x);
369 screen_info->max_dpi_y = std::max(dpi_y, screen_info->max_dpi_y);
370 return TRUE;
371}
372
373void WriteScreenDPIInformationProto(SystemProfileProto::Hardware* hardware) {
374 HDC desktop_dc = GetDC(NULL);
375 if (desktop_dc) {
[email protected]ffaf7322013-05-15 00:26:11376 ScreenDPIInformation si = {0, 0};
[email protected]76869ff2013-01-15 16:13:47377 if (EnumDisplayMonitors(desktop_dc, NULL, GetMonitorDPICallback,
378 reinterpret_cast<LPARAM>(&si))) {
379 hardware->set_max_dpi_x(si.max_dpi_x);
380 hardware->set_max_dpi_y(si.max_dpi_y);
381 }
382 ReleaseDC(GetDesktopWindow(), desktop_dc);
383 }
384}
385
386#endif // defined(OS_WIN)
387
[email protected]86573d12013-07-11 19:48:32388// Round a timestamp measured in seconds since epoch to one with a granularity
389// of an hour. This can be used before uploaded potentially sensitive
390// timestamps.
391int64 RoundSecondsToHour(int64 time_in_seconds) {
392 return 3600 * (time_in_seconds / 3600);
393}
394
[email protected]1df44b72012-01-19 05:20:34395} // namespace
396
[email protected]197c0772012-05-14 23:50:51397GoogleUpdateMetrics::GoogleUpdateMetrics() : is_system_install(false) {}
398
399GoogleUpdateMetrics::~GoogleUpdateMetrics() {}
400
[email protected]67f92bc32012-01-26 01:56:19401static base::LazyInstance<std::string>::Leaky
[email protected]0f2f7792013-11-28 16:09:14402 g_version_extension = LAZY_INSTANCE_INITIALIZER;
[email protected]054c8012011-11-16 00:12:42403
[email protected]9eae4032014-04-09 19:15:19404MetricsLog::MetricsLog(const std::string& client_id,
405 int session_id,
406 LogType log_type)
407 : MetricsLogBase(client_id, session_id, log_type,
408 MetricsLog::GetVersionString()),
[email protected]b84ee6e2014-03-15 22:51:54409 creation_time_(base::TimeTicks::Now()),
410 extension_metrics_(uma_proto()->client_id()) {
[email protected]064107e2014-05-02 00:59:06411 uma_proto()->mutable_system_profile()->set_channel(
[email protected]1e6f9e22014-05-09 09:10:28412 AsProtobufChannel(chrome::VersionInfo::GetChannel()));
[email protected]064107e2014-05-02 00:59:06413
[email protected]afc03f02013-10-11 06:01:35414#if defined(OS_CHROMEOS)
[email protected]c9afed6e2014-02-03 22:12:29415 metrics_log_chromeos_.reset(new MetricsLogChromeOS(uma_proto()));
416#endif // OS_CHROMEOS
[email protected]afc03f02013-10-11 06:01:35417}
[email protected]5ed7d4572009-12-23 17:42:41418
[email protected]1226abb2010-06-10 18:01:28419MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:29420
421// static
[email protected]b1de2c72013-02-06 02:45:47422void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
423 registry->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29424}
425
[email protected]1df44b72012-01-19 05:20:34426// static
[email protected]1226abb2010-06-10 18:01:28427std::string MetricsLog::GetVersionString() {
[email protected]0211f57e2010-08-27 20:28:42428 chrome::VersionInfo version_info;
[email protected]30c91802010-12-18 00:40:17429 if (!version_info.is_valid()) {
430 NOTREACHED() << "Unable to retrieve version info.";
431 return std::string();
432 }
433
[email protected]0211f57e2010-08-27 20:28:42434 std::string version = version_info.Version();
[email protected]054c8012011-11-16 00:12:42435 if (!version_extension().empty())
436 version += version_extension();
[email protected]0211f57e2010-08-27 20:28:42437 if (!version_info.IsOfficialBuild())
438 version.append("-devel");
439 return version;
[email protected]1226abb2010-06-10 18:01:28440}
441
[email protected]054c8012011-11-16 00:12:42442// static
443void MetricsLog::set_version_extension(const std::string& extension) {
444 g_version_extension.Get() = extension;
445}
446
447// static
448const std::string& MetricsLog::version_extension() {
449 return g_version_extension.Get();
450}
451
[email protected]85791b0b2014-05-20 15:18:58452void MetricsLog::RecordStabilityMetrics(
453 const std::vector<metrics::MetricsProvider*>& metrics_providers,
454 base::TimeDelta incremental_uptime,
455 base::TimeDelta uptime) {
[email protected]767c9d92012-03-02 16:04:34456 DCHECK(!locked());
[email protected]0f2f7792013-11-28 16:09:14457 DCHECK(HasEnvironment());
458 DCHECK(!HasStabilityMetrics());
[email protected]0b33f80b2008-12-17 21:34:36459
[email protected]767c9d92012-03-02 16:04:34460 PrefService* pref = GetPrefService();
[email protected]0b33f80b2008-12-17 21:34:36461 DCHECK(pref);
462
initial.commit09911bf2008-07-26 23:55:29463 // Get stability attributes out of Local State, zeroing out stored values.
464 // NOTE: This could lead to some data loss if this report isn't successfully
465 // sent, but that's true for all the metrics.
466
[email protected]147bbc0b2009-01-06 19:37:40467 WriteRequiredStabilityAttributes(pref);
[email protected]250f7b662013-11-23 02:36:51468 WritePluginStabilityElements(pref);
[email protected]0edf8762013-11-21 18:33:30469
470 // Record recent delta for critical stability metrics. We can't wait for a
471 // restart to gather these, as that delay biases our observation away from
472 // users that run happily for a looooong time. We send increments with each
473 // uma log upload, just as we send histogram data.
[email protected]076961c2014-03-12 22:23:56474 WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
initial.commit09911bf2008-07-26 23:55:29475
[email protected]85791b0b2014-05-20 15:18:58476 SystemProfileProto::Stability* stability =
477 uma_proto()->mutable_system_profile()->mutable_stability();
478 for (size_t i = 0; i < metrics_providers.size(); ++i)
479 metrics_providers[i]->ProvideStabilityMetrics(stability);
480
[email protected]0edf8762013-11-21 18:33:30481 // Omit some stats unless this is the initial stability log.
[email protected]9eae4032014-04-09 19:15:19482 if (log_type() != INITIAL_STABILITY_LOG)
[email protected]0edf8762013-11-21 18:33:30483 return;
484
[email protected]fe58acc22012-02-29 01:29:58485 int incomplete_shutdown_count =
486 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
[email protected]e324f6b2012-02-28 05:43:37487 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]fe58acc22012-02-29 01:29:58488 int breakpad_registration_success_count =
489 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
[email protected]e324f6b2012-02-28 05:43:37490 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
[email protected]fe58acc22012-02-29 01:29:58491 int breakpad_registration_failure_count =
492 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
[email protected]e324f6b2012-02-28 05:43:37493 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
[email protected]fe58acc22012-02-29 01:29:58494 int debugger_present_count =
495 pref->GetInteger(prefs::kStabilityDebuggerPresent);
[email protected]e324f6b2012-02-28 05:43:37496 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
[email protected]fe58acc22012-02-29 01:29:58497 int debugger_not_present_count =
498 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
[email protected]e324f6b2012-02-28 05:43:37499 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
[email protected]b2a4812d2012-02-28 05:31:31500
[email protected]fe58acc22012-02-29 01:29:58501 // TODO(jar): The following are all optional, so we *could* optimize them for
502 // values of zero (and not include them).
[email protected]fe58acc22012-02-29 01:29:58503 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
504 stability->set_breakpad_registration_success_count(
505 breakpad_registration_success_count);
506 stability->set_breakpad_registration_failure_count(
507 breakpad_registration_failure_count);
508 stability->set_debugger_present_count(debugger_present_count);
509 stability->set_debugger_not_present_count(debugger_not_present_count);
[email protected]0edf8762013-11-21 18:33:30510}
[email protected]fe58acc22012-02-29 01:29:58511
[email protected]85791b0b2014-05-20 15:18:58512void MetricsLog::RecordGeneralMetrics(
513 const std::vector<metrics::MetricsProvider*>& metrics_providers) {
514 for (size_t i = 0; i < metrics_providers.size(); ++i)
515 metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
516}
517
[email protected]0edf8762013-11-21 18:33:30518PrefService* MetricsLog::GetPrefService() {
519 return g_browser_process->local_state();
520}
521
522gfx::Size MetricsLog::GetScreenSize() const {
523 return gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
524}
525
526float MetricsLog::GetScreenDeviceScaleFactor() const {
527 return gfx::Screen::GetNativeScreen()->
528 GetPrimaryDisplay().device_scale_factor();
529}
530
531int MetricsLog::GetScreenCount() const {
532 // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312
533 return gfx::Screen::GetNativeScreen()->GetNumDisplays();
534}
535
536void MetricsLog::GetFieldTrialIds(
537 std::vector<ActiveGroupId>* field_trial_ids) const {
[email protected]b3610d42014-05-19 18:07:23538 variations::GetFieldTrialActiveGroupIds(field_trial_ids);
[email protected]147bbc0b2009-01-06 19:37:40539}
540
[email protected]0f2f7792013-11-28 16:09:14541bool MetricsLog::HasEnvironment() const {
542 return uma_proto()->system_profile().has_uma_enabled_date();
543}
544
545bool MetricsLog::HasStabilityMetrics() const {
546 return uma_proto()->system_profile().stability().has_launch_count();
547}
548
[email protected]250f7b662013-11-23 02:36:51549void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
[email protected]147bbc0b2009-01-06 19:37:40550 // Now log plugin stability info.
[email protected]cb1078de2013-12-23 20:04:22551 const base::ListValue* plugin_stats_list = pref->GetList(
initial.commit09911bf2008-07-26 23:55:29552 prefs::kStabilityPluginStats);
[email protected]1df44b72012-01-19 05:20:34553 if (!plugin_stats_list)
554 return;
initial.commit09911bf2008-07-26 23:55:29555
[email protected]ebd71962012-12-20 02:56:55556#if defined(ENABLE_PLUGINS)
[email protected]fe58acc22012-02-29 01:29:58557 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34558 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]cb1078de2013-12-23 20:04:22559 for (base::ListValue::const_iterator iter = plugin_stats_list->begin();
[email protected]1df44b72012-01-19 05:20:34560 iter != plugin_stats_list->end(); ++iter) {
[email protected]cb1078de2013-12-23 20:04:22561 if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
[email protected]1df44b72012-01-19 05:20:34562 NOTREACHED();
563 continue;
initial.commit09911bf2008-07-26 23:55:29564 }
[email protected]cb1078de2013-12-23 20:04:22565 base::DictionaryValue* plugin_dict =
566 static_cast<base::DictionaryValue*>(*iter);
initial.commit09911bf2008-07-26 23:55:29567
[email protected]fe58acc22012-02-29 01:29:58568 // Note that this search is potentially a quadratic operation, but given the
569 // low number of plugins installed on a "reasonable" setup, this should be
570 // fine.
571 // TODO(isherman): Verify that this does not show up as a hotspot in
572 // profiler runs.
[email protected]250f7b662013-11-23 02:36:51573 const SystemProfileProto::Plugin* system_profile_plugin = NULL;
[email protected]f31a01a2013-07-13 02:51:11574 std::string plugin_name;
575 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
[email protected]250f7b662013-11-23 02:36:51576 const SystemProfileProto& system_profile = uma_proto()->system_profile();
577 for (int i = 0; i < system_profile.plugin_size(); ++i) {
578 if (system_profile.plugin(i).name() == plugin_name) {
579 system_profile_plugin = &system_profile.plugin(i);
[email protected]fe58acc22012-02-29 01:29:58580 break;
581 }
582 }
583
[email protected]250f7b662013-11-23 02:36:51584 if (!system_profile_plugin) {
[email protected]fe58acc22012-02-29 01:29:58585 NOTREACHED();
586 continue;
587 }
588
589 SystemProfileProto::Stability::PluginStability* plugin_stability =
590 stability->add_plugin_stability();
[email protected]250f7b662013-11-23 02:36:51591 *plugin_stability->mutable_plugin() = *system_profile_plugin;
[email protected]f31a01a2013-07-13 02:51:11592
593 int launches = 0;
594 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
595 if (launches > 0)
596 plugin_stability->set_launch_count(launches);
597
598 int instances = 0;
599 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
600 if (instances > 0)
601 plugin_stability->set_instance_count(instances);
602
603 int crashes = 0;
604 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
605 if (crashes > 0)
606 plugin_stability->set_crash_count(crashes);
607
608 int loading_errors = 0;
609 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
610 &loading_errors);
611 if (loading_errors > 0)
612 plugin_stability->set_loading_error_count(loading_errors);
initial.commit09911bf2008-07-26 23:55:29613 }
[email protected]ebd71962012-12-20 02:56:55614#endif // defined(ENABLE_PLUGINS)
[email protected]1df44b72012-01-19 05:20:34615
616 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29617}
618
[email protected]fe58acc22012-02-29 01:29:58619// The server refuses data that doesn't have certain values. crashcount and
620// launchcount are currently "required" in the "stability" group.
621// TODO(isherman): Stop writing these attributes specially once the migration to
622// protobufs is complete.
[email protected]147bbc0b2009-01-06 19:37:40623void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]fe58acc22012-02-29 01:29:58624 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
[email protected]0b33f80b2008-12-17 21:34:36625 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
[email protected]fe58acc22012-02-29 01:29:58626 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
[email protected]0b33f80b2008-12-17 21:34:36627 pref->SetInteger(prefs::kStabilityCrashCount, 0);
[email protected]fe58acc22012-02-29 01:29:58628
[email protected]fe58acc22012-02-29 01:29:58629 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34630 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]fe58acc22012-02-29 01:29:58631 stability->set_launch_count(launch_count);
632 stability->set_crash_count(crash_count);
[email protected]0b33f80b2008-12-17 21:34:36633}
634
[email protected]c68a2b9b2013-10-09 18:16:36635void MetricsLog::WriteRealtimeStabilityAttributes(
636 PrefService* pref,
[email protected]076961c2014-03-12 22:23:56637 base::TimeDelta incremental_uptime,
638 base::TimeDelta uptime) {
[email protected]0b33f80b2008-12-17 21:34:36639 // Update the stats which are critical for real-time stability monitoring.
640 // Since these are "optional," only list ones that are non-zero, as the counts
[email protected]250f7b662013-11-23 02:36:51641 // are aggregated (summed) server side.
[email protected]0b33f80b2008-12-17 21:34:36642
[email protected]fe58acc22012-02-29 01:29:58643 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34644 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]0b33f80b2008-12-17 21:34:36645 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
646 if (count) {
[email protected]fe58acc22012-02-29 01:29:58647 stability->set_page_load_count(count);
[email protected]0b33f80b2008-12-17 21:34:36648 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
649 }
650
651 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
652 if (count) {
[email protected]fe58acc22012-02-29 01:29:58653 stability->set_renderer_crash_count(count);
[email protected]0b33f80b2008-12-17 21:34:36654 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
655 }
656
[email protected]1f085622009-12-04 05:33:45657 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
658 if (count) {
[email protected]fe58acc22012-02-29 01:29:58659 stability->set_extension_renderer_crash_count(count);
[email protected]1f085622009-12-04 05:33:45660 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
661 }
662
[email protected]0b33f80b2008-12-17 21:34:36663 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
664 if (count) {
[email protected]fe58acc22012-02-29 01:29:58665 stability->set_renderer_hang_count(count);
[email protected]0b33f80b2008-12-17 21:34:36666 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
667 }
[email protected]1f085622009-12-04 05:33:45668
669 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
670 if (count) {
[email protected]fe58acc22012-02-29 01:29:58671 stability->set_child_process_crash_count(count);
[email protected]1f085622009-12-04 05:33:45672 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
673 }
[email protected]9165f742010-03-10 22:55:01674
[email protected]c1834a92011-01-21 18:21:03675#if defined(OS_CHROMEOS)
[email protected]c9afed6e2014-02-03 22:12:29676 metrics_log_chromeos_->WriteRealtimeStabilityAttributes(pref);
[email protected]c1834a92011-01-21 18:21:03677#endif // OS_CHROMEOS
678
[email protected]076961c2014-03-12 22:23:56679 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
680 if (incremental_uptime_sec)
681 stability->set_incremental_uptime_sec(incremental_uptime_sec);
682 const uint64 uptime_sec = uptime.InSeconds();
[email protected]c68a2b9b2013-10-09 18:16:36683 if (uptime_sec)
684 stability->set_uptime_sec(uptime_sec);
[email protected]0b33f80b2008-12-17 21:34:36685}
686
initial.commit09911bf2008-07-26 23:55:29687void MetricsLog::WritePluginList(
[email protected]d7bd3e52013-07-21 04:29:20688 const std::vector<content::WebPluginInfo>& plugin_list) {
[email protected]767c9d92012-03-02 16:04:34689 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29690
[email protected]ebd71962012-12-20 02:56:55691#if defined(ENABLE_PLUGINS)
692 PluginPrefs* plugin_prefs = GetPluginPrefs();
[email protected]767c9d92012-03-02 16:04:34693 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
[email protected]d7bd3e52013-07-21 04:29:20694 for (std::vector<content::WebPluginInfo>::const_iterator iter =
[email protected]191eb3f72010-12-21 06:27:50695 plugin_list.begin();
initial.commit09911bf2008-07-26 23:55:29696 iter != plugin_list.end(); ++iter) {
[email protected]0eecc2542013-06-23 04:22:02697 SystemProfileProto::Plugin* plugin = system_profile->add_plugin();
698 SetPluginInfo(*iter, plugin_prefs, plugin);
initial.commit09911bf2008-07-26 23:55:29699 }
[email protected]ebd71962012-12-20 02:56:55700#endif // defined(ENABLE_PLUGINS)
initial.commit09911bf2008-07-26 23:55:29701}
702
703void MetricsLog::RecordEnvironment(
[email protected]85791b0b2014-05-20 15:18:58704 const std::vector<metrics::MetricsProvider*>& metrics_providers,
[email protected]c68a2b9b2013-10-09 18:16:36705 const std::vector<content::WebPluginInfo>& plugin_list,
706 const GoogleUpdateMetrics& google_update_metrics,
[email protected]b3610d42014-05-19 18:07:23707 const std::vector<variations::ActiveGroupId>& synthetic_trials) {
[email protected]0f2f7792013-11-28 16:09:14708 DCHECK(!HasEnvironment());
709
[email protected]bc66d532012-03-23 01:57:05710 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
[email protected]24b9bb392013-01-29 20:29:29711
712 std::string brand_code;
713 if (google_util::GetBrand(&brand_code))
714 system_profile->set_brand_code(brand_code);
715
[email protected]cc5d7f42012-10-30 00:30:09716 int enabled_date;
717 bool success = base::StringToInt(GetMetricsEnabledDate(GetPrefService()),
718 &enabled_date);
[email protected]bc66d532012-03-23 01:57:05719 DCHECK(success);
[email protected]86573d12013-07-11 19:48:32720
721 // Reduce granularity of the enabled_date field to nearest hour.
722 system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
723
724 int64 install_date = GetPrefService()->GetInt64(prefs::kInstallDate);
725
726 // Reduce granularity of the install_date field to nearest hour.
727 system_profile->set_install_date(RoundSecondsToHour(install_date));
[email protected]bc66d532012-03-23 01:57:05728
729 system_profile->set_application_locale(
[email protected]eabbfb12013-04-05 23:28:35730 g_browser_process->GetApplicationLocale());
[email protected]bc66d532012-03-23 01:57:05731
732 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
[email protected]0b6a4fb2012-10-16 01:58:21733 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
[email protected]bc66d532012-03-23 01:57:05734 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
735#if defined(OS_WIN)
736 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
737#endif
738
[email protected]74b299e2013-01-29 01:24:42739 SystemProfileProto::Network* network = system_profile->mutable_network();
740 network->set_connection_type_is_ambiguous(
[email protected]5eae204c2013-02-13 15:53:42741 network_observer_.connection_type_is_ambiguous());
742 network->set_connection_type(network_observer_.connection_type());
743 network->set_wifi_phy_layer_protocol_is_ambiguous(
744 network_observer_.wifi_phy_layer_protocol_is_ambiguous());
745 network->set_wifi_phy_layer_protocol(
746 network_observer_.wifi_phy_layer_protocol());
747 network_observer_.Reset();
[email protected]74b299e2013-01-29 01:24:42748
[email protected]bc66d532012-03-23 01:57:05749 SystemProfileProto::OS* os = system_profile->mutable_os();
[email protected]1bb25e02012-08-03 22:39:39750 std::string os_name = base::SysInfo::OperatingSystemName();
751#if defined(OS_WIN)
752 // TODO(mad): This only checks whether the main process is a Metro process at
753 // upload time; not whether the collected metrics were all gathered from
754 // Metro. This is ok as an approximation for now, since users will rarely be
755 // switching from Metro to Desktop mode; but we should re-evaluate whether we
756 // can distinguish metrics more cleanly in the future: http://crbug.com/140568
757 if (base::win::IsMetroProcess())
758 os_name += " (Metro)";
759#endif
760 os->set_name(os_name);
[email protected]bc66d532012-03-23 01:57:05761 os->set_version(base::SysInfo::OperatingSystemVersion());
[email protected]5106b3a2012-10-03 20:10:44762#if defined(OS_ANDROID)
763 os->set_fingerprint(
764 base::android::BuildInfo::GetInstance()->android_build_fp());
765#endif
[email protected]bc66d532012-03-23 01:57:05766
[email protected]5c8f89f692013-07-18 11:13:28767 base::CPU cpu_info;
768 SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
769 cpu->set_vendor_name(cpu_info.vendor_name());
770 cpu->set_signature(cpu_info.signature());
771
[email protected]d7b5cc72013-05-23 20:05:00772 const gpu::GPUInfo& gpu_info =
[email protected]bc66d532012-03-23 01:57:05773 GpuDataManager::GetInstance()->GetGPUInfo();
774 SystemProfileProto::Hardware::Graphics* gpu = hardware->mutable_gpu();
[email protected]a094e2c2012-05-10 23:02:42775 gpu->set_vendor_id(gpu_info.gpu.vendor_id);
776 gpu->set_device_id(gpu_info.gpu.device_id);
[email protected]bc66d532012-03-23 01:57:05777 gpu->set_driver_version(gpu_info.driver_version);
778 gpu->set_driver_date(gpu_info.driver_date);
779 SystemProfileProto::Hardware::Graphics::PerformanceStatistics*
780 gpu_performance = gpu->mutable_performance_statistics();
781 gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics);
782 gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming);
783 gpu_performance->set_overall_score(gpu_info.performance_stats.overall);
[email protected]d6bdfae2013-01-18 20:23:01784 gpu->set_gl_vendor(gpu_info.gl_vendor);
785 gpu->set_gl_renderer(gpu_info.gl_renderer);
[email protected]bc66d532012-03-23 01:57:05786
787 const gfx::Size display_size = GetScreenSize();
788 hardware->set_primary_screen_width(display_size.width());
789 hardware->set_primary_screen_height(display_size.height());
[email protected]aa96417972012-08-22 03:16:44790 hardware->set_primary_screen_scale_factor(GetScreenDeviceScaleFactor());
[email protected]bc66d532012-03-23 01:57:05791 hardware->set_screen_count(GetScreenCount());
792
[email protected]76869ff2013-01-15 16:13:47793#if defined(OS_WIN)
794 WriteScreenDPIInformationProto(hardware);
795#endif
796
[email protected]197c0772012-05-14 23:50:51797 WriteGoogleUpdateProto(google_update_metrics);
798
[email protected]0eecc2542013-06-23 04:22:02799 WritePluginList(plugin_list);
[email protected]b84ee6e2014-03-15 22:51:54800 extension_metrics_.WriteExtensionList(uma_proto()->mutable_system_profile());
[email protected]bc66d532012-03-23 01:57:05801
[email protected]0c8b7ad2012-11-06 07:08:14802 std::vector<ActiveGroupId> field_trial_ids;
[email protected]767c9d92012-03-02 16:04:34803 GetFieldTrialIds(&field_trial_ids);
804 WriteFieldTrials(field_trial_ids, system_profile);
[email protected]60677562013-11-17 15:52:55805 WriteFieldTrials(synthetic_trials, system_profile);
[email protected]f65859e2013-02-04 20:00:25806
807#if defined(OS_CHROMEOS)
[email protected]c9afed6e2014-02-03 22:12:29808 metrics_log_chromeos_->LogChromeOSMetrics();
809#endif // OS_CHROMEOS
[email protected]0f2f7792013-11-28 16:09:14810
[email protected]85791b0b2014-05-20 15:18:58811 for (size_t i = 0; i < metrics_providers.size(); ++i)
812 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
813
[email protected]0f2f7792013-11-28 16:09:14814 std::string serialied_system_profile;
815 std::string base64_system_profile;
[email protected]33fca122013-12-11 01:48:50816 if (system_profile->SerializeToString(&serialied_system_profile)) {
817 base::Base64Encode(serialied_system_profile, &base64_system_profile);
[email protected]0f2f7792013-11-28 16:09:14818 PrefService* local_state = GetPrefService();
819 local_state->SetString(prefs::kStabilitySavedSystemProfile,
820 base64_system_profile);
821 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
822 ComputeSHA1(serialied_system_profile));
823 }
824}
825
826bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
827 PrefService* local_state = GetPrefService();
828 const std::string base64_system_profile =
829 local_state->GetString(prefs::kStabilitySavedSystemProfile);
830 if (base64_system_profile.empty())
831 return false;
832
833 const std::string system_profile_hash =
834 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
835 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
836 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
837
838 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
839 std::string serialied_system_profile;
840 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
841 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
842 system_profile->ParseFromString(serialied_system_profile);
initial.commit09911bf2008-07-26 23:55:29843}
844
[email protected]ed0fd002012-04-25 23:10:34845void MetricsLog::RecordProfilerData(
846 const tracked_objects::ProcessDataSnapshot& process_data,
[email protected]f3b357692013-03-22 05:16:13847 int process_type) {
[email protected]ed0fd002012-04-25 23:10:34848 DCHECK(!locked());
849
[email protected]8f829682012-04-27 00:36:49850 if (tracked_objects::GetTimeSourceType() !=
851 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
[email protected]ed0fd002012-04-25 23:10:34852 // We currently only support the default time source, wall clock time.
853 return;
854 }
855
856 ProfilerEventProto* profile;
857 if (!uma_proto()->profiler_event_size()) {
858 // For the first process's data, add a new field to the protocol buffer.
859 profile = uma_proto()->add_profiler_event();
860 profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
861 profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
862 } else {
863 // For the remaining calls, re-use the existing field.
864 profile = uma_proto()->mutable_profiler_event(0);
865 }
866
867 WriteProfilerData(process_data, process_type, profile);
868}
869
[email protected]0fafc8d2013-06-01 00:09:50870void MetricsLog::RecordOmniboxOpenedURL(const OmniboxLog& log) {
[email protected]767c9d92012-03-02 16:04:34871 DCHECK(!locked());
initial.commit09911bf2008-07-26 23:55:29872
[email protected]8790210c2013-12-02 05:29:53873 std::vector<base::string16> terms;
[email protected]65956312012-04-19 21:28:12874 const int num_terms =
[email protected]8790210c2013-12-02 05:29:53875 static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));
initial.commit09911bf2008-07-26 23:55:29876
[email protected]767c9d92012-03-02 16:04:34877 OmniboxEventProto* omnibox_event = uma_proto()->add_omnibox_event();
[email protected]fe58acc22012-02-29 01:29:58878 omnibox_event->set_time(MetricsLogBase::GetCurrentTime());
879 if (log.tab_id != -1) {
880 // If we know what tab the autocomplete URL was opened in, log it.
881 omnibox_event->set_tab_id(log.tab_id);
882 }
883 omnibox_event->set_typed_length(log.text.length());
[email protected]660fe8f32012-05-03 20:01:08884 omnibox_event->set_just_deleted_text(log.just_deleted_text);
[email protected]65956312012-04-19 21:28:12885 omnibox_event->set_num_typed_terms(num_terms);
[email protected]fe58acc22012-02-29 01:29:58886 omnibox_event->set_selected_index(log.selected_index);
[email protected]439f1e32013-12-09 20:09:09887 if (log.completed_length != base::string16::npos)
[email protected]62b3ef52013-01-18 00:13:02888 omnibox_event->set_completed_length(log.completed_length);
[email protected]a0151412014-05-01 18:55:02889 const base::TimeDelta default_time_delta =
890 base::TimeDelta::FromMilliseconds(-1);
[email protected]65956312012-04-19 21:28:12891 if (log.elapsed_time_since_user_first_modified_omnibox !=
[email protected]a0151412014-05-01 18:55:02892 default_time_delta) {
[email protected]65956312012-04-19 21:28:12893 // Only upload the typing duration if it is set/valid.
894 omnibox_event->set_typing_duration_ms(
895 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
896 }
[email protected]a0151412014-05-01 18:55:02897 if (log.elapsed_time_since_last_change_to_default_match !=
898 default_time_delta) {
899 omnibox_event->set_duration_since_last_default_match_update_ms(
900 log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
901 }
[email protected]ee902532012-05-01 23:54:49902 omnibox_event->set_current_page_classification(
[email protected]3c919d6a2013-07-28 18:35:52903 AsOmniboxEventPageClassification(log.current_page_classification));
[email protected]fe58acc22012-02-29 01:29:58904 omnibox_event->set_input_type(AsOmniboxEventInputType(log.input_type));
[email protected]a0151412014-05-01 18:55:02905 // We consider a paste-and-search/paste-and-go action to have a closed popup
906 // (as explained in omnibox_event.proto) even if it was not, because such
907 // actions ignore the contents of the popup so it doesn't matter that it was
908 // open.
909 const bool consider_popup_open = log.is_popup_open && !log.is_paste_and_go;
910 omnibox_event->set_is_popup_open(consider_popup_open);
911 omnibox_event->set_is_paste_and_go(log.is_paste_and_go);
912 if (consider_popup_open) {
913 omnibox_event->set_is_top_result_hidden_in_dropdown(
914 log.result.ShouldHideTopMatch());
915 }
[email protected]1fce7dd42013-08-06 02:29:17916
[email protected]fe58acc22012-02-29 01:29:58917 for (AutocompleteResult::const_iterator i(log.result.begin());
918 i != log.result.end(); ++i) {
919 OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
[email protected]0e9e8782012-05-15 23:01:51920 suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
[email protected]fe58acc22012-02-29 01:29:58921 suggestion->set_result_type(AsOmniboxEventResultType(i->type));
922 suggestion->set_relevance(i->relevance);
[email protected]cf6256f2012-06-12 23:36:01923 if (i->typed_count != -1)
924 suggestion->set_typed_count(i->typed_count);
[email protected]fe58acc22012-02-29 01:29:58925 suggestion->set_is_starred(i->starred);
926 }
[email protected]0e9e8782012-05-15 23:01:51927 for (ProvidersInfo::const_iterator i(log.providers_info.begin());
928 i != log.providers_info.end(); ++i) {
929 OmniboxEventProto::ProviderInfo* provider_info =
930 omnibox_event->add_provider_info();
931 provider_info->CopyFrom(*i);
932 }
[email protected]fe58acc22012-02-29 01:29:58933
initial.commit09911bf2008-07-26 23:55:29934 ++num_events_;
935}
[email protected]197c0772012-05-14 23:50:51936
937void MetricsLog::WriteGoogleUpdateProto(
938 const GoogleUpdateMetrics& google_update_metrics) {
939#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
940 SystemProfileProto::GoogleUpdate* google_update =
941 uma_proto()->mutable_system_profile()->mutable_google_update();
942
943 google_update->set_is_system_install(google_update_metrics.is_system_install);
944
945 if (!google_update_metrics.last_started_au.is_null()) {
946 google_update->set_last_automatic_start_timestamp(
947 google_update_metrics.last_started_au.ToTimeT());
948 }
949
950 if (!google_update_metrics.last_checked.is_null()) {
951 google_update->set_last_update_check_timestamp(
952 google_update_metrics.last_checked.ToTimeT());
953 }
954
955 if (!google_update_metrics.google_update_data.version.empty()) {
956 ProductDataToProto(google_update_metrics.google_update_data,
957 google_update->mutable_google_update_status());
958 }
959
960 if (!google_update_metrics.product_data.version.empty()) {
961 ProductDataToProto(google_update_metrics.product_data,
962 google_update->mutable_client_status());
963 }
964#endif // defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
965}