signin_oauth_helper.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 "components/signin/core/browser/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
11SigninOAuthHelper::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
22SigninOAuthHelper::~SigninOAuthHelper() {}
23
24void SigninOAuthHelper::OnClientOAuthSuccess(const ClientOAuthResult& result) {
25  refresh_token_ = result.refresh_token;
26  gaia_auth_fetcher_.StartOAuthLogin(result.access_token,
27                                     GaiaConstants::kGaiaService);
28}
29
30void SigninOAuthHelper::OnClientOAuthFailure(
31    const GoogleServiceAuthError& error) {
32  VLOG(1) << "SigninOAuthHelper::OnClientOAuthFailure: " << error.ToString();
33  consumer_->OnSigninOAuthInformationFailure(error);
34}
35
36void SigninOAuthHelper::OnClientLoginSuccess(const ClientLoginResult& result) {
37  gaia_auth_fetcher_.StartGetUserInfo(result.lsid);
38}
39
40void SigninOAuthHelper::OnClientLoginFailure(
41    const GoogleServiceAuthError& error) {
42  VLOG(1) << "SigninOAuthHelper::OnClientLoginFailure: " << error.ToString();
43  consumer_->OnSigninOAuthInformationFailure(error);
44}
45
46void 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(
60        email_iter->second, display_email_iter->second, refresh_token_);
61  }
62}
63
64void SigninOAuthHelper::OnGetUserInfoFailure(
65    const GoogleServiceAuthError& error) {
66  VLOG(1) << "SigninOAuthHelper::OnGetUserInfoFailure : " << error.ToString();
67  consumer_->OnSigninOAuthInformationFailure(error);
68}
69