simple_geolocation_request.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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
  3. // found in the LICENSE file.
  4. #ifndef ASH_COMPONENTS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_
  5. #define ASH_COMPONENTS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_
  6. #include <memory>
  7. #include "ash/components/geolocation/geoposition.h"
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/component_export.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "base/time/time.h"
  14. #include "base/timer/timer.h"
  15. #include "chromeos/ash/components/network/network_util.h"
  16. #include "url/gurl.h"
  17. namespace network {
  18. class SimpleURLLoader;
  19. class SharedURLLoaderFactory;
  20. } // namespace network
  21. namespace ash {
  22. class SimpleGeolocationRequestTestMonitor;
  23. // Sends request to a server to get local geolocation information.
  24. // It performs formatting of the request and interpretation of the response.
  25. // Request is owned and destroyed by caller (usually SimpleGeolocationProvider).
  26. // - If error occurs, request is retried until timeout.
  27. // - On successul response, callback is called.
  28. // - On timeout, callback with last (failed) position is called.
  29. // (position.status is set to STATUS_TIMEOUT.)
  30. // - If request is destroyed while callback has not beed called yet, request
  31. // is silently cancelled.
  32. //
  33. // Note: we need COMPONENT_EXPORT(ASH_GEOLOCATION) for tests.
  34. class COMPONENT_EXPORT(ASH_GEOLOCATION) SimpleGeolocationRequest {
  35. public:
  36. // Called when a new geolocation information is available.
  37. // The second argument indicates whether there was a server error or not.
  38. // It is true when there was a server or network error - either no response
  39. // or a 500 error code.
  40. using ResponseCallback = base::OnceCallback<void(const Geoposition& position,
  41. bool server_error,
  42. base::TimeDelta elapsed)>;
  43. // |url| is the server address to which the request wil be sent.
  44. // |timeout| retry request on error until timeout.
  45. // If wifi_data is not null, it will be sent to the geolocation server.
  46. // If cell_tower_data is not null, it will be sent to the geolocation server.
  47. SimpleGeolocationRequest(
  48. scoped_refptr<network::SharedURLLoaderFactory> factory,
  49. const GURL& service_url,
  50. base::TimeDelta timeout,
  51. std::unique_ptr<WifiAccessPointVector> wifi_data,
  52. std::unique_ptr<CellTowerVector> cell_tower_data);
  53. SimpleGeolocationRequest(const SimpleGeolocationRequest&) = delete;
  54. SimpleGeolocationRequest& operator=(const SimpleGeolocationRequest&) = delete;
  55. ~SimpleGeolocationRequest();
  56. // Initiates request.
  57. // Note: if request object is destroyed before callback is called,
  58. // request will be silently cancelled.
  59. void MakeRequest(ResponseCallback callback);
  60. void set_retry_sleep_on_server_error_for_testing(
  61. const base::TimeDelta value) {
  62. retry_sleep_on_server_error_ = value;
  63. }
  64. void set_retry_sleep_on_bad_response_for_testing(
  65. const base::TimeDelta value) {
  66. retry_sleep_on_bad_response_ = value;
  67. }
  68. // Sets global requests monitoring object for testing.
  69. static void SetTestMonitor(SimpleGeolocationRequestTestMonitor* monitor);
  70. std::string FormatRequestBodyForTesting() const;
  71. private:
  72. void OnSimpleURLLoaderComplete(std::unique_ptr<std::string> response_body);
  73. // Start new request.
  74. void StartRequest();
  75. // Schedules retry.
  76. void Retry(bool server_error);
  77. // Run callback and destroy "this".
  78. void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error);
  79. // Called by timeout_timer_ .
  80. void OnTimeout();
  81. // Returns API request body.
  82. std::string FormatRequestBody() const;
  83. scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
  84. // Service URL from constructor arguments.
  85. const GURL service_url_;
  86. ResponseCallback callback_;
  87. // Actual URL with parameters.
  88. GURL request_url_;
  89. std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
  90. // When request was actually started.
  91. base::Time request_started_at_;
  92. base::TimeDelta retry_sleep_on_server_error_;
  93. base::TimeDelta retry_sleep_on_bad_response_;
  94. const base::TimeDelta timeout_;
  95. // Pending retry.
  96. base::OneShotTimer request_scheduled_;
  97. // Stop request on timeout.
  98. base::OneShotTimer timeout_timer_;
  99. // Number of retry attempts.
  100. unsigned retries_;
  101. // This is updated on each retry.
  102. Geoposition position_;
  103. std::unique_ptr<WifiAccessPointVector> wifi_data_;
  104. std::unique_ptr<CellTowerVector> cell_tower_data_;
  105. // Creation and destruction should happen on the same thread.
  106. base::ThreadChecker thread_checker_;
  107. };
  108. } // namespace ash
  109. #endif // ASH_COMPONENTS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_