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/browser/chromeos/metrics_cros_settings_provider.h"
6
7#include "base/string_util.h"
8#include "base/threading/thread_restrictions.h"
9#include "base/values.h"
10#include "chrome/browser/chromeos/cros_settings.h"
11#include "chrome/browser/chromeos/cros_settings_names.h"
12#include "chrome/browser/chromeos/login/user_manager.h"
13#include "chrome/browser/ui/options/options_util.h"
14#include "chrome/installer/util/google_update_settings.h"
15
16#if defined(USE_LINUX_BREAKPAD)
17#include "chrome/app/breakpad_linux.h"
18#endif
19
20namespace chromeos {
21
22void MetricsCrosSettingsProvider::DoSet(const std::string& path,
23                                        Value* value) {
24  DCHECK(path == kStatsReportingPref);
25  bool enabled = false;
26  CHECK(value->GetAsBoolean(&enabled));
27  if (SetMetricsStatus(enabled)) {
28    CrosSettings::Get()->FireObservers(path.c_str());
29  }
30}
31
32bool MetricsCrosSettingsProvider::Get(const std::string& path,
33                                      Value** value) const {
34  DCHECK(path == kStatsReportingPref);
35  *value = Value::CreateBooleanValue(GetMetricsStatus());
36  return true;
37}
38
39// static
40bool MetricsCrosSettingsProvider::SetMetricsStatus(bool enabled) {
41  // We must be not logged in (on EULA page) or logged is as an owner to be able
42  // to modify the setting.
43  UserManager *user = UserManager::Get();
44  if (user->user_is_logged_in() && !user->current_user_is_owner())
45    return false;
46  VLOG(1) << "Setting cros stats/crash metric reporting to " << enabled;
47  bool collect_stats_consent = false;
48  {
49    // Loading consent file state causes us to do blocking IO on UI thread.
50    // Temporarily allow it until we fix http://crbug.com/62626
51    base::ThreadRestrictions::ScopedAllowIO allow_io;
52    collect_stats_consent = GoogleUpdateSettings::GetCollectStatsConsent();
53  }
54  if (enabled != collect_stats_consent) {
55    bool new_enabled = OptionsUtil::ResolveMetricsReportingEnabled(enabled);
56#if defined(USE_LINUX_BREAKPAD)
57    if (new_enabled)
58      InitCrashReporter();
59    // Else, if (!new_enabled), we should have turned crash reporting off
60    // here, but there is no API for that currently (while we use
61    // BreakPad). But this is not a big deal: crash reporting will be off
62    // after reboot for the current process while other Chrome processes
63    // will start when the setting is already set up. Other ChromeOS
64    // processes does not use BreakPad.
65#endif
66    return new_enabled == enabled;
67  }
68  return false;
69}
70
71// static
72bool MetricsCrosSettingsProvider::GetMetricsStatus() {
73  // Loading consent file state causes us to do blocking IO on UI thread.
74  // Temporarily allow it until we fix http://crbug.com/62626
75  base::ThreadRestrictions::ScopedAllowIO allow_io;
76  return GoogleUpdateSettings::GetCollectStatsConsent();
77}
78
79bool MetricsCrosSettingsProvider::HandlesSetting(const std::string& path) {
80  return ::StartsWithASCII(path, kStatsReportingPref, true);
81}
82
83};  // namespace chromeos
84