google_update_settings_posix.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/string_util.h"
10#include "chrome/common/chrome_paths.h"
11
12namespace google_update {
13
14static std::string& posix_guid() {
15  CR_DEFINE_STATIC_LOCAL(std::string, guid, ());
16  return guid;
17}
18
19}  // namespace google_update
20
21// File name used in the user data dir to indicate consent.
22static const char kConsentToSendStats[] = "Consent To Send Stats";
23
24// static
25bool GoogleUpdateSettings::GetCollectStatsConsent() {
26  FilePath consent_file;
27  PathService::Get(chrome::DIR_USER_DATA, &consent_file);
28  consent_file = consent_file.Append(kConsentToSendStats);
29  std::string tmp_guid;
30  bool consented = file_util::ReadFileToString(consent_file, &tmp_guid);
31  if (consented)
32    google_update::posix_guid().assign(tmp_guid);
33  return consented;
34}
35
36// static
37bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
38  FilePath consent_dir;
39  PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
40  if (!file_util::DirectoryExists(consent_dir))
41    return false;
42
43  FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
44  if (consented) {
45    if ((!file_util::PathExists(consent_file)) ||
46        (file_util::PathExists(consent_file) &&
47         !google_update::posix_guid().empty())) {
48      const char* c_str = google_update::posix_guid().c_str();
49      int size = google_update::posix_guid().size();
50      return file_util::WriteFile(consent_file, c_str, size) == size;
51    }
52  } else {
53    google_update::posix_guid().clear();
54    return file_util::Delete(consent_file, false);
55  }
56  return true;
57}
58
59bool GoogleUpdateSettings::SetMetricsId(const std::wstring& client_id) {
60  // Make sure that user has consented to send crashes.
61  FilePath consent_dir;
62  PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
63  if (!file_util::DirectoryExists(consent_dir) ||
64      !GoogleUpdateSettings::GetCollectStatsConsent())
65    return false;
66
67  // Since user has consented, write the metrics id to the file.
68  google_update::posix_guid() = WideToASCII(client_id);
69  return GoogleUpdateSettings::SetCollectStatsConsent(true);
70}
71
72// GetLastRunTime and SetLastRunTime are not implemented for posix. Their
73// current return values signal failure which the caller is designed to
74// handle.
75
76// static
77int GoogleUpdateSettings::GetLastRunTime() {
78  return -1;
79}
80
81// static
82bool GoogleUpdateSettings::SetLastRunTime() {
83  return false;
84}
85