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/browser/ui/webui/options/chromeos/stats_options_handler.h"
6
7#include "base/basictypes.h"
8#include "base/callback.h"
9#include "base/utf_string_conversions.h"
10#include "base/values.h"
11#include "chrome/browser/chromeos/cros_settings_names.h"
12#include "chrome/browser/chromeos/login/user_manager.h"
13#include "chrome/browser/chromeos/metrics_cros_settings_provider.h"
14#include "chrome/browser/metrics/user_metrics.h"
15
16namespace chromeos {
17
18StatsOptionsHandler::StatsOptionsHandler()
19    : CrosOptionsPageUIHandler(new MetricsCrosSettingsProvider()) {
20}
21
22// OptionsPageUIHandler implementation.
23void StatsOptionsHandler::GetLocalizedValues(
24    DictionaryValue* localized_strings) {
25}
26void StatsOptionsHandler::Initialize() {
27  SetupMetricsReportingCheckbox(false);
28}
29
30// WebUIMessageHandler implementation.
31void StatsOptionsHandler::RegisterMessages() {
32  web_ui_->RegisterMessageCallback(
33      "metricsReportingCheckboxAction",
34      NewCallback(this, &StatsOptionsHandler::HandleMetricsReportingCheckbox));
35}
36
37MetricsCrosSettingsProvider* StatsOptionsHandler::provider() const {
38  return static_cast<MetricsCrosSettingsProvider*>(settings_provider_.get());
39}
40
41void StatsOptionsHandler::HandleMetricsReportingCheckbox(
42    const ListValue* args) {
43#if defined(GOOGLE_CHROME_BUILD)
44  const std::string checked_str = UTF16ToUTF8(ExtractStringValue(args));
45  const bool enabled = (checked_str == "true");
46  UserMetricsRecordAction(
47      enabled ?
48      UserMetricsAction("Options_MetricsReportingCheckbox_Enable") :
49      UserMetricsAction("Options_MetricsReportingCheckbox_Disable"));
50  const bool is_enabled = MetricsCrosSettingsProvider::GetMetricsStatus();
51  SetupMetricsReportingCheckbox(enabled == is_enabled);
52#endif
53}
54
55void StatsOptionsHandler::SetupMetricsReportingCheckbox(bool user_changed) {
56#if defined(GOOGLE_CHROME_BUILD)
57  FundamentalValue checked(MetricsCrosSettingsProvider::GetMetricsStatus());
58  FundamentalValue disabled(!UserManager::Get()->current_user_is_owner());
59  FundamentalValue user_has_changed(user_changed);
60  web_ui_->CallJavascriptFunction(
61      "options.AdvancedOptions.SetMetricsReportingCheckboxState", checked,
62      disabled, user_has_changed);
63#endif
64}
65
66}  // namespace chromeos
67