OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROMEOS_TIMEZONE_TIMEZONE_RESOLVER_H_ |
| 6 #define CHROMEOS_TIMEZONE_TIMEZONE_RESOLVER_H_ |
| 7 |
| 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" |
| 14 |
| 15 class PrefRegistrySimple; |
| 16 class PrefService; |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 struct TimeZoneResponseData; |
| 21 |
| 22 // This class implements periodic timezone synchronization. |
| 23 class CHROMEOS_EXPORT TimeZoneResolver { |
| 24 public: |
| 25 class TimeZoneResolverImpl; |
| 26 |
| 27 // This callback will be called when new timezone arrives. |
| 28 using ApplyTimeZoneCallback = |
| 29 base::Callback<void(const TimeZoneResponseData*)>; |
| 30 |
| 31 // chromeos::DelayNetworkCall cannot be used directly due to link |
| 32 // restrictions. |
| 33 using DelayNetworkCallClosure = base::Callback<void(const base::Closure&)>; |
| 34 |
| 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[]; |
| 38 |
| 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(); |
| 45 |
| 46 // Starts periodic timezone refresh. |
| 47 void Start(); |
| 48 |
| 49 // Cancels current request and stops periodic timezone refresh. |
| 50 void Stop(); |
| 51 |
| 52 // Register prefs to LocalState. |
| 53 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 54 |
| 55 scoped_refptr<net::URLRequestContextGetter> context() const { |
| 56 return context_; |
| 57 } |
| 58 |
| 59 DelayNetworkCallClosure delay_network_call() const { |
| 60 return delay_network_call_; |
| 61 } |
| 62 |
| 63 ApplyTimeZoneCallback apply_timezone() const { return apply_timezone_; } |
| 64 |
| 65 PrefService* local_state() const { return local_state_; } |
| 66 |
| 67 // Expose internal fuctions for testing. |
| 68 static int MaxRequestsCountForIntervalForTesting( |
| 69 const double interval_seconds); |
| 70 static int IntervalForNextRequestForTesting(const int requests); |
| 71 |
| 72 private: |
| 73 scoped_refptr<net::URLRequestContextGetter> context_; |
| 74 const GURL url_; |
| 75 |
| 76 const ApplyTimeZoneCallback apply_timezone_; |
| 77 const DelayNetworkCallClosure delay_network_call_; |
| 78 PrefService* local_state_; |
| 79 |
| 80 scoped_ptr<TimeZoneResolverImpl> implementation_; |
| 81 |
| 82 base::ThreadChecker thread_checker_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(TimeZoneResolver); |
| 85 }; |
| 86 |
| 87 } // namespace chromeos |
| 88 |
| 89 #endif // CHROMEOS_TIMEZONE_TIMEZONE_RESOLVER_H_ |
OLD | NEW |