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/files/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 #if defined(OS_MACOSX)
16 #include "components/crash/app/crashpad_mac.h"
21 base::LazyInstance
<std::string
>::Leaky g_posix_client_id
=
22 LAZY_INSTANCE_INITIALIZER
;
23 base::LazyInstance
<base::Lock
>::Leaky g_posix_client_id_lock
=
24 LAZY_INSTANCE_INITIALIZER
;
26 // File name used in the user data dir to indicate consent.
27 const char kConsentToSendStats
[] = "Consent To Send Stats";
29 void SetConsentFilePermissionIfNeeded(const base::FilePath
& consent_file
) {
30 #if defined(OS_CHROMEOS)
31 // The consent file needs to be world readable. See http://crbug.com/383003
33 if (base::GetPosixFilePermissions(consent_file
, &permissions
) &&
34 (permissions
& base::FILE_PERMISSION_READ_BY_OTHERS
) == 0) {
35 permissions
|= base::FILE_PERMISSION_READ_BY_OTHERS
;
36 base::SetPosixFilePermissions(consent_file
, permissions
);
44 bool GoogleUpdateSettings::GetCollectStatsConsent() {
45 base::FilePath consent_file
;
46 PathService::Get(chrome::DIR_USER_DATA
, &consent_file
);
47 consent_file
= consent_file
.Append(kConsentToSendStats
);
49 if (!base::DirectoryExists(consent_file
.DirName()))
53 bool consented
= base::ReadFileToString(consent_file
, &tmp_guid
);
55 SetConsentFilePermissionIfNeeded(consent_file
);
57 base::AutoLock
lock(g_posix_client_id_lock
.Get());
58 g_posix_client_id
.Get().assign(tmp_guid
);
64 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented
) {
65 #if defined(OS_MACOSX)
66 crash_reporter::SetUploadsEnabled(consented
);
69 base::FilePath consent_dir
;
70 PathService::Get(chrome::DIR_USER_DATA
, &consent_dir
);
71 if (!base::DirectoryExists(consent_dir
))
74 base::AutoLock
lock(g_posix_client_id_lock
.Get());
76 base::FilePath consent_file
= consent_dir
.AppendASCII(kConsentToSendStats
);
78 g_posix_client_id
.Get().clear();
79 return base::DeleteFile(consent_file
, false);
82 const std::string
& client_id
= g_posix_client_id
.Get();
83 if (base::PathExists(consent_file
) && client_id
.empty())
86 int size
= client_id
.size();
88 base::WriteFile(consent_file
, client_id
.c_str(), size
) == size
;
90 SetConsentFilePermissionIfNeeded(consent_file
);
95 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
96 scoped_ptr
<metrics::ClientInfo
> GoogleUpdateSettings::LoadMetricsClientInfo() {
97 scoped_ptr
<metrics::ClientInfo
> client_info(new metrics::ClientInfo
);
99 base::AutoLock
lock(g_posix_client_id_lock
.Get());
100 if (g_posix_client_id
.Get().empty())
101 return scoped_ptr
<metrics::ClientInfo
>();
102 client_info
->client_id
= g_posix_client_id
.Get();
104 return client_info
.Pass();
108 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
109 void GoogleUpdateSettings::StoreMetricsClientInfo(
110 const metrics::ClientInfo
& client_info
) {
111 // Make sure that user has consented to send crashes.
112 if (!GoogleUpdateSettings::GetCollectStatsConsent())
116 // Since user has consented, write the metrics id to the file.
117 base::AutoLock
lock(g_posix_client_id_lock
.Get());
118 g_posix_client_id
.Get() = client_info
.client_id
;
120 GoogleUpdateSettings::SetCollectStatsConsent(true);
123 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their
124 // current return values signal failure which the caller is designed to
128 int GoogleUpdateSettings::GetLastRunTime() {
133 bool GoogleUpdateSettings::SetLastRunTime() {