| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/installer/util/google_update_settings.h" | 5 #include "chrome/installer/util/google_update_settings.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 13 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 base::LazyInstance<std::string>::Leaky g_posix_client_id = | 19 struct ClientInfo { |
| 20 enum ConsentState { |
| 21 CONSENT_ACCEPTED, |
| 22 CONSENT_DENIED, |
| 23 CONSENT_UNKNOWN |
| 24 } |
| 25 |
| 26 ClientInfo() : consent_state(CONSENT_UNKNOWN) {} |
| 27 |
| 28 ConsentState consent_state; |
| 29 std::string client_id; |
| 30 int64 installation_date; |
| 31 }; |
| 32 |
| 33 // Contains the ClientInfo for this user. If consent wasn't checked yet, |
| 34 // |consent_state| is set to CONSENT_UNKNOWN. Once consent is verified it is set |
| 35 // to CONSENT_ACCEPTED or CONSENT_DENIED accordingly. If CONSENT_ACCEPTED, |
| 36 // |client_id| and |installation_date| will also be set as retrieved from the |
| 37 // consent file. |
| 38 base::LazyInstance<ClientInfo>::Leaky g_posix_client_info = |
| 18 LAZY_INSTANCE_INITIALIZER; | 39 LAZY_INSTANCE_INITIALIZER; |
| 19 base::LazyInstance<base::Lock>::Leaky g_posix_client_id_lock = | 40 base::LazyInstance<base::Lock>::Leaky g_posix_client_info_lock = |
| 20 LAZY_INSTANCE_INITIALIZER; | 41 LAZY_INSTANCE_INITIALIZER; |
| 21 | 42 |
| 22 // File name used in the user data dir to indicate consent. | 43 // File name used in the user data dir to indicate consent. |
| 23 const char kConsentToSendStats[] = "Consent To Send Stats"; | 44 const char kConsentToSendStats[] = "Consent To Send Stats"; |
| 24 | 45 |
| 46 const char kConsentInfoPartsDelimiter = '/'; |
| 47 |
| 25 } // namespace | 48 } // namespace |
| 26 | 49 |
| 27 // static | 50 // static |
| 28 bool GoogleUpdateSettings::GetCollectStatsConsent() { | 51 bool GoogleUpdateSettings::GetCollectStatsConsent() { |
| 52 base::AutoLock lock(g_posix_client_info_lock.Get()); |
| 53 ClientInfo* client_info = g_posix_client_info.Pointer(); |
| 54 if (client_info->consent_state != ClientInfo::CONSENT_UNKNOWN) |
| 55 return client_info->consent_state; |
| 56 |
| 29 base::FilePath consent_file; | 57 base::FilePath consent_file; |
| 30 PathService::Get(chrome::DIR_USER_DATA, &consent_file); | 58 PathService::Get(chrome::DIR_USER_DATA, &consent_file); |
| 31 consent_file = consent_file.Append(kConsentToSendStats); | 59 consent_file = consent_file.Append(kConsentToSendStats); |
| 32 | 60 |
| 33 if (!base::DirectoryExists(consent_file.DirName())) | 61 if (!base::DirectoryExists(consent_file.DirName())) |
| 34 return false; | 62 return false; |
| 35 | 63 |
| 36 std::string tmp_guid; | 64 std::string client_info_str; |
| 37 bool consented = base::ReadFileToString(consent_file, &tmp_guid); | 65 bool consented = base::ReadFileToString(consent_file, &client_info_str); |
| 38 if (consented) { | 66 if (consented) { |
| 39 base::AutoLock lock(g_posix_client_id_lock.Get()); | 67 client_info->consent_state = CONSENT_ACCEPTED; |
| 40 g_posix_client_id.Get().assign(tmp_guid); | 68 |
| 69 std::vector<std::string> client_info_parts; |
| 70 base::SplitString( |
| 71 client_info_str, kConsentInfoPartsDelimiter, &client_info_parts); |
| 72 client_info->client_id.assign(client_info_parts[0]); |
| 73 client_info->installation_date = base::StringToInt64(client_info_parts[1]); |
| 74 } else { |
| 75 client_info->consent_state = CONSENT_DENIED; |
| 41 } | 76 } |
| 42 return consented; | 77 return consented; |
| 43 } | 78 } |
| 44 | 79 |
| 45 // static | 80 // static |
| 46 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) { | 81 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) { |
| 47 base::FilePath consent_dir; | 82 base::FilePath consent_dir; |
| 48 PathService::Get(chrome::DIR_USER_DATA, &consent_dir); | 83 PathService::Get(chrome::DIR_USER_DATA, &consent_dir); |
| 49 if (!base::DirectoryExists(consent_dir)) | 84 if (!base::DirectoryExists(consent_dir)) |
| 50 return false; | 85 return false; |
| 51 | 86 |
| 52 base::AutoLock lock(g_posix_client_id_lock.Get()); | 87 base::AutoLock lock(g_posix_client_info_lock.Get()); |
| 88 ClientInfo* client_info = g_posix_client_info.Pointer(); |
| 53 | 89 |
| 54 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats); | 90 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats); |
| 55 if (consented) { | 91 if (consented) { |
| 56 if ((!base::PathExists(consent_file)) || | 92 client_info->consent_state = CONSENT_ACCEPTED; |
| 57 (base::PathExists(consent_file) && !g_posix_client_id.Get().empty())) { | 93 // Write consent to disk if it wasn't previously accepted or if the client |
| 58 const char* c_str = g_posix_client_id.Get().c_str(); | 94 // ID was just set. |
| 59 int size = g_posix_client_id.Get().size(); | 95 if (!base::PathExists(consent_file) || !client_info->client_id.empty()) { |
| 60 return base::WriteFile(consent_file, c_str, size) == size; | 96 std::vector<std::string> client_info_parts; |
| 97 client_info_parts.push_back(client_info->cliend_id); |
| 98 client_info_parts.push_back( |
| 99 base::Int64ToString(client_info->installation_date)); |
| 100 |
| 101 const std::string client_info_str = |
| 102 base::JoinString(client_info_parts, kConsentInfoPartsDelimiter); |
| 103 return base::WriteFile( |
| 104 consent_file, |
| 105 client_info_str.c_str(), |
| 106 client_info_str.length()) == client_info_str.length(); |
| 61 } | 107 } |
| 62 } else { | 108 } else { |
| 63 g_posix_client_id.Get().clear(); | 109 client_info->consent_state = CONSENT_DENIED; |
| 110 client_info->client_id.clear(); |
| 111 client_info->installation_date = 0L; |
| 64 return base::DeleteFile(consent_file, false); | 112 return base::DeleteFile(consent_file, false); |
| 65 } | 113 } |
| 66 return true; | 114 return true; |
| 67 } | 115 } |
| 68 | 116 |
| 69 // static | 117 // static |
| 70 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. | 118 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. |
| 71 scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() { | 119 scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() { |
| 72 scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo); | 120 scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo); |
| 73 | 121 |
| 74 base::AutoLock lock(g_posix_client_id_lock.Get()); | 122 base::AutoLock lock(g_posix_client_id_lock.Get()); |
| 75 if (g_posix_client_id.Get().empty()) | 123 if (g_posix_client_id.Get().empty()) |
| 76 return scoped_ptr<metrics::ClientInfo>(); | 124 return scoped_ptr<metrics::ClientInfo>(); |
| 77 client_info->client_id = g_posix_client_id.Get(); | 125 client_info->client_id = g_posix_client_id.Get(); |
| 78 | 126 |
| 79 return client_info.Pass(); | 127 return client_info.Pass(); |
| 128 |
| 129 |
| 130 /* |
| 131 base::AutoLock lock(g_posix_client_info_lock.Get()); |
| 132 |
| 133 // Read the ClientInfo if it hasn't been read into the global instance yet. |
| 134 if (g_posix_client_info.Get().client_id.empty()) { |
| 135 base::AutoUnlock unlock(g_posix_client_info_lock.Get()); |
| 136 if (!GetCollectStatsConsent()) |
| 137 return false; |
| 138 } |
| 139 const ClientInfo* client_info = g_posix_client_info.Pointer(); |
| 140 |
| 141 DCHECK(!client_info->client_id.empty()); |
| 142 DCHECK_EQ(client_info, CONSENT_ACCEPTED); |
| 143 |
| 144 if (metrics_id) |
| 145 *metrics_id = client_info->client_id; |
| 146 if (installation_date) |
| 147 *installation_date = client_info->installation_date; |
| 148 return true; |
| 149 */ |
| 80 } | 150 } |
| 81 | 151 |
| 82 // static | 152 // static |
| 83 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. | 153 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX. |
| 84 void GoogleUpdateSettings::StoreMetricsClientInfo( | 154 void GoogleUpdateSettings::StoreMetricsClientInfo( |
| 85 const metrics::ClientInfo& client_info) { | 155 const metrics::ClientInfo& client_info) { |
| 86 // Make sure that user has consented to send crashes. | 156 // Make sure that user has consented to send crashes. |
| 87 if (!GoogleUpdateSettings::GetCollectStatsConsent()) | 157 if (!GoogleUpdateSettings::GetCollectStatsConsent()) |
| 88 return; | 158 return; |
| 89 | 159 |
| 90 { | 160 { |
| 91 // Since user has consented, write the metrics id to the file. | 161 // Since user has consented, write the metrics id to the file. |
| 92 base::AutoLock lock(g_posix_client_id_lock.Get()); | 162 base::AutoLock lock(g_posix_client_id_lock.Get()); |
| 93 g_posix_client_id.Get() = client_info.client_id; | 163 g_posix_client_id.Get() = client_info.client_id; |
| 164 |
| 165 |
| 166 /* |
| 167 // Since user has consented, write the metrics info to the consent file. |
| 168 base::AutoLock lock(g_posix_client_info_lock.Get()); |
| 169 ClientInfo* client_info = g_posix_client_info.Pointer(); |
| 170 client_info->client_id = client_id; |
| 171 client_info->installation_date = installation_date; |
| 172 */ |
| 94 } | 173 } |
| 95 GoogleUpdateSettings::SetCollectStatsConsent(true); | 174 GoogleUpdateSettings::SetCollectStatsConsent(true); |
| 96 } | 175 } |
| 97 | 176 |
| 98 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their | 177 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their |
| 99 // current return values signal failure which the caller is designed to | 178 // current return values signal failure which the caller is designed to |
| 100 // handle. | 179 // handle. |
| 101 | 180 |
| 102 // static | 181 // static |
| 103 int GoogleUpdateSettings::GetLastRunTime() { | 182 int GoogleUpdateSettings::GetLastRunTime() { |
| 104 return -1; | 183 return -1; |
| 105 } | 184 } |
| 106 | 185 |
| 107 // static | 186 // static |
| 108 bool GoogleUpdateSettings::SetLastRunTime() { | 187 bool GoogleUpdateSettings::SetLastRunTime() { |
| 109 return false; | 188 return false; |
| 110 } | 189 } |
| OLD | NEW |