OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/signin/token_handle_fetcher.h" | 5 #include "chrome/browser/chromeos/login/signin/token_handle_fetcher.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" | 8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" |
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 35 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
36 } | 36 } |
37 ~ShutdownNotifierFactory() override {} | 37 ~ShutdownNotifierFactory() override {} |
38 | 38 |
39 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); | 39 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); |
40 }; | 40 }; |
41 | 41 |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, | 44 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, |
45 const user_manager::UserID& user_id) | 45 const AccountId& account_id) |
46 : OAuth2TokenService::Consumer("user_session_manager"), | 46 : OAuth2TokenService::Consumer("user_session_manager"), |
47 token_handle_util_(util), | 47 token_handle_util_(util), |
48 user_id_(user_id), | 48 account_id_(account_id) {} |
49 token_service_(nullptr), | |
50 waiting_for_refresh_token_(false), | |
51 profile_(nullptr), | |
52 tokeninfo_response_start_time_(base::TimeTicks()) { | |
53 } | |
54 | 49 |
55 TokenHandleFetcher::~TokenHandleFetcher() { | 50 TokenHandleFetcher::~TokenHandleFetcher() { |
56 if (waiting_for_refresh_token_) | 51 if (waiting_for_refresh_token_) |
57 token_service_->RemoveObserver(this); | 52 token_service_->RemoveObserver(this); |
58 } | 53 } |
59 | 54 |
60 void TokenHandleFetcher::BackfillToken(Profile* profile, | 55 void TokenHandleFetcher::BackfillToken(Profile* profile, |
61 const TokenFetchingCallback& callback) { | 56 const TokenFetchingCallback& callback) { |
62 profile_ = profile; | 57 profile_ = profile; |
63 callback_ = callback; | 58 callback_ = callback; |
64 | 59 |
65 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 60 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
66 SigninManagerBase* signin_manager = | 61 SigninManagerBase* signin_manager = |
67 SigninManagerFactory::GetForProfile(profile); | 62 SigninManagerFactory::GetForProfile(profile); |
68 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | 63 const std::string user_email = signin_manager->GetAuthenticatedAccountId(); |
69 if (!token_service_->RefreshTokenIsAvailable(account_id)) { | 64 if (!token_service_->RefreshTokenIsAvailable(user_email)) { |
70 account_without_token_ = account_id; | 65 account_without_token_ = user_email; |
71 profile_shutdown_notification_ = | 66 profile_shutdown_notification_ = |
72 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( | 67 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
73 base::Bind(&TokenHandleFetcher::OnProfileDestroyed, | 68 base::Bind(&TokenHandleFetcher::OnProfileDestroyed, |
74 base::Unretained(this))); | 69 base::Unretained(this))); |
75 | 70 |
76 token_service_->AddObserver(this); | 71 token_service_->AddObserver(this); |
77 waiting_for_refresh_token_ = true; | 72 waiting_for_refresh_token_ = true; |
78 return; | 73 return; |
79 } | 74 } |
80 RequestAccessToken(account_id); | 75 RequestAccessToken(user_email); |
81 } | 76 } |
82 | 77 |
83 void TokenHandleFetcher::OnRefreshTokenAvailable( | 78 void TokenHandleFetcher::OnRefreshTokenAvailable( |
84 const std::string& account_id) { | 79 const std::string& user_email) { |
85 if (account_without_token_ != account_id) | 80 if (account_without_token_ != user_email) |
86 return; | 81 return; |
87 waiting_for_refresh_token_ = false; | 82 waiting_for_refresh_token_ = false; |
88 token_service_->RemoveObserver(this); | 83 token_service_->RemoveObserver(this); |
89 RequestAccessToken(account_id); | 84 RequestAccessToken(user_email); |
90 } | 85 } |
91 | 86 |
92 void TokenHandleFetcher::RequestAccessToken(const std::string& account_id) { | 87 void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) { |
93 OAuth2TokenService::ScopeSet scopes; | 88 OAuth2TokenService::ScopeSet scopes; |
94 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 89 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
95 oauth2_access_token_request_ = | 90 oauth2_access_token_request_ = |
96 token_service_->StartRequest(account_id, scopes, this); | 91 token_service_->StartRequest(user_email, scopes, this); |
97 } | 92 } |
98 | 93 |
99 void TokenHandleFetcher::OnGetTokenSuccess( | 94 void TokenHandleFetcher::OnGetTokenSuccess( |
100 const OAuth2TokenService::Request* request, | 95 const OAuth2TokenService::Request* request, |
101 const std::string& access_token, | 96 const std::string& access_token, |
102 const base::Time& expiration_time) { | 97 const base::Time& expiration_time) { |
103 oauth2_access_token_request_.reset(); | 98 oauth2_access_token_request_.reset(); |
104 FillForAccessToken(access_token); | 99 FillForAccessToken(access_token); |
105 } | 100 } |
106 | 101 |
107 void TokenHandleFetcher::OnGetTokenFailure( | 102 void TokenHandleFetcher::OnGetTokenFailure( |
108 const OAuth2TokenService::Request* request, | 103 const OAuth2TokenService::Request* request, |
109 const GoogleServiceAuthError& error) { | 104 const GoogleServiceAuthError& error) { |
110 oauth2_access_token_request_.reset(); | 105 oauth2_access_token_request_.reset(); |
111 LOG(ERROR) << "Could not get access token to backfill token handler" | 106 LOG(ERROR) << "Could not get access token to backfill token handler" |
112 << error.ToString(); | 107 << error.ToString(); |
113 callback_.Run(user_id_, false); | 108 callback_.Run(account_id_, false); |
114 } | 109 } |
115 | 110 |
116 void TokenHandleFetcher::FillForNewUser(const std::string& access_token, | 111 void TokenHandleFetcher::FillForNewUser(const std::string& access_token, |
117 const TokenFetchingCallback& callback) { | 112 const TokenFetchingCallback& callback) { |
118 profile_ = chromeos::ProfileHelper::Get()->GetSigninProfile(); | 113 profile_ = chromeos::ProfileHelper::Get()->GetSigninProfile(); |
119 callback_ = callback; | 114 callback_ = callback; |
120 FillForAccessToken(access_token); | 115 FillForAccessToken(access_token); |
121 } | 116 } |
122 | 117 |
123 void TokenHandleFetcher::FillForAccessToken(const std::string& access_token) { | 118 void TokenHandleFetcher::FillForAccessToken(const std::string& access_token) { |
124 if (!gaia_client_.get()) | 119 if (!gaia_client_.get()) |
125 gaia_client_.reset( | 120 gaia_client_.reset( |
126 new gaia::GaiaOAuthClient(profile_->GetRequestContext())); | 121 new gaia::GaiaOAuthClient(profile_->GetRequestContext())); |
127 tokeninfo_response_start_time_ = base::TimeTicks::Now(); | 122 tokeninfo_response_start_time_ = base::TimeTicks::Now(); |
128 gaia_client_->GetTokenInfo(access_token, kMaxRetries, this); | 123 gaia_client_->GetTokenInfo(access_token, kMaxRetries, this); |
129 } | 124 } |
130 | 125 |
131 void TokenHandleFetcher::OnOAuthError() { | 126 void TokenHandleFetcher::OnOAuthError() { |
132 callback_.Run(user_id_, false); | 127 callback_.Run(account_id_, false); |
133 } | 128 } |
134 | 129 |
135 void TokenHandleFetcher::OnNetworkError(int response_code) { | 130 void TokenHandleFetcher::OnNetworkError(int response_code) { |
136 callback_.Run(user_id_, false); | 131 callback_.Run(account_id_, false); |
137 } | 132 } |
138 | 133 |
139 void TokenHandleFetcher::OnGetTokenInfoResponse( | 134 void TokenHandleFetcher::OnGetTokenInfoResponse( |
140 scoped_ptr<base::DictionaryValue> token_info) { | 135 scoped_ptr<base::DictionaryValue> token_info) { |
141 bool success = false; | 136 bool success = false; |
142 if (!token_info->HasKey("error")) { | 137 if (!token_info->HasKey("error")) { |
143 std::string handle; | 138 std::string handle; |
144 if (token_info->GetString("token_handle", &handle)) { | 139 if (token_info->GetString("token_handle", &handle)) { |
145 success = true; | 140 success = true; |
146 token_handle_util_->StoreTokenHandle(user_id_, handle); | 141 token_handle_util_->StoreTokenHandle(account_id_, handle); |
147 } | 142 } |
148 } | 143 } |
149 const base::TimeDelta duration = | 144 const base::TimeDelta duration = |
150 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 145 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
151 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); | 146 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); |
152 callback_.Run(user_id_, success); | 147 callback_.Run(account_id_, success); |
153 } | 148 } |
154 | 149 |
155 void TokenHandleFetcher::OnProfileDestroyed() { | 150 void TokenHandleFetcher::OnProfileDestroyed() { |
156 callback_.Run(user_id_, false); | 151 callback_.Run(account_id_, false); |
157 } | 152 } |
OLD | NEW |