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/host_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 "grit/generated_resources.h"
10
11namespace chromeos {
12
13namespace {
14
15const char kJsScreenPath[] = "login.HostPairingScreen";
16
17const char kMethodContextChanged[] = "contextChanged";
18
19// Sent from JS when screen is ready to receive context updates.
20// TODO(dzhioev): Move 'contextReady' logic to the base screen handler when
21// all screens migrate to context-based communications.
22const char kCallbackContextReady[] = "contextReady";
23
24}  // namespace
25
26HostPairingScreenHandler::HostPairingScreenHandler()
27    : BaseScreenHandler(kJsScreenPath),
28      delegate_(NULL),
29      show_on_init_(false),
30      js_context_ready_(false) {
31}
32
33HostPairingScreenHandler::~HostPairingScreenHandler() {
34  if (delegate_)
35    delegate_->OnActorDestroyed(this);
36}
37
38void HostPairingScreenHandler::HandleContextReady() {
39  js_context_ready_ = true;
40  OnContextChanged(context_cache_.storage());
41}
42
43void HostPairingScreenHandler::Initialize() {
44  if (!page_is_ready() || !delegate_)
45    return;
46
47  if (show_on_init_) {
48    Show();
49    show_on_init_ = false;
50  }
51}
52
53void HostPairingScreenHandler::DeclareLocalizedValues(
54    LocalizedValuesBuilder* builder) {
55  // TODO(dzhioev): Move the prefix logic to the base screen handler after
56  // migration.
57  std::string prefix;
58  base::ReplaceChars(kJsScreenPath, ".", "_", &prefix);
59  prefix += "_";
60
61  builder->Add(prefix + "welcomeTitle", IDS_PAIRING_HOST_WELCOME_TITLE);
62  builder->Add(prefix + "welcomeText", IDS_PAIRING_HOST_WELCOME_TEXT);
63  builder->Add(prefix + "confirmationTitle",
64               IDS_PAIRING_HOST_CONFIRMATION_TITLE);
65  builder->Add(prefix + "updatingTitle", IDS_PAIRING_HOST_UPDATING_TITLE);
66  builder->Add(prefix + "updatingText", IDS_PAIRING_HOST_UPDATING_TEXT);
67  builder->Add(prefix + "enrollTitle", IDS_PAIRING_ENROLL_TITLE);
68  builder->Add(prefix + "enrollingTitle",
69               IDS_PAIRING_ENROLLMENT_IN_PROGRESS);
70  builder->Add(prefix + "doneTitle", IDS_PAIRING_HOST_DONE_TITLE);
71  builder->Add(prefix + "doneText", IDS_PAIRING_HOST_DONE_TEXT);
72  builder->Add(prefix + "enrollmentErrorTitle",
73               IDS_PAIRING_ENROLLMENT_ERROR_TITLE);
74  builder->Add(prefix + "errorNeedsRestart",
75               IDS_PAIRING_HOST_EROLLMENT_ERROR_NEEDS_RESTART);
76}
77
78void HostPairingScreenHandler::RegisterMessages() {
79  AddPrefixedCallback(kCallbackContextReady,
80                      &HostPairingScreenHandler::HandleContextReady);
81}
82
83void HostPairingScreenHandler::Show() {
84  if (!page_is_ready()) {
85    show_on_init_ = true;
86    return;
87  }
88  ShowScreen(OobeUI::kScreenHostPairing, NULL);
89}
90
91void HostPairingScreenHandler::Hide() {
92}
93
94void HostPairingScreenHandler::SetDelegate(Delegate* delegate) {
95  delegate_ = delegate;
96  if (page_is_ready())
97    Initialize();
98}
99
100void HostPairingScreenHandler::OnContextChanged(
101    const base::DictionaryValue& diff) {
102  if (!js_context_ready_) {
103    context_cache_.ApplyChanges(diff, NULL);
104    return;
105  }
106  CallJS(kMethodContextChanged, diff);
107}
108
109}  // namespace chromeos
110