1// Copyright 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.
4
5#include "chromeos/login/auth/online_attempt_host.h"
6
7#include "base/bind.h"
8#include "base/location.h"
9#include "base/message_loop/message_loop_proxy.h"
10#include "chromeos/login/auth/auth_attempt_state.h"
11#include "chromeos/login/auth/online_attempt.h"
12#include "chromeos/login/auth/user_context.h"
13#include "components/user_manager/user_type.h"
14
15namespace chromeos {
16
17OnlineAttemptHost::OnlineAttemptHost(Delegate* delegate)
18    : message_loop_(base::MessageLoopProxy::current()),
19      delegate_(delegate),
20      weak_ptr_factory_(this) {
21}
22
23OnlineAttemptHost::~OnlineAttemptHost() {
24  Reset();
25}
26
27void OnlineAttemptHost::Check(net::URLRequestContextGetter* request_context,
28                              const UserContext& user_context) {
29  if (user_context != current_attempt_user_context_) {
30    Reset();
31    current_attempt_user_context_ = user_context;
32
33    state_.reset(new AuthAttemptState(user_context,
34                                      user_manager::USER_TYPE_REGULAR,
35                                      false,    // unlock
36                                      false,    // online_complete
37                                      false));  // user_is_new
38    online_attempt_.reset(new OnlineAttempt(state_.get(), this));
39    online_attempt_->Initiate(request_context);
40  }
41}
42
43void OnlineAttemptHost::Reset() {
44  online_attempt_.reset(NULL);
45  current_attempt_user_context_ = UserContext();
46}
47
48void OnlineAttemptHost::Resolve() {
49  if (state_->online_complete()) {
50    bool success = state_->online_outcome().reason() == AuthFailure::NONE;
51    message_loop_->PostTask(FROM_HERE,
52                            base::Bind(&OnlineAttemptHost::ResolveOnUIThread,
53                                       weak_ptr_factory_.GetWeakPtr(),
54                                       success));
55  }
56}
57
58void OnlineAttemptHost::ResolveOnUIThread(bool success) {
59  delegate_->OnChecked(current_attempt_user_context_.GetUserID(), success);
60  Reset();
61}
62
63}  // namespace chromeos
64