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/memory/ref_counted.h" |
| 13 #include "base/prefs/pref_store.h" |
11 #include "net/base/sdch_observer.h" | 14 #include "net/base/sdch_observer.h" |
12 #include "net/url_request/sdch_dictionary_fetcher.h" | 15 #include "net/url_request/sdch_dictionary_fetcher.h" |
13 | 16 |
14 class GURL; | 17 class GURL; |
| 18 class PersistentPrefStore; |
| 19 class ValueMapPrefStore; |
| 20 class WriteablePrefStore; |
15 | 21 |
16 namespace base { | 22 namespace base { |
17 class Clock; | 23 class Clock; |
18 } | 24 } |
19 | 25 |
20 namespace net { | 26 namespace net { |
21 class SdchManager; | 27 class SdchManager; |
22 class URLRequestContext; | 28 class URLRequestContext; |
23 | 29 |
24 // 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 |
25 // exposes interface for setting SDCH policy. It should be instantiated by | 31 // exposes interface for setting SDCH policy. It should be instantiated by |
26 // the net/ embedder. | 32 // the net/ embedder. |
27 // TODO(rdsmith): Implement dictionary prioritization. | 33 // TODO(rdsmith): Implement dictionary prioritization. |
28 class NET_EXPORT SdchOwner : public net::SdchObserver { | 34 class NET_EXPORT SdchOwner : public net::SdchObserver, |
| 35 public PrefStore::Observer { |
29 public: | 36 public: |
30 static const size_t kMaxTotalDictionarySize; | 37 static const size_t kMaxTotalDictionarySize; |
31 static const size_t kMinSpaceForDictionaryFetch; | 38 static const size_t kMinSpaceForDictionaryFetch; |
32 | 39 |
33 // Consumer must guarantee that |sdch_manager| and |context| outlive | 40 // Consumer must guarantee that |sdch_manager| and |context| outlive |
34 // this object. | 41 // this object. |
35 SdchOwner(net::SdchManager* sdch_manager, net::URLRequestContext* context); | 42 SdchOwner(net::SdchManager* sdch_manager, net::URLRequestContext* context); |
36 ~SdchOwner() override; | 43 ~SdchOwner() override; |
37 | 44 |
| 45 // Enables use of pref persistence. Note that |pref_store| is owned |
| 46 // by the caller, but must be guaranteed to outlive SdchOwner. The |
| 47 // actual mechanisms by which the PersistentPrefStore are persisted |
| 48 // are the responsibility of the caller. This routine may only be |
| 49 // called once per SdchOwner instance. |
| 50 void EnablePersistentStorage(PersistentPrefStore* pref_store); |
| 51 |
38 // Defaults to kMaxTotalDictionarySize. | 52 // Defaults to kMaxTotalDictionarySize. |
39 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); | 53 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size); |
40 | 54 |
41 // Defaults to kMinSpaceForDictionaryFetch. | 55 // Defaults to kMinSpaceForDictionaryFetch. |
42 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); | 56 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch); |
43 | 57 |
44 // SdchObserver implementation. | 58 // SdchObserver implementation. |
45 void OnDictionaryUsed(SdchManager* manager, | 59 void OnDictionaryUsed(SdchManager* manager, |
46 const std::string& server_hash) override; | 60 const std::string& server_hash) override; |
47 void OnGetDictionary(net::SdchManager* manager, | 61 void OnGetDictionary(net::SdchManager* manager, |
48 const GURL& request_url, | 62 const GURL& request_url, |
49 const GURL& dictionary_url) override; | 63 const GURL& dictionary_url) override; |
50 void OnClearDictionaries(net::SdchManager* manager) override; | 64 void OnClearDictionaries(net::SdchManager* manager) override; |
51 | 65 |
52 // Implementation detail--this is the pathway through which the | 66 // PrefStore::Observer implementation. |
53 // fetcher informs the SdchOwner that it's gotten the dictionary. | 67 void OnPrefValueChanged(const std::string& key) override; |
| 68 void OnInitializationCompleted(bool succeeded) override; |
| 69 |
| 70 // Implementation detail--this is the function callback by the callback |
| 71 // passed to the fetcher through which the fetcher informs the SdchOwner |
| 72 // that it's gotten the dictionary. The first two arguments are |
| 73 // bound locally. |
54 // Public for testing. | 74 // Public for testing. |
55 void OnDictionaryFetched(const std::string& dictionary_text, | 75 void OnDictionaryFetched(base::Time last_used, |
| 76 int use_count, |
| 77 const std::string& dictionary_text, |
56 const GURL& dictionary_url, | 78 const GURL& dictionary_url, |
57 const net::BoundNetLog& net_log); | 79 const net::BoundNetLog& net_log); |
58 | 80 |
59 void SetClockForTesting(scoped_ptr<base::Clock> clock); | 81 void SetClockForTesting(scoped_ptr<base::Clock> clock); |
60 | 82 |
| 83 // Returns the total number of dictionaries loaded. |
| 84 int GetDictionaryCountForTesting() const; |
| 85 |
| 86 // Returns whether this SdchOwner has dictionary from |url| loaded. |
| 87 bool HasDictionaryFromURLForTesting(const GURL& url) const; |
| 88 |
| 89 void SetFetcherForTesting(scoped_ptr<SdchDictionaryFetcher> fetcher); |
| 90 |
61 private: | 91 private: |
62 // For each active dictionary, stores local info. | 92 // For each active dictionary, stores local info. |
63 // Indexed by server hash. | 93 // Indexed by the server hash of the dictionary. |
64 struct DictionaryInfo { | 94 struct DictionaryInfo { |
65 base::Time last_used; | 95 base::Time last_used; |
66 int use_count; | 96 int use_count; |
67 size_t size; | 97 size_t size; |
68 | 98 |
69 DictionaryInfo() : use_count(0), size(0) {} | 99 DictionaryInfo() : use_count(0), size(0) {} |
70 DictionaryInfo(const base::Time& last_used, size_t size) | 100 DictionaryInfo(const base::Time& last_used, size_t size) |
71 : last_used(last_used), use_count(0), size(size) {} | 101 : last_used(last_used), use_count(0), size(size) {} |
72 DictionaryInfo(const DictionaryInfo& rhs) = default; | 102 DictionaryInfo(const DictionaryInfo& rhs) = default; |
73 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; | 103 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; |
74 }; | 104 }; |
75 | 105 |
76 void OnMemoryPressure( | 106 void OnMemoryPressure( |
77 base::MemoryPressureListener::MemoryPressureLevel level); | 107 base::MemoryPressureListener::MemoryPressureLevel level); |
78 | 108 |
| 109 // Schedule loading of all dictionaries described in |persisted_info|. |
| 110 // Returns false and does not schedule a load if |persisted_info| has an |
| 111 // unsupported version or no dictionaries key. Skips any dictionaries that are |
| 112 // malformed in |persisted_info|. |
| 113 bool SchedulePersistedDictionaryLoads( |
| 114 const base::DictionaryValue& persisted_info); |
| 115 |
| 116 bool IsPersistingDictionaries() const; |
| 117 |
79 net::SdchManager* manager_; | 118 net::SdchManager* manager_; |
80 net::SdchDictionaryFetcher fetcher_; | 119 scoped_ptr<net::SdchDictionaryFetcher> fetcher_; |
81 | 120 |
82 std::map<std::string, DictionaryInfo> local_dictionary_info_; | |
83 size_t total_dictionary_bytes_; | 121 size_t total_dictionary_bytes_; |
84 | 122 |
85 scoped_ptr<base::Clock> clock_; | 123 scoped_ptr<base::Clock> clock_; |
86 | 124 |
87 size_t max_total_dictionary_size_; | 125 size_t max_total_dictionary_size_; |
88 size_t min_space_for_dictionary_fetch_; | 126 size_t min_space_for_dictionary_fetch_; |
89 | 127 |
90 #if defined(OS_CHROMEOS) | 128 #if defined(OS_CHROMEOS) |
91 // For debugging http://crbug.com/454198; remove when resolved. | 129 // For debugging http://crbug.com/454198; remove when resolved. |
92 unsigned int destroyed_; | 130 unsigned int destroyed_; |
93 #endif | 131 #endif |
94 | 132 |
95 // TODO(rmcilroy) Add back memory_pressure_listener_ when | 133 // TODO(rmcilroy) Add back memory_pressure_listener_ when |
96 // http://crbug.com/447208 is fixed | 134 // http://crbug.com/447208 is fixed |
97 | 135 |
| 136 // Dictionary persistence machinery. |
| 137 // * |in_memory_pref_store_| is created on construction and used in |
| 138 // the absence of any call to EnablePersistentStorage(). |
| 139 // * |external_pref_store_| holds the preference store specified |
| 140 // by EnablePersistentStorage() (if any), while it is being read in. |
| 141 // A non-null value here signals that the SdchOwner is observing |
| 142 // the pref store; when read-in completes and observation is no longer |
| 143 // needed, the pointer is set to null. This is to avoid lots of |
| 144 // extra irrelevant function calls; the only observer interface this |
| 145 // class is interested in is OnInitializationCompleted(). |
| 146 // * |pref_store_| holds an unowned pointer to the currently |
| 147 // active pref store (one of the preceding two). |
| 148 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; |
| 149 PersistentPrefStore* external_pref_store_; |
| 150 |
| 151 WriteablePrefStore* pref_store_; |
| 152 |
| 153 // The use counts of dictionaries when they were loaded from the persistent |
| 154 // store, keyed by server hash. These are stored to avoid generating |
| 155 // misleading ever-increasing use counts for dictionaries that are persisted, |
| 156 // since the UMA histogram for use counts is only supposed to be since last |
| 157 // load. |
| 158 std::map<std::string, int> use_counts_at_load_; |
| 159 |
98 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 160 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
99 }; | 161 }; |
100 | 162 |
101 } // namespace net | 163 } // namespace net |
102 | 164 |
103 #endif // NET_SDCH_SDCH_OWNER_H_ | 165 #endif // NET_SDCH_SDCH_OWNER_H_ |
OLD | NEW |