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