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/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/common/chrome_paths.h"
13 namespace google_update {
15 static std::string& posix_guid() {
16 CR_DEFINE_STATIC_LOCAL(std::string, guid, ());
20 } // namespace google_update
22 // File name used in the user data dir to indicate consent.
23 static const char kConsentToSendStats[] = "Consent To Send Stats";
26 bool GoogleUpdateSettings::GetCollectStatsConsent() {
27 base::FilePath consent_file;
28 PathService::Get(chrome::DIR_USER_DATA, &consent_file);
29 consent_file = consent_file.Append(kConsentToSendStats);
31 bool consented = base::ReadFileToString(consent_file, &tmp_guid);
33 google_update::posix_guid().assign(tmp_guid);
38 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
39 base::FilePath consent_dir;
40 PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
41 if (!base::DirectoryExists(consent_dir))
44 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
46 if ((!base::PathExists(consent_file)) ||
47 (base::PathExists(consent_file) &&
48 !google_update::posix_guid().empty())) {
49 const char* c_str = google_update::posix_guid().c_str();
50 int size = google_update::posix_guid().size();
51 return file_util::WriteFile(consent_file, c_str, size) == size;
54 google_update::posix_guid().clear();
55 return base::DeleteFile(consent_file, false);
61 bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
62 *metrics_id = google_update::posix_guid();
67 bool GoogleUpdateSettings::SetMetricsId(const std::string& client_id) {
68 // Make sure that user has consented to send crashes.
69 base::FilePath consent_dir;
70 PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
71 if (!base::DirectoryExists(consent_dir) ||
72 !GoogleUpdateSettings::GetCollectStatsConsent()) {
76 // Since user has consented, write the metrics id to the file.
77 google_update::posix_guid() = client_id;
78 return GoogleUpdateSettings::SetCollectStatsConsent(true);
81 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their
82 // current return values signal failure which the caller is designed to
86 int GoogleUpdateSettings::GetLastRunTime() {
91 bool GoogleUpdateSettings::SetLastRunTime() {