blob: 848d031ab2f33a682fc0cf331d4e62ba9a857690 [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
dcheng0a6e80c2016-04-08 18:37:388#include <memory>
9
[email protected]72660d92014-03-24 02:25:2610#include "base/callback.h"
11#include "base/compiler_specific.h"
avi6e1a22d2015-12-21 03:43:2012#include "base/macros.h"
[email protected]72660d92014-03-24 02:25:2613#include "base/memory/ref_counted.h"
[email protected]72660d92014-03-24 02:25:2614#include "base/threading/thread_checker.h"
15#include "base/timer/timer.h"
satorux07e690912014-12-16 08:29:5316#include "chromeos/chromeos_export.h"
satoruxde6a5dc2014-12-12 22:30:3717#include "chromeos/geolocation/geoposition.h"
[email protected]72660d92014-03-24 02:25:2618#include "net/url_request/url_fetcher.h"
19#include "net/url_request/url_fetcher_delegate.h"
20#include "url/gurl.h"
21
22namespace net {
23class URLRequestContextGetter;
24}
25
26namespace chromeos {
27
satorux07e690912014-12-16 08:29:5328struct CHROMEOS_EXPORT TimeZoneResponseData {
[email protected]72660d92014-03-24 02:25:2629 enum Status {
30 OK,
31 INVALID_REQUEST,
32 OVER_QUERY_LIMIT,
33 REQUEST_DENIED,
34 UNKNOWN_ERROR,
35 ZERO_RESULTS,
36 REQUEST_ERROR // local problem
37 };
38
39 TimeZoneResponseData();
40
41 std::string ToStringForDebug() const;
42
43 double dstOffset;
44 double rawOffset;
45 std::string timeZoneId;
46 std::string timeZoneName;
47 std::string error_message;
48 Status status;
49};
50
51// Returns default timezone service URL.
satorux07e690912014-12-16 08:29:5352CHROMEOS_EXPORT GURL DefaultTimezoneProviderURL();
[email protected]72660d92014-03-24 02:25:2653
54// Takes Geoposition and sends it to a server to get local timezone information.
55// It performs formatting of the request and interpretation of the response.
56// If error occurs, request is retried until timeout.
57// Zero timeout indicates single request.
58// Request is owned and destroyed by caller (usually TimeZoneProvider).
59// If request is destroyed while callback has not beed called yet, request
60// is silently cancelled.
satorux07e690912014-12-16 08:29:5361class CHROMEOS_EXPORT TimeZoneRequest : private net::URLFetcherDelegate {
[email protected]72660d92014-03-24 02:25:2662 public:
63 // Called when a new geo timezone information is available.
64 // The second argument indicates whether there was a server error or not.
65 // It is true when there was a server or network error - either no response
66 // or a 500 error code.
dcheng0a6e80c2016-04-08 18:37:3867 typedef base::Callback<void(
68 std::unique_ptr<TimeZoneResponseData> /* timezone */,
69 bool /* server_error */)>
[email protected]72660d92014-03-24 02:25:2670 TimeZoneResponseCallback;
71
72 // |url| is the server address to which the request wil be sent.
73 // |geoposition| is the location to query timezone for.
[email protected]72660d92014-03-24 02:25:2674 // |retry_timeout| retry request on error until timeout.
75 TimeZoneRequest(net::URLRequestContextGetter* url_context_getter,
76 const GURL& service_url,
[email protected]9e844cf2014-04-19 02:36:0177 const Geoposition& geoposition,
[email protected]72660d92014-03-24 02:25:2678 base::TimeDelta retry_timeout);
79
dchengae98daa2015-01-21 20:30:4980 ~TimeZoneRequest() override;
[email protected]72660d92014-03-24 02:25:2681
82 // Initiates request.
83 // Note: if request object is destroyed before callback is called,
84 // request will be silently cancelled.
85 void MakeRequest(TimeZoneResponseCallback callback);
86
[email protected]56d77552014-05-22 05:51:5187 void set_retry_sleep_on_server_error_for_testing(
88 const base::TimeDelta value) {
89 retry_sleep_on_server_error_ = value;
90 }
91
92 void set_retry_sleep_on_bad_response_for_testing(
93 const base::TimeDelta value) {
94 retry_sleep_on_bad_response_ = value;
95 }
96
[email protected]72660d92014-03-24 02:25:2697 private:
98 // net::URLFetcherDelegate
dchengae98daa2015-01-21 20:30:4999 void OnURLFetchComplete(const net::URLFetcher* source) override;
[email protected]72660d92014-03-24 02:25:26100
101 // Start new request.
102 void StartRequest();
103
104 // Schedules retry.
105 void Retry(bool server_error);
106
107 scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
108 const GURL service_url_;
[email protected]9e844cf2014-04-19 02:36:01109 Geoposition geoposition_;
[email protected]72660d92014-03-24 02:25:26110
111 TimeZoneResponseCallback callback_;
112
113 GURL request_url_;
dcheng0a6e80c2016-04-08 18:37:38114 std::unique_ptr<net::URLFetcher> url_fetcher_;
[email protected]72660d92014-03-24 02:25:26115
116 // When request was actually started.
117 base::Time request_started_at_;
118
119 // Absolute time, when it is passed no more retry requests are allowed.
120 base::Time retry_timeout_abs_;
121
122 // Pending retry.
danakj8c3eb802015-09-24 07:53:00123 base::OneShotTimer timezone_request_scheduled_;
[email protected]72660d92014-03-24 02:25:26124
[email protected]56d77552014-05-22 05:51:51125 base::TimeDelta retry_sleep_on_server_error_;
126
127 base::TimeDelta retry_sleep_on_bad_response_;
128
[email protected]72660d92014-03-24 02:25:26129 // Number of retry attempts.
130 unsigned retries_;
131
132 // Creation and destruction should happen on the same thread.
133 base::ThreadChecker thread_checker_;
134
135 DISALLOW_COPY_AND_ASSIGN(TimeZoneRequest);
136};
137
138} // namespace chromeos
139
satorux07e690912014-12-16 08:29:53140#endif // CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_