OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SDCH_SDCH_OWNER_H_ | 5 #ifndef NET_SDCH_SDCH_OWNER_H_ |
6 #define NET_SDCH_SDCH_OWNER_H_ | 6 #define NET_SDCH_SDCH_OWNER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/memory_pressure_listener.h" | 15 #include "base/memory/memory_pressure_listener.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/prefs/pref_store.h" | |
18 #include "net/base/sdch_observer.h" | 17 #include "net/base/sdch_observer.h" |
19 #include "net/url_request/sdch_dictionary_fetcher.h" | 18 #include "net/url_request/sdch_dictionary_fetcher.h" |
20 | 19 |
21 class GURL; | 20 class GURL; |
22 class PersistentPrefStore; | |
23 class ValueMapPrefStore; | |
24 class WriteablePrefStore; | |
25 | 21 |
26 namespace base { | 22 namespace base { |
27 class Clock; | 23 class Clock; |
28 } | 24 } |
29 | 25 |
30 namespace net { | 26 namespace net { |
31 class SdchManager; | 27 class SdchManager; |
32 class URLRequestContext; | 28 class URLRequestContext; |
33 | 29 |
34 // This class owns the SDCH objects not owned as part of URLRequestContext, and | 30 // This class owns the SDCH objects not owned as part of URLRequestContext, and |
35 // exposes interface for setting SDCH policy. It should be instantiated by | 31 // exposes interface for setting SDCH policy. It should be instantiated by |
36 // the net/ embedder. | 32 // the net/ embedder. |
37 // TODO(rdsmith): Implement dictionary prioritization. | 33 // TODO(rdsmith): Implement dictionary prioritization. |
38 class NET_EXPORT SdchOwner : public SdchObserver, public PrefStore::Observer { | 34 class NET_EXPORT SdchOwner : public SdchObserver { |
39 public: | 35 public: |
| 36 // Abstact storage interface for storing settings that allows the embedder |
| 37 // to provide the appropriate storage backend. |
| 38 class NET_EXPORT PrefStorage { |
| 39 public: |
| 40 // Possible values returned by GetReadError. This is a subset of the error |
| 41 // values of Chromium's pref storage that we care about. |
| 42 // |
| 43 // DO NOT CHANGE VALUES. This is logged persistently in a histogram. |
| 44 enum ReadError { |
| 45 PERSISTENCE_FAILURE_NONE = 0, |
| 46 |
| 47 // File didn't exist; is being created. |
| 48 PERSISTENCE_FAILURE_REASON_NO_FILE = 1, |
| 49 |
| 50 // Error reading in information, but should be able to write. |
| 51 PERSISTENCE_FAILURE_REASON_READ_FAILED = 2, |
| 52 |
| 53 // Error leading to abort on attempted persistence. |
| 54 PERSISTENCE_FAILURE_REASON_WRITE_FAILED = 3, |
| 55 |
| 56 // Anything else. |
| 57 PERSISTENCE_FAILURE_REASON_OTHER = 4, |
| 58 |
| 59 PERSISTENCE_FAILURE_REASON_MAX = 5 |
| 60 // See RecordPersistenceFailure for UMA logging of this value if |
| 61 // adding a value here. |
| 62 }; |
| 63 |
| 64 virtual ~PrefStorage(); |
| 65 |
| 66 // Returns the read error if any. Valid to be called after initialization |
| 67 // is complete (see IsInitializationComplete). |
| 68 virtual ReadError GetReadError() const = 0; |
| 69 |
| 70 // Gets or sets the value in the preferences store. |
| 71 virtual bool GetValue(const base::DictionaryValue** result) const = 0; |
| 72 virtual bool GetMutableValue(base::DictionaryValue** result) = 0; |
| 73 virtual void SetValue(scoped_ptr<base::DictionaryValue> value) = 0; |
| 74 |
| 75 // Notifies the storage system that a value was changed via mutating the |
| 76 // result of GetMutableValue(). |
| 77 virtual void ReportValueChanged() = 0; |
| 78 |
| 79 // Returns true if the store's init is complete. See the Start/Stop |
| 80 // functions below for observing changes to this value. |
| 81 virtual bool IsInitializationComplete() = 0; |
| 82 |
| 83 // Starts and stops observing preferences storage init. There will only |
| 84 // be one observer active at a time. The store should call |
| 85 // OnPrefStorageInitializationComplete() when it transitions to initialized |
| 86 // and there is an observer active. See also IsInitializationComplete(). |
| 87 virtual void StartObservingInit(SdchOwner* observer) = 0; |
| 88 virtual void StopObservingInit() = 0; |
| 89 }; |
| 90 |
40 static const size_t kMaxTotalDictionarySize; | 91 static const size_t kMaxTotalDictionarySize; |
41 static const size_t kMinSpaceForDictionaryFetch; | 92 static const size_t kMinSpaceForDictionaryFetch; |
42 | 93 |
43 // Consumer must guarantee that |sdch_manager| and |context| outlive | 94 // Consumer must guarantee that |sdch_manager| and |context| outlive |
44 // this object. | 95 // this object. |
45 SdchOwner(SdchManager* sdch_manager, URLRequestContext* context); | 96 SdchOwner(SdchManager* sdch_manager, URLRequestContext* context); |
46 ~SdchOwner() override; | 97 ~SdchOwner() override; |
47 | 98 |
48 // Enables use of pref persistence. Note that |pref_store| is owned | 99 // Enables use of pref persistence. Ownership of the storage will be passed. |
49 // by the caller, but must be guaranteed to outlive SdchOwner. The | 100 // This routine may only be called once per SdchOwner instance. |
50 // actual mechanisms by which the PersistentPrefStore are persisted | 101 void EnablePersistentStorage(scoped_ptr<PrefStorage> pref_store); |
51 // are the responsibility of the caller. This routine may only be | |
52 // called once per SdchOwner instance. | |
53 void EnablePersistentStorage(PersistentPrefStore* pref_store); | |
54 | 102 |
55 // Defaults to kMaxTotalDictionarySize. | 103 // Defaults to kMaxTotalDictionarySize. |
56 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); | 104 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); |
57 | 105 |
58 // Defaults to kMinSpaceForDictionaryFetch. | 106 // Defaults to kMinSpaceForDictionaryFetch. |
59 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); | 107 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); |
60 | 108 |
61 // SdchObserver implementation. | 109 // SdchObserver implementation. |
62 void OnDictionaryAdded(const GURL& dictionary_url, | 110 void OnDictionaryAdded(const GURL& dictionary_url, |
63 const std::string& server_hash) override; | 111 const std::string& server_hash) override; |
64 void OnDictionaryRemoved(const std::string& server_hash) override; | 112 void OnDictionaryRemoved(const std::string& server_hash) override; |
65 void OnDictionaryUsed(const std::string& server_hash) override; | 113 void OnDictionaryUsed(const std::string& server_hash) override; |
66 void OnGetDictionary(const GURL& request_url, | 114 void OnGetDictionary(const GURL& request_url, |
67 const GURL& dictionary_url) override; | 115 const GURL& dictionary_url) override; |
68 void OnClearDictionaries() override; | 116 void OnClearDictionaries() override; |
69 | 117 |
70 // PrefStore::Observer implementation. | 118 // Called by the PrefStorage implementation when initialization is complete. |
71 void OnPrefValueChanged(const std::string& key) override; | 119 // See PrefStorage::StartObservingInit(). |
72 void OnInitializationCompleted(bool succeeded) override; | 120 void OnPrefStorageInitializationComplete(bool succeeded); |
73 | 121 |
74 // Implementation detail--this is the function callback by the callback passed | 122 // Implementation detail--this is the function callback by the callback passed |
75 // to the fetcher through which the fetcher informs the SdchOwner that it's | 123 // to the fetcher through which the fetcher informs the SdchOwner that it's |
76 // gotten the dictionary. The first two arguments are bound locally. | 124 // gotten the dictionary. The first two arguments are bound locally. |
77 // Public for testing. | 125 // Public for testing. |
78 void OnDictionaryFetched(base::Time last_used, | 126 void OnDictionaryFetched(base::Time last_used, |
79 base::Time created_time, | 127 base::Time created_time, |
80 int use_count, | 128 int use_count, |
81 const std::string& dictionary_text, | 129 const std::string& dictionary_text, |
82 const GURL& dictionary_url, | 130 const GURL& dictionary_url, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 size_t total_dictionary_bytes_; | 220 size_t total_dictionary_bytes_; |
173 | 221 |
174 scoped_ptr<base::Clock> clock_; | 222 scoped_ptr<base::Clock> clock_; |
175 | 223 |
176 size_t max_total_dictionary_size_; | 224 size_t max_total_dictionary_size_; |
177 size_t min_space_for_dictionary_fetch_; | 225 size_t min_space_for_dictionary_fetch_; |
178 | 226 |
179 base::MemoryPressureListener memory_pressure_listener_; | 227 base::MemoryPressureListener memory_pressure_listener_; |
180 | 228 |
181 // Dictionary persistence machinery. | 229 // Dictionary persistence machinery. |
182 // * |in_memory_pref_store_| is created on construction and used in | 230 // * |in_memory_pref_store_| is created on construction and used in the |
183 // the absence of any call to EnablePersistentStorage(). | 231 // absence of any call to EnablePersistentStorage(). |
184 // * |external_pref_store_| holds the preference store specified | 232 // * |external_pref_store_| holds the preference store specified by |
185 // by EnablePersistentStorage() (if any), while it is being read in. | 233 // EnablePersistentStorage() (if any). |
186 // A non-null value here signals that the SdchOwner is observing | 234 // * The external pref store is initialized asynchronously. During this time, |
187 // the pref store; when read-in completes and observation is no longer | 235 // both pointers will be value, pref_store_ will point to the in-memory |
188 // needed, the pointer is set to null. This is to avoid lots of | 236 // one, and this class will be observing the initialization of the external |
189 // extra irrelevant function calls; the only observer interface this | 237 // store. |
190 // class is interested in is OnInitializationCompleted(). | 238 // * When the external pref store is initialized, the in-memory version will |
| 239 // be freed, and pref_store_ will point to the external one. |
191 // * |pref_store_| holds an unowned pointer to the currently | 240 // * |pref_store_| holds an unowned pointer to the currently |
192 // active pref store (one of the preceding two). | 241 // active pref store (one of the preceding two). |
193 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; | 242 scoped_ptr<PrefStorage> in_memory_pref_store_; |
194 PersistentPrefStore* external_pref_store_; | 243 scoped_ptr<PrefStorage> external_pref_store_; |
195 | 244 PrefStorage* pref_store_; |
196 WriteablePrefStore* pref_store_; | |
197 | 245 |
198 // The use counts of dictionaries when they were loaded from the persistent | 246 // The use counts of dictionaries when they were loaded from the persistent |
199 // store, keyed by server hash. These are stored to avoid generating | 247 // store, keyed by server hash. These are stored to avoid generating |
200 // misleading ever-increasing use counts for dictionaries that are persisted, | 248 // misleading ever-increasing use counts for dictionaries that are persisted, |
201 // since the UMA histogram for use counts is only supposed to be since last | 249 // since the UMA histogram for use counts is only supposed to be since last |
202 // load. | 250 // load. |
203 std::map<std::string, int> use_counts_at_load_; | 251 std::map<std::string, int> use_counts_at_load_; |
204 | 252 |
205 // Load times for loaded dictionaries, keyed by server hash. These are used to | 253 // Load times for loaded dictionaries, keyed by server hash. These are used to |
206 // track the durations that dictionaries are in memory. | 254 // track the durations that dictionaries are in memory. |
207 std::map<std::string, base::Time> load_times_; | 255 std::map<std::string, base::Time> load_times_; |
208 | 256 |
209 // Byte-seconds consumed by dictionaries that have been unloaded. These are | 257 // Byte-seconds consumed by dictionaries that have been unloaded. These are |
210 // stored for later uploading in the SdchOwner destructor. | 258 // stored for later uploading in the SdchOwner destructor. |
211 std::vector<int64_t> consumed_byte_seconds_; | 259 std::vector<int64_t> consumed_byte_seconds_; |
212 | 260 |
213 // Creation time for this SdchOwner object, used for reporting temporal memory | 261 // Creation time for this SdchOwner object, used for reporting temporal memory |
214 // pressure. | 262 // pressure. |
215 base::Time creation_time_; | 263 base::Time creation_time_; |
216 | 264 |
217 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 265 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
218 }; | 266 }; |
219 | 267 |
220 } // namespace net | 268 } // namespace net |
221 | 269 |
222 #endif // NET_SDCH_SDCH_OWNER_H_ | 270 #endif // NET_SDCH_SDCH_OWNER_H_ |
OLD | NEW |