| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/net/gaia/oauth2_mint_token_flow.h" | 5 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 OAuth2MintTokenFlow::OAuth2MintTokenFlow( | 102 OAuth2MintTokenFlow::OAuth2MintTokenFlow( |
| 103 URLRequestContextGetter* context, | 103 URLRequestContextGetter* context, |
| 104 Delegate* delegate, | 104 Delegate* delegate, |
| 105 const Parameters& parameters) | 105 const Parameters& parameters) |
| 106 : OAuth2ApiCallFlow( | 106 : OAuth2ApiCallFlow( |
| 107 context, parameters.login_refresh_token, | 107 context, parameters.login_refresh_token, |
| 108 "", std::vector<std::string>()), | 108 "", std::vector<std::string>()), |
| 109 context_(context), | 109 context_(context), |
| 110 delegate_(delegate), | 110 delegate_(delegate), |
| 111 parameters_(parameters), | 111 parameters_(parameters) { |
| 112 delete_when_done_(false), | |
| 113 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 114 } | 112 } |
| 115 | 113 |
| 116 OAuth2MintTokenFlow::~OAuth2MintTokenFlow() { } | 114 OAuth2MintTokenFlow::~OAuth2MintTokenFlow() { } |
| 117 | 115 |
| 118 void OAuth2MintTokenFlow::Start() { | 116 void OAuth2MintTokenFlow::Start() { |
| 119 if (g_interceptor_for_tests) { | 117 if (g_interceptor_for_tests) { |
| 120 std::string auth_token; | 118 std::string auth_token; |
| 121 GoogleServiceAuthError error = GoogleServiceAuthError::None(); | 119 GoogleServiceAuthError error = GoogleServiceAuthError::None(); |
| 122 | 120 |
| 123 // We use PostTask, instead of calling the delegate directly, because the | 121 // We use PostTask, instead of calling the delegate directly, because the |
| 124 // message loop will run a few times before we notify the delegate in the | 122 // message loop will run a few times before we notify the delegate in the |
| 125 // real implementation. | 123 // real implementation. |
| 126 if (g_interceptor_for_tests->DoIntercept(this, &auth_token, &error)) { | 124 if (g_interceptor_for_tests->DoIntercept(this, &auth_token, &error)) { |
| 127 MessageLoop::current()->PostTask( | 125 MessageLoop::current()->PostTask( |
| 128 FROM_HERE, | 126 FROM_HERE, |
| 129 base::Bind(&OAuth2MintTokenFlow::ReportSuccess, | 127 base::Bind(&OAuth2MintTokenFlow::Delegate::OnMintTokenSuccess, |
| 130 weak_factory_.GetWeakPtr(), auth_token)); | 128 base::Unretained(delegate_), auth_token)); |
| 131 } else { | 129 } else { |
| 132 MessageLoop::current()->PostTask( | 130 MessageLoop::current()->PostTask( |
| 133 FROM_HERE, | 131 FROM_HERE, |
| 134 base::Bind(&OAuth2MintTokenFlow::ReportFailure, | 132 base::Bind(&OAuth2MintTokenFlow::Delegate::OnMintTokenFailure, |
| 135 weak_factory_.GetWeakPtr(), error)); | 133 base::Unretained(delegate_), error)); |
| 136 } | 134 } |
| 137 return; | 135 return; |
| 138 } | 136 } |
| 139 | 137 |
| 140 | |
| 141 OAuth2ApiCallFlow::Start(); | 138 OAuth2ApiCallFlow::Start(); |
| 142 } | 139 } |
| 143 | 140 |
| 144 void OAuth2MintTokenFlow::FireAndForget() { | 141 void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token) { |
| 145 delete_when_done_ = true; | 142 if (delegate_) { |
| 146 Start(); | 143 delegate_->OnMintTokenSuccess(access_token); |
| 144 } |
| 147 } | 145 } |
| 148 | 146 |
| 149 void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token) { | 147 void OAuth2MintTokenFlow::ReportSuccess(const IssueAdviceInfo& issue_advice) { |
| 150 if (delegate_) | 148 if (delegate_) { |
| 151 delegate_->OnMintTokenSuccess(access_token); | |
| 152 | |
| 153 if (delete_when_done_) | |
| 154 delete this; | |
| 155 } | |
| 156 | |
| 157 void OAuth2MintTokenFlow::ReportIssueAdviceSuccess( | |
| 158 const IssueAdviceInfo& issue_advice) { | |
| 159 if (delegate_) | |
| 160 delegate_->OnIssueAdviceSuccess(issue_advice); | 149 delegate_->OnIssueAdviceSuccess(issue_advice); |
| 161 | 150 } |
| 162 if (delete_when_done_) | |
| 163 delete this; | |
| 164 } | 151 } |
| 165 | 152 |
| 166 void OAuth2MintTokenFlow::ReportFailure( | 153 void OAuth2MintTokenFlow::ReportFailure( |
| 167 const GoogleServiceAuthError& error) { | 154 const GoogleServiceAuthError& error) { |
| 168 if (delegate_) | 155 if (delegate_) { |
| 169 delegate_->OnMintTokenFailure(error); | 156 delegate_->OnMintTokenFailure(error); |
| 170 | 157 } |
| 171 if (delete_when_done_) | |
| 172 delete this; | |
| 173 } | 158 } |
| 174 | 159 |
| 175 GURL OAuth2MintTokenFlow::CreateApiCallUrl() { | 160 GURL OAuth2MintTokenFlow::CreateApiCallUrl() { |
| 176 return GURL(GaiaUrls::GetInstance()->oauth2_issue_token_url()); | 161 return GURL(GaiaUrls::GetInstance()->oauth2_issue_token_url()); |
| 177 } | 162 } |
| 178 | 163 |
| 179 std::string OAuth2MintTokenFlow::CreateApiCallBody() { | 164 std::string OAuth2MintTokenFlow::CreateApiCallBody() { |
| 180 const char* force_value = | 165 const char* force_value = |
| 181 (parameters_.mode == MODE_MINT_TOKEN_FORCE || | 166 (parameters_.mode == MODE_MINT_TOKEN_FORCE || |
| 182 parameters_.mode == MODE_RECORD_GRANT) | 167 parameters_.mode == MODE_RECORD_GRANT) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 209 } | 194 } |
| 210 | 195 |
| 211 std::string issue_advice; | 196 std::string issue_advice; |
| 212 if (!dict->GetString(kIssueAdviceKey, &issue_advice)) { | 197 if (!dict->GetString(kIssueAdviceKey, &issue_advice)) { |
| 213 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | 198 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 214 return; | 199 return; |
| 215 } | 200 } |
| 216 if (issue_advice == kIssueAdviceValueConsent) { | 201 if (issue_advice == kIssueAdviceValueConsent) { |
| 217 IssueAdviceInfo issue_advice; | 202 IssueAdviceInfo issue_advice; |
| 218 if (ParseIssueAdviceResponse(dict, &issue_advice)) | 203 if (ParseIssueAdviceResponse(dict, &issue_advice)) |
| 219 ReportIssueAdviceSuccess(issue_advice); | 204 ReportSuccess(issue_advice); |
| 220 else | 205 else |
| 221 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | 206 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 222 } else { | 207 } else { |
| 223 std::string access_token; | 208 std::string access_token; |
| 224 if (ParseMintTokenResponse(dict, &access_token)) | 209 if (ParseMintTokenResponse(dict, &access_token)) |
| 225 ReportSuccess(access_token); | 210 ReportSuccess(access_token); |
| 226 else | 211 else |
| 227 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | 212 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 228 } | 213 } |
| 229 | |
| 230 // |this| may be deleted! | |
| 231 } | 214 } |
| 232 | 215 |
| 233 void OAuth2MintTokenFlow::ProcessApiCallFailure( | 216 void OAuth2MintTokenFlow::ProcessApiCallFailure( |
| 234 const net::URLFetcher* source) { | 217 const net::URLFetcher* source) { |
| 235 ReportFailure(CreateAuthError(source->GetStatus())); | 218 ReportFailure(CreateAuthError(source->GetStatus())); |
| 236 } | 219 } |
| 237 void OAuth2MintTokenFlow::ProcessNewAccessToken( | 220 void OAuth2MintTokenFlow::ProcessNewAccessToken( |
| 238 const std::string& access_token) { | 221 const std::string& access_token) { |
| 239 // We don't currently store new access tokens. We generate one every time. | 222 // We don't currently store new access tokens. We generate one every time. |
| 240 // So we have nothing to do here. | 223 // So we have nothing to do here. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 264 |
| 282 Tokenize(detail, kDetailSeparators, &entry.details); | 265 Tokenize(detail, kDetailSeparators, &entry.details); |
| 283 issue_advice->push_back(entry); | 266 issue_advice->push_back(entry); |
| 284 } | 267 } |
| 285 | 268 |
| 286 if (!success) | 269 if (!success) |
| 287 issue_advice->clear(); | 270 issue_advice->clear(); |
| 288 | 271 |
| 289 return success; | 272 return success; |
| 290 } | 273 } |
| OLD | NEW |