reset_screen_handler.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// Copyright (c) 2012 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/reset_screen_handler.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/metrics/histogram.h"
11#include "base/prefs/pref_service.h"
12#include "base/values.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/chromeos/login/help_app_launcher.h"
15#include "chrome/browser/chromeos/reset/metrics.h"
16#include "chrome/common/pref_names.h"
17#include "chromeos/chromeos_switches.h"
18#include "chromeos/dbus/dbus_thread_manager.h"
19#include "chromeos/dbus/power_manager_client.h"
20#include "chromeos/dbus/session_manager_client.h"
21#include "chromeos/dbus/update_engine_client.h"
22#include "grit/browser_resources.h"
23#include "grit/chromium_strings.h"
24#include "grit/generated_resources.h"
25
26namespace {
27
28const char kJsScreenPath[] = "login.ResetScreen";
29
30// Reset screen id.
31const char kResetScreen[] = "reset";
32
33}  // namespace
34
35namespace chromeos {
36
37ResetScreenHandler::ResetScreenHandler()
38    : BaseScreenHandler(kJsScreenPath),
39      delegate_(NULL),
40      show_on_init_(false),
41      restart_required_(true),
42      reboot_was_requested_(false),
43      rollback_available_(false),
44      weak_factory_(this) {
45}
46
47ResetScreenHandler::~ResetScreenHandler() {
48  if (delegate_)
49    delegate_->OnActorDestroyed(this);
50}
51
52void ResetScreenHandler::PrepareToShow() {
53}
54
55void ResetScreenHandler::ShowWithParams() {
56  int dialog_type;
57  if (reboot_was_requested_) {
58    dialog_type = rollback_available_ ?
59        reset::DIALOG_SHORTCUT_CONFIRMING_POWERWASH_AND_ROLLBACK :
60        reset::DIALOG_SHORTCUT_CONFIRMING_POWERWASH_ONLY;
61  } else {
62    dialog_type = rollback_available_ ?
63      reset::DIALOG_SHORTCUT_OFFERING_ROLLBACK_AVAILABLE :
64      reset::DIALOG_SHORTCUT_OFFERING_ROLLBACK_UNAVAILABLE;
65  }
66  UMA_HISTOGRAM_ENUMERATION("Reset.ChromeOS.PowerwashDialogShown",
67                            dialog_type,
68                            reset::DIALOG_VIEW_TYPE_SIZE);
69
70  base::DictionaryValue reset_screen_params;
71  reset_screen_params.SetBoolean("showRestartMsg", restart_required_);
72  reset_screen_params.SetBoolean(
73      "showRollbackOption", rollback_available_ && !reboot_was_requested_);
74  reset_screen_params.SetBoolean(
75      "simpleConfirm", reboot_was_requested_ && !rollback_available_);
76  reset_screen_params.SetBoolean(
77      "rollbackConfirm", reboot_was_requested_ && rollback_available_);
78
79  PrefService* prefs = g_browser_process->local_state();
80  prefs->SetBoolean(prefs::kFactoryResetRequested, false);
81  prefs->SetBoolean(prefs::kRollbackRequested, false);
82  prefs->CommitPendingWrite();
83  ShowScreen(kResetScreen, &reset_screen_params);
84}
85
86void ResetScreenHandler::Show() {
87  if (!page_is_ready()) {
88    show_on_init_ = true;
89    return;
90  }
91
92  PrefService* prefs = g_browser_process->local_state();
93  restart_required_ = !CommandLine::ForCurrentProcess()->HasSwitch(
94      switches::kFirstExecAfterBoot);
95  reboot_was_requested_ = false;
96  rollback_available_ = false;
97  if (!restart_required_)  // First exec after boot.
98    reboot_was_requested_ = prefs->GetBoolean(prefs::kFactoryResetRequested);
99  if (!CommandLine::ForCurrentProcess()->HasSwitch(
100      switches::kEnableRollbackOption)) {
101    rollback_available_ = false;
102    ShowWithParams();
103  } else if (!restart_required_ && reboot_was_requested_) {
104    // First exec after boot.
105    rollback_available_ = prefs->GetBoolean(prefs::kRollbackRequested);
106    ShowWithParams();
107  } else {
108    chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->
109        CanRollbackCheck(base::Bind(&ResetScreenHandler::OnRollbackCheck,
110        weak_factory_.GetWeakPtr()));
111  }
112}
113
114void ResetScreenHandler::Hide() {
115}
116
117void ResetScreenHandler::SetDelegate(Delegate* delegate) {
118  delegate_ = delegate;
119  if (page_is_ready())
120    Initialize();
121}
122
123void ResetScreenHandler::DeclareLocalizedValues(
124    LocalizedValuesBuilder* builder) {
125  builder->Add("resetScreenTitle", IDS_RESET_SCREEN_TITLE);
126  builder->Add("cancelButton", IDS_CANCEL);
127
128  builder->Add("resetWarningDataDetails",
129               IDS_RESET_SCREEN_WARNING_DETAILS_DATA);
130  builder->Add("resetRestartMessage", IDS_RESET_SCREEN_RESTART_MSG);
131  builder->AddF("resetRollbackOption",
132                IDS_RESET_SCREEN_ROLLBACK_OPTION,
133                IDS_SHORT_PRODUCT_NAME);
134
135  // Different variants of the same UI elements for all dialog cases.
136  builder->Add("resetButtonReset", IDS_RESET_SCREEN_RESET);
137  builder->Add("resetButtonRelaunch", IDS_RELAUNCH_BUTTON);
138  builder->Add("resetButtonPowerwash", IDS_RESET_SCREEN_POWERWASH);
139
140  builder->AddF(
141      "resetAndRollbackWarningTextConfirmational",
142      IDS_RESET_SCREEN_CONFIRMATION_WARNING_POWERWASH_AND_ROLLBACK_MSG,
143      IDS_SHORT_PRODUCT_NAME);
144  builder->AddF("resetWarningTextConfirmational",
145                IDS_RESET_SCREEN_CONFIRMATION_WARNING_POWERWASH_MSG,
146                IDS_SHORT_PRODUCT_NAME);
147  builder->AddF("resetWarningTextInitial",
148                IDS_RESET_SCREEN_WARNING_MSG,
149                IDS_SHORT_PRODUCT_NAME);
150
151  builder->AddF(
152      "resetAndRollbackWarningDetailsConfirmational",
153      IDS_RESET_SCREEN_CONFIRMATION_WARNING_ROLLBACK_DETAILS,
154      IDS_SHORT_PRODUCT_NAME);
155  builder->AddF("resetWarningDetailsConfirmational",
156                IDS_RESET_SCREEN_CONFIRMATION_WARNING_DETAILS,
157                IDS_SHORT_PRODUCT_NAME);
158  builder->AddF("resetWarningDetailsInitial",
159                IDS_RESET_SCREEN_WARNING_DETAILS,
160                IDS_SHORT_PRODUCT_NAME);
161}
162
163// Invoked from call to CanRollbackCheck upon completion of the DBus call.
164void ResetScreenHandler::OnRollbackCheck(bool can_rollback) {
165  VLOG(1) << "Callback from CanRollbackCheck, result " << can_rollback;
166  rollback_available_ = can_rollback;
167  ShowWithParams();
168}
169
170void ResetScreenHandler::Initialize() {
171  if (!page_is_ready() || !delegate_)
172    return;
173
174  if (show_on_init_) {
175    Show();
176    show_on_init_ = false;
177  }
178}
179
180void ResetScreenHandler::RegisterMessages() {
181  AddCallback("cancelOnReset", &ResetScreenHandler::HandleOnCancel);
182  AddCallback("restartOnReset", &ResetScreenHandler::HandleOnRestart);
183  AddCallback("powerwashOnReset", &ResetScreenHandler::HandleOnPowerwash);
184  AddCallback("resetOnLearnMore", &ResetScreenHandler::HandleOnLearnMore);
185}
186
187void ResetScreenHandler::HandleOnCancel() {
188  if (delegate_)
189    delegate_->OnExit();
190}
191
192void ResetScreenHandler::HandleOnRestart(bool should_rollback) {
193  PrefService* prefs = g_browser_process->local_state();
194  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
195  prefs->SetBoolean(prefs::kRollbackRequested, should_rollback);
196  prefs->CommitPendingWrite();
197
198  chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart();
199}
200
201void ResetScreenHandler::HandleOnPowerwash(bool rollback_checked) {
202  if (rollback_checked && rollback_available_) {
203      chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->Rollback();
204  } else {
205    if (rollback_checked)
206      NOTREACHED() <<
207          "Rollback was checked but not available. Starting powerwash.";
208    chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
209        StartDeviceWipe();
210  }
211}
212
213void ResetScreenHandler::HandleOnLearnMore() {
214  if (!help_app_.get())
215    help_app_ = new HelpAppLauncher(GetNativeWindow());
216  help_app_->ShowHelpTopic(HelpAppLauncher::HELP_POWERWASH);
217}
218
219}  // namespace chromeos
220