| 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 #ifndef CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ | 5 #ifndef CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ |
| 6 #define CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ | 6 #define CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 15 #include "chromeos/chromeos_export.h" |
| 15 #include "chromeos/geolocation/geoposition.h" | 16 #include "chromeos/geolocation/geoposition.h" |
| 17 #include "chromeos/network/network_util.h" |
| 16 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
| 22 } | 24 } |
| 23 | 25 |
| 24 namespace chromeos { | 26 namespace chromeos { |
| 25 | 27 |
| 28 class SimpleGeolocationRequestTestMonitor; |
| 29 |
| 26 // Sends request to a server to get local geolocation information. | 30 // Sends request to a server to get local geolocation information. |
| 27 // It performs formatting of the request and interpretation of the response. | 31 // It performs formatting of the request and interpretation of the response. |
| 28 // Request is owned and destroyed by caller (usually SimpleGeolocationProvider). | 32 // Request is owned and destroyed by caller (usually SimpleGeolocationProvider). |
| 29 // - If error occurs, request is retried until timeout. | 33 // - If error occurs, request is retried until timeout. |
| 30 // - On successul response, callback is called. | 34 // - On successul response, callback is called. |
| 31 // - On timeout, callback with last (failed) position is called. | 35 // - On timeout, callback with last (failed) position is called. |
| 32 // (position.status is set to STATUS_TIMEOUT.) | 36 // (position.status is set to STATUS_TIMEOUT.) |
| 33 // - If request is destroyed while callback has not beed called yet, request | 37 // - If request is destroyed while callback has not beed called yet, request |
| 34 // is silently cancelled. | 38 // is silently cancelled. |
| 35 class SimpleGeolocationRequest : private net::URLFetcherDelegate { | 39 // |
| 40 // Note: we need CHROMEOS_EXPORT for tests. |
| 41 class CHROMEOS_EXPORT SimpleGeolocationRequest |
| 42 : private net::URLFetcherDelegate { |
| 36 public: | 43 public: |
| 37 // Called when a new geo geolocation information is available. | 44 // Called when a new geo geolocation information is available. |
| 38 // The second argument indicates whether there was a server error or not. | 45 // The second argument indicates whether there was a server error or not. |
| 39 // It is true when there was a server or network error - either no response | 46 // It is true when there was a server or network error - either no response |
| 40 // or a 500 error code. | 47 // or a 500 error code. |
| 41 typedef base::Callback<void(const Geoposition& /* position*/, | 48 typedef base::Callback<void(const Geoposition& /* position*/, |
| 42 bool /* server_error */, | 49 bool /* server_error */, |
| 43 const base::TimeDelta elapsed)> ResponseCallback; | 50 const base::TimeDelta elapsed)> ResponseCallback; |
| 44 | 51 |
| 45 // |url| is the server address to which the request wil be sent. | 52 // |url| is the server address to which the request wil be sent. |
| 46 // |timeout| retry request on error until timeout. | 53 // |timeout| retry request on error until timeout. |
| 54 // If wifi_data is not null, it will be sent to the geolocation server. |
| 47 SimpleGeolocationRequest(net::URLRequestContextGetter* url_context_getter, | 55 SimpleGeolocationRequest(net::URLRequestContextGetter* url_context_getter, |
| 48 const GURL& service_url, | 56 const GURL& service_url, |
| 49 base::TimeDelta timeout); | 57 base::TimeDelta timeout, |
| 58 scoped_ptr<WifiAccessPointVector> wifi_data); |
| 50 | 59 |
| 51 ~SimpleGeolocationRequest() override; | 60 ~SimpleGeolocationRequest() override; |
| 52 | 61 |
| 53 // Initiates request. | 62 // Initiates request. |
| 54 // Note: if request object is destroyed before callback is called, | 63 // Note: if request object is destroyed before callback is called, |
| 55 // request will be silently cancelled. | 64 // request will be silently cancelled. |
| 56 void MakeRequest(const ResponseCallback& callback); | 65 void MakeRequest(const ResponseCallback& callback); |
| 57 | 66 |
| 58 void set_retry_sleep_on_server_error_for_testing( | 67 void set_retry_sleep_on_server_error_for_testing( |
| 59 const base::TimeDelta value) { | 68 const base::TimeDelta value) { |
| 60 retry_sleep_on_server_error_ = value; | 69 retry_sleep_on_server_error_ = value; |
| 61 } | 70 } |
| 62 | 71 |
| 63 void set_retry_sleep_on_bad_response_for_testing( | 72 void set_retry_sleep_on_bad_response_for_testing( |
| 64 const base::TimeDelta value) { | 73 const base::TimeDelta value) { |
| 65 retry_sleep_on_bad_response_ = value; | 74 retry_sleep_on_bad_response_ = value; |
| 66 } | 75 } |
| 67 | 76 |
| 77 // Sets global requests monitoring object for testing. |
| 78 static void SetTestMonitor(SimpleGeolocationRequestTestMonitor* monitor); |
| 79 |
| 80 std::string FormatRequestBodyForTesting() const; |
| 81 |
| 68 private: | 82 private: |
| 69 // net::URLFetcherDelegate | 83 // net::URLFetcherDelegate |
| 70 void OnURLFetchComplete(const net::URLFetcher* source) override; | 84 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 71 | 85 |
| 72 // Start new request. | 86 // Start new request. |
| 73 void StartRequest(); | 87 void StartRequest(); |
| 74 | 88 |
| 75 // Schedules retry. | 89 // Schedules retry. |
| 76 void Retry(bool server_error); | 90 void Retry(bool server_error); |
| 77 | 91 |
| 78 // Run callback and destroy "this". | 92 // Run callback and destroy "this". |
| 79 void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error); | 93 void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error); |
| 80 | 94 |
| 81 // Called by timeout_timer_ . | 95 // Called by timeout_timer_ . |
| 82 void OnTimeout(); | 96 void OnTimeout(); |
| 83 | 97 |
| 98 // Returns API request body. |
| 99 std::string FormatRequestBody() const; |
| 100 |
| 84 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | 101 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; |
| 85 | 102 |
| 86 // Service URL from constructor arguments. | 103 // Service URL from constructor arguments. |
| 87 const GURL service_url_; | 104 const GURL service_url_; |
| 88 | 105 |
| 89 ResponseCallback callback_; | 106 ResponseCallback callback_; |
| 90 | 107 |
| 91 // Actual URL with parameters. | 108 // Actual URL with parameters. |
| 92 GURL request_url_; | 109 GURL request_url_; |
| 93 | 110 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 107 | 124 |
| 108 // Stop request on timeout. | 125 // Stop request on timeout. |
| 109 base::OneShotTimer timeout_timer_; | 126 base::OneShotTimer timeout_timer_; |
| 110 | 127 |
| 111 // Number of retry attempts. | 128 // Number of retry attempts. |
| 112 unsigned retries_; | 129 unsigned retries_; |
| 113 | 130 |
| 114 // This is updated on each retry. | 131 // This is updated on each retry. |
| 115 Geoposition position_; | 132 Geoposition position_; |
| 116 | 133 |
| 134 scoped_ptr<WifiAccessPointVector> wifi_data_; |
| 135 |
| 117 // Creation and destruction should happen on the same thread. | 136 // Creation and destruction should happen on the same thread. |
| 118 base::ThreadChecker thread_checker_; | 137 base::ThreadChecker thread_checker_; |
| 119 | 138 |
| 120 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationRequest); | 139 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationRequest); |
| 121 }; | 140 }; |
| 122 | 141 |
| 123 } // namespace chromeos | 142 } // namespace chromeos |
| 124 | 143 |
| 125 #endif // CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ | 144 #endif // CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ |
| OLD | NEW |