LocalizedValuesBuilder moved to components/login.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / enable_debugging_screen_handler.cc
blob341f71abac81435151143bca82d5cc4c61e170ae
1 // Copyright (c) 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/chromeos/login/enable_debugging_screen_handler.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/prefs/pref_registry_simple.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/ui/login_web_dialog.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/chromium_strings.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "chromeos/chromeos_switches.h"
21 #include "chromeos/dbus/cryptohome_client.h"
22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "chromeos/dbus/debug_daemon_client.h"
24 #include "chromeos/dbus/power_manager_client.h"
25 #include "components/login/localized_values_builder.h"
26 #include "ui/base/l10n/l10n_util.h"
28 namespace {
30 const char kJsScreenPath[] = "login.EnableDebuggingScreen";
32 const char kEnableDebuggingScreen[] = "debugging";
34 } // namespace
36 namespace chromeos {
38 EnableDebuggingScreenHandler::EnableDebuggingScreenHandler()
39 : BaseScreenHandler(kJsScreenPath),
40 delegate_(NULL),
41 show_on_init_(false),
42 weak_ptr_factory_(this) {
45 EnableDebuggingScreenHandler::~EnableDebuggingScreenHandler() {
46 if (delegate_)
47 delegate_->OnActorDestroyed(this);
50 void EnableDebuggingScreenHandler::PrepareToShow() {
53 void EnableDebuggingScreenHandler::ShowWithParams() {
54 ShowScreen(kEnableDebuggingScreen, NULL);
56 UpdateUIState(UI_STATE_WAIT);
58 DVLOG(1) << "Showing enable debugging screen.";
60 // Wait for cryptohomed before checking debugd. See http://crbug.com/440506.
61 chromeos::CryptohomeClient* client =
62 chromeos::DBusThreadManager::Get()->GetCryptohomeClient();
63 client->WaitForServiceToBeAvailable(base::Bind(
64 &EnableDebuggingScreenHandler::OnCryptohomeDaemonAvailabilityChecked,
65 weak_ptr_factory_.GetWeakPtr()));
68 void EnableDebuggingScreenHandler::Show() {
69 if (!page_is_ready()) {
70 show_on_init_ = true;
71 return;
74 ShowWithParams();
77 void EnableDebuggingScreenHandler::Hide() {
78 weak_ptr_factory_.InvalidateWeakPtrs();
81 void EnableDebuggingScreenHandler::SetDelegate(Delegate* delegate) {
82 delegate_ = delegate;
83 if (page_is_ready())
84 Initialize();
87 void EnableDebuggingScreenHandler::DeclareLocalizedValues(
88 ::login::LocalizedValuesBuilder* builder) {
89 builder->Add("enableDebuggingScreenTitle",
90 IDS_ENABLE_DEBUGGING_SCREEN_TITLE);
91 builder->Add("enableDebuggingScreenAccessibleTitle",
92 IDS_ENABLE_DEBUGGING_SCREEN_TITLE);
93 builder->Add("enableDebuggingCancelButton", IDS_CANCEL);
94 builder->Add("enableDebuggingOKButton", IDS_OK);
95 builder->Add("enableDebuggingRemoveButton",
96 IDS_ENABLE_DEBUGGING_REMOVE_ROOTFS_BUTTON);
97 builder->Add("enableDebuggingEnableButton",
98 IDS_ENABLE_DEBUGGING_ENABLE_BUTTON);
99 builder->Add("enableDebuggingRemveRootfsMessage",
100 IDS_ENABLE_DEBUGGING_SCREEN_ROOTFS_REMOVE_MSG);
101 builder->Add("enableDebuggingLearnMore",
102 IDS_ENABLE_DEBUGGING_LEARN_MORE);
103 builder->Add("enableDebuggingSetupMessage",
104 IDS_ENABLE_DEBUGGING_SETUP_MESSAGE);
105 builder->Add("enableDebuggingWaitMessage",
106 IDS_ENABLE_DEBUGGING_WAIT_MESSAGE);
107 builder->AddF("enableDebuggingWarningTitle",
108 IDS_ENABLE_DEBUGGING_SCREEN_WARNING_MSG,
109 IDS_SHORT_PRODUCT_NAME);
110 builder->AddF("enableDebuggingDoneMessage",
111 IDS_ENABLE_DEBUGGING_DONE_MESSAGE,
112 IDS_SHORT_PRODUCT_NAME);
113 builder->Add("enableDebuggingErrorTitle",
114 IDS_ENABLE_DEBUGGING_ERROR_TITLE);
115 builder->AddF("enableDebuggingErrorMessage",
116 IDS_ENABLE_DEBUGGING_ERROR_MESSAGE,
117 IDS_SHORT_PRODUCT_NAME);
118 builder->Add("enableDebuggingPasswordLabel",
119 IDS_ENABLE_DEBUGGING_ROOT_PASSWORD_LABEL);
120 builder->Add("enableDebuggingConfirmPasswordLabel",
121 IDS_ENABLE_DEBUGGING_CONFIRM_PASSWORD_LABEL);
122 builder->Add("enableDebuggingPasswordLengthNote",
123 IDS_ENABLE_DEBUGGING_EMPTY_ROOT_PASSWORD_LABEL);
126 // static
127 void EnableDebuggingScreenHandler::RegisterPrefs(PrefRegistrySimple* registry) {
128 registry->RegisterBooleanPref(prefs::kDebuggingFeaturesRequested, false);
131 void EnableDebuggingScreenHandler::Initialize() {
132 if (!page_is_ready() || !delegate_)
133 return;
135 if (show_on_init_) {
136 Show();
137 show_on_init_ = false;
141 void EnableDebuggingScreenHandler::RegisterMessages() {
142 AddCallback("enableDebuggingOnCancel",
143 &EnableDebuggingScreenHandler::HandleOnCancel);
144 AddCallback("enableDebuggingOnDone",
145 &EnableDebuggingScreenHandler::HandleOnDone);
146 AddCallback("enableDebuggingOnLearnMore",
147 &EnableDebuggingScreenHandler::HandleOnLearnMore);
148 AddCallback("enableDebuggingOnRemoveRootFSProtection",
149 &EnableDebuggingScreenHandler::HandleOnRemoveRootFSProtection);
150 AddCallback("enableDebuggingOnSetup",
151 &EnableDebuggingScreenHandler::HandleOnSetup);
154 void EnableDebuggingScreenHandler::HandleOnCancel() {
155 if (delegate_)
156 delegate_->OnExit(false);
159 void EnableDebuggingScreenHandler::HandleOnDone() {
160 if (delegate_)
161 delegate_->OnExit(true);
164 void EnableDebuggingScreenHandler::HandleOnRemoveRootFSProtection() {
165 UpdateUIState(UI_STATE_WAIT);
166 chromeos::DebugDaemonClient* client =
167 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
168 client->RemoveRootfsVerification(
169 base::Bind(&EnableDebuggingScreenHandler::OnRemoveRootfsVerification,
170 weak_ptr_factory_.GetWeakPtr()));
173 void EnableDebuggingScreenHandler::HandleOnSetup(
174 const std::string& password) {
175 UpdateUIState(UI_STATE_WAIT);
176 chromeos::DebugDaemonClient* client =
177 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
178 client->EnableDebuggingFeatures(
179 password,
180 base::Bind(&EnableDebuggingScreenHandler::OnEnableDebuggingFeatures,
181 weak_ptr_factory_.GetWeakPtr()));
184 void EnableDebuggingScreenHandler::OnCryptohomeDaemonAvailabilityChecked(
185 bool service_is_available) {
186 DVLOG(1) << "Enable-debugging-screen: cryptohomed availability="
187 << service_is_available;
188 if (!service_is_available) {
189 LOG(ERROR) << "Crypthomed is not available.";
190 UpdateUIState(UI_STATE_ERROR);
191 return;
194 chromeos::DebugDaemonClient* client =
195 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
196 client->WaitForServiceToBeAvailable(base::Bind(
197 &EnableDebuggingScreenHandler::OnDebugDaemonServiceAvailabilityChecked,
198 weak_ptr_factory_.GetWeakPtr()));
201 void EnableDebuggingScreenHandler::OnDebugDaemonServiceAvailabilityChecked(
202 bool service_is_available) {
203 DVLOG(1) << "Enable-debugging-screen: debugd availability="
204 << service_is_available;
205 if (!service_is_available) {
206 LOG(ERROR) << "Debug daemon is not available.";
207 UpdateUIState(UI_STATE_ERROR);
208 return;
211 // Check the status of debugging features.
212 chromeos::DebugDaemonClient* client =
213 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
214 client->QueryDebuggingFeatures(
215 base::Bind(&EnableDebuggingScreenHandler::OnQueryDebuggingFeatures,
216 weak_ptr_factory_.GetWeakPtr()));
219 // Removes rootfs verification, add flag to start with enable debugging features
220 // screen and reboots the machine.
221 void EnableDebuggingScreenHandler::OnRemoveRootfsVerification(bool success) {
222 if (!success) {
223 UpdateUIState(UI_STATE_ERROR);
224 return;
227 PrefService* prefs = g_browser_process->local_state();
228 prefs->SetBoolean(prefs::kDebuggingFeaturesRequested, true);
229 prefs->CommitPendingWrite();
230 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart();
233 void EnableDebuggingScreenHandler::OnEnableDebuggingFeatures(bool success) {
234 if (!success) {
235 UpdateUIState(UI_STATE_ERROR);
236 return;
239 UpdateUIState(UI_STATE_DONE);
242 void EnableDebuggingScreenHandler::OnQueryDebuggingFeatures(bool success,
243 int features_flag) {
244 DVLOG(1) << "Enable-debugging-screen: OnQueryDebuggingFeatures"
245 << ", success=" << success
246 << ", features=" << features_flag;
247 if (!success || features_flag == DebugDaemonClient::DEV_FEATURES_DISABLED) {
248 UpdateUIState(UI_STATE_ERROR);
249 return;
252 if ((features_flag &
253 DebugDaemonClient::DEV_FEATURE_ROOTFS_VERIFICATION_REMOVED) == 0) {
254 UpdateUIState(UI_STATE_REMOVE_PROTECTION);
255 return;
258 if ((features_flag & DebugDaemonClient::DEV_FEATURE_ALL_ENABLED) !=
259 DebugDaemonClient::DEV_FEATURE_ALL_ENABLED) {
260 UpdateUIState(UI_STATE_SETUP);
261 } else {
262 UpdateUIState(UI_STATE_DONE);
266 void EnableDebuggingScreenHandler::UpdateUIState(
267 EnableDebuggingScreenHandler::UIState state) {
268 if (state == UI_STATE_SETUP ||
269 state == UI_STATE_ERROR ||
270 state == UI_STATE_DONE) {
271 PrefService* prefs = g_browser_process->local_state();
272 prefs->ClearPref(prefs::kDebuggingFeaturesRequested);
273 prefs->CommitPendingWrite();
276 web_ui()->CallJavascriptFunction(
277 "login.EnableDebuggingScreen.updateState",
278 base::FundamentalValue(static_cast<int>(state)));
281 void EnableDebuggingScreenHandler::HandleOnLearnMore() {
282 VLOG(1) << "Trying to view the help article about debugging features.";
283 const std::string help_content =
284 l10n_util::GetStringUTF8(IDS_ENABLE_DEBUGGING_HELP);
285 const GURL data_url = GURL("data:text/html;charset=utf-8," + help_content);
287 LoginWebDialog* dialog = new LoginWebDialog(
288 Profile::FromWebUI(web_ui()),
289 NULL,
290 GetNativeWindow(),
291 base::string16(),
292 data_url);
293 dialog->Show();
296 } // namespace chromeos