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 "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" | 5 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 std::string body; | 192 std::string body; |
193 base::JSONWriter::Write(dict, &body); | 193 base::JSONWriter::Write(dict, &body); |
194 (*it)->url_fetcher->SetUploadData("application/json", body); | 194 (*it)->url_fetcher->SetUploadData("application/json", body); |
195 | 195 |
196 (*it)->url_fetcher->Start(); | 196 (*it)->url_fetcher->Start(); |
197 } | 197 } |
198 | 198 |
199 void PermissionRequestCreatorApiary::OnGetTokenFailure( | 199 void PermissionRequestCreatorApiary::OnGetTokenFailure( |
200 const OAuth2TokenService::Request* request, | 200 const OAuth2TokenService::Request* request, |
201 const GoogleServiceAuthError& error) { | 201 const GoogleServiceAuthError& error) { |
202 VLOG(1) << "Couldn't get token"; | |
203 RequestIterator it = requests_.begin(); | 202 RequestIterator it = requests_.begin(); |
204 while (it != requests_.end()) { | 203 while (it != requests_.end()) { |
205 if (request == (*it)->access_token_request.get()) | 204 if (request == (*it)->access_token_request.get()) |
206 break; | 205 break; |
207 ++it; | 206 ++it; |
208 } | 207 } |
209 DCHECK(it != requests_.end()); | 208 DCHECK(it != requests_.end()); |
210 (*it)->callback.Run(false); | 209 LOG(WARNING) << "Token error: " << error.ToString(); |
211 requests_.erase(it); | 210 DispatchResult(it, false); |
212 } | 211 } |
213 | 212 |
214 void PermissionRequestCreatorApiary::OnURLFetchComplete( | 213 void PermissionRequestCreatorApiary::OnURLFetchComplete( |
215 const URLFetcher* source) { | 214 const URLFetcher* source) { |
216 RequestIterator it = requests_.begin(); | 215 RequestIterator it = requests_.begin(); |
217 while (it != requests_.end()) { | 216 while (it != requests_.end()) { |
218 if (source == (*it)->url_fetcher.get()) | 217 if (source == (*it)->url_fetcher.get()) |
219 break; | 218 break; |
220 ++it; | 219 ++it; |
221 } | 220 } |
222 DCHECK(it != requests_.end()); | 221 DCHECK(it != requests_.end()); |
223 | 222 |
224 const net::URLRequestStatus& status = source->GetStatus(); | 223 const net::URLRequestStatus& status = source->GetStatus(); |
225 if (!status.is_success()) { | 224 if (!status.is_success()) { |
226 DispatchNetworkError(it, status.error()); | 225 LOG(WARNING) << "Network error " << status.error(); |
| 226 DispatchResult(it, false); |
227 return; | 227 return; |
228 } | 228 } |
229 | 229 |
230 int response_code = source->GetResponseCode(); | 230 int response_code = source->GetResponseCode(); |
231 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) { | 231 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) { |
232 (*it)->access_token_expired = true; | 232 (*it)->access_token_expired = true; |
233 OAuth2TokenService::ScopeSet scopes; | 233 OAuth2TokenService::ScopeSet scopes; |
234 scopes.insert(GetApiScope()); | 234 scopes.insert(GetApiScope()); |
235 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes, | 235 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes, |
236 (*it)->access_token); | 236 (*it)->access_token); |
237 StartFetching(*it); | 237 StartFetching(*it); |
238 return; | 238 return; |
239 } | 239 } |
240 | 240 |
241 if (response_code != net::HTTP_OK) { | 241 if (response_code != net::HTTP_OK) { |
242 LOG(WARNING) << "HTTP error " << response_code; | 242 LOG(WARNING) << "HTTP error " << response_code; |
243 DispatchGoogleServiceAuthError( | 243 DispatchResult(it, false); |
244 it, GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)); | |
245 return; | 244 return; |
246 } | 245 } |
247 | 246 |
248 std::string response_body; | 247 std::string response_body; |
249 source->GetResponseAsString(&response_body); | 248 source->GetResponseAsString(&response_body); |
250 scoped_ptr<base::Value> value = base::JSONReader::Read(response_body); | 249 scoped_ptr<base::Value> value = base::JSONReader::Read(response_body); |
251 base::DictionaryValue* dict = NULL; | 250 base::DictionaryValue* dict = NULL; |
252 if (!value || !value->GetAsDictionary(&dict)) { | 251 if (!value || !value->GetAsDictionary(&dict)) { |
253 DispatchNetworkError(it, net::ERR_INVALID_RESPONSE); | 252 LOG(WARNING) << "Invalid top-level dictionary"; |
| 253 DispatchResult(it, false); |
254 return; | 254 return; |
255 } | 255 } |
256 base::DictionaryValue* permission_dict = NULL; | 256 base::DictionaryValue* permission_dict = NULL; |
257 if (!dict->GetDictionary(kPermissionRequestKey, &permission_dict)) { | 257 if (!dict->GetDictionary(kPermissionRequestKey, &permission_dict)) { |
258 DispatchNetworkError(it, net::ERR_INVALID_RESPONSE); | 258 LOG(WARNING) << "Permission request not found"; |
| 259 DispatchResult(it, false); |
259 return; | 260 return; |
260 } | 261 } |
261 std::string id; | 262 std::string id; |
262 if (!permission_dict->GetString(kIdKey, &id)) { | 263 if (!permission_dict->GetString(kIdKey, &id)) { |
263 DispatchNetworkError(it, net::ERR_INVALID_RESPONSE); | 264 LOG(WARNING) << "ID not found"; |
| 265 DispatchResult(it, false); |
264 return; | 266 return; |
265 } | 267 } |
266 (*it)->callback.Run(true); | 268 DispatchResult(it, true); |
| 269 } |
| 270 |
| 271 void PermissionRequestCreatorApiary::DispatchResult(RequestIterator it, |
| 272 bool success) { |
| 273 (*it)->callback.Run(success); |
267 requests_.erase(it); | 274 requests_.erase(it); |
268 } | 275 } |
269 | |
270 void PermissionRequestCreatorApiary::DispatchNetworkError(RequestIterator it, | |
271 int error_code) { | |
272 DispatchGoogleServiceAuthError( | |
273 it, GoogleServiceAuthError::FromConnectionError(error_code)); | |
274 } | |
275 | |
276 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( | |
277 RequestIterator it, | |
278 const GoogleServiceAuthError& error) { | |
279 VLOG(1) << "GoogleServiceAuthError: " << error.ToString(); | |
280 (*it)->callback.Run(false); | |
281 requests_.erase(it); | |
282 } | |
OLD | NEW |