summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_update_settings_posix.cc
blob: ff5bb4ca967e7f13a5de249fff7f2065b7c12bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/installer/util/google_update_settings.h"

#include "base/command_line.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/string_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"

namespace google_update {
std::string posix_guid;
}

// File name used in the user data dir to indicate consent.
static const char kConsentToSendStats[] = "Consent To Send Stats";

// static
bool GoogleUpdateSettings::GetCollectStatsConsent() {
  bool forced_enable = CommandLine::ForCurrentProcess()->
      HasSwitch(switches::kEnableCrashReporter);
  FilePath consent_file;
  PathService::Get(chrome::DIR_USER_DATA, &consent_file);
  consent_file = consent_file.Append(kConsentToSendStats);
  std::string tmp_guid;
  bool consented = file_util::ReadFileToString(consent_file, &tmp_guid);
  if (forced_enable || consented)
    google_update::posix_guid.assign(tmp_guid);
  return forced_enable || consented;
}

// static
bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
  FilePath consent_dir;
  PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
  if (!file_util::DirectoryExists(consent_dir))
    return false;

  FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
  if (consented) {
    if ((!file_util::PathExists(consent_file)) ||
        (file_util::PathExists(consent_file) &&
         !google_update::posix_guid.empty())) {
      const char* c_str = google_update::posix_guid.c_str();
      int size = google_update::posix_guid.size();
      return file_util::WriteFile(consent_file, c_str, size) == size;
    }
  } else {
    google_update::posix_guid.clear();
    return file_util::Delete(consent_file, false);
  }
  return true;
}

bool GoogleUpdateSettings::SetMetricsId(const std::wstring& client_id) {
  // Make sure that user has consented to send crashes.
  FilePath consent_dir;
  PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
  if (!file_util::DirectoryExists(consent_dir) ||
      !GoogleUpdateSettings::GetCollectStatsConsent())
    return false;

  // Since user has consented, write the metrics id to the file.
  google_update::posix_guid = WideToASCII(client_id);
  return GoogleUpdateSettings::SetCollectStatsConsent(true);
}