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 COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
10 #include "base/containers/scoped_ptr_hash_map.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
18 class PrefChangeRegistrar;
23 class SequencedTaskRunner;
26 namespace data_reduction_proxy {
28 // Data reduction proxy delayed pref service reduces the number calls to pref
29 // service by storing prefs in memory and writing to the given PrefService after
30 // |delay| amount of time. If |delay| is zero, the delayed pref service writes
31 // directly to the PrefService and does not store the prefs in memory. All
32 // prefs must be stored and read on the UI thread.
33 class DataReductionProxyStatisticsPrefs {
35 // Constructs a data reduction proxy delayed pref service object using
36 // |pref_service|. Writes prefs to |pref_service| after |delay|
37 // and stores them in |pref_map_| and |list_pref_map| between writes.
38 // If |delay| is zero, writes directly to the PrefService and does not store
40 DataReductionProxyStatisticsPrefs(
41 PrefService* pref_service,
42 scoped_refptr<base::SequencedTaskRunner> task_runner,
43 const base::TimeDelta& delay);
44 ~DataReductionProxyStatisticsPrefs();
46 // Loads all data_reduction_proxy::prefs into the |pref_map_| and
50 void ShutdownOnUIThread();
52 void OnUpdateContentLengths();
54 // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|.
55 int64 GetInt64(const char* pref_path);
57 // Updates the pref value in the |DataReductionProxyPrefMap| map.
58 // The pref is later written to |pref service_|.
59 void SetInt64(const char* pref_path, int64 pref_value);
61 // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|.
62 base::ListValue* GetList(const char* pref_path);
64 // Writes the prefs stored in |DataReductionProxyPrefMap| and
65 // |DataReductionProxyListPrefMap| to |pref_service|.
69 typedef std::map<const char*, int64> DataReductionProxyPrefMap;
70 typedef base::ScopedPtrHashMap<const char*, base::ListValue>
71 DataReductionProxyListPrefMap;
73 // Gets the value of |pref| from the pref service and adds it to the
75 void InitInt64Pref(const char* pref);
77 // Gets the value of |pref| from the pref service and adds it to the
79 void InitListPref(const char* pref);
81 // Writes the stored prefs to |pref_service| and then posts another a delayed
82 // task to write prefs again in |kMinutesBetweenWrites|.
83 void DelayedWritePrefs();
85 // Copies the values at each index of |from_list| to the same index in
87 void TransferList(const base::ListValue& from_list,
88 base::ListValue* to_list);
90 // Gets an int64, stored as a string, in a ListPref at the specified
92 int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index);
94 PrefService* pref_service_;
95 scoped_refptr<base::SequencedTaskRunner> task_runner_;
96 const base::TimeDelta delay_;
97 bool delayed_task_posted_;
98 DataReductionProxyPrefMap pref_map_;
99 DataReductionProxyListPrefMap list_pref_map_;
100 base::WeakPtrFactory<DataReductionProxyStatisticsPrefs> weak_factory_;
101 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
102 base::ThreadChecker thread_checker_;
104 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyStatisticsPrefs);
107 } // namespace data_reduction_proxy
109 #endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_