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