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 CHROME_BROWSER_CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_
6 #define CHROME_BROWSER_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 "chrome/browser/chromeos/geolocation/geoposition.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_fetcher_delegate.h"
21 class URLRequestContextGetter
;
26 struct TimeZoneResponseData
{
34 REQUEST_ERROR
// local problem
37 TimeZoneResponseData();
39 std::string
ToStringForDebug() const;
43 std::string timeZoneId
;
44 std::string timeZoneName
;
45 std::string error_message
;
49 // Returns default timezone service URL.
50 GURL
DefaultTimezoneProviderURL();
52 // Takes Geoposition and sends it to a server to get local timezone information.
53 // It performs formatting of the request and interpretation of the response.
54 // If error occurs, request is retried until timeout.
55 // Zero timeout indicates single request.
56 // Request is owned and destroyed by caller (usually TimeZoneProvider).
57 // If request is destroyed while callback has not beed called yet, request
58 // is silently cancelled.
59 class TimeZoneRequest
: private net::URLFetcherDelegate
{
61 // Called when a new geo timezone information is available.
62 // The second argument indicates whether there was a server error or not.
63 // It is true when there was a server or network error - either no response
64 // or a 500 error code.
65 typedef base::Callback
<void(scoped_ptr
<TimeZoneResponseData
> /* timezone */,
66 bool /* server_error */)>
67 TimeZoneResponseCallback
;
69 // |url| is the server address to which the request wil be sent.
70 // |geoposition| is the location to query timezone for.
71 // |sensor| if this location was determined using hardware sensor.
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
,
77 base::TimeDelta retry_timeout
);
79 virtual ~TimeZoneRequest();
82 // Note: if request object is destroyed before callback is called,
83 // request will be silently cancelled.
84 void MakeRequest(TimeZoneResponseCallback callback
);
87 // net::URLFetcherDelegate
88 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
94 void Retry(bool server_error
);
96 scoped_refptr
<net::URLRequestContextGetter
> url_context_getter_
;
97 const GURL service_url_
;
98 Geoposition geoposition_
;
101 TimeZoneResponseCallback callback_
;
104 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
106 // When request was actually started.
107 base::Time request_started_at_
;
109 // Absolute time, when it is passed no more retry requests are allowed.
110 base::Time retry_timeout_abs_
;
113 base::OneShotTimer
<TimeZoneRequest
> timezone_request_scheduled_
;
115 // Number of retry attempts.
118 // Creation and destruction should happen on the same thread.
119 base::ThreadChecker thread_checker_
;
121 DISALLOW_COPY_AND_ASSIGN(TimeZoneRequest
);
124 } // namespace chromeos
126 #endif // CHROME_BROWSER_CHROMEOS_TIMEZONE_TIMEZONE_REQUEST_H_