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 WriteablePrefStore are persisted | |
Randy Smith (Not in Mondays)
2015/02/16 01:10:05
nit: WriteablePrefStore -> PersistentPrefStore (al
Elly Fong-Jones
2015/02/17 20:35:29
Done.
| |
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 GetDictionaryCount() const; | |
85 | |
86 // Returns whether this SdchOwner has dictionary from |url| loaded. | |
87 bool HasDictionaryFrom(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 if the persisted_info has a bad format. | |
Randy Smith (Not in Mondays)
2015/02/16 01:10:05
nit, suggestion: Comment should describe what happ
Elly Fong-Jones
2015/02/17 20:35:29
Done.
| |
111 bool SchedulePersistedDictionaryLoads( | |
112 const base::DictionaryValue& persisted_info); | |
113 | |
114 bool IsPersistingDictionaries() const; | |
115 | |
79 net::SdchManager* manager_; | 116 net::SdchManager* manager_; |
80 net::SdchDictionaryFetcher fetcher_; | 117 scoped_ptr<net::SdchDictionaryFetcher> fetcher_; |
81 | 118 |
82 std::map<std::string, DictionaryInfo> local_dictionary_info_; | |
83 size_t total_dictionary_bytes_; | 119 size_t total_dictionary_bytes_; |
84 | 120 |
85 scoped_ptr<base::Clock> clock_; | 121 scoped_ptr<base::Clock> clock_; |
86 | 122 |
87 size_t max_total_dictionary_size_; | 123 size_t max_total_dictionary_size_; |
88 size_t min_space_for_dictionary_fetch_; | 124 size_t min_space_for_dictionary_fetch_; |
89 | 125 |
126 // Dictionary persistence machinery. | |
127 // * |in_memory_pref_store_| is created on construction and used in | |
128 // the absence of any call to EnablePersistentStorage(). | |
129 // * |external_pref_store_| holds the preference store specified | |
130 // by EnablePersistentStorage() (if any), while it is being read in. | |
131 // it is only set while the external pref store is being observed. | |
Randy Smith (Not in Mondays)
2015/02/16 01:10:05
nit: Capitalize "it". Alternatively, delete this
Elly Fong-Jones
2015/02/17 20:35:29
Done.
| |
132 // A non-null value here signals that the SdchOwner is observing | |
133 // the pref store; when read-in completes and observation is no longer | |
134 // needed, the pointer is set to null. This is to avoid lots of | |
135 // extra irrelevant function calls; the only observer interface this | |
136 // class is interested in is OnInitializationCompleted(). | |
137 // * |pref_store_| holds an unowned pointer to the currently | |
138 // active pref store (one of the preceding two). | |
139 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; | |
140 PersistentPrefStore* external_pref_store_; | |
141 | |
142 WriteablePrefStore* pref_store_; | |
143 | |
90 base::MemoryPressureListener memory_pressure_listener_; | 144 base::MemoryPressureListener memory_pressure_listener_; |
91 | 145 |
92 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 146 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
93 }; | 147 }; |
94 | 148 |
95 } // namespace net | 149 } // namespace net |
96 | 150 |
97 #endif // NET_SDCH_SDCH_OWNER_H_ | 151 #endif // NET_SDCH_SDCH_OWNER_H_ |
OLD | NEW |