Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / core / 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_CORE_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_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/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
17
18 class PrefChangeRegistrar;
19 class PrefService;
20
21 namespace base {
22 class ListValue;
23 class SequencedTaskRunner;
24 }
25
26 namespace data_reduction_proxy {
27
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 {
34 public:
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
39   // in the maps.
40   DataReductionProxyStatisticsPrefs(
41       PrefService* pref_service,
42       scoped_refptr<base::SequencedTaskRunner> task_runner,
43       const base::TimeDelta& delay);
44   ~DataReductionProxyStatisticsPrefs();
45
46   // Loads all data_reduction_proxy::prefs into the |pref_map_| and
47   // |list_pref_map_|.
48   void Init();
49
50   void ShutdownOnUIThread();
51
52   void OnUpdateContentLengths();
53
54   // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|.
55   int64 GetInt64(const char* pref_path);
56
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);
60
61   // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|.
62   base::ListValue* GetList(const char* pref_path);
63
64   // Writes the prefs stored in |DataReductionProxyPrefMap| and
65   // |DataReductionProxyListPrefMap| to |pref_service|.
66   void WritePrefs();
67
68 private:
69   typedef std::map<const char*, int64> DataReductionProxyPrefMap;
70   typedef base::ScopedPtrHashMap<const char*, base::ListValue>
71       DataReductionProxyListPrefMap;
72
73   // Gets the value of |pref| from the pref service and adds it to the
74   // |pref_map|.
75   void InitInt64Pref(const char* pref);
76
77   // Gets the value of |pref| from the pref service and adds it to the
78   // |list_pref_map|.
79   void InitListPref(const char* pref);
80
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();
84
85   // Copies the values at each index of |from_list| to the same index in
86   // |to_list|.
87   void TransferList(const base::ListValue& from_list,
88                     base::ListValue* to_list);
89
90   // Gets an int64, stored as a string, in a ListPref at the specified
91   // index.
92   int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index);
93
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_;
103
104   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyStatisticsPrefs);
105 };
106
107 }  // namespace data_reduction_proxy
108
109 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATISTICS_PREFS_H_