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 scoped_ptr<URLFetcher> CreateFetcher(URLRequestContextGetter* getter, | 93 static std::unique_ptr<URLFetcher> CreateFetcher( |
94 const GURL& url, | 94 URLRequestContextGetter* getter, |
95 const std::string& body, | 95 const GURL& url, |
96 URLFetcherDelegate* delegate) { | 96 const std::string& body, |
| 97 URLFetcherDelegate* delegate) { |
97 bool empty_body = body.empty(); | 98 bool empty_body = body.empty(); |
98 scoped_ptr<URLFetcher> result = net::URLFetcher::Create( | 99 std::unique_ptr<URLFetcher> result = net::URLFetcher::Create( |
99 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate); | 100 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate); |
100 | 101 |
101 result->SetRequestContext(getter); | 102 result->SetRequestContext(getter); |
102 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 103 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
103 net::LOAD_DO_NOT_SAVE_COOKIES); | 104 net::LOAD_DO_NOT_SAVE_COOKIES); |
104 // Fetchers are sometimes cancelled because a network change was detected, | 105 // Fetchers are sometimes cancelled because a network change was detected, |
105 // especially at startup and after sign-in on ChromeOS. Retrying once should | 106 // 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. | 107 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
107 // http://crbug.com/163710 | 108 // http://crbug.com/163710 |
108 result->SetAutomaticallyRetryOnNetworkChanges(3); | 109 result->SetAutomaticallyRetryOnNetworkChanges(3); |
109 | 110 |
110 if (!empty_body) | 111 if (!empty_body) |
111 result->SetUploadData("application/x-www-form-urlencoded", body); | 112 result->SetUploadData("application/x-www-form-urlencoded", body); |
112 | 113 |
113 return result; | 114 return result; |
114 } | 115 } |
115 | 116 |
116 scoped_ptr<base::DictionaryValue> ParseGetAccessTokenResponse( | 117 std::unique_ptr<base::DictionaryValue> ParseGetAccessTokenResponse( |
117 const net::URLFetcher* source) { | 118 const net::URLFetcher* source) { |
118 CHECK(source); | 119 CHECK(source); |
119 | 120 |
120 std::string data; | 121 std::string data; |
121 source->GetResponseAsString(&data); | 122 source->GetResponseAsString(&data); |
122 scoped_ptr<base::Value> value = base::JSONReader::Read(data); | 123 std::unique_ptr<base::Value> value = base::JSONReader::Read(data); |
123 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) | 124 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) |
124 value.reset(); | 125 value.reset(); |
125 | 126 |
126 return scoped_ptr<base::DictionaryValue>( | 127 return std::unique_ptr<base::DictionaryValue>( |
127 static_cast<base::DictionaryValue*>(value.release())); | 128 static_cast<base::DictionaryValue*>(value.release())); |
128 } | 129 } |
129 | 130 |
130 } // namespace | 131 } // namespace |
131 | 132 |
132 OAuth2AccessTokenFetcherImpl::OAuth2AccessTokenFetcherImpl( | 133 OAuth2AccessTokenFetcherImpl::OAuth2AccessTokenFetcherImpl( |
133 OAuth2AccessTokenConsumer* consumer, | 134 OAuth2AccessTokenConsumer* consumer, |
134 net::URLRequestContextGetter* getter, | 135 net::URLRequestContextGetter* getter, |
135 const std::string& refresh_token) | 136 const std::string& refresh_token) |
136 : OAuth2AccessTokenFetcher(consumer), | 137 : OAuth2AccessTokenFetcher(consumer), |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 net::EscapeUrlEncodedData(scopes_string, true).c_str()); | 293 net::EscapeUrlEncodedData(scopes_string, true).c_str()); |
293 } | 294 } |
294 } | 295 } |
295 | 296 |
296 // static | 297 // static |
297 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse( | 298 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse( |
298 const net::URLFetcher* source, | 299 const net::URLFetcher* source, |
299 std::string* access_token, | 300 std::string* access_token, |
300 int* expires_in) { | 301 int* expires_in) { |
301 CHECK(access_token); | 302 CHECK(access_token); |
302 scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source); | 303 std::unique_ptr<base::DictionaryValue> value = |
| 304 ParseGetAccessTokenResponse(source); |
303 if (value.get() == NULL) | 305 if (value.get() == NULL) |
304 return false; | 306 return false; |
305 | 307 |
306 return value->GetString(kAccessTokenKey, access_token) && | 308 return value->GetString(kAccessTokenKey, access_token) && |
307 value->GetInteger(kExpiresInKey, expires_in); | 309 value->GetInteger(kExpiresInKey, expires_in); |
308 } | 310 } |
309 | 311 |
310 // static | 312 // static |
311 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse( | 313 bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse( |
312 const net::URLFetcher* source, | 314 const net::URLFetcher* source, |
313 std::string* error) { | 315 std::string* error) { |
314 CHECK(error); | 316 CHECK(error); |
315 scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source); | 317 std::unique_ptr<base::DictionaryValue> value = |
| 318 ParseGetAccessTokenResponse(source); |
316 if (value.get() == NULL) | 319 if (value.get() == NULL) |
317 return false; | 320 return false; |
318 return value->GetString(kErrorKey, error); | 321 return value->GetString(kErrorKey, error); |
319 } | 322 } |
OLD | NEW |