- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / online_attempt_host.cc
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/chromeos/login/online_attempt_host.h"
6
7 #include "base/bind.h"
8 #include "base/sha1.h"
9 #include "chrome/browser/chromeos/login/auth_attempt_state.h"
10 #include "chrome/browser/chromeos/login/online_attempt.h"
11 #include "chrome/browser/chromeos/login/user.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "google_apis/gaia/gaia_auth_util.h"
15
16 namespace chromeos {
17
18 OnlineAttemptHost::OnlineAttemptHost(Delegate* delegate)
19     : delegate_(delegate), weak_ptr_factory_(this) {}
20
21 OnlineAttemptHost::~OnlineAttemptHost() {
22   Reset();
23 }
24
25 void OnlineAttemptHost::Check(Profile* profile,
26                               const UserContext& user_context) {
27   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
28   std::string attempt_hash = base::SHA1HashString(
29       user_context.username + "\n" + user_context.password);
30   if (attempt_hash != current_attempt_hash_) {
31     Reset();
32     current_attempt_hash_ = attempt_hash;
33     current_username_ = user_context.username;
34
35     state_.reset(
36         new AuthAttemptState(
37             UserContext(gaia::CanonicalizeEmail(user_context.username),
38                         user_context.password,
39                         user_context.auth_code),
40             std::string(),  // login_token
41             std::string(),  // login_captcha
42             User::USER_TYPE_REGULAR,
43             false));  // user_is_new.
44     online_attempt_.reset(new OnlineAttempt(state_.get(),
45                                             this));
46     online_attempt_->Initiate(profile);
47   }
48 }
49
50 void OnlineAttemptHost::Reset() {
51   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
52   online_attempt_.reset(NULL);
53   current_attempt_hash_.clear();
54   current_username_.clear();
55 }
56
57 void OnlineAttemptHost::Resolve() {
58   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
59   if (state_->online_complete()) {
60     bool success = state_->online_outcome().reason() == LoginFailure::NONE;
61     content::BrowserThread::PostTask(
62         content::BrowserThread::UI,
63         FROM_HERE,
64         base::Bind(&OnlineAttemptHost::ResolveOnUIThread,
65                    weak_ptr_factory_.GetWeakPtr(),
66                    success));
67   }
68 }
69
70 void OnlineAttemptHost::ResolveOnUIThread(bool success) {
71   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
72   delegate_->OnChecked(current_username_, success);
73   Reset();
74 }
75
76 }  // namespace chromeos