1 // Copyright 2015 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.
5 #include "components/signin/core/browser/account_info_fetcher.h"
7 #include "base/trace_event/trace_event.h"
8 #include "components/signin/core/browser/account_fetcher_service.h"
9 #include "google_apis/gaia/gaia_constants.h"
11 AccountInfoFetcher::AccountInfoFetcher(
12 OAuth2TokenService
* token_service
,
13 net::URLRequestContextGetter
* request_context_getter
,
14 AccountFetcherService
* service
,
15 const std::string
& account_id
)
16 : OAuth2TokenService::Consumer("gaia_account_tracker"),
17 token_service_(token_service
),
18 request_context_getter_(request_context_getter
),
20 account_id_(account_id
) {
21 TRACE_EVENT_ASYNC_BEGIN1("AccountFetcherService", "AccountIdFetcher", this,
22 "account_id", account_id
);
25 AccountInfoFetcher::~AccountInfoFetcher() {
26 TRACE_EVENT_ASYNC_END0("AccountFetcherService", "AccountIdFetcher", this);
29 void AccountInfoFetcher::Start() {
30 OAuth2TokenService::ScopeSet scopes
;
31 scopes
.insert(GaiaConstants::kGoogleUserInfoEmail
);
32 scopes
.insert(GaiaConstants::kGoogleUserInfoProfile
);
33 login_token_request_
=
34 token_service_
->StartRequest(account_id_
, scopes
, this);
37 void AccountInfoFetcher::OnGetTokenSuccess(
38 const OAuth2TokenService::Request
* request
,
39 const std::string
& access_token
,
40 const base::Time
& expiration_time
) {
41 TRACE_EVENT_ASYNC_STEP_PAST0("AccountFetcherService", "AccountIdFetcher",
42 this, "OnGetTokenSuccess");
43 DCHECK_EQ(request
, login_token_request_
.get());
45 gaia_oauth_client_
.reset(new gaia::GaiaOAuthClient(request_context_getter_
));
46 const int kMaxRetries
= 3;
47 gaia_oauth_client_
->GetUserInfo(access_token
, kMaxRetries
, this);
50 void AccountInfoFetcher::OnGetTokenFailure(
51 const OAuth2TokenService::Request
* request
,
52 const GoogleServiceAuthError
& error
) {
53 TRACE_EVENT_ASYNC_STEP_PAST1("AccountFetcherService", "AccountIdFetcher",
54 this, "OnGetTokenFailure",
55 "google_service_auth_error", error
.ToString());
56 LOG(ERROR
) << "OnGetTokenFailure: " << error
.ToString();
57 DCHECK_EQ(request
, login_token_request_
.get());
58 service_
->OnUserInfoFetchFailure(account_id_
);
61 void AccountInfoFetcher::OnGetUserInfoResponse(
62 scoped_ptr
<base::DictionaryValue
> user_info
) {
63 TRACE_EVENT_ASYNC_STEP_PAST1("AccountFetcherService", "AccountIdFetcher",
64 this, "OnGetUserInfoResponse", "account_id",
66 service_
->OnUserInfoFetchSuccess(account_id_
, user_info
.Pass());
69 void AccountInfoFetcher::OnOAuthError() {
70 TRACE_EVENT_ASYNC_STEP_PAST0("AccountFetcherService", "AccountIdFetcher",
71 this, "OnOAuthError");
72 LOG(ERROR
) << "OnOAuthError";
73 service_
->OnUserInfoFetchFailure(account_id_
);
76 void AccountInfoFetcher::OnNetworkError(int response_code
) {
77 TRACE_EVENT_ASYNC_STEP_PAST1("AccountFetcherService", "AccountIdFetcher",
78 this, "OnNetworkError", "response_code",
80 LOG(ERROR
) << "OnNetworkError " << response_code
;
81 service_
->OnUserInfoFetchFailure(account_id_
);