Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / google / google_update_settings_posix.cc
blob31e73018c572566f1a411bc5022a6500c4c96866
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/files/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"
15 #if defined(OS_MACOSX)
16 #include "components/crash/app/crashpad_mac.h"
17 #endif
19 namespace {
21 base::LazyInstance<std::string>::Leaky g_posix_client_id =
22 LAZY_INSTANCE_INITIALIZER;
23 base::LazyInstance<base::Lock>::Leaky g_posix_client_id_lock =
24 LAZY_INSTANCE_INITIALIZER;
26 // File name used in the user data dir to indicate consent.
27 const char kConsentToSendStats[] = "Consent To Send Stats";
29 void SetConsentFilePermissionIfNeeded(const base::FilePath& consent_file) {
30 #if defined(OS_CHROMEOS)
31 // The consent file needs to be world readable. See http://crbug.com/383003
32 int permissions;
33 if (base::GetPosixFilePermissions(consent_file, &permissions) &&
34 (permissions & base::FILE_PERMISSION_READ_BY_OTHERS) == 0) {
35 permissions |= base::FILE_PERMISSION_READ_BY_OTHERS;
36 base::SetPosixFilePermissions(consent_file, permissions);
38 #endif
41 } // namespace
43 // static
44 bool GoogleUpdateSettings::GetCollectStatsConsent() {
45 base::FilePath consent_file;
46 PathService::Get(chrome::DIR_USER_DATA, &consent_file);
47 consent_file = consent_file.Append(kConsentToSendStats);
49 if (!base::DirectoryExists(consent_file.DirName()))
50 return false;
52 std::string tmp_guid;
53 bool consented = base::ReadFileToString(consent_file, &tmp_guid);
54 if (consented) {
55 SetConsentFilePermissionIfNeeded(consent_file);
57 base::AutoLock lock(g_posix_client_id_lock.Get());
58 g_posix_client_id.Get().assign(tmp_guid);
60 return consented;
63 // static
64 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
65 #if defined(OS_MACOSX)
66 crash_reporter::SetUploadsEnabled(consented);
67 #endif
69 base::FilePath consent_dir;
70 PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
71 if (!base::DirectoryExists(consent_dir))
72 return false;
74 base::AutoLock lock(g_posix_client_id_lock.Get());
76 base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
77 if (!consented) {
78 g_posix_client_id.Get().clear();
79 return base::DeleteFile(consent_file, false);
82 const std::string& client_id = g_posix_client_id.Get();
83 if (base::PathExists(consent_file) && client_id.empty())
84 return true;
86 int size = client_id.size();
87 bool write_success =
88 base::WriteFile(consent_file, client_id.c_str(), size) == size;
89 if (write_success)
90 SetConsentFilePermissionIfNeeded(consent_file);
91 return write_success;
94 // static
95 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
96 scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() {
97 scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo);
99 base::AutoLock lock(g_posix_client_id_lock.Get());
100 if (g_posix_client_id.Get().empty())
101 return scoped_ptr<metrics::ClientInfo>();
102 client_info->client_id = g_posix_client_id.Get();
104 return client_info.Pass();
107 // static
108 // TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
109 void GoogleUpdateSettings::StoreMetricsClientInfo(
110 const metrics::ClientInfo& client_info) {
111 // Make sure that user has consented to send crashes.
112 if (!GoogleUpdateSettings::GetCollectStatsConsent())
113 return;
116 // Since user has consented, write the metrics id to the file.
117 base::AutoLock lock(g_posix_client_id_lock.Get());
118 g_posix_client_id.Get() = client_info.client_id;
120 GoogleUpdateSettings::SetCollectStatsConsent(true);
123 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their
124 // current return values signal failure which the caller is designed to
125 // handle.
127 // static
128 int GoogleUpdateSettings::GetLastRunTime() {
129 return -1;
132 // static
133 bool GoogleUpdateSettings::SetLastRunTime() {
134 return false;