Roll src/third_party/skia 726cf90:183b57f
[chromium-blink-merge.git] / chromeos / timezone / timezone_request.h
blobe90857823046cb641cc0675547719a99a7f8fd8b
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_TIMEZONE_TIMEZONE_REQUEST_H_
6 #define CHROMEOS_TIMEZONE_TIMEZONE_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/chromeos_export.h"
16 #include "chromeos/geolocation/geoposition.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "url/gurl.h"
21 namespace net {
22 class URLRequestContextGetter;
25 namespace chromeos {
27 struct CHROMEOS_EXPORT TimeZoneResponseData {
28 enum Status {
29 OK,
30 INVALID_REQUEST,
31 OVER_QUERY_LIMIT,
32 REQUEST_DENIED,
33 UNKNOWN_ERROR,
34 ZERO_RESULTS,
35 REQUEST_ERROR // local problem
38 TimeZoneResponseData();
40 std::string ToStringForDebug() const;
42 double dstOffset;
43 double rawOffset;
44 std::string timeZoneId;
45 std::string timeZoneName;
46 std::string error_message;
47 Status status;
50 // Returns default timezone service URL.
51 CHROMEOS_EXPORT GURL DefaultTimezoneProviderURL();
53 // Takes Geoposition and sends it to a server to get local timezone information.
54 // It performs formatting of the request and interpretation of the response.
55 // If error occurs, request is retried until timeout.
56 // Zero timeout indicates single request.
57 // Request is owned and destroyed by caller (usually TimeZoneProvider).
58 // If request is destroyed while callback has not beed called yet, request
59 // is silently cancelled.
60 class CHROMEOS_EXPORT TimeZoneRequest : private net::URLFetcherDelegate {
61 public:
62 // Called when a new geo timezone information is available.
63 // The second argument indicates whether there was a server error or not.
64 // It is true when there was a server or network error - either no response
65 // or a 500 error code.
66 typedef base::Callback<void(scoped_ptr<TimeZoneResponseData> /* timezone */,
67 bool /* server_error */)>
68 TimeZoneResponseCallback;
70 // |url| is the server address to which the request wil be sent.
71 // |geoposition| is the location to query timezone for.
72 // |retry_timeout| retry request on error until timeout.
73 TimeZoneRequest(net::URLRequestContextGetter* url_context_getter,
74 const GURL& service_url,
75 const Geoposition& geoposition,
76 base::TimeDelta retry_timeout);
78 ~TimeZoneRequest() override;
80 // Initiates request.
81 // Note: if request object is destroyed before callback is called,
82 // request will be silently cancelled.
83 void MakeRequest(TimeZoneResponseCallback callback);
85 void set_retry_sleep_on_server_error_for_testing(
86 const base::TimeDelta value) {
87 retry_sleep_on_server_error_ = value;
90 void set_retry_sleep_on_bad_response_for_testing(
91 const base::TimeDelta value) {
92 retry_sleep_on_bad_response_ = value;
95 private:
96 // net::URLFetcherDelegate
97 void OnURLFetchComplete(const net::URLFetcher* source) override;
99 // Start new request.
100 void StartRequest();
102 // Schedules retry.
103 void Retry(bool server_error);
105 scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
106 const GURL service_url_;
107 Geoposition geoposition_;
109 TimeZoneResponseCallback callback_;
111 GURL request_url_;
112 scoped_ptr<net::URLFetcher> url_fetcher_;
114 // When request was actually started.
115 base::Time request_started_at_;
117 // Absolute time, when it is passed no more retry requests are allowed.
118 base::Time retry_timeout_abs_;
120 // Pending retry.
121 base::OneShotTimer<TimeZoneRequest> timezone_request_scheduled_;
123 base::TimeDelta retry_sleep_on_server_error_;
125 base::TimeDelta retry_sleep_on_bad_response_;
127 // Number of retry attempts.
128 unsigned retries_;
130 // Creation and destruction should happen on the same thread.
131 base::ThreadChecker thread_checker_;
133 DISALLOW_COPY_AND_ASSIGN(TimeZoneRequest);
136 } // namespace chromeos
138 #endif // CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_