Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / geolocation / simple_geolocation_request.h
blob3cc47e3ea1304ad7e87acfc0bf8e95b19e072fbe
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 CHROME_BROWSER_CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_
6 #define CHROME_BROWSER_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 "chrome/browser/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 virtual ~SimpleGeolocationRequest();
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 private:
59 // net::URLFetcherDelegate
60 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
62 // Start new request.
63 void StartRequest();
65 // Schedules retry.
66 void Retry(bool server_error);
68 // Run callback and destroy "this".
69 void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error);
71 // Called by timeout_timer_ .
72 void OnTimeout();
74 scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
76 // Service URL from constructor arguments.
77 const GURL service_url_;
79 ResponseCallback callback_;
81 // Actual URL with parameters.
82 GURL request_url_;
84 scoped_ptr<net::URLFetcher> url_fetcher_;
86 // When request was actually started.
87 base::Time request_started_at_;
89 const base::TimeDelta timeout_;
91 // Pending retry.
92 base::OneShotTimer<SimpleGeolocationRequest> request_scheduled_;
94 // Stop request on timeout.
95 base::OneShotTimer<SimpleGeolocationRequest> timeout_timer_;
97 // Number of retry attempts.
98 unsigned retries_;
100 // This is updated on each retry.
101 Geoposition position_;
103 // Creation and destruction should happen on the same thread.
104 base::ThreadChecker thread_checker_;
106 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationRequest);
109 } // namespace chromeos
111 #endif // CHROME_BROWSER_CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_