Roll src/third_party/WebKit dfec292:200a784 (svn 202235:202237)
[chromium-blink-merge.git] / chromeos / timezone / timezone_resolver.h
blob231575bb21aa415b6c5ea6ea64ee9d58bcf1181f
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"
13 #include "url/gurl.h"
15 class PrefRegistrySimple;
16 class PrefService;
18 namespace chromeos {
20 struct TimeZoneResponseData;
22 // This class implements periodic timezone synchronization.
23 class CHROMEOS_EXPORT TimeZoneResolver {
24 public:
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
32 // restrictions.
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,
40 const GURL& url,
41 const ApplyTimeZoneCallback& apply_timezone,
42 const DelayNetworkCallClosure& delay_network_call,
43 PrefService* local_state);
44 ~TimeZoneResolver();
46 // Starts periodic timezone refresh.
47 void Start();
49 // Cancels current request and stops periodic timezone refresh.
50 void Stop();
52 // Register prefs to LocalState.
53 static void RegisterPrefs(PrefRegistrySimple* registry);
55 scoped_refptr<net::URLRequestContextGetter> context() const {
56 return context_;
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);
72 private:
73 scoped_refptr<net::URLRequestContextGetter> context_;
74 const GURL url_;
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_