blob: 66af10ca68ab6d52a770a2fe2186cb6a40a5aca8 [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
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"
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.
72 // |sensor| if this location was determined using hardware sensor.
73 // |retry_timeout| retry request on error until timeout.
74 TimeZoneRequest(net::URLRequestContextGetter* url_context_getter,
75 const GURL& service_url,
[email protected]9e844cf2014-04-19 02:36:0176 const Geoposition& geoposition,
[email protected]72660d92014-03-24 02:25:2677 bool sensor,
78 base::TimeDelta retry_timeout);
79
80 virtual ~TimeZoneRequest();
81
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
mostynb9108efb92014-10-03 23:46:1599 virtual 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 const bool sensor_;
111
112 TimeZoneResponseCallback callback_;
113
114 GURL request_url_;
115 scoped_ptr<net::URLFetcher> url_fetcher_;
116
117 // When request was actually started.
118 base::Time request_started_at_;
119
120 // Absolute time, when it is passed no more retry requests are allowed.
121 base::Time retry_timeout_abs_;
122
123 // Pending retry.
124 base::OneShotTimer<TimeZoneRequest> timezone_request_scheduled_;
125
[email protected]56d77552014-05-22 05:51:51126 base::TimeDelta retry_sleep_on_server_error_;
127
128 base::TimeDelta retry_sleep_on_bad_response_;
129
[email protected]72660d92014-03-24 02:25:26130 // Number of retry attempts.
131 unsigned retries_;
132
133 // Creation and destruction should happen on the same thread.
134 base::ThreadChecker thread_checker_;
135
136 DISALLOW_COPY_AND_ASSIGN(TimeZoneRequest);
137};
138
139} // namespace chromeos
140
satorux07e690912014-12-16 08:29:53141#endif // CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_