Roll libjpeg_turbo to git hash feec46f80444b8eed4126a86a2c0e2cffe1c9673
[chromium-blink-merge.git] / chromeos / geolocation / simple_geolocation_request.h
blob179efcb21a1093a387eea1023a6634ccce9cfc76
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.
5 #ifndef CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_
6 #define CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/timer/timer.h"
15 #include "chromeos/geolocation/geoposition.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "url/gurl.h"
20 namespace net {
21 class URLRequestContextGetter;
24 namespace chromeos {
26 // Sends request to a server to get local geolocation information.
27 // It performs formatting of the request and interpretation of the response.
28 // Request is owned and destroyed by caller (usually SimpleGeolocationProvider).
29 // - If error occurs, request is retried until timeout.
30 // - On successul response, callback is called.
31 // - On timeout, callback with last (failed) position is called.
32 // (position.status is set to STATUS_TIMEOUT.)
33 // - If request is destroyed while callback has not beed called yet, request
34 // is silently cancelled.
35 class SimpleGeolocationRequest : private net::URLFetcherDelegate {
36 public:
37 // Called when a new geo geolocation information is available.
38 // 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
40 // or a 500 error code.
41 typedef base::Callback<void(const Geoposition& /* position*/,
42 bool /* server_error */,
43 const base::TimeDelta elapsed)> ResponseCallback;
45 // |url| is the server address to which the request wil be sent.
46 // |timeout| retry request on error until timeout.
47 SimpleGeolocationRequest(net::URLRequestContextGetter* url_context_getter,
48 const GURL& service_url,
49 base::TimeDelta timeout);
51 ~SimpleGeolocationRequest() override;
53 // Initiates request.
54 // Note: if request object is destroyed before callback is called,
55 // request will be silently cancelled.
56 void MakeRequest(const ResponseCallback& callback);
58 void set_retry_sleep_on_server_error_for_testing(
59 const base::TimeDelta value) {
60 retry_sleep_on_server_error_ = value;
63 void set_retry_sleep_on_bad_response_for_testing(
64 const base::TimeDelta value) {
65 retry_sleep_on_bad_response_ = value;
68 private:
69 // net::URLFetcherDelegate
70 void OnURLFetchComplete(const net::URLFetcher* source) override;
72 // Start new request.
73 void StartRequest();
75 // Schedules retry.
76 void Retry(bool server_error);
78 // Run callback and destroy "this".
79 void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error);
81 // Called by timeout_timer_ .
82 void OnTimeout();
84 scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
86 // Service URL from constructor arguments.
87 const GURL service_url_;
89 ResponseCallback callback_;
91 // Actual URL with parameters.
92 GURL request_url_;
94 scoped_ptr<net::URLFetcher> url_fetcher_;
96 // When request was actually started.
97 base::Time request_started_at_;
99 base::TimeDelta retry_sleep_on_server_error_;
101 base::TimeDelta retry_sleep_on_bad_response_;
103 const base::TimeDelta timeout_;
105 // Pending retry.
106 base::OneShotTimer<SimpleGeolocationRequest> request_scheduled_;
108 // Stop request on timeout.
109 base::OneShotTimer<SimpleGeolocationRequest> timeout_timer_;
111 // Number of retry attempts.
112 unsigned retries_;
114 // This is updated on each retry.
115 Geoposition position_;
117 // Creation and destruction should happen on the same thread.
118 base::ThreadChecker thread_checker_;
120 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationRequest);
123 } // namespace chromeos
125 #endif // CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_