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 PERSISTENCE_FAILURE_REASON_MAX = 4 | |
57 // See RecordPersistenceFailure for UMA logging of this value if | |
58 // adding a value here. | |
59 }; | |
60 | |
61 virtual ~PrefStorage(); | |
62 | |
63 // Returns the read error if any. Valid to be called after initialization | |
64 // is complete (see IsInitializationComplete). | |
65 virtual ReadError GetReadError() const = 0; | |
Randy Smith (Not in Mondays)
2016/01/27 21:48:17
I think it's worth documenting that it's possible
| |
66 | |
67 // Gets or sets the value in the preferences store. | |
68 virtual bool GetValue(const base::DictionaryValue** result) const = 0; | |
69 virtual bool GetMutableValue(base::DictionaryValue** result) = 0; | |
70 virtual void SetValue(scoped_ptr<base::DictionaryValue> value) = 0; | |
71 | |
72 // Notifies the storage system that a value was changed via mutating the | |
73 // result of GetMutableValue(). | |
74 virtual void ReportValueChanged() = 0; | |
75 | |
76 // Returns true if the store's init is complete. See the Start/Stop | |
77 // functions below for observing changes to this value. | |
78 virtual bool IsInitializationComplete() = 0; | |
79 | |
80 // Starts and stops observing preferences storage changes. There will only | |
81 // be one observer active at a time. The store should call | |
82 // OnPrefStorageInitializationComplete() when the preferences store has | |
83 // been initialized and there is an observer registered. | |
84 virtual void StartObservingInit(SdchOwner* observer) = 0; | |
85 virtual void StopObservingInit() = 0; | |
86 }; | |
87 | |
40 static const size_t kMaxTotalDictionarySize; | 88 static const size_t kMaxTotalDictionarySize; |
41 static const size_t kMinSpaceForDictionaryFetch; | 89 static const size_t kMinSpaceForDictionaryFetch; |
42 | 90 |
43 // Consumer must guarantee that |sdch_manager| and |context| outlive | 91 // Consumer must guarantee that |sdch_manager| and |context| outlive |
44 // this object. | 92 // this object. |
45 SdchOwner(SdchManager* sdch_manager, URLRequestContext* context); | 93 SdchOwner(SdchManager* sdch_manager, URLRequestContext* context); |
46 ~SdchOwner() override; | 94 ~SdchOwner() override; |
47 | 95 |
48 // Enables use of pref persistence. Note that |pref_store| is owned | 96 // Enables use of pref persistence. Ownership of the storage will be passed. |
49 // by the caller, but must be guaranteed to outlive SdchOwner. The | 97 // This routine may only be called once per SdchOwner instance. |
50 // actual mechanisms by which the PersistentPrefStore are persisted | 98 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 | 99 |
55 // Defaults to kMaxTotalDictionarySize. | 100 // Defaults to kMaxTotalDictionarySize. |
56 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); | 101 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); |
57 | 102 |
58 // Defaults to kMinSpaceForDictionaryFetch. | 103 // Defaults to kMinSpaceForDictionaryFetch. |
59 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); | 104 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); |
60 | 105 |
61 // SdchObserver implementation. | 106 // SdchObserver implementation. |
62 void OnDictionaryAdded(const GURL& dictionary_url, | 107 void OnDictionaryAdded(const GURL& dictionary_url, |
63 const std::string& server_hash) override; | 108 const std::string& server_hash) override; |
64 void OnDictionaryRemoved(const std::string& server_hash) override; | 109 void OnDictionaryRemoved(const std::string& server_hash) override; |
65 void OnDictionaryUsed(const std::string& server_hash) override; | 110 void OnDictionaryUsed(const std::string& server_hash) override; |
66 void OnGetDictionary(const GURL& request_url, | 111 void OnGetDictionary(const GURL& request_url, |
67 const GURL& dictionary_url) override; | 112 const GURL& dictionary_url) override; |
68 void OnClearDictionaries() override; | 113 void OnClearDictionaries() override; |
69 | 114 |
70 // PrefStore::Observer implementation. | 115 // Called by the PrefStorage implementation when initialization is complete. |
71 void OnPrefValueChanged(const std::string& key) override; | 116 // See PrefStorage::StartObservingInit(). |
72 void OnInitializationCompleted(bool succeeded) override; | 117 void OnPrefStorageInitializationComplete(bool succeeded); |
73 | 118 |
74 // Implementation detail--this is the function callback by the callback passed | 119 // 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 | 120 // to the fetcher through which the fetcher informs the SdchOwner that it's |
76 // gotten the dictionary. The first two arguments are bound locally. | 121 // gotten the dictionary. The first two arguments are bound locally. |
77 // Public for testing. | 122 // Public for testing. |
78 void OnDictionaryFetched(base::Time last_used, | 123 void OnDictionaryFetched(base::Time last_used, |
79 base::Time created_time, | 124 base::Time created_time, |
80 int use_count, | 125 int use_count, |
81 const std::string& dictionary_text, | 126 const std::string& dictionary_text, |
82 const GURL& dictionary_url, | 127 const GURL& dictionary_url, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 size_t total_dictionary_bytes_; | 217 size_t total_dictionary_bytes_; |
173 | 218 |
174 scoped_ptr<base::Clock> clock_; | 219 scoped_ptr<base::Clock> clock_; |
175 | 220 |
176 size_t max_total_dictionary_size_; | 221 size_t max_total_dictionary_size_; |
177 size_t min_space_for_dictionary_fetch_; | 222 size_t min_space_for_dictionary_fetch_; |
178 | 223 |
179 base::MemoryPressureListener memory_pressure_listener_; | 224 base::MemoryPressureListener memory_pressure_listener_; |
180 | 225 |
181 // Dictionary persistence machinery. | 226 // Dictionary persistence machinery. |
182 // * |in_memory_pref_store_| is created on construction and used in | 227 // * |in_memory_pref_store_| is created on construction and used in the |
183 // the absence of any call to EnablePersistentStorage(). | 228 // absence of any call to EnablePersistentStorage(). |
184 // * |external_pref_store_| holds the preference store specified | 229 // * |external_pref_store_| holds the preference store specified by |
185 // by EnablePersistentStorage() (if any), while it is being read in. | 230 // EnablePersistentStorage() (if any). |
186 // A non-null value here signals that the SdchOwner is observing | 231 // * The external pref store is initialized asynchronously. During this time, |
187 // the pref store; when read-in completes and observation is no longer | 232 // 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 | 233 // one, and this class will be observing the initialization of the external |
189 // extra irrelevant function calls; the only observer interface this | 234 // store. |
190 // class is interested in is OnInitializationCompleted(). | 235 // * When the external pref store is initialized, the in-memory version will |
236 // be freed, and pref_store_ will point to the external one. | |
191 // * |pref_store_| holds an unowned pointer to the currently | 237 // * |pref_store_| holds an unowned pointer to the currently |
192 // active pref store (one of the preceding two). | 238 // active pref store (one of the preceding two). |
193 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; | 239 scoped_ptr<PrefStorage> in_memory_pref_store_; |
194 PersistentPrefStore* external_pref_store_; | 240 scoped_ptr<PrefStorage> external_pref_store_; |
195 | 241 PrefStorage* pref_store_; |
196 WriteablePrefStore* pref_store_; | |
197 | 242 |
198 // The use counts of dictionaries when they were loaded from the persistent | 243 // The use counts of dictionaries when they were loaded from the persistent |
199 // store, keyed by server hash. These are stored to avoid generating | 244 // store, keyed by server hash. These are stored to avoid generating |
200 // misleading ever-increasing use counts for dictionaries that are persisted, | 245 // 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 | 246 // since the UMA histogram for use counts is only supposed to be since last |
202 // load. | 247 // load. |
203 std::map<std::string, int> use_counts_at_load_; | 248 std::map<std::string, int> use_counts_at_load_; |
204 | 249 |
205 // Load times for loaded dictionaries, keyed by server hash. These are used to | 250 // Load times for loaded dictionaries, keyed by server hash. These are used to |
206 // track the durations that dictionaries are in memory. | 251 // track the durations that dictionaries are in memory. |
207 std::map<std::string, base::Time> load_times_; | 252 std::map<std::string, base::Time> load_times_; |
208 | 253 |
209 // Byte-seconds consumed by dictionaries that have been unloaded. These are | 254 // Byte-seconds consumed by dictionaries that have been unloaded. These are |
210 // stored for later uploading in the SdchOwner destructor. | 255 // stored for later uploading in the SdchOwner destructor. |
211 std::vector<int64_t> consumed_byte_seconds_; | 256 std::vector<int64_t> consumed_byte_seconds_; |
212 | 257 |
213 // Creation time for this SdchOwner object, used for reporting temporal memory | 258 // Creation time for this SdchOwner object, used for reporting temporal memory |
214 // pressure. | 259 // pressure. |
215 base::Time creation_time_; | 260 base::Time creation_time_; |
216 | 261 |
217 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 262 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
218 }; | 263 }; |
219 | 264 |
220 } // namespace net | 265 } // namespace net |
221 | 266 |
222 #endif // NET_SDCH_SDCH_OWNER_H_ | 267 #endif // NET_SDCH_SDCH_OWNER_H_ |
OLD | NEW |