[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / google / google_update_settings_posix.cc
blob67e63cc8d18d24af216b7399f02a0781a67f133c
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/installer/util/google_update_settings.h"
7 #include "base/file_util.h"
8 #include "base/lazy_instance.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/lock.h"
13 #include "chrome/common/chrome_paths.h"
15 namespace {
17 base::LazyInstance<std::string>::Leaky g_posix_guid = LAZY_INSTANCE_INITIALIZER;
18 base::LazyInstance<base::Lock>::Leaky g_posix_guid_lock =
19 LAZY_INSTANCE_INITIALIZER;
21 // File name used in the user data dir to indicate consent.
22 const char kConsentToSendStats[] = "Consent To Send Stats";
24 } // namespace
26 // static
27 bool GoogleUpdateSettings::GetCollectStatsConsent() {
28 base::FilePath consent_file;
29 PathService::Get(chrome::DIR_USER_DATA, &consent_file);
30 consent_file = consent_file.Append(kConsentToSendStats);
31 std::string tmp_guid;
32 bool consented = base::ReadFileToString(consent_file, &tmp_guid);
33 if (consented) {
34 base::AutoLock lock(g_posix_guid_lock.Get());
35 g_posix_guid.Get().assign(tmp_guid);
37 return consented;
40 // static
41 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
42 base::FilePath consent_dir;
43 PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
44 if (!base::DirectoryExists(consent_dir))
45 return false;
47 base::AutoLock lock(g_posix_guid_lock.Get());
49 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
50 if (consented) {
51 if ((!base::PathExists(consent_file)) ||
52 (base::PathExists(consent_file) && !g_posix_guid.Get().empty())) {
53 const char* c_str = g_posix_guid.Get().c_str();
54 int size = g_posix_guid.Get().size();
55 return base::WriteFile(consent_file, c_str, size) == size;
57 } else {
58 g_posix_guid.Get().clear();
59 return base::DeleteFile(consent_file, false);
61 return true;
64 // static
65 bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
66 base::AutoLock lock(g_posix_guid_lock.Get());
67 *metrics_id = g_posix_guid.Get();
68 return true;
71 // static
72 bool GoogleUpdateSettings::SetMetricsId(const std::string& client_id) {
73 // Make sure that user has consented to send crashes.
74 base::FilePath consent_dir;
75 PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
76 if (!base::DirectoryExists(consent_dir) ||
77 !GoogleUpdateSettings::GetCollectStatsConsent()) {
78 return false;
82 // Since user has consented, write the metrics id to the file.
83 base::AutoLock lock(g_posix_guid_lock.Get());
84 g_posix_guid.Get() = client_id;
86 return GoogleUpdateSettings::SetCollectStatsConsent(true);
89 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their
90 // current return values signal failure which the caller is designed to
91 // handle.
93 // static
94 int GoogleUpdateSettings::GetLastRunTime() {
95 return -1;
98 // static
99 bool GoogleUpdateSettings::SetLastRunTime() {
100 return false;