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 <map> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/memory/memory_pressure_listener.h" | 11 #include "base/memory/memory_pressure_listener.h" |
12 #include "base/prefs/pref_store.h" | |
11 #include "net/base/sdch_observer.h" | 13 #include "net/base/sdch_observer.h" |
12 #include "net/url_request/sdch_dictionary_fetcher.h" | 14 #include "net/url_request/sdch_dictionary_fetcher.h" |
13 | 15 |
14 class GURL; | 16 class GURL; |
17 class PersistentPrefStore; | |
18 class ValueMapPrefStore; | |
19 class WriteablePrefStore; | |
15 | 20 |
16 namespace base { | 21 namespace base { |
17 class Clock; | 22 class Clock; |
18 } | 23 } |
19 | 24 |
20 namespace net { | 25 namespace net { |
21 class SdchManager; | 26 class SdchManager; |
22 class URLRequestContext; | 27 class URLRequestContext; |
23 | 28 |
24 // This class owns the SDCH objects not owned as part of URLRequestContext, and | 29 // This class owns the SDCH objects not owned as part of URLRequestContext, and |
25 // exposes interface for setting SDCH policy. It should be instantiated by | 30 // exposes interface for setting SDCH policy. It should be instantiated by |
26 // the net/ embedder. | 31 // the net/ embedder. |
27 // TODO(rdsmith): Implement dictionary prioritization. | 32 // TODO(rdsmith): Implement dictionary prioritization. |
28 class NET_EXPORT SdchOwner : public net::SdchObserver { | 33 class NET_EXPORT SdchOwner : public net::SdchObserver, |
34 public PrefStore::Observer { | |
29 public: | 35 public: |
30 static const size_t kMaxTotalDictionarySize; | 36 static const size_t kMaxTotalDictionarySize; |
31 static const size_t kMinSpaceForDictionaryFetch; | 37 static const size_t kMinSpaceForDictionaryFetch; |
32 | 38 |
33 // Consumer must guarantee that |sdch_manager| and |context| outlive | 39 // Consumer must guarantee that |sdch_manager| and |context| outlive |
34 // this object. | 40 // this object. |
35 SdchOwner(net::SdchManager* sdch_manager, net::URLRequestContext* context); | 41 SdchOwner(net::SdchManager* sdch_manager, net::URLRequestContext* context); |
36 ~SdchOwner() override; | 42 ~SdchOwner() override; |
37 | 43 |
44 // Preference name used by this class in PersistentPrefStore | |
45 // argument to EnablePersistentStorage(). | |
46 static std::string PreferenceName(); | |
mmenke
2015/02/04 21:14:39
Should we just make this a static const char[]? O
Elly Fong-Jones
2015/02/05 23:03:12
Done.
| |
47 | |
48 // Enables use of pref persistence. Note that |pref_store| is owned | |
49 // by the caller, but must be guaranteed to outlive SdchOwner. The | |
50 // actual mechanisms by which the WriteablePrefStore are persisted | |
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 | |
38 // Defaults to kMaxTotalDictionarySize. | 55 // Defaults to kMaxTotalDictionarySize. |
39 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); | 56 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); |
40 | 57 |
41 // Defaults to kMinSpaceForDictionaryFetch. | 58 // Defaults to kMinSpaceForDictionaryFetch. |
42 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); | 59 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); |
43 | 60 |
44 // SdchObserver implementation. | 61 // SdchObserver implementation. |
45 void OnDictionaryUsed(SdchManager* manager, | 62 void OnDictionaryUsed(SdchManager* manager, |
46 const std::string& server_hash) override; | 63 const std::string& server_hash) override; |
47 void OnGetDictionary(net::SdchManager* manager, | 64 void OnGetDictionary(net::SdchManager* manager, |
48 const GURL& request_url, | 65 const GURL& request_url, |
49 const GURL& dictionary_url) override; | 66 const GURL& dictionary_url) override; |
50 void OnClearDictionaries(net::SdchManager* manager) override; | 67 void OnClearDictionaries(net::SdchManager* manager) override; |
51 | 68 |
52 // Implementation detail--this is the pathway through which the | 69 // PrefStore::Observer implementation. |
53 // fetcher informs the SdchOwner that it's gotten the dictionary. | 70 void OnPrefValueChanged(const std::string& key) override; |
71 void OnInitializationCompleted(bool succeeded) override; | |
72 | |
73 // Implementation detail--this is the function callback by the callback | |
74 // passed to the fetcher through which the fetcher informs the SdchOwner | |
75 // that it's gotten the dictionary. The first two arguments are | |
76 // bound locally. | |
54 // Public for testing. | 77 // Public for testing. |
55 void OnDictionaryFetched(const std::string& dictionary_text, | 78 void OnDictionaryFetched(base::Time last_used, |
79 int use_count, | |
80 const std::string& dictionary_text, | |
56 const GURL& dictionary_url, | 81 const GURL& dictionary_url, |
57 const net::BoundNetLog& net_log); | 82 const net::BoundNetLog& net_log); |
58 | 83 |
59 void SetClockForTesting(scoped_ptr<base::Clock> clock); | 84 void SetClockForTesting(scoped_ptr<base::Clock> clock); |
60 | 85 |
61 private: | 86 private: |
62 // For each active dictionary, stores local info. | 87 // For each active dictionary, stores local info. |
63 // Indexed by server hash. | 88 // Indexed by the server hash of the dictionary. |
64 struct DictionaryInfo { | 89 struct DictionaryInfo { |
65 base::Time last_used; | 90 base::Time last_used; |
66 int use_count; | 91 int use_count; |
67 size_t size; | 92 size_t size; |
68 | 93 |
69 DictionaryInfo() : use_count(0), size(0) {} | 94 DictionaryInfo() : use_count(0), size(0) {} |
70 DictionaryInfo(const base::Time& last_used, size_t size) | 95 DictionaryInfo(const base::Time& last_used, size_t size) |
71 : last_used(last_used), use_count(0), size(size) {} | 96 : last_used(last_used), use_count(0), size(size) {} |
72 DictionaryInfo(const DictionaryInfo& rhs) = default; | 97 DictionaryInfo(const DictionaryInfo& rhs) = default; |
73 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; | 98 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; |
74 }; | 99 }; |
75 | 100 |
76 void OnMemoryPressure( | 101 void OnMemoryPressure( |
77 base::MemoryPressureListener::MemoryPressureLevel level); | 102 base::MemoryPressureListener::MemoryPressureLevel level); |
78 | 103 |
104 // Schedule loading of all dictionaries described in |persisted_info|. | |
105 // Returns false if the persisted_info has a bad format. | |
106 bool SchedulePersistedDictionaryLoads( | |
107 const base::DictionaryValue& persisted_info); | |
108 | |
79 net::SdchManager* manager_; | 109 net::SdchManager* manager_; |
80 net::SdchDictionaryFetcher fetcher_; | 110 net::SdchDictionaryFetcher fetcher_; |
81 | 111 |
82 std::map<std::string, DictionaryInfo> local_dictionary_info_; | |
83 size_t total_dictionary_bytes_; | 112 size_t total_dictionary_bytes_; |
84 | 113 |
85 scoped_ptr<base::Clock> clock_; | 114 scoped_ptr<base::Clock> clock_; |
86 | 115 |
87 size_t max_total_dictionary_size_; | 116 size_t max_total_dictionary_size_; |
88 size_t min_space_for_dictionary_fetch_; | 117 size_t min_space_for_dictionary_fetch_; |
89 | 118 |
119 // Dictionary persistence machinery. | |
120 // * |in_memory_pref_store_| is created on construction and used in | |
121 // the absence of any call to EnablePersistentStorage(). | |
122 // * |external_pref_store_| holds the preference store specified | |
123 // by EnablePersistentStorage() (if any), while it is being read in. | |
124 // it is only set while the external pref store is being observed. | |
125 // A non-null value here signals that the SdchOwner is observing | |
126 // the pref store; when read-in completes and observation is no longer | |
127 // needed, the pointer is set to null. This is to avoid lots of | |
128 // extra irrelevant function calls; the only observer interface this | |
129 // class is interested in is OnInitializationCompleted(). | |
130 // * |pref_store_| holds an unowned pointer to the currently | |
131 // active pref store (one of the preceding two). | |
132 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; | |
133 PersistentPrefStore* external_pref_store_; | |
134 | |
135 WriteablePrefStore* pref_store_; | |
136 | |
90 base::MemoryPressureListener memory_pressure_listener_; | 137 base::MemoryPressureListener memory_pressure_listener_; |
91 | 138 |
92 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 139 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
93 }; | 140 }; |
94 | 141 |
95 } // namespace net | 142 } // namespace net |
96 | 143 |
97 #endif // NET_SDCH_SDCH_OWNER_H_ | 144 #endif // NET_SDCH_SDCH_OWNER_H_ |
OLD | NEW |