Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_statistics_prefs.h
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 COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
7
8 #include <map>
9
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"
16
17 class PrefService;
18
19 namespace base {
20 class ListValue;
21 class SequencedTaskRunner;
22 }
23
24 namespace data_reduction_proxy {
25
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 {
32 public:
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
37   // in the maps.
38   DataReductionProxyStatisticsPrefs(
39       PrefService* pref_service,
40       scoped_refptr<base::SequencedTaskRunner> task_runner,
41       const base::TimeDelta& delay);
42   ~DataReductionProxyStatisticsPrefs();
43
44   // Loads all data_reduction_proxy::prefs into the |pref_map_| and
45   // |list_pref_map_|.
46   void Init();
47
48   // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|.
49   int64 GetInt64(const char* pref_path);
50
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);
54
55   // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|.
56   base::ListValue* GetList(const char* pref_path);
57
58   // Writes the prefs stored in |DataReductionProxyPrefMap| and
59   // |DataReductionProxyListPrefMap| to |pref_service|.
60   void WritePrefs();
61
62 private:
63   typedef std::map<const char*, int64> DataReductionProxyPrefMap;
64   typedef base::ScopedPtrHashMap<const char*, base::ListValue>
65       DataReductionProxyListPrefMap;
66
67   // Gets the value of |pref| from the pref service and adds it to the
68   // |pref_map|.
69   void InitInt64Pref(const char* pref);
70
71   // Gets the value of |pref| from the pref service and adds it to the
72   // |list_pref_map|.
73   void InitListPref(const char* pref);
74
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();
78
79   // Copies the values at each index of |from_list| to the same index in
80   // |to_list|.
81   void TransferList(const base::ListValue& from_list,
82                     base::ListValue* to_list);
83
84   // Gets an int64, stored as a string, in a ListPref at the specified
85   // index.
86   int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index);
87
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_;
95
96   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyStatisticsPrefs);
97 };
98
99 }  // namespace data_reduction_proxy
100
101 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_