google_update_settings_posix.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_client_id = 18 LAZY_INSTANCE_INITIALIZER; 19base::LazyInstance<base::Lock>::Leaky g_posix_client_id_lock = 20 LAZY_INSTANCE_INITIALIZER; 21 22// File name used in the user data dir to indicate consent. 23const char kConsentToSendStats[] = "Consent To Send Stats"; 24 25} // namespace 26 27// static 28bool GoogleUpdateSettings::GetCollectStatsConsent() { 29 base::FilePath consent_file; 30 PathService::Get(chrome::DIR_USER_DATA, &consent_file); 31 consent_file = consent_file.Append(kConsentToSendStats); 32 33 if (!base::DirectoryExists(consent_file.DirName())) 34 return false; 35 36 std::string tmp_guid; 37 bool consented = base::ReadFileToString(consent_file, &tmp_guid); 38 if (consented) { 39 base::AutoLock lock(g_posix_client_id_lock.Get()); 40 g_posix_client_id.Get().assign(tmp_guid); 41 } 42 return consented; 43} 44 45// static 46bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) { 47 base::FilePath consent_dir; 48 PathService::Get(chrome::DIR_USER_DATA, &consent_dir); 49 if (!base::DirectoryExists(consent_dir)) 50 return false; 51 52 base::AutoLock lock(g_posix_client_id_lock.Get()); 53 54 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats); 55 if (consented) { 56 if ((!base::PathExists(consent_file)) || 57 (base::PathExists(consent_file) && !g_posix_client_id.Get().empty())) { 58 const char* c_str = g_posix_client_id.Get().c_str(); 59 int size = g_posix_client_id.Get().size(); 60 return base::WriteFile(consent_file, c_str, size) == size; 61 } 62 } else { 63 g_posix_client_id.Get().clear(); 64 return base::DeleteFile(consent_file, false); 65 } 66 return true; 67} 68 69// static 70// TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. 71scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() { 72 scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo); 73 74 base::AutoLock lock(g_posix_client_id_lock.Get()); 75 if (g_posix_client_id.Get().empty()) 76 return scoped_ptr<metrics::ClientInfo>(); 77 client_info->client_id = g_posix_client_id.Get(); 78 79 return client_info.Pass(); 80} 81 82// static 83// TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. 84void GoogleUpdateSettings::StoreMetricsClientInfo( 85 const metrics::ClientInfo& client_info) { 86 // Make sure that user has consented to send crashes. 87 if (!GoogleUpdateSettings::GetCollectStatsConsent()) 88 return; 89 90 { 91 // Since user has consented, write the metrics id to the file. 92 base::AutoLock lock(g_posix_client_id_lock.Get()); 93 g_posix_client_id.Get() = client_info.client_id; 94 } 95 GoogleUpdateSettings::SetCollectStatsConsent(true); 96} 97 98// GetLastRunTime and SetLastRunTime are not implemented for posix. Their 99// current return values signal failure which the caller is designed to 100// handle. 101 102// static 103int GoogleUpdateSettings::GetLastRunTime() { 104 return -1; 105} 106 107// static 108bool GoogleUpdateSettings::SetLastRunTime() { 109 return false; 110} 111