Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / signin / core / browser / account_service_flag_fetcher.cc
blob4159ee1fee6c0f6e81f2bc26ec03c8de5c109fbd
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.
5 #include "components/signin/core/browser/account_service_flag_fetcher.h"
7 #include "base/strings/string_split.h"
8 #include "components/signin/core/browser/profile_oauth2_token_service.h"
9 #include "google_apis/gaia/gaia_constants.h"
11 AccountServiceFlagFetcher::AccountServiceFlagFetcher(
12 const std::string& account_id,
13 ProfileOAuth2TokenService* token_service,
14 net::URLRequestContextGetter* request_context,
15 const ResultCallback& callback)
16 : OAuth2TokenService::Consumer("account_service_flag_fetcher"),
17 account_id_(account_id),
18 token_service_(token_service),
19 gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, request_context),
20 callback_(callback) {
21 Start();
24 AccountServiceFlagFetcher::~AccountServiceFlagFetcher() {
25 // Ensures PO2TS observation is cleared when AccountServiceFlagFetcher is
26 // destructed before refresh token is available.
27 token_service_->RemoveObserver(this);
29 gaia_auth_fetcher_.CancelRequest();
32 void AccountServiceFlagFetcher::Start() {
33 if (token_service_->RefreshTokenIsAvailable(account_id_)) {
34 StartFetchingOAuth2AccessToken();
35 } else {
36 // Wait until we get a refresh token.
37 token_service_->AddObserver(this);
41 void AccountServiceFlagFetcher::OnRefreshTokenAvailable(
42 const std::string& account_id) {
43 // Wait until we get a refresh token for the requested account.
44 if (account_id != account_id_)
45 return;
47 token_service_->RemoveObserver(this);
49 StartFetchingOAuth2AccessToken();
52 void AccountServiceFlagFetcher::OnRefreshTokensLoaded() {
53 token_service_->RemoveObserver(this);
55 // The PO2TS has loaded all tokens, but we didn't get one for the account we
56 // want. We probably won't get one any time soon, so report an error.
57 DLOG(WARNING) << "AccountServiceFlagFetcher::OnRefreshTokensLoaded: "
58 << "Did not get a refresh token for account " << account_id_;
59 callback_.Run(TOKEN_ERROR, std::vector<std::string>());
62 void AccountServiceFlagFetcher::StartFetchingOAuth2AccessToken() {
63 OAuth2TokenService::ScopeSet scopes;
64 scopes.insert(GaiaConstants::kOAuth1LoginScope);
65 oauth2_access_token_request_ = token_service_->StartRequest(
66 account_id_, scopes, this);
69 void AccountServiceFlagFetcher::OnGetTokenSuccess(
70 const OAuth2TokenService::Request* request,
71 const std::string& access_token,
72 const base::Time& expiration_time) {
73 DCHECK_EQ(oauth2_access_token_request_.get(), request);
74 oauth2_access_token_request_.reset();
76 gaia_auth_fetcher_.StartOAuthLogin(access_token, GaiaConstants::kGaiaService);
79 void AccountServiceFlagFetcher::OnGetTokenFailure(
80 const OAuth2TokenService::Request* request,
81 const GoogleServiceAuthError& error) {
82 DCHECK_EQ(oauth2_access_token_request_.get(), request);
83 oauth2_access_token_request_.reset();
85 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetTokenFailure: "
86 << error.ToString();
87 callback_.Run(TOKEN_ERROR, std::vector<std::string>());
90 void AccountServiceFlagFetcher::OnClientLoginSuccess(
91 const ClientLoginResult& result) {
92 gaia_auth_fetcher_.StartGetUserInfo(result.lsid);
95 void AccountServiceFlagFetcher::OnClientLoginFailure(
96 const GoogleServiceAuthError& error) {
97 DLOG(WARNING) << "AccountServiceFlagFetcher::OnClientLoginFailure: "
98 << error.ToString();
99 callback_.Run(SERVICE_ERROR, std::vector<std::string>());
102 void AccountServiceFlagFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) {
103 ResultCode result = SERVICE_ERROR;
104 std::vector<std::string> services;
105 UserInfoMap::const_iterator services_iter = data.find("allServices");
106 if (services_iter != data.end()) {
107 result = SUCCESS;
108 base::SplitString(services_iter->second, ',', &services);
109 } else {
110 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoSuccess: "
111 << "GetUserInfo response didn't include allServices field.";
113 callback_.Run(result, services);
116 void AccountServiceFlagFetcher::OnGetUserInfoFailure(
117 const GoogleServiceAuthError& error) {
118 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoFailure: "
119 << error.ToString();
120 callback_.Run(SERVICE_ERROR, std::vector<std::string>());