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"
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";
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
);
32 bool consented
= base::ReadFileToString(consent_file
, &tmp_guid
);
34 base::AutoLock
lock(g_posix_guid_lock
.Get());
35 g_posix_guid
.Get().assign(tmp_guid
);
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
))
47 base::AutoLock
lock(g_posix_guid_lock
.Get());
49 base::FilePath consent_file
= consent_dir
.AppendASCII(kConsentToSendStats
);
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
;
58 g_posix_guid
.Get().clear();
59 return base::DeleteFile(consent_file
, false);
65 bool GoogleUpdateSettings::GetMetricsId(std::string
* metrics_id
) {
66 base::AutoLock
lock(g_posix_guid_lock
.Get());
67 *metrics_id
= g_posix_guid
.Get();
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()) {
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
94 int GoogleUpdateSettings::GetLastRunTime() {
99 bool GoogleUpdateSettings::SetLastRunTime() {