| 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 "google_apis/gaia/oauth2_access_token_fetcher.h" | 5 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 CHECK(!status.is_success()); | 49 CHECK(!status.is_success()); |
| 50 if (status.status() == URLRequestStatus::CANCELED) { | 50 if (status.status() == URLRequestStatus::CANCELED) { |
| 51 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 51 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 52 } else { | 52 } else { |
| 53 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | 53 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 54 << status.error(); | 54 << status.error(); |
| 55 return GoogleServiceAuthError::FromConnectionError(status.error()); | 55 return GoogleServiceAuthError::FromConnectionError(status.error()); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 static URLFetcher* CreateFetcher(URLRequestContextGetter* getter, | 59 static URLFetcher* CreateFetcher(int id, |
| 60 URLRequestContextGetter* getter, |
| 60 const GURL& url, | 61 const GURL& url, |
| 61 const std::string& body, | 62 const std::string& body, |
| 62 URLFetcherDelegate* delegate) { | 63 URLFetcherDelegate* delegate) { |
| 63 bool empty_body = body.empty(); | 64 bool empty_body = body.empty(); |
| 64 URLFetcher* result = net::URLFetcher::Create( | 65 URLFetcher* result = net::URLFetcher::Create( |
| 65 0, url, | 66 id, url, |
| 66 empty_body ? URLFetcher::GET : URLFetcher::POST, | 67 empty_body ? URLFetcher::GET : URLFetcher::POST, |
| 67 delegate); | 68 delegate); |
| 68 | 69 |
| 69 result->SetRequestContext(getter); | 70 result->SetRequestContext(getter); |
| 70 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 71 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 71 net::LOAD_DO_NOT_SAVE_COOKIES); | 72 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 72 // Fetchers are sometimes cancelled because a network change was detected, | 73 // Fetchers are sometimes cancelled because a network change was detected, |
| 73 // especially at startup and after sign-in on ChromeOS. Retrying once should | 74 // especially at startup and after sign-in on ChromeOS. Retrying once should |
| 74 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 75 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
| 75 // http://crbug.com/163710 | 76 // http://crbug.com/163710 |
| 76 result->SetAutomaticallyRetryOnNetworkChanges(3); | 77 result->SetAutomaticallyRetryOnNetworkChanges(3); |
| 77 | 78 |
| 78 if (!empty_body) | 79 if (!empty_body) |
| 79 result->SetUploadData("application/x-www-form-urlencoded", body); | 80 result->SetUploadData("application/x-www-form-urlencoded", body); |
| 80 | 81 |
| 81 return result; | 82 return result; |
| 82 } | 83 } |
| 83 } // namespace | 84 } // namespace |
| 84 | 85 |
| 86 int OAuth2AccessTokenFetcher::last_fetcher_id_ = 0; |
| 87 |
| 85 OAuth2AccessTokenFetcher::OAuth2AccessTokenFetcher( | 88 OAuth2AccessTokenFetcher::OAuth2AccessTokenFetcher( |
| 86 OAuth2AccessTokenConsumer* consumer, | 89 OAuth2AccessTokenConsumer* consumer, |
| 87 URLRequestContextGetter* getter) | 90 URLRequestContextGetter* getter) |
| 88 : consumer_(consumer), | 91 : consumer_(consumer), |
| 89 getter_(getter), | 92 getter_(getter), |
| 90 state_(INITIAL) { } | 93 state_(INITIAL) { } |
| 91 | 94 |
| 92 OAuth2AccessTokenFetcher::~OAuth2AccessTokenFetcher() { } | 95 OAuth2AccessTokenFetcher::~OAuth2AccessTokenFetcher() { } |
| 93 | 96 |
| 94 void OAuth2AccessTokenFetcher::CancelRequest() { | 97 void OAuth2AccessTokenFetcher::CancelRequest() { |
| 95 fetcher_.reset(); | 98 fetcher_.reset(); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void OAuth2AccessTokenFetcher::Start(const std::string& client_id, | 101 void OAuth2AccessTokenFetcher::Start(const std::string& client_id, |
| 99 const std::string& client_secret, | 102 const std::string& client_secret, |
| 100 const std::string& refresh_token, | 103 const std::string& refresh_token, |
| 101 const std::vector<std::string>& scopes) { | 104 const std::vector<std::string>& scopes) { |
| 102 client_id_ = client_id; | 105 client_id_ = client_id; |
| 103 client_secret_ = client_secret; | 106 client_secret_ = client_secret; |
| 104 refresh_token_ = refresh_token; | 107 refresh_token_ = refresh_token; |
| 105 scopes_ = scopes; | 108 scopes_ = scopes; |
| 106 StartGetAccessToken(); | 109 StartGetAccessToken(); |
| 107 } | 110 } |
| 108 | 111 |
| 109 void OAuth2AccessTokenFetcher::StartGetAccessToken() { | 112 void OAuth2AccessTokenFetcher::StartGetAccessToken() { |
| 110 CHECK_EQ(INITIAL, state_); | 113 CHECK_EQ(INITIAL, state_); |
| 111 state_ = GET_ACCESS_TOKEN_STARTED; | 114 state_ = GET_ACCESS_TOKEN_STARTED; |
| 112 fetcher_.reset(CreateFetcher( | 115 fetcher_.reset(CreateFetcher( |
| 116 last_fetcher_id_++, |
| 113 getter_, | 117 getter_, |
| 114 MakeGetAccessTokenUrl(), | 118 MakeGetAccessTokenUrl(), |
| 115 MakeGetAccessTokenBody( | 119 MakeGetAccessTokenBody( |
| 116 client_id_, client_secret_, refresh_token_, scopes_), | 120 client_id_, client_secret_, refresh_token_, scopes_), |
| 117 this)); | 121 this)); |
| 118 fetcher_->Start(); // OnURLFetchComplete will be called. | 122 fetcher_->Start(); // OnURLFetchComplete will be called. |
| 119 } | 123 } |
| 120 | 124 |
| 121 void OAuth2AccessTokenFetcher::EndGetAccessToken( | 125 void OAuth2AccessTokenFetcher::EndGetAccessToken( |
| 122 const net::URLFetcher* source) { | 126 const net::URLFetcher* source) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 source->GetResponseAsString(&data); | 228 source->GetResponseAsString(&data); |
| 225 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | 229 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
| 226 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) | 230 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) |
| 227 return false; | 231 return false; |
| 228 | 232 |
| 229 base::DictionaryValue* dict = | 233 base::DictionaryValue* dict = |
| 230 static_cast<base::DictionaryValue*>(value.get()); | 234 static_cast<base::DictionaryValue*>(value.get()); |
| 231 return dict->GetString(kAccessTokenKey, access_token) && | 235 return dict->GetString(kAccessTokenKey, access_token) && |
| 232 dict->GetInteger(kExpiresInKey, expires_in); | 236 dict->GetInteger(kExpiresInKey, expires_in); |
| 233 } | 237 } |
| 238 |
| 239 // static |
| 240 void OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest() { |
| 241 last_fetcher_id_ = 0; |
| 242 } |
| OLD | NEW |