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    bool pending_requests,
32    bool using_oauth) {
33  // Profile changes after login. Ensure AutomationProvider refers to
34  // the correct one.
35  if (automation_) {
36    automation_->set_profile(
37        g_browser_process->profile_manager()->GetLastUsedProfile());
38  }
39  VLOG(1) << "Successfully logged in.";
40  _NotifyLoginEvent(std::string());
41}
42
43void LoginEventObserver::_NotifyLoginEvent(const std::string& error_string) {
44  DictionaryValue* dict = new DictionaryValue;
45  dict->SetString("type", "login_event");
46  if (error_string.length())
47    dict->SetString("error_string", error_string);
48  NotifyEvent(dict);
49  ExistingUserController* controller =
50      ExistingUserController::current_controller();
51  if (controller)
52    controller->set_login_status_consumer(NULL);
53  RemoveIfDone();
54}
55