OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/local_discovery/privet_url_fetcher.h" | 5 #include "chrome/browser/local_discovery/privet_url_fetcher.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 delegate_->OnError(this, URL_FETCH_ERROR); | 276 delegate_->OnError(this, URL_FETCH_ERROR); |
277 return true; | 277 return true; |
278 } | 278 } |
279 | 279 |
280 return delegate_->OnRawData(this, false, response_str, base::FilePath()); | 280 return delegate_->OnRawData(this, false, response_str, base::FilePath()); |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 void PrivetURLFetcher::OnURLFetchCompleteParseData( | 284 void PrivetURLFetcher::OnURLFetchCompleteParseData( |
285 const net::URLFetcher* source) { | 285 const net::URLFetcher* source) { |
286 if (source->GetResponseCode() != net::HTTP_OK) { | 286 // Response contains error description. |
| 287 bool is_error_response = false; |
| 288 if (v3_mode_ && source->GetResponseCode() == net::HTTP_BAD_REQUEST) { |
| 289 is_error_response = true; |
| 290 } else if (source->GetResponseCode() != net::HTTP_OK) { |
287 delegate_->OnError(this, RESPONSE_CODE_ERROR); | 291 delegate_->OnError(this, RESPONSE_CODE_ERROR); |
288 return; | 292 return; |
289 } | 293 } |
290 | 294 |
291 std::string response_str; | 295 std::string response_str; |
292 | |
293 if (!source->GetResponseAsString(&response_str)) { | 296 if (!source->GetResponseAsString(&response_str)) { |
294 delegate_->OnError(this, URL_FETCH_ERROR); | 297 delegate_->OnError(this, URL_FETCH_ERROR); |
295 return; | 298 return; |
296 } | 299 } |
297 | 300 |
298 base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); | 301 base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); |
299 scoped_ptr<base::Value> value; | 302 scoped_ptr<base::Value> value; |
300 | 303 |
301 value.reset(json_reader.ReadToValue(response_str)); | 304 value.reset(json_reader.ReadToValue(response_str)); |
302 | 305 |
303 if (!value) { | 306 if (!value) { |
304 delegate_->OnError(this, JSON_PARSE_ERROR); | 307 delegate_->OnError(this, JSON_PARSE_ERROR); |
305 return; | 308 return; |
306 } | 309 } |
307 | 310 |
308 const base::DictionaryValue* dictionary_value = NULL; | 311 const base::DictionaryValue* dictionary_value = NULL; |
309 | 312 |
310 if (!value->GetAsDictionary(&dictionary_value)) { | 313 if (!value->GetAsDictionary(&dictionary_value)) { |
311 delegate_->OnError(this, JSON_PARSE_ERROR); | 314 delegate_->OnError(this, JSON_PARSE_ERROR); |
312 return; | 315 return; |
313 } | 316 } |
314 | 317 |
315 std::string error; | 318 std::string error; |
316 if (dictionary_value->GetString(kPrivetKeyError, &error)) { | 319 if (!v3_mode_ && dictionary_value->GetString(kPrivetKeyError, &error)) { |
317 if (error == kPrivetErrorInvalidXPrivetToken) { | 320 if (error == kPrivetErrorInvalidXPrivetToken) { |
318 RequestTokenRefresh(); | 321 RequestTokenRefresh(); |
319 return; | 322 return; |
320 } else if (PrivetErrorTransient(error)) { | 323 } else if (PrivetErrorTransient(error)) { |
321 if (!do_not_retry_on_transient_error_) { | 324 if (!do_not_retry_on_transient_error_) { |
322 int timeout_seconds; | 325 int timeout_seconds; |
323 if (!dictionary_value->GetInteger(kPrivetKeyTimeout, | 326 if (!dictionary_value->GetInteger(kPrivetKeyTimeout, |
324 &timeout_seconds)) { | 327 &timeout_seconds)) { |
325 timeout_seconds = kPrivetDefaultTimeout; | 328 timeout_seconds = kPrivetDefaultTimeout; |
326 } | 329 } |
327 | 330 |
328 ScheduleRetry(timeout_seconds); | 331 ScheduleRetry(timeout_seconds); |
329 return; | 332 return; |
330 } | 333 } |
331 } | 334 } |
| 335 is_error_response = true; |
332 } | 336 } |
333 | 337 |
334 delegate_->OnParsedJson( | 338 delegate_->OnParsedJson(this, *dictionary_value, is_error_response); |
335 this, *dictionary_value, dictionary_value->HasKey(kPrivetKeyError)); | |
336 } | 339 } |
337 | 340 |
338 void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) { | 341 void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) { |
339 double random_scaling_factor = | 342 double random_scaling_factor = |
340 1 + base::RandDouble() * kPrivetMaximumTimeRandomAddition; | 343 1 + base::RandDouble() * kPrivetMaximumTimeRandomAddition; |
341 | 344 |
342 int timeout_seconds_randomized = | 345 int timeout_seconds_randomized = |
343 static_cast<int>(timeout_seconds * random_scaling_factor); | 346 static_cast<int>(timeout_seconds * random_scaling_factor); |
344 | 347 |
345 timeout_seconds_randomized = | 348 timeout_seconds_randomized = |
(...skipping 21 matching lines...) Expand all Loading... |
367 } | 370 } |
368 | 371 |
369 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { | 372 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { |
370 return (error == kPrivetErrorDeviceBusy) || | 373 return (error == kPrivetErrorDeviceBusy) || |
371 (error == kPrivetV3ErrorDeviceBusy) || | 374 (error == kPrivetV3ErrorDeviceBusy) || |
372 (error == kPrivetErrorPendingUserAction) || | 375 (error == kPrivetErrorPendingUserAction) || |
373 (error == kPrivetErrorPrinterBusy); | 376 (error == kPrivetErrorPrinterBusy); |
374 } | 377 } |
375 | 378 |
376 } // namespace local_discovery | 379 } // namespace local_discovery |
OLD | NEW |