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/automation/automation_event_observers.h"
6#include "chrome/browser/browser_process.h"
7#include "chrome/browser/chromeos/login/existing_user_controller.h"
8
9using chromeos::ExistingUserController;
10
11LoginEventObserver::LoginEventObserver(
12    AutomationEventQueue* event_queue,
13    AutomationProvider* automation)
14    : AutomationEventObserver(event_queue, false),
15      automation_(automation->AsWeakPtr()) {
16  ExistingUserController* controller =
17      ExistingUserController::current_controller();
18  DCHECK(controller);
19  controller->set_login_status_consumer(this);
20}
21
22LoginEventObserver::~LoginEventObserver() {}
23
24void LoginEventObserver::OnLoginFailure(const chromeos::LoginFailure& error) {
25  VLOG(1) << "Login failed, error=" << error.GetErrorString();
26  _NotifyLoginEvent(error.GetErrorString());
27}
28
29void LoginEventObserver::OnLoginSuccess(
30    const chromeos::UserContext& user_context) {
31  // Profile changes after login. Ensure AutomationProvider refers to
32  // the correct one.
33  if (automation_) {
34    automation_->set_profile(
35        g_browser_process->profile_manager()->GetLastUsedProfile());
36  }
37  VLOG(1) << "Successfully logged in.";
38  _NotifyLoginEvent(std::string());
39}
40
41void LoginEventObserver::_NotifyLoginEvent(const std::string& error_string) {
42  DictionaryValue* dict = new DictionaryValue;
43  dict->SetString("type", "login_event");
44  if (error_string.length())
45    dict->SetString("error_string", error_string);
46  NotifyEvent(dict);
47  ExistingUserController* controller =
48      ExistingUserController::current_controller();
49  if (controller)
50    controller->set_login_status_consumer(NULL);
51  RemoveIfDone();
52}
53