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/chromeos/login/controller_pairing_screen_handler.h"
6
7#include "base/strings/string_util.h"
8#include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
9#include "content/public/browser/web_contents.h"
10#include "grit/generated_resources.h"
11
12namespace chromeos {
13
14namespace {
15
16const char kJsScreenPath[] = "login.ControllerPairingScreen";
17
18const char kMethodContextChanged[] = "contextChanged";
19
20const char kCallbackUserActed[] = "userActed";
21const char kCallbackContextChanged[] = "contextChanged";
22
23}  // namespace
24
25ControllerPairingScreenHandler::ControllerPairingScreenHandler()
26    : BaseScreenHandler(kJsScreenPath), delegate_(NULL), show_on_init_(false) {
27}
28
29ControllerPairingScreenHandler::~ControllerPairingScreenHandler() {
30  if (delegate_)
31    delegate_->OnActorDestroyed(this);
32}
33
34void ControllerPairingScreenHandler::HandleUserActed(
35    const std::string& action) {
36  if (!delegate_)
37    return;
38  delegate_->OnUserActed(action);
39}
40
41void ControllerPairingScreenHandler::HandleContextChanged(
42    const base::DictionaryValue* diff) {
43  if (!delegate_)
44    return;
45  delegate_->OnScreenContextChanged(*diff);
46}
47
48void ControllerPairingScreenHandler::Initialize() {
49  if (!page_is_ready() || !delegate_)
50    return;
51
52  if (show_on_init_) {
53    Show();
54    show_on_init_ = false;
55  }
56}
57
58void ControllerPairingScreenHandler::DeclareLocalizedValues(
59    LocalizedValuesBuilder* builder) {
60  // TODO(dzhioev): Move the prefix logic to the base screen handler after
61  // migration.
62  std::string prefix;
63  base::ReplaceChars(kJsScreenPath, ".", "_", &prefix);
64  prefix += "_";
65
66  builder->Add(prefix + "welcome", IDS_PAIRING_CONTROLLER_WELCOME);
67  builder->Add(prefix + "searching", IDS_PAIRING_CONTROLLER_SEARCHING);
68  builder->Add(prefix + "needHelp", IDS_PAIRING_NEED_HELP);
69  builder->Add(prefix + "troubleConnecting",
70               IDS_PAIRING_CONTROLLER_TROUBLE_CONNECTING);
71  builder->Add(prefix + "connectingAdvice",
72               IDS_PAIRING_CONTROLLER_CONNECTING_ADVICE);
73  builder->Add(prefix + "adviceGotIt", IDS_PAIRING_CONTROLLER_ADVICE_GOT_IT);
74  builder->Add(prefix + "selectTitle", IDS_PAIRING_CONTROLLER_SELECT_TITLE);
75  builder->Add(prefix + "connect", IDS_PAIRING_CONTROLLER_CONNECT);
76  builder->Add(prefix + "connecting", IDS_PAIRING_CONTROLLER_CONNECTING);
77  builder->Add(prefix + "confirmationTitle",
78               IDS_PAIRING_CONTROLLER_CONFIRMATION_TITLE);
79  builder->Add(prefix + "confirmationQuestion",
80               IDS_PAIRING_CONTROLLER_CONFIRMATION_QUESTION);
81  builder->Add(prefix + "rejectCode", IDS_PAIRING_CONTROLLER_REJECT_CODE);
82  builder->Add(prefix + "acceptCode", IDS_PAIRING_CONTROLLER_ACCEPT_CODE);
83  builder->Add(prefix + "updateTitle", IDS_PAIRING_CONTROLLER_UPDATE_TITLE);
84  builder->Add(prefix + "updateText", IDS_PAIRING_CONTROLLER_UPDATE_TEXT);
85  builder->Add(prefix + "connectionLostTitle",
86               IDS_PAIRING_CONTROLLER_CONNECTION_LOST_TITLE);
87  builder->Add(prefix + "connectionLostText",
88               IDS_PAIRING_CONTROLLER_CONNECTION_LOST_TEXT);
89  builder->Add(prefix + "enrollTitle", IDS_PAIRING_ENROLL_TITLE);
90  builder->Add(prefix + "enrollText1", IDS_PAIRING_CONTROLLER_ENROLL_TEXT_1);
91  builder->Add(prefix + "enrollText2", IDS_PAIRING_CONTROLLER_ENROLL_TEXT_2);
92  builder->Add(prefix + "continue", IDS_PAIRING_CONTROLLER_CONTINUE);
93  builder->Add(prefix + "enrollmentInProgress",
94               IDS_PAIRING_ENROLLMENT_IN_PROGRESS);
95  builder->Add(prefix + "enrollmentErrorTitle",
96               IDS_PAIRING_ENROLLMENT_ERROR_TITLE);
97  builder->Add(prefix + "enrollmentErrorHostRestarts",
98               IDS_PAIRING_CONTROLLER_ENROLLMENT_ERROR_HOST_RESTARTS);
99  builder->Add(prefix + "successTitle", IDS_PAIRING_CONTROLLER_SUCCESS_TITLE);
100  builder->Add(prefix + "successText", IDS_PAIRING_CONTROLLER_SUCCESS_TEXT);
101  builder->Add(prefix + "continueToHangouts",
102               IDS_PAIRING_CONTROLLER_CONTINUE_TO_HANGOUTS);
103}
104
105void ControllerPairingScreenHandler::RegisterMessages() {
106  AddPrefixedCallback(kCallbackUserActed,
107                      &ControllerPairingScreenHandler::HandleUserActed);
108  AddPrefixedCallback(kCallbackContextChanged,
109                      &ControllerPairingScreenHandler::HandleContextChanged);
110}
111
112void ControllerPairingScreenHandler::Show() {
113  if (!page_is_ready()) {
114    show_on_init_ = true;
115    return;
116  }
117  ShowScreen(OobeUI::kScreenControllerPairing, NULL);
118}
119
120void ControllerPairingScreenHandler::Hide() {
121}
122
123void ControllerPairingScreenHandler::SetDelegate(Delegate* delegate) {
124  delegate_ = delegate;
125  if (page_is_ready())
126    Initialize();
127}
128
129void ControllerPairingScreenHandler::OnContextChanged(
130    const base::DictionaryValue& diff) {
131  CallJS(kMethodContextChanged, diff);
132}
133
134content::BrowserContext* ControllerPairingScreenHandler::GetBrowserContext() {
135  return web_ui()->GetWebContents()->GetBrowserContext();
136}
137
138}  // namespace chromeos
139