| 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 COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATIS
TICS_PREFS_H_ | |
| 6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STATIS
TICS_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 class Value; | |
| 25 } | |
| 26 | |
| 27 namespace data_reduction_proxy { | |
| 28 | |
| 29 // Data reduction proxy delayed pref service reduces the number calls to pref | |
| 30 // service by storing prefs in memory and writing to the given PrefService after | |
| 31 // |delay| amount of time. If |delay| is zero, the delayed pref service writes | |
| 32 // directly to the PrefService and does not store the prefs in memory. All | |
| 33 // prefs must be stored and read on the UI thread. | |
| 34 // TODO(bengr): Rename as DataReductionProxyCompressionStats. | |
| 35 class DataReductionProxyStatisticsPrefs { | |
| 36 public: | |
| 37 // Constructs a data reduction proxy delayed pref service object using | |
| 38 // |pref_service|. Writes prefs to |pref_service| after |delay| | |
| 39 // and stores them in |pref_map_| and |list_pref_map| between writes. | |
| 40 // If |delay| is zero, writes directly to the PrefService and does not store | |
| 41 // in the maps. | |
| 42 DataReductionProxyStatisticsPrefs( | |
| 43 PrefService* pref_service, | |
| 44 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 45 const base::TimeDelta& delay); | |
| 46 ~DataReductionProxyStatisticsPrefs(); | |
| 47 | |
| 48 // Loads all data_reduction_proxy::prefs into the |pref_map_| and | |
| 49 // |list_pref_map_|. | |
| 50 void Init(); | |
| 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 // Creates a |Value| summary of the persistent state of the network session. | |
| 69 // The caller is responsible for deleting the returned value. | |
| 70 // Must be called on the UI thread. | |
| 71 base::Value* HistoricNetworkStatsInfoToValue(); | |
| 72 | |
| 73 base::WeakPtr<DataReductionProxyStatisticsPrefs> GetWeakPtr(); | |
| 74 | |
| 75 private: | |
| 76 typedef std::map<const char*, int64> DataReductionProxyPrefMap; | |
| 77 typedef base::ScopedPtrHashMap<const char*, base::ListValue> | |
| 78 DataReductionProxyListPrefMap; | |
| 79 | |
| 80 // Gets the value of |pref| from the pref service and adds it to the | |
| 81 // |pref_map|. | |
| 82 void InitInt64Pref(const char* pref); | |
| 83 | |
| 84 // Gets the value of |pref| from the pref service and adds it to the | |
| 85 // |list_pref_map|. | |
| 86 void InitListPref(const char* pref); | |
| 87 | |
| 88 // Writes the stored prefs to |pref_service| and then posts another a delayed | |
| 89 // task to write prefs again in |kMinutesBetweenWrites|. | |
| 90 void DelayedWritePrefs(); | |
| 91 | |
| 92 // Copies the values at each index of |from_list| to the same index in | |
| 93 // |to_list|. | |
| 94 void TransferList(const base::ListValue& from_list, | |
| 95 base::ListValue* to_list); | |
| 96 | |
| 97 // Gets an int64, stored as a string, in a ListPref at the specified | |
| 98 // index. | |
| 99 int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index); | |
| 100 | |
| 101 // Clears all data saving statistics. | |
| 102 void ClearDataSavingStatistics(); | |
| 103 | |
| 104 PrefService* pref_service_; | |
| 105 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 106 const base::TimeDelta delay_; | |
| 107 bool delayed_task_posted_; | |
| 108 DataReductionProxyPrefMap pref_map_; | |
| 109 DataReductionProxyListPrefMap list_pref_map_; | |
| 110 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
| 111 base::ThreadChecker thread_checker_; | |
| 112 | |
| 113 base::WeakPtrFactory<DataReductionProxyStatisticsPrefs> weak_factory_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyStatisticsPrefs); | |
| 116 }; | |
| 117 | |
| 118 } // namespace data_reduction_proxy | |
| 119 | |
| 120 #endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_STA
TISTICS_PREFS_H_ | |
| OLD | NEW |