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.
5 #include "chrome/browser/ui/webui/options/easy_unlock_handler.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"
16 EasyUnlockHandler::EasyUnlockHandler() {
19 EasyUnlockHandler::~EasyUnlockHandler() {
20 EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->RemoveObserver(this);
23 void 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},
41 RegisterStrings(values, resources, arraysize(resources));
44 void EasyUnlockHandler::InitializeHandler() {
45 EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->AddObserver(this);
48 void 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)));
63 void EasyUnlockHandler::OnTurnOffOperationStatusChanged() {
64 SendTurnOffOperationStatus();
67 void EasyUnlockHandler::SendTurnOffOperationStatus() {
68 EasyUnlockService::TurnOffFlowStatus status =
69 EasyUnlockService::Get(Profile::FromWebUI(web_ui()))
70 ->turn_off_flow_status();
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;
76 case EasyUnlockService::IDLE:
77 status_string = "idle";
79 case EasyUnlockService::PENDING:
80 status_string = "pending";
82 case EasyUnlockService::FAIL:
83 status_string = "server-error";
86 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status;
87 status_string = "idle";
90 web_ui()->CallJavascriptFunction("EasyUnlockTurnOffOverlay.updateUIState",
91 base::StringValue(status_string));
94 void EasyUnlockHandler::HandleGetTurnOffFlowStatus(
95 const base::ListValue* args) {
96 SendTurnOffOperationStatus();
99 void EasyUnlockHandler::HandleRequestTurnOff(const base::ListValue* args) {
100 EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->RunTurnOffFlow();
103 void EasyUnlockHandler::HandlePageDismissed(const base::ListValue* args) {
104 EasyUnlockService::Get(Profile::FromWebUI(web_ui()))->ResetTurnOffFlow();
107 } // namespace options