google_update_settings_posix.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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. 4 5#include "chrome/installer/util/google_update_settings.h" 6 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" 14 15namespace { 16 17base::LazyInstance<std::string>::Leaky g_posix_guid = LAZY_INSTANCE_INITIALIZER; 18base::LazyInstance<base::Lock>::Leaky g_posix_guid_lock = 19 LAZY_INSTANCE_INITIALIZER; 20 21// File name used in the user data dir to indicate consent. 22const char kConsentToSendStats[] = "Consent To Send Stats"; 23 24} // namespace 25 26// static 27bool 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); 36 } 37 return consented; 38} 39 40// static 41bool 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; 46 47 base::AutoLock lock(g_posix_guid_lock.Get()); 48 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; 56 } 57 } else { 58 g_posix_guid.Get().clear(); 59 return base::DeleteFile(consent_file, false); 60 } 61 return true; 62} 63 64// static 65bool 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; 69} 70 71// static 72bool 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; 79 } 80 81 { 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; 85 } 86 return GoogleUpdateSettings::SetCollectStatsConsent(true); 87} 88 89// GetLastRunTime and SetLastRunTime are not implemented for posix. Their 90// current return values signal failure which the caller is designed to 91// handle. 92 93// static 94int GoogleUpdateSettings::GetLastRunTime() { 95 return -1; 96} 97 98// static 99bool GoogleUpdateSettings::SetLastRunTime() { 100 return false; 101} 102