Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Side by Side Diff: chrome/browser/signin/dice_response_handler.cc

Issue 2950073002: [Signin] Handle multiple concurrent Dice responses (Closed)
Patch Set: add tests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/signin/dice_response_handler.h" 5 #include "chrome/browser/signin/dice_response_handler.h"
6 6
7 #include "base/logging.h"
7 #include "base/memory/singleton.h" 8 #include "base/memory/singleton.h"
8 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/account_tracker_service_factory.h" 10 #include "chrome/browser/signin/account_tracker_service_factory.h"
10 #include "chrome/browser/signin/chrome_signin_client_factory.h" 11 #include "chrome/browser/signin/chrome_signin_client_factory.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h" 13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
13 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" 14 #include "components/keyed_service/content/browser_context_keyed_service_factory .h"
14 #include "components/signin/core/browser/account_tracker_service.h" 15 #include "components/signin/core/browser/account_tracker_service.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h" 16 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "components/signin/core/browser/signin_client.h" 17 #include "components/signin/core/browser/signin_client.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 Profile* profile = static_cast<Profile*>(context); 58 Profile* profile = static_cast<Profile*>(context);
58 return new DiceResponseHandler( 59 return new DiceResponseHandler(
59 ChromeSigninClientFactory::GetForProfile(profile), 60 ChromeSigninClientFactory::GetForProfile(profile),
60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), 61 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
61 AccountTrackerServiceFactory::GetForProfile(profile)); 62 AccountTrackerServiceFactory::GetForProfile(profile));
62 } 63 }
63 }; 64 };
64 65
65 } // namespace 66 } // namespace
66 67
68 ////////////////////////////////////////////////////////////////////////////////
69 // DiceTokenFetcher
70 ////////////////////////////////////////////////////////////////////////////////
71
72 DiceResponseHandler::DiceTokenFetcher::DiceTokenFetcher(
73 const std::string& gaia_id,
74 const std::string& email,
75 const std::string& authorization_code,
76 SigninClient* signin_client,
77 DiceResponseHandler* dice_response_handler)
78 : gaia_id_(gaia_id),
79 email_(email),
80 dice_response_handler_(dice_response_handler) {
81 gaia_auth_fetcher_ = signin_client->CreateGaiaAuthFetcher(
82 this, GaiaConstants::kChromeSource,
83 signin_client->GetURLRequestContext());
84 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code);
85
86 // TODO(droger): The token exchange must complete quickly or be cancelled. Add
87 // a timeout logic.
88 }
89
90 DiceResponseHandler::DiceTokenFetcher::~DiceTokenFetcher() {}
91
92 void DiceResponseHandler::DiceTokenFetcher::OnClientOAuthSuccess(
93 const GaiaAuthConsumer::ClientOAuthResult& result) {
94 dice_response_handler_->OnTokenExchangeSuccess(this, gaia_id_, email_,
95 result);
96 }
97
98 void DiceResponseHandler::DiceTokenFetcher::OnClientOAuthFailure(
99 const GoogleServiceAuthError& error) {
100 dice_response_handler_->OnTokenExchangeFailure(this, error);
101 }
102
103 ////////////////////////////////////////////////////////////////////////////////
104 // DiceResponseHandler
105 ////////////////////////////////////////////////////////////////////////////////
106
67 // static 107 // static
68 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) { 108 DiceResponseHandler* DiceResponseHandler::GetForProfile(Profile* profile) {
69 return DiceResponseHandlerFactory::GetForProfile(profile); 109 return DiceResponseHandlerFactory::GetForProfile(profile);
70 } 110 }
71 111
72 DiceResponseHandler::DiceResponseHandler( 112 DiceResponseHandler::DiceResponseHandler(
73 SigninClient* signin_client, 113 SigninClient* signin_client,
74 ProfileOAuth2TokenService* profile_oauth2_token_service, 114 ProfileOAuth2TokenService* profile_oauth2_token_service,
75 AccountTrackerService* account_tracker_service) 115 AccountTrackerService* account_tracker_service)
76 : signin_client_(signin_client), 116 : signin_client_(signin_client),
(...skipping 26 matching lines...) Expand all
103 } 143 }
104 144
105 NOTREACHED(); 145 NOTREACHED();
106 return; 146 return;
107 } 147 }
108 148
109 void DiceResponseHandler::ProcessDiceSigninHeader( 149 void DiceResponseHandler::ProcessDiceSigninHeader(
110 const std::string& gaia_id, 150 const std::string& gaia_id,
111 const std::string& email, 151 const std::string& email,
112 const std::string& authorization_code) { 152 const std::string& authorization_code) {
113 DCHECK(!gaia_auth_fetcher_); 153 for (auto it = token_fetchers_.begin(); it != token_fetchers_.end(); ++it) {
114 DCHECK(gaia_id_.empty()); 154 if ((it->get()->gaia_id() == gaia_id) && (it->get()->email() == email))
msarda 2017/06/22 09:51:31 This is a tricky case - in this case the Gaia cook
droger 2017/06/22 11:54:20 Thanks fro bringing that up. Summary of offline d
115 DCHECK(email_.empty()); 155 return; // There is already a request in flight with the same parameters.
116 gaia_id_ = gaia_id; 156 }
117 email_ = email;
118 gaia_auth_fetcher_ = signin_client_->CreateGaiaAuthFetcher(
119 this, GaiaConstants::kChromeSource,
120 signin_client_->GetURLRequestContext());
121 gaia_auth_fetcher_->StartAuthCodeForOAuth2TokenExchange(authorization_code);
122 157
123 // TODO(droger): The token exchange must complete quickly or be cancelled. Add 158 token_fetchers_.push_back(base::MakeUnique<DiceTokenFetcher>(
124 // a timeout logic. 159 gaia_id, email, authorization_code, signin_client_, this));
125 } 160 }
126 161
127 void DiceResponseHandler::OnClientOAuthSuccess( 162 void DiceResponseHandler::DeleteTokenFetcher(DiceTokenFetcher* token_fetcher) {
128 const ClientOAuthResult& result) { 163 for (auto it = token_fetchers_.begin(); it != token_fetchers_.end(); ++it) {
164 if (it->get() == token_fetcher) {
165 token_fetchers_.erase(it);
166 return;
167 }
168 }
169 NOTREACHED();
170 }
171
172 void DiceResponseHandler::OnTokenExchangeSuccess(
173 DiceTokenFetcher* token_fetcher,
174 const std::string& gaia_id,
175 const std::string& email,
176 const GaiaAuthConsumer::ClientOAuthResult& result) {
129 std::string account_id = 177 std::string account_id =
130 account_tracker_service_->SeedAccountInfo(gaia_id_, email_); 178 account_tracker_service_->SeedAccountInfo(gaia_id, email);
131 profile_oauth2_token_service_->UpdateCredentials(account_id, 179 profile_oauth2_token_service_->UpdateCredentials(account_id,
132 result.refresh_token); 180 result.refresh_token);
133 gaia_id_.clear(); 181 DeleteTokenFetcher(token_fetcher);
134 email_.clear();
135 gaia_auth_fetcher_.reset();
136 } 182 }
137 183
138 void DiceResponseHandler::OnClientOAuthFailure( 184 void DiceResponseHandler::OnTokenExchangeFailure(
185 DiceTokenFetcher* token_fetcher,
139 const GoogleServiceAuthError& error) { 186 const GoogleServiceAuthError& error) {
140 // TODO(droger): Handle authentication errors. 187 // TODO(droger): Handle authentication errors.
141 DLOG(ERROR) << "Dice OAuth failed: " << error.error_message(); 188 DLOG(ERROR) << "Dice OAuth failed: " << error.error_message();
142 gaia_id_.clear(); 189 DeleteTokenFetcher(token_fetcher);
143 email_.clear();
144 gaia_auth_fetcher_.reset();
145 } 190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698