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"
22 class URLRequestContextGetter
;
27 struct CHROMEOS_EXPORT TimeZoneResponseData
{
35 REQUEST_ERROR
// local problem
38 TimeZoneResponseData();
40 std::string
ToStringForDebug() const;
44 std::string timeZoneId
;
45 std::string timeZoneName
;
46 std::string error_message
;
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
{
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
;
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
;
96 // net::URLFetcherDelegate
97 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
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_
;
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_
;
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.
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_