Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / google / google_update_settings_posix.cc
blobb8615769aaba79593fd420138752572763cb04cf
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, ());
17 return 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";
25 // static
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);
30 std::string tmp_guid;
31 bool consented = base::ReadFileToString(consent_file, &tmp_guid);
32 if (consented)
33 google_update::posix_guid().assign(tmp_guid);
34 return consented;
37 // static
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))
42 return false;
44 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
45 if (consented) {
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;
53 } else {
54 google_update::posix_guid().clear();
55 return base::DeleteFile(consent_file, false);
57 return true;
60 // static
61 bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
62 *metrics_id = google_update::posix_guid();
63 return true;
66 // static
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()) {
73 return false;
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
83 // handle.
85 // static
86 int GoogleUpdateSettings::GetLastRunTime() {
87 return -1;
90 // static
91 bool GoogleUpdateSettings::SetLastRunTime() {
92 return false;