Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_oauth_helper.cc
1 // Copyright 2013 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/signin/signin_oauth_helper.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "google_apis/gaia/gaia_auth_fetcher.h"
9 #include "google_apis/gaia/gaia_constants.h"
10
11 SigninOAuthHelper::SigninOAuthHelper(net::URLRequestContextGetter* getter,
12                                      const std::string& session_index,
13                                      Consumer* consumer)
14     : gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, getter),
15       consumer_(consumer) {
16   DCHECK(consumer_);
17   DCHECK(getter);
18   DCHECK(!session_index.empty());
19   gaia_auth_fetcher_.StartCookieForOAuthLoginTokenExchange(session_index);
20 }
21
22 SigninOAuthHelper::~SigninOAuthHelper() {}
23
24 void SigninOAuthHelper::OnClientOAuthSuccess(const ClientOAuthResult& result) {
25   refresh_token_ = result.refresh_token;
26   gaia_auth_fetcher_.StartOAuthLogin(result.access_token,
27                                      GaiaConstants::kGaiaService);
28 }
29
30 void SigninOAuthHelper::OnClientOAuthFailure(
31       const GoogleServiceAuthError& error) {
32   VLOG(1) << "SigninOAuthHelper::OnClientOAuthFailure: " << error.ToString();
33   consumer_->OnSigninOAuthInformationFailure(error);
34 }
35
36 void SigninOAuthHelper::OnClientLoginSuccess(const ClientLoginResult& result) {
37   gaia_auth_fetcher_.StartGetUserInfo(result.lsid);
38 }
39
40 void SigninOAuthHelper::OnClientLoginFailure(
41     const GoogleServiceAuthError& error) {
42   VLOG(1) << "SigninOAuthHelper::OnClientLoginFailure: " << error.ToString();
43   consumer_->OnSigninOAuthInformationFailure(error);
44 }
45
46 void SigninOAuthHelper::OnGetUserInfoSuccess(const UserInfoMap& data) {
47   UserInfoMap::const_iterator email_iter = data.find("email");
48   UserInfoMap::const_iterator display_email_iter = data.find("displayEmail");
49   if (email_iter == data.end() || display_email_iter == data.end()) {
50     VLOG(1) << "SigninOAuthHelper::OnGetUserInfoSuccess: no email found:"
51             << " email=" << email_iter->second
52             << " displayEmail=" << display_email_iter->second;
53     consumer_->OnSigninOAuthInformationFailure(
54         GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
55   } else {
56     VLOG(1) << "SigninOAuthHelper::OnGetUserInfoSuccess:"
57             << " email=" << email_iter->second
58             << " displayEmail=" << display_email_iter->second;
59     consumer_->OnSigninOAuthInformationAvailable(email_iter->second,
60                                                  display_email_iter->second,
61                                                  refresh_token_);
62   }
63 }
64
65 void SigninOAuthHelper::OnGetUserInfoFailure(
66     const GoogleServiceAuthError& error) {
67   VLOG(1) << "SigninOAuthHelper::OnGetUserInfoFailure : " << error.ToString();
68   consumer_->OnSigninOAuthInformationFailure(error);
69 }