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_RESOLVER_H_
6 #define CHROMEOS_TIMEZONE_TIMEZONE_RESOLVER_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/threading/thread_checker.h"
11 #include "chromeos/chromeos_export.h"
12 #include "net/url_request/url_request_context_getter.h"
15 class PrefRegistrySimple
;
20 struct TimeZoneResponseData
;
22 // This class implements periodic timezone synchronization.
23 class CHROMEOS_EXPORT TimeZoneResolver
{
25 class TimeZoneResolverImpl
;
27 // This callback will be called when new timezone arrives.
28 using ApplyTimeZoneCallback
=
29 base::Callback
<void(const TimeZoneResponseData
*)>;
31 // chromeos::DelayNetworkCall cannot be used directly due to link
33 using DelayNetworkCallClosure
= base::Callback
<void(const base::Closure
&)>;
35 // This is a LocalState preference to store base::Time value of the last
36 // request. It is used to limit request rate on browser restart.
37 static const char kLastTimeZoneRefreshTime
[];
39 TimeZoneResolver(scoped_refptr
<net::URLRequestContextGetter
> context
,
41 const ApplyTimeZoneCallback
& apply_timezone
,
42 const DelayNetworkCallClosure
& delay_network_call
,
43 PrefService
* local_state
);
46 // Starts periodic timezone refresh.
49 // Cancels current request and stops periodic timezone refresh.
52 // Register prefs to LocalState.
53 static void RegisterPrefs(PrefRegistrySimple
* registry
);
55 scoped_refptr
<net::URLRequestContextGetter
> context() const {
59 DelayNetworkCallClosure
delay_network_call() const {
60 return delay_network_call_
;
63 ApplyTimeZoneCallback
apply_timezone() const { return apply_timezone_
; }
65 PrefService
* local_state() const { return local_state_
; }
67 // Expose internal fuctions for testing.
68 static int MaxRequestsCountForIntervalForTesting(
69 const double interval_seconds
);
70 static int IntervalForNextRequestForTesting(const int requests
);
73 scoped_refptr
<net::URLRequestContextGetter
> context_
;
76 const ApplyTimeZoneCallback apply_timezone_
;
77 const DelayNetworkCallClosure delay_network_call_
;
78 PrefService
* local_state_
;
80 scoped_ptr
<TimeZoneResolverImpl
> implementation_
;
82 base::ThreadChecker thread_checker_
;
84 DISALLOW_COPY_AND_ASSIGN(TimeZoneResolver
);
87 } // namespace chromeos
89 #endif // CHROMEOS_TIMEZONE_TIMEZONE_RESOLVER_H_