1// Copyright 2014 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/easy_unlock_handler.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "base/values.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/signin/easy_unlock_service.h"
13#include "chrome/grit/generated_resources.h"
14#include "content/public/browser/web_ui.h"
15
16namespace options {
17
18EasyUnlockHandler::EasyUnlockHandler() {
19}
20
21EasyUnlockHandler::~EasyUnlockHandler() {
22  EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->RemoveObserver(this);
23}
24
25void EasyUnlockHandler::GetLocalizedValues(base::DictionaryValue* values) {
26  static OptionsStringResource resources[] = {
27      {"easyUnlockTurnOffButton", IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_BUTTON},
28      {"easyUnlockTurnOffTitle", IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_TITLE},
29      {"easyUnlockTurnOffDescription",
30       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_DESCRIPTION},
31      {"easyUnlockTurnOffOfflineTitle",
32       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_OFFLINE_TITLE},
33      {"easyUnlockTurnOffOfflineMessage",
34       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_OFFLINE_MESSAGE},
35      {"easyUnlockTurnOffErrorTitle",
36       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_ERROR_TITLE},
37      {"easyUnlockTurnOffErrorMessage",
38       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_ERROR_MESSAGE},
39      {"easyUnlockTurnOffRetryButton",
40       IDS_OPTIONS_EASY_UNLOCK_TURN_OFF_RETRY_BUTTON},
41  };
42
43  RegisterStrings(values, resources, arraysize(resources));
44}
45
46void EasyUnlockHandler::InitializeHandler() {
47  EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->AddObserver(this);
48}
49
50void EasyUnlockHandler::RegisterMessages() {
51  web_ui()->RegisterMessageCallback(
52      "easyUnlockGetTurnOffFlowStatus",
53      base::Bind(&EasyUnlockHandler::HandleGetTurnOffFlowStatus,
54                 base::Unretained(this)));
55  web_ui()->RegisterMessageCallback(
56      "easyUnlockRequestTurnOff",
57      base::Bind(&EasyUnlockHandler::HandleRequestTurnOff,
58                 base::Unretained(this)));
59  web_ui()->RegisterMessageCallback(
60      "easyUnlockTurnOffOverlayDismissed",
61      base::Bind(&EasyUnlockHandler::HandlePageDismissed,
62                 base::Unretained(this)));
63}
64
65void EasyUnlockHandler::OnTurnOffOperationStatusChanged() {
66  SendTurnOffOperationStatus();
67}
68
69void EasyUnlockHandler::SendTurnOffOperationStatus() {
70  EasyUnlockService::TurnOffFlowStatus status =
71      EasyUnlockService::Get(Profile::FromWebUI(web_ui()))
72          ->GetTurnOffFlowStatus();
73
74  // Translate status into JS UI state string. Note the translated string
75  // should match UIState defined in easy_unlock_turn_off_overlay.js.
76  std::string status_string;
77  switch (status) {
78    case EasyUnlockService::IDLE:
79      status_string = "idle";
80      break;
81    case EasyUnlockService::PENDING:
82      status_string = "pending";
83      break;
84    case EasyUnlockService::FAIL:
85      status_string = "server-error";
86      break;
87    default:
88      LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status;
89      status_string = "idle";
90      break;
91  }
92  web_ui()->CallJavascriptFunction("EasyUnlockTurnOffOverlay.updateUIState",
93                                   base::StringValue(status_string));
94}
95
96void EasyUnlockHandler::HandleGetTurnOffFlowStatus(
97    const base::ListValue* args) {
98  SendTurnOffOperationStatus();
99}
100
101void EasyUnlockHandler::HandleRequestTurnOff(const base::ListValue* args) {
102  EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->RunTurnOffFlow();
103}
104
105void EasyUnlockHandler::HandlePageDismissed(const base::ListValue* args) {
106  EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->ResetTurnOffFlow();
107}
108
109}  // namespace options
110