google_update_settings_posix.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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/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"
12
13namespace google_update {
14
15static std::string& posix_guid() {
16  CR_DEFINE_STATIC_LOCAL(std::string, guid, ());
17  return guid;
18}
19
20}  // namespace google_update
21
22// File name used in the user data dir to indicate consent.
23static const char kConsentToSendStats[] = "Consent To Send Stats";
24
25// static
26bool 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;
35}
36
37// static
38bool 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;
43
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;
52    }
53  } else {
54    google_update::posix_guid().clear();
55    return base::DeleteFile(consent_file, false);
56  }
57  return true;
58}
59
60// static
61bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
62  *metrics_id = google_update::posix_guid();
63  return true;
64}
65
66// static
67bool 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;
74  }
75
76  // Since user has consented, write the metrics id to the file.
77  google_update::posix_guid() = client_id;
78  return GoogleUpdateSettings::SetCollectStatsConsent(true);
79}
80
81// GetLastRunTime and SetLastRunTime are not implemented for posix. Their
82// current return values signal failure which the caller is designed to
83// handle.
84
85// static
86int GoogleUpdateSettings::GetLastRunTime() {
87  return -1;
88}
89
90// static
91bool GoogleUpdateSettings::SetLastRunTime() {
92  return false;
93}
94