blob: 314200d8f07231accc9e84d733c92070b15c20b4 [file] [log] [blame]
[email protected]91b1d912014-06-05 10:52:081// Copyright 2014 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]91b1d912014-06-05 10:52:085#include "components/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]5c8f89f692013-07-18 11:13:2813#include "base/cpu.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/scoped_ptr.h"
[email protected]bfb77b52014-06-07 01:54:0115#include "base/metrics/histogram.h"
16#include "base/metrics/histogram_samples.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]0f2f7792013-11-28 16:09:1419#include "base/sha1.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]f9b294362013-06-10 20:22:3121#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1822#include "base/strings/utf_string_conversions.h"
[email protected]fadf97f2008-09-18 12:18:1423#include "base/sys_info.h"
[email protected]84813472013-06-28 00:25:1924#include "base/time/time.h"
[email protected]bfb77b52014-06-07 01:54:0125#include "components/metrics/metrics_hashes.h"
[email protected]91b1d912014-06-05 10:52:0826#include "components/metrics/metrics_pref_names.h"
[email protected]85791b0b2014-05-20 15:18:5827#include "components/metrics/metrics_provider.h"
[email protected]09dee82d2014-05-22 14:00:5328#include "components/metrics/metrics_service_client.h"
[email protected]bfb77b52014-06-07 01:54:0129#include "components/metrics/proto/histogram_event.pb.h"
[email protected]064107e2014-05-02 00:59:0630#include "components/metrics/proto/system_profile.pb.h"
[email protected]bfb77b52014-06-07 01:54:0131#include "components/metrics/proto/user_action_event.pb.h"
[email protected]b3610d42014-05-19 18:07:2332#include "components/variations/active_field_trials.h"
initial.commit09911bf2008-07-26 23:55:2933
[email protected]5106b3a2012-10-03 20:10:4434#if defined(OS_ANDROID)
35#include "base/android/build_info.h"
36#endif
37
[email protected]d1be67b2008-11-19 20:28:3838#if defined(OS_WIN)
[email protected]1bb25e02012-08-03 22:39:3939#include "base/win/metro.h"
40
41// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
[email protected]d1be67b2008-11-19 20:28:3842extern "C" IMAGE_DOS_HEADER __ImageBase;
43#endif
44
[email protected]bfb77b52014-06-07 01:54:0145using base::SampleCountIterator;
[email protected]b3610d42014-05-19 18:07:2346typedef variations::ActiveGroupId ActiveGroupId;
[email protected]79078df2012-02-16 01:22:3247
asvitkinecbd420732014-08-26 22:15:4048namespace metrics {
49
[email protected]1df44b72012-01-19 05:20:3450namespace {
51
[email protected]bfb77b52014-06-07 01:54:0152// Any id less than 16 bytes is considered to be a testing id.
53bool IsTestingID(const std::string& id) {
54 return id.size() < 16;
55}
56
[email protected]1df44b72012-01-19 05:20:3457// Returns the date at which the current metrics client ID was created as
[email protected]ca5ac4b2012-12-14 07:46:4058// a string containing seconds since the epoch, or "0" if none was found.
[email protected]cc5d7f42012-10-30 00:30:0959std::string GetMetricsEnabledDate(PrefService* pref) {
[email protected]767c9d92012-03-02 16:04:3460 if (!pref) {
[email protected]1df44b72012-01-19 05:20:3461 NOTREACHED();
62 return "0";
63 }
[email protected]767c9d92012-03-02 16:04:3464
[email protected]16a30912014-06-04 00:20:0465 return pref->GetString(metrics::prefs::kMetricsReportingEnabledTimestamp);
[email protected]1df44b72012-01-19 05:20:3466}
67
[email protected]0f2f7792013-11-28 16:09:1468// Computes a SHA-1 hash of |data| and returns it as a hex string.
69std::string ComputeSHA1(const std::string& data) {
70 const std::string sha1 = base::SHA1HashString(data);
71 return base::HexEncode(sha1.data(), sha1.size());
72}
73
[email protected]0c8b7ad2012-11-06 07:08:1474void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
[email protected]767c9d92012-03-02 16:04:3475 SystemProfileProto* system_profile) {
[email protected]0c8b7ad2012-11-06 07:08:1476 for (std::vector<ActiveGroupId>::const_iterator it =
[email protected]ad2461c2012-04-27 21:11:0377 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
[email protected]767c9d92012-03-02 16:04:3478 SystemProfileProto::FieldTrial* field_trial =
79 system_profile->add_field_trial();
80 field_trial->set_name_id(it->name);
81 field_trial->set_group_id(it->group);
82 }
83}
84
[email protected]86573d12013-07-11 19:48:3285// Round a timestamp measured in seconds since epoch to one with a granularity
86// of an hour. This can be used before uploaded potentially sensitive
87// timestamps.
88int64 RoundSecondsToHour(int64 time_in_seconds) {
89 return 3600 * (time_in_seconds / 3600);
90}
91
[email protected]1df44b72012-01-19 05:20:3492} // namespace
93
[email protected]9eae4032014-04-09 19:15:1994MetricsLog::MetricsLog(const std::string& client_id,
95 int session_id,
[email protected]09dee82d2014-05-22 14:00:5396 LogType log_type,
[email protected]24f81ca2014-05-26 15:59:3497 metrics::MetricsServiceClient* client,
98 PrefService* local_state)
[email protected]bfb77b52014-06-07 01:54:0199 : closed_(false),
100 log_type_(log_type),
[email protected]09dee82d2014-05-22 14:00:53101 client_(client),
[email protected]24f81ca2014-05-26 15:59:34102 creation_time_(base::TimeTicks::Now()),
103 local_state_(local_state) {
[email protected]bfb77b52014-06-07 01:54:01104 if (IsTestingID(client_id))
105 uma_proto_.set_client_id(0);
106 else
107 uma_proto_.set_client_id(Hash(client_id));
108
109 uma_proto_.set_session_id(session_id);
110
111 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile();
112 system_profile->set_build_timestamp(GetBuildTime());
113 system_profile->set_app_version(client_->GetVersionString());
114 system_profile->set_channel(client_->GetChannel());
[email protected]afc03f02013-10-11 06:01:35115}
[email protected]5ed7d4572009-12-23 17:42:41116
[email protected]bfb77b52014-06-07 01:54:01117MetricsLog::~MetricsLog() {
118}
initial.commit09911bf2008-07-26 23:55:29119
[email protected]91b1d912014-06-05 10:52:08120// static
121void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
122 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0);
123 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0);
124 registry->RegisterIntegerPref(
125 metrics::prefs::kStabilityIncompleteSessionEndCount, 0);
126 registry->RegisterIntegerPref(
127 metrics::prefs::kStabilityBreakpadRegistrationFail, 0);
128 registry->RegisterIntegerPref(
129 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0);
130 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0);
131 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent,
132 0);
133 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile,
134 std::string());
135 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash,
136 std::string());
137}
138
[email protected]bfb77b52014-06-07 01:54:01139// static
140uint64 MetricsLog::Hash(const std::string& value) {
141 uint64 hash = metrics::HashMetricName(value);
142
143 // The following log is VERY helpful when folks add some named histogram into
144 // the code, but forgot to update the descriptive list of histograms. When
145 // that happens, all we get to see (server side) is a hash of the histogram
146 // name. We can then use this logging to find out what histogram name was
147 // being hashed to a given MD5 value by just running the version of Chromium
148 // in question with --enable-logging.
149 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]";
150
151 return hash;
152}
153
154// static
155int64 MetricsLog::GetBuildTime() {
156 static int64 integral_build_time = 0;
157 if (!integral_build_time) {
158 base::Time time;
159 static const char kDateTime[] = __DATE__ " " __TIME__ " GMT";
160 bool result = base::Time::FromString(kDateTime, &time);
161 DCHECK(result);
162 integral_build_time = static_cast<int64>(time.ToTimeT());
163 }
164 return integral_build_time;
165}
166
167// static
168int64 MetricsLog::GetCurrentTime() {
169 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds();
170}
171
172void MetricsLog::RecordUserAction(const std::string& key) {
173 DCHECK(!closed_);
174
175 UserActionEventProto* user_action = uma_proto_.add_user_action_event();
176 user_action->set_name_hash(Hash(key));
177 user_action->set_time(GetCurrentTime());
178}
179
180void MetricsLog::RecordHistogramDelta(const std::string& histogram_name,
181 const base::HistogramSamples& snapshot) {
182 DCHECK(!closed_);
183 DCHECK_NE(0, snapshot.TotalCount());
184
185 // We will ignore the MAX_INT/infinite value in the last element of range[].
186
187 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event();
188 histogram_proto->set_name_hash(Hash(histogram_name));
189 histogram_proto->set_sum(snapshot.sum());
190
191 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done();
192 it->Next()) {
193 base::Histogram::Sample min;
194 base::Histogram::Sample max;
195 base::Histogram::Count count;
196 it->Get(&min, &max, &count);
197 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket();
198 bucket->set_min(min);
199 bucket->set_max(max);
200 bucket->set_count(count);
201 }
202
203 // Omit fields to save space (see rules in histogram_event.proto comments).
204 for (int i = 0; i < histogram_proto->bucket_size(); ++i) {
205 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i);
206 if (i + 1 < histogram_proto->bucket_size() &&
207 bucket->max() == histogram_proto->bucket(i + 1).min()) {
208 bucket->clear_max();
209 } else if (bucket->max() == bucket->min() + 1) {
210 bucket->clear_min();
211 }
212 }
213}
214
[email protected]85791b0b2014-05-20 15:18:58215void MetricsLog::RecordStabilityMetrics(
216 const std::vector<metrics::MetricsProvider*>& metrics_providers,
217 base::TimeDelta incremental_uptime,
218 base::TimeDelta uptime) {
[email protected]bfb77b52014-06-07 01:54:01219 DCHECK(!closed_);
[email protected]0f2f7792013-11-28 16:09:14220 DCHECK(HasEnvironment());
221 DCHECK(!HasStabilityMetrics());
[email protected]0b33f80b2008-12-17 21:34:36222
[email protected]24f81ca2014-05-26 15:59:34223 PrefService* pref = local_state_;
[email protected]0b33f80b2008-12-17 21:34:36224 DCHECK(pref);
225
initial.commit09911bf2008-07-26 23:55:29226 // Get stability attributes out of Local State, zeroing out stored values.
227 // NOTE: This could lead to some data loss if this report isn't successfully
228 // sent, but that's true for all the metrics.
229
[email protected]147bbc0b2009-01-06 19:37:40230 WriteRequiredStabilityAttributes(pref);
[email protected]0edf8762013-11-21 18:33:30231
232 // Record recent delta for critical stability metrics. We can't wait for a
233 // restart to gather these, as that delay biases our observation away from
234 // users that run happily for a looooong time. We send increments with each
235 // uma log upload, just as we send histogram data.
[email protected]076961c2014-03-12 22:23:56236 WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
initial.commit09911bf2008-07-26 23:55:29237
[email protected]48ff2c7f2014-05-23 09:57:45238 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
[email protected]85791b0b2014-05-20 15:18:58239 for (size_t i = 0; i < metrics_providers.size(); ++i)
[email protected]48ff2c7f2014-05-23 09:57:45240 metrics_providers[i]->ProvideStabilityMetrics(system_profile);
[email protected]85791b0b2014-05-20 15:18:58241
[email protected]0edf8762013-11-21 18:33:30242 // Omit some stats unless this is the initial stability log.
[email protected]9eae4032014-04-09 19:15:19243 if (log_type() != INITIAL_STABILITY_LOG)
[email protected]0edf8762013-11-21 18:33:30244 return;
245
[email protected]fe58acc22012-02-29 01:29:58246 int incomplete_shutdown_count =
[email protected]91b1d912014-06-05 10:52:08247 pref->GetInteger(metrics::prefs::kStabilityIncompleteSessionEndCount);
248 pref->SetInteger(metrics::prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]fe58acc22012-02-29 01:29:58249 int breakpad_registration_success_count =
[email protected]91b1d912014-06-05 10:52:08250 pref->GetInteger(metrics::prefs::kStabilityBreakpadRegistrationSuccess);
251 pref->SetInteger(metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0);
[email protected]fe58acc22012-02-29 01:29:58252 int breakpad_registration_failure_count =
[email protected]91b1d912014-06-05 10:52:08253 pref->GetInteger(metrics::prefs::kStabilityBreakpadRegistrationFail);
254 pref->SetInteger(metrics::prefs::kStabilityBreakpadRegistrationFail, 0);
[email protected]fe58acc22012-02-29 01:29:58255 int debugger_present_count =
[email protected]91b1d912014-06-05 10:52:08256 pref->GetInteger(metrics::prefs::kStabilityDebuggerPresent);
257 pref->SetInteger(metrics::prefs::kStabilityDebuggerPresent, 0);
[email protected]fe58acc22012-02-29 01:29:58258 int debugger_not_present_count =
[email protected]91b1d912014-06-05 10:52:08259 pref->GetInteger(metrics::prefs::kStabilityDebuggerNotPresent);
260 pref->SetInteger(metrics::prefs::kStabilityDebuggerNotPresent, 0);
[email protected]b2a4812d2012-02-28 05:31:31261
[email protected]fe58acc22012-02-29 01:29:58262 // TODO(jar): The following are all optional, so we *could* optimize them for
263 // values of zero (and not include them).
[email protected]48ff2c7f2014-05-23 09:57:45264 SystemProfileProto::Stability* stability =
265 system_profile->mutable_stability();
[email protected]fe58acc22012-02-29 01:29:58266 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
267 stability->set_breakpad_registration_success_count(
268 breakpad_registration_success_count);
269 stability->set_breakpad_registration_failure_count(
270 breakpad_registration_failure_count);
271 stability->set_debugger_present_count(debugger_present_count);
272 stability->set_debugger_not_present_count(debugger_not_present_count);
[email protected]0edf8762013-11-21 18:33:30273}
[email protected]fe58acc22012-02-29 01:29:58274
[email protected]85791b0b2014-05-20 15:18:58275void MetricsLog::RecordGeneralMetrics(
276 const std::vector<metrics::MetricsProvider*>& metrics_providers) {
277 for (size_t i = 0; i < metrics_providers.size(); ++i)
278 metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
279}
280
[email protected]0edf8762013-11-21 18:33:30281void MetricsLog::GetFieldTrialIds(
282 std::vector<ActiveGroupId>* field_trial_ids) const {
[email protected]b3610d42014-05-19 18:07:23283 variations::GetFieldTrialActiveGroupIds(field_trial_ids);
[email protected]147bbc0b2009-01-06 19:37:40284}
285
[email protected]0f2f7792013-11-28 16:09:14286bool MetricsLog::HasEnvironment() const {
287 return uma_proto()->system_profile().has_uma_enabled_date();
288}
289
290bool MetricsLog::HasStabilityMetrics() const {
291 return uma_proto()->system_profile().stability().has_launch_count();
292}
293
[email protected]fe58acc22012-02-29 01:29:58294// The server refuses data that doesn't have certain values. crashcount and
295// launchcount are currently "required" in the "stability" group.
296// TODO(isherman): Stop writing these attributes specially once the migration to
297// protobufs is complete.
[email protected]147bbc0b2009-01-06 19:37:40298void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]91b1d912014-06-05 10:52:08299 int launch_count = pref->GetInteger(metrics::prefs::kStabilityLaunchCount);
300 pref->SetInteger(metrics::prefs::kStabilityLaunchCount, 0);
301 int crash_count = pref->GetInteger(metrics::prefs::kStabilityCrashCount);
302 pref->SetInteger(metrics::prefs::kStabilityCrashCount, 0);
[email protected]fe58acc22012-02-29 01:29:58303
[email protected]fe58acc22012-02-29 01:29:58304 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34305 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]fe58acc22012-02-29 01:29:58306 stability->set_launch_count(launch_count);
307 stability->set_crash_count(crash_count);
[email protected]0b33f80b2008-12-17 21:34:36308}
309
[email protected]c68a2b9b2013-10-09 18:16:36310void MetricsLog::WriteRealtimeStabilityAttributes(
311 PrefService* pref,
[email protected]076961c2014-03-12 22:23:56312 base::TimeDelta incremental_uptime,
313 base::TimeDelta uptime) {
[email protected]0b33f80b2008-12-17 21:34:36314 // Update the stats which are critical for real-time stability monitoring.
315 // Since these are "optional," only list ones that are non-zero, as the counts
[email protected]250f7b662013-11-23 02:36:51316 // are aggregated (summed) server side.
[email protected]0b33f80b2008-12-17 21:34:36317
[email protected]fe58acc22012-02-29 01:29:58318 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34319 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]0b33f80b2008-12-17 21:34:36320
[email protected]076961c2014-03-12 22:23:56321 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
322 if (incremental_uptime_sec)
323 stability->set_incremental_uptime_sec(incremental_uptime_sec);
324 const uint64 uptime_sec = uptime.InSeconds();
[email protected]c68a2b9b2013-10-09 18:16:36325 if (uptime_sec)
326 stability->set_uptime_sec(uptime_sec);
[email protected]0b33f80b2008-12-17 21:34:36327}
328
initial.commit09911bf2008-07-26 23:55:29329void MetricsLog::RecordEnvironment(
[email protected]85791b0b2014-05-20 15:18:58330 const std::vector<metrics::MetricsProvider*>& metrics_providers,
[email protected]65801452014-07-09 05:42:41331 const std::vector<variations::ActiveGroupId>& synthetic_trials,
332 int64 install_date) {
[email protected]0f2f7792013-11-28 16:09:14333 DCHECK(!HasEnvironment());
334
[email protected]bc66d532012-03-23 01:57:05335 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
[email protected]24b9bb392013-01-29 20:29:29336
337 std::string brand_code;
[email protected]09dee82d2014-05-22 14:00:53338 if (client_->GetBrand(&brand_code))
[email protected]24b9bb392013-01-29 20:29:29339 system_profile->set_brand_code(brand_code);
340
[email protected]cc5d7f42012-10-30 00:30:09341 int enabled_date;
[email protected]24f81ca2014-05-26 15:59:34342 bool success =
343 base::StringToInt(GetMetricsEnabledDate(local_state_), &enabled_date);
[email protected]bc66d532012-03-23 01:57:05344 DCHECK(success);
[email protected]86573d12013-07-11 19:48:32345
346 // Reduce granularity of the enabled_date field to nearest hour.
347 system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
348
[email protected]86573d12013-07-11 19:48:32349 // Reduce granularity of the install_date field to nearest hour.
350 system_profile->set_install_date(RoundSecondsToHour(install_date));
[email protected]bc66d532012-03-23 01:57:05351
[email protected]09dee82d2014-05-22 14:00:53352 system_profile->set_application_locale(client_->GetApplicationLocale());
[email protected]bc66d532012-03-23 01:57:05353
354 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
[email protected]51994b22014-05-30 13:24:21355
356 // By default, the hardware class is empty (i.e., unknown).
357 hardware->set_hardware_class(std::string());
358
[email protected]0b6a4fb2012-10-16 01:58:21359 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
[email protected]bc66d532012-03-23 01:57:05360 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
361#if defined(OS_WIN)
362 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
363#endif
364
365 SystemProfileProto::OS* os = system_profile->mutable_os();
[email protected]1bb25e02012-08-03 22:39:39366 std::string os_name = base::SysInfo::OperatingSystemName();
367#if defined(OS_WIN)
368 // TODO(mad): This only checks whether the main process is a Metro process at
369 // upload time; not whether the collected metrics were all gathered from
370 // Metro. This is ok as an approximation for now, since users will rarely be
371 // switching from Metro to Desktop mode; but we should re-evaluate whether we
372 // can distinguish metrics more cleanly in the future: http://crbug.com/140568
373 if (base::win::IsMetroProcess())
374 os_name += " (Metro)";
375#endif
376 os->set_name(os_name);
[email protected]bc66d532012-03-23 01:57:05377 os->set_version(base::SysInfo::OperatingSystemVersion());
[email protected]5106b3a2012-10-03 20:10:44378#if defined(OS_ANDROID)
379 os->set_fingerprint(
380 base::android::BuildInfo::GetInstance()->android_build_fp());
381#endif
[email protected]bc66d532012-03-23 01:57:05382
[email protected]5c8f89f692013-07-18 11:13:28383 base::CPU cpu_info;
384 SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
385 cpu->set_vendor_name(cpu_info.vendor_name());
386 cpu->set_signature(cpu_info.signature());
387
[email protected]0c8b7ad2012-11-06 07:08:14388 std::vector<ActiveGroupId> field_trial_ids;
[email protected]767c9d92012-03-02 16:04:34389 GetFieldTrialIds(&field_trial_ids);
390 WriteFieldTrials(field_trial_ids, system_profile);
[email protected]60677562013-11-17 15:52:55391 WriteFieldTrials(synthetic_trials, system_profile);
[email protected]f65859e2013-02-04 20:00:25392
[email protected]85791b0b2014-05-20 15:18:58393 for (size_t i = 0; i < metrics_providers.size(); ++i)
394 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
395
[email protected]0f2f7792013-11-28 16:09:14396 std::string serialied_system_profile;
397 std::string base64_system_profile;
[email protected]33fca122013-12-11 01:48:50398 if (system_profile->SerializeToString(&serialied_system_profile)) {
399 base::Base64Encode(serialied_system_profile, &base64_system_profile);
[email protected]24f81ca2014-05-26 15:59:34400 PrefService* local_state = local_state_;
[email protected]91b1d912014-06-05 10:52:08401 local_state->SetString(metrics::prefs::kStabilitySavedSystemProfile,
[email protected]0f2f7792013-11-28 16:09:14402 base64_system_profile);
[email protected]91b1d912014-06-05 10:52:08403 local_state->SetString(metrics::prefs::kStabilitySavedSystemProfileHash,
[email protected]0f2f7792013-11-28 16:09:14404 ComputeSHA1(serialied_system_profile));
405 }
406}
407
408bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
[email protected]24f81ca2014-05-26 15:59:34409 PrefService* local_state = local_state_;
[email protected]0f2f7792013-11-28 16:09:14410 const std::string base64_system_profile =
[email protected]91b1d912014-06-05 10:52:08411 local_state->GetString(metrics::prefs::kStabilitySavedSystemProfile);
[email protected]0f2f7792013-11-28 16:09:14412 if (base64_system_profile.empty())
413 return false;
414
415 const std::string system_profile_hash =
[email protected]91b1d912014-06-05 10:52:08416 local_state->GetString(metrics::prefs::kStabilitySavedSystemProfileHash);
417 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile);
418 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash);
[email protected]0f2f7792013-11-28 16:09:14419
420 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
421 std::string serialied_system_profile;
422 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
423 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
424 system_profile->ParseFromString(serialied_system_profile);
initial.commit09911bf2008-07-26 23:55:29425}
426
[email protected]bfb77b52014-06-07 01:54:01427void MetricsLog::CloseLog() {
428 DCHECK(!closed_);
429 closed_ = true;
430}
431
432void MetricsLog::GetEncodedLog(std::string* encoded_log) {
433 DCHECK(closed_);
434 uma_proto_.SerializeToString(encoded_log);
435}
asvitkinecbd420732014-08-26 22:15:40436
437} // namespace metrics