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