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