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" |
| 11 #include "base/location.h" |
11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/thread_task_runner_handle.h" |
15 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/local_discovery/privet_constants.h" | 18 #include "chrome/browser/local_discovery/privet_constants.h" |
17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
18 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
19 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
20 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
21 | 23 |
22 namespace local_discovery { | 24 namespace local_discovery { |
23 | 25 |
24 namespace { | 26 namespace { |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) { | 344 void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) { |
343 double random_scaling_factor = | 345 double random_scaling_factor = |
344 1 + base::RandDouble() * kPrivetMaximumTimeRandomAddition; | 346 1 + base::RandDouble() * kPrivetMaximumTimeRandomAddition; |
345 | 347 |
346 int timeout_seconds_randomized = | 348 int timeout_seconds_randomized = |
347 static_cast<int>(timeout_seconds * random_scaling_factor); | 349 static_cast<int>(timeout_seconds * random_scaling_factor); |
348 | 350 |
349 timeout_seconds_randomized = | 351 timeout_seconds_randomized = |
350 std::max(timeout_seconds_randomized, kPrivetMinimumTimeout); | 352 std::max(timeout_seconds_randomized, kPrivetMinimumTimeout); |
351 | 353 |
352 base::MessageLoop::current()->PostDelayedTask( | 354 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
353 FROM_HERE, | 355 FROM_HERE, base::Bind(&PrivetURLFetcher::Try, weak_factory_.GetWeakPtr()), |
354 base::Bind(&PrivetURLFetcher::Try, weak_factory_.GetWeakPtr()), | |
355 base::TimeDelta::FromSeconds(timeout_seconds_randomized)); | 356 base::TimeDelta::FromSeconds(timeout_seconds_randomized)); |
356 } | 357 } |
357 | 358 |
358 void PrivetURLFetcher::RequestTokenRefresh() { | 359 void PrivetURLFetcher::RequestTokenRefresh() { |
359 delegate_->OnNeedPrivetToken( | 360 delegate_->OnNeedPrivetToken( |
360 this, | 361 this, |
361 base::Bind(&PrivetURLFetcher::RefreshToken, weak_factory_.GetWeakPtr())); | 362 base::Bind(&PrivetURLFetcher::RefreshToken, weak_factory_.GetWeakPtr())); |
362 } | 363 } |
363 | 364 |
364 void PrivetURLFetcher::RefreshToken(const std::string& token) { | 365 void PrivetURLFetcher::RefreshToken(const std::string& token) { |
365 if (token.empty()) { | 366 if (token.empty()) { |
366 delegate_->OnError(this, TOKEN_ERROR); | 367 delegate_->OnError(this, TOKEN_ERROR); |
367 } else { | 368 } else { |
368 SetTokenForHost(GetHostString(), token); | 369 SetTokenForHost(GetHostString(), token); |
369 Try(); | 370 Try(); |
370 } | 371 } |
371 } | 372 } |
372 | 373 |
373 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { | 374 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { |
374 return (error == kPrivetErrorDeviceBusy) || | 375 return (error == kPrivetErrorDeviceBusy) || |
375 (error == kPrivetV3ErrorDeviceBusy) || | 376 (error == kPrivetV3ErrorDeviceBusy) || |
376 (error == kPrivetErrorPendingUserAction) || | 377 (error == kPrivetErrorPendingUserAction) || |
377 (error == kPrivetErrorPrinterBusy); | 378 (error == kPrivetErrorPrinterBusy); |
378 } | 379 } |
379 | 380 |
380 } // namespace local_discovery | 381 } // namespace local_discovery |
OLD | NEW |