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_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_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/time/time.h"
15 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
21 class SequencedTaskRunner;
24 namespace data_reduction_proxy {
26 // Data reduction proxy delayed pref service reduces the number calls to pref
27 // service by storing prefs in memory and writing to the given PrefService after
28 // |delay| amount of time. If |delay| is zero, the delayed pref service writes
29 // directly to the PrefService and does not store the prefs in memory. All
30 // prefs must be stored and read on the UI thread.
31 class DataReductionProxyStatisticsPrefs {
33 // Constructs a data reduction proxy delayed pref service object using
34 // |pref_service|. Writes prefs to |pref_service| after |delay|
35 // and stores them in |pref_map_| and |list_pref_map| between writes.
36 // If |delay| is zero, writes directly to the PrefService and does not store
38 DataReductionProxyStatisticsPrefs(
39 PrefService* pref_service,
40 scoped_refptr<base::SequencedTaskRunner> task_runner,
41 const base::TimeDelta& delay);
42 ~DataReductionProxyStatisticsPrefs();
44 // Loads all data_reduction_proxy::prefs into the |pref_map_| and
48 // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|.
49 int64 GetInt64(const char* pref_path);
51 // Updates the pref value in the |DataReductionProxyPrefMap| map.
52 // The pref is later written to |pref service_|.
53 void SetInt64(const char* pref_path, int64 pref_value);
55 // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|.
56 base::ListValue* GetList(const char* pref_path);
58 // Writes the prefs stored in |DataReductionProxyPrefMap| and
59 // |DataReductionProxyListPrefMap| to |pref_service|.
63 typedef std::map<const char*, int64> DataReductionProxyPrefMap;
64 typedef base::ScopedPtrHashMap<const char*, base::ListValue>
65 DataReductionProxyListPrefMap;
67 // Gets the value of |pref| from the pref service and adds it to the
69 void InitInt64Pref(const char* pref);
71 // Gets the value of |pref| from the pref service and adds it to the
73 void InitListPref(const char* pref);
75 // Writes the stored prefs to |pref_service| and then posts another a delayed
76 // task to write prefs again in |kMinutesBetweenWrites|.
77 void DelayedWritePrefs();
79 // Copies the values at each index of |from_list| to the same index in
81 void TransferList(const base::ListValue& from_list,
82 base::ListValue* to_list);
84 // Gets an int64, stored as a string, in a ListPref at the specified
86 int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index);
88 PrefService* pref_service_;
89 scoped_refptr<base::SequencedTaskRunner> task_runner_;
90 base::WeakPtrFactory<DataReductionProxyStatisticsPrefs> weak_factory_;
91 const base::TimeDelta delay_;
92 bool delayed_task_posted_;
93 DataReductionProxyPrefMap pref_map_;
94 DataReductionProxyListPrefMap list_pref_map_;
96 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyStatisticsPrefs);
99 } // namespace data_reduction_proxy
101 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_