OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_impl.h" | 5 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 CHECK(!status.is_success()); | 83 CHECK(!status.is_success()); |
84 if (status.status() == URLRequestStatus::CANCELED) { | 84 if (status.status() == URLRequestStatus::CANCELED) { |
85 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 85 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
86 } else { | 86 } else { |
87 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | 87 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " |
88 << status.error(); | 88 << status.error(); |
89 return GoogleServiceAuthError::FromConnectionError(status.error()); | 89 return GoogleServiceAuthError::FromConnectionError(status.error()); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 static URLFetcher* CreateFetcher(URLRequestContextGetter* getter, | 93 static scoped_ptr<URLFetcher> CreateFetcher(URLRequestContextGetter* getter, |
94 const GURL& url, | 94 const GURL& url, |
95 const std::string& body, | 95 const std::string& body, |
96 URLFetcherDelegate* delegate) { | 96 URLFetcherDelegate* delegate) { |
97 bool empty_body = body.empty(); | 97 bool empty_body = body.empty(); |
98 URLFetcher* result = net::URLFetcher::Create( | 98 scoped_ptr<URLFetcher> result = net::URLFetcher::Create( |
99 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate); | 99 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate); |
100 | 100 |
101 result->SetRequestContext(getter); | 101 result->SetRequestContext(getter); |
102 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 102 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
103 net::LOAD_DO_NOT_SAVE_COOKIES); | 103 net::LOAD_DO_NOT_SAVE_COOKIES); |
104 // Fetchers are sometimes cancelled because a network change was detected, | 104 // Fetchers are sometimes cancelled because a network change was detected, |
105 // especially at startup and after sign-in on ChromeOS. Retrying once should | 105 // especially at startup and after sign-in on ChromeOS. Retrying once should |
106 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 106 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
107 // http://crbug.com/163710 | 107 // http://crbug.com/163710 |
108 result->SetAutomaticallyRetryOnNetworkChanges(3); | 108 result->SetAutomaticallyRetryOnNetworkChanges(3); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 const std::vector<std::string>& scopes) { | 148 const std::vector<std::string>& scopes) { |
149 client_id_ = client_id; | 149 client_id_ = client_id; |
150 client_secret_ = client_secret; | 150 client_secret_ = client_secret; |
151 scopes_ = scopes; | 151 scopes_ = scopes; |
152 StartGetAccessToken(); | 152 StartGetAccessToken(); |
153 } | 153 } |
154 | 154 |
155 void OAuth2AccessTokenFetcherImpl::StartGetAccessToken() { | 155 void OAuth2AccessTokenFetcherImpl::StartGetAccessToken() { |
156 CHECK_EQ(INITIAL, state_); | 156 CHECK_EQ(INITIAL, state_); |
157 state_ = GET_ACCESS_TOKEN_STARTED; | 157 state_ = GET_ACCESS_TOKEN_STARTED; |
158 fetcher_.reset( | 158 fetcher_ = CreateFetcher(getter_, MakeGetAccessTokenUrl(), |
159 CreateFetcher(getter_, | 159 MakeGetAccessTokenBody(client_id_, client_secret_, |
160 MakeGetAccessTokenUrl(), | 160 refresh_token_, scopes_), |
161 MakeGetAccessTokenBody( | 161 this); |
162 client_id_, client_secret_, refresh_token_, scopes_), | |
163 this)); | |
164 fetcher_->Start(); // OnURLFetchComplete will be called. | 162 fetcher_->Start(); // OnURLFetchComplete will be called. |
165 } | 163 } |
166 | 164 |
167 void OAuth2AccessTokenFetcherImpl::EndGetAccessToken( | 165 void OAuth2AccessTokenFetcherImpl::EndGetAccessToken( |
168 const net::URLFetcher* source) { | 166 const net::URLFetcher* source) { |
169 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); | 167 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); |
170 state_ = GET_ACCESS_TOKEN_DONE; | 168 state_ = GET_ACCESS_TOKEN_DONE; |
171 | 169 |
172 URLRequestStatus status = source->GetStatus(); | 170 URLRequestStatus status = source->GetStatus(); |
173 int histogram_value = | 171 int histogram_value = |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 // static | 310 // static |
313 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse( | 311 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse( |
314 const net::URLFetcher* source, | 312 const net::URLFetcher* source, |
315 std::string* error) { | 313 std::string* error) { |
316 CHECK(error); | 314 CHECK(error); |
317 scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source); | 315 scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source); |
318 if (value.get() == NULL) | 316 if (value.get() == NULL) |
319 return false; | 317 return false; |
320 return value->GetString(kErrorKey, error); | 318 return value->GetString(kErrorKey, error); |
321 } | 319 } |
OLD | NEW |