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.
5 #include "chrome/browser/automation/automation_provider_observers.h"
7 #include "base/values.h"
8 #include "chrome/browser/automation/automation_provider.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/login/authentication_notification_details.h"
11 #include "chrome/browser/chromeos/login/enrollment/enrollment_screen_actor.h"
12 #include "chrome/browser/chromeos/login/existing_user_controller.h"
13 #include "chrome/browser/chromeos/login/screen_locker.h"
14 #include "chrome/browser/chromeos/login/screens/wizard_screen.h"
15 #include "chrome/browser/chromeos/login/user_image.h"
16 #include "chrome/browser/chromeos/login/user_image_manager.h"
17 #include "chrome/browser/chromeos/login/user_manager.h"
18 #include "chrome/browser/chromeos/login/wizard_controller.h"
19 #include "content/public/browser/notification_service.h"
21 using chromeos::WizardController
;
25 // Fake screen name for the user session (reported by WizardControllerObserver).
26 const char kSessionScreenName
[] = "session";
30 OOBEWebuiReadyObserver::OOBEWebuiReadyObserver(AutomationProvider
* automation
)
31 : automation_(automation
->AsWeakPtr()) {
32 if (WizardController::default_controller() &&
33 WizardController::default_controller()->current_screen()) {
36 registrar_
.Add(this, chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
37 content::NotificationService::AllSources());
41 void OOBEWebuiReadyObserver::Observe(
43 const content::NotificationSource
& source
,
44 const content::NotificationDetails
& details
) {
45 DCHECK(type
== chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
);
49 void OOBEWebuiReadyObserver::OOBEWebuiReady() {
51 automation_
->OnOOBEWebuiReady();
55 LoginObserver::LoginObserver(chromeos::ExistingUserController
* controller
,
56 AutomationProvider
* automation
,
57 IPC::Message
* reply_message
)
58 : controller_(controller
),
59 automation_(automation
->AsWeakPtr()),
60 reply_message_(reply_message
) {
61 controller_
->set_login_status_consumer(this);
64 LoginObserver::~LoginObserver() {
65 controller_
->set_login_status_consumer(NULL
);
68 void LoginObserver::OnLoginFailure(const chromeos::LoginFailure
& error
) {
69 scoped_ptr
<base::DictionaryValue
> return_value(new base::DictionaryValue
);
70 return_value
->SetString("error_string", error
.GetErrorString());
71 AutomationJSONReply(automation_
.get(), reply_message_
.release())
72 .SendSuccess(return_value
.get());
76 void LoginObserver::OnLoginSuccess(const chromeos::UserContext
& user_context
) {
77 controller_
->set_login_status_consumer(NULL
);
78 AutomationJSONReply(automation_
.get(), reply_message_
.release())
83 WizardControllerObserver::WizardControllerObserver(
84 WizardController
* wizard_controller
,
85 AutomationProvider
* automation
,
86 IPC::Message
* reply_message
)
87 : wizard_controller_(wizard_controller
),
88 automation_(automation
->AsWeakPtr()),
89 reply_message_(reply_message
) {
90 wizard_controller_
->AddObserver(this);
91 registrar_
.Add(this, chrome::NOTIFICATION_LOGIN_WEBUI_LOADED
,
92 content::NotificationService::AllSources());
95 WizardControllerObserver::~WizardControllerObserver() {
96 wizard_controller_
->RemoveObserver(this);
99 void WizardControllerObserver::OnScreenChanged(
100 chromeos::WizardScreen
* next_screen
) {
101 std::string screen_name
= next_screen
->GetName();
102 if (screen_to_wait_for_
.empty() || screen_to_wait_for_
== screen_name
) {
103 SendReply(screen_name
);
105 DVLOG(2) << "Still waiting for " << screen_to_wait_for_
;
109 void WizardControllerObserver::OnSessionStart() {
110 SendReply(kSessionScreenName
);
113 void WizardControllerObserver::Observe(
115 const content::NotificationSource
& source
,
116 const content::NotificationDetails
& details
) {
117 DCHECK(type
== chrome::NOTIFICATION_LOGIN_WEBUI_LOADED
);
118 SendReply(WizardController::kLoginScreenName
);
121 void WizardControllerObserver::SendReply(const std::string
& screen_name
) {
122 scoped_ptr
<base::DictionaryValue
> return_value(new base::DictionaryValue
);
123 return_value
->SetString("next_screen", screen_name
);
124 AutomationJSONReply(automation_
.get(), reply_message_
.release())
125 .SendSuccess(return_value
.get());
129 ScreenLockUnlockObserver::ScreenLockUnlockObserver(
130 AutomationProvider
* automation
,
131 IPC::Message
* reply_message
,
133 : automation_(automation
->AsWeakPtr()),
134 reply_message_(reply_message
),
135 lock_screen_(lock_screen
) {
136 registrar_
.Add(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
137 content::NotificationService::AllSources());
140 ScreenLockUnlockObserver::~ScreenLockUnlockObserver() {}
142 void ScreenLockUnlockObserver::Observe(
144 const content::NotificationSource
& source
,
145 const content::NotificationDetails
& details
) {
146 DCHECK(type
== chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
);
148 AutomationJSONReply
reply(automation_
.get(), reply_message_
.release());
149 bool is_screen_locked
= *content::Details
<bool>(details
).ptr();
150 if (lock_screen_
== is_screen_locked
)
151 reply
.SendSuccess(NULL
);
153 reply
.SendError("Screen lock failure.");
158 ScreenUnlockObserver::ScreenUnlockObserver(AutomationProvider
* automation
,
159 IPC::Message
* reply_message
)
160 : ScreenLockUnlockObserver(automation
, reply_message
, false) {
161 chromeos::ScreenLocker::default_screen_locker()->SetLoginStatusConsumer(this);
164 ScreenUnlockObserver::~ScreenUnlockObserver() {
165 chromeos::ScreenLocker
* screen_locker
=
166 chromeos::ScreenLocker::default_screen_locker();
168 screen_locker
->SetLoginStatusConsumer(NULL
);
171 void ScreenUnlockObserver::OnLoginFailure(const chromeos::LoginFailure
& error
) {
173 scoped_ptr
<base::DictionaryValue
> return_value(new base::DictionaryValue
);
174 return_value
->SetString("error_string", error
.GetErrorString());
175 AutomationJSONReply(automation_
.get(), reply_message_
.release())
176 .SendSuccess(return_value
.get());