blob: 8c584ccd9a1722c9d531c45c17a3366055c1a2e8 [file] [log] [blame]
[email protected]72660d92014-03-24 02:25:261// 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
satorux07e690912014-12-16 08:29:535#ifndef CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_
6#define CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_
[email protected]72660d92014-03-24 02:25:267
[email protected]72660d92014-03-24 02:25:268#include "base/callback.h"
9#include "base/compiler_specific.h"
avi6e1a22d2015-12-21 03:43:2010#include "base/macros.h"
[email protected]72660d92014-03-24 02:25:2611#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"
satorux07e690912014-12-16 08:29:5315#include "chromeos/chromeos_export.h"
satoruxde6a5dc2014-12-12 22:30:3716#include "chromeos/geolocation/geoposition.h"
[email protected]72660d92014-03-24 02:25:2617#include "net/url_request/url_fetcher.h"
18#include "net/url_request/url_fetcher_delegate.h"
19#include "url/gurl.h"
20
21namespace net {
22class URLRequestContextGetter;
23}
24
25namespace chromeos {
26
satorux07e690912014-12-16 08:29:5327struct CHROMEOS_EXPORT TimeZoneResponseData {
[email protected]72660d92014-03-24 02:25:2628 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
36 };
37
38 TimeZoneResponseData();
39
40 std::string ToStringForDebug() const;
41
42 double dstOffset;
43 double rawOffset;
44 std::string timeZoneId;
45 std::string timeZoneName;
46 std::string error_message;
47 Status status;
48};
49
50// Returns default timezone service URL.
satorux07e690912014-12-16 08:29:5351CHROMEOS_EXPORT GURL DefaultTimezoneProviderURL();
[email protected]72660d92014-03-24 02:25:2652
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.
satorux07e690912014-12-16 08:29:5360class CHROMEOS_EXPORT TimeZoneRequest : private net::URLFetcherDelegate {
[email protected]72660d92014-03-24 02:25:2661 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;
69
70 // |url| is the server address to which the request wil be sent.
71 // |geoposition| is the location to query timezone for.
[email protected]72660d92014-03-24 02:25:2672 // |retry_timeout| retry request on error until timeout.
73 TimeZoneRequest(net::URLRequestContextGetter* url_context_getter,
74 const GURL& service_url,
[email protected]9e844cf2014-04-19 02:36:0175 const Geoposition& geoposition,
[email protected]72660d92014-03-24 02:25:2676 base::TimeDelta retry_timeout);
77
dchengae98daa2015-01-21 20:30:4978 ~TimeZoneRequest() override;
[email protected]72660d92014-03-24 02:25:2679
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);
84
[email protected]56d77552014-05-22 05:51:5185 void set_retry_sleep_on_server_error_for_testing(
86 const base::TimeDelta value) {
87 retry_sleep_on_server_error_ = value;
88 }
89
90 void set_retry_sleep_on_bad_response_for_testing(
91 const base::TimeDelta value) {
92 retry_sleep_on_bad_response_ = value;
93 }
94
[email protected]72660d92014-03-24 02:25:2695 private:
96 // net::URLFetcherDelegate
dchengae98daa2015-01-21 20:30:4997 void OnURLFetchComplete(const net::URLFetcher* source) override;
[email protected]72660d92014-03-24 02:25:2698
99 // Start new request.
100 void StartRequest();
101
102 // Schedules retry.
103 void Retry(bool server_error);
104
105 scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
106 const GURL service_url_;
[email protected]9e844cf2014-04-19 02:36:01107 Geoposition geoposition_;
[email protected]72660d92014-03-24 02:25:26108
109 TimeZoneResponseCallback callback_;
110
111 GURL request_url_;
112 scoped_ptr<net::URLFetcher> url_fetcher_;
113
114 // When request was actually started.
115 base::Time request_started_at_;
116
117 // Absolute time, when it is passed no more retry requests are allowed.
118 base::Time retry_timeout_abs_;
119
120 // Pending retry.
danakj8c3eb802015-09-24 07:53:00121 base::OneShotTimer timezone_request_scheduled_;
[email protected]72660d92014-03-24 02:25:26122
[email protected]56d77552014-05-22 05:51:51123 base::TimeDelta retry_sleep_on_server_error_;
124
125 base::TimeDelta retry_sleep_on_bad_response_;
126
[email protected]72660d92014-03-24 02:25:26127 // Number of retry attempts.
128 unsigned retries_;
129
130 // Creation and destruction should happen on the same thread.
131 base::ThreadChecker thread_checker_;
132
133 DISALLOW_COPY_AND_ASSIGN(TimeZoneRequest);
134};
135
136} // namespace chromeos
137
satorux07e690912014-12-16 08:29:53138#endif // CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_