| 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 |
| 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 |
| 61 private: | 83 private: |
| 62 // For each active dictionary, stores local info. | 84 // For each active dictionary, stores local info. |
| 63 // Indexed by server hash. | 85 // Indexed by the server hash of the dictionary. |
| 64 struct DictionaryInfo { | 86 struct DictionaryInfo { |
| 65 base::Time last_used; | 87 base::Time last_used; |
| 66 int use_count; | 88 int use_count; |
| 67 size_t size; | 89 size_t size; |
| 68 | 90 |
| 69 DictionaryInfo() : use_count(0), size(0) {} | 91 DictionaryInfo() : use_count(0), size(0) {} |
| 70 DictionaryInfo(const base::Time& last_used, size_t size) | 92 DictionaryInfo(const base::Time& last_used, size_t size) |
| 71 : last_used(last_used), use_count(0), size(size) {} | 93 : last_used(last_used), use_count(0), size(size) {} |
| 72 DictionaryInfo(const DictionaryInfo& rhs) = default; | 94 DictionaryInfo(const DictionaryInfo& rhs) = default; |
| 73 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; | 95 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default; |
| 74 }; | 96 }; |
| 75 | 97 |
| 76 void OnMemoryPressure( | 98 void OnMemoryPressure( |
| 77 base::MemoryPressureListener::MemoryPressureLevel level); | 99 base::MemoryPressureListener::MemoryPressureLevel level); |
| 78 | 100 |
| 101 // Schedule loading of all dictionaries described in |persisted_info|. |
| 102 // Returns false if the persisted_info has a bad format. |
| 103 bool SchedulePersistedDictionaryLoads( |
| 104 const base::DictionaryValue& persisted_info); |
| 105 |
| 106 bool IsPersistingDictionaries() const; |
| 107 |
| 79 net::SdchManager* manager_; | 108 net::SdchManager* manager_; |
| 80 net::SdchDictionaryFetcher fetcher_; | 109 net::SdchDictionaryFetcher fetcher_; |
| 81 | 110 |
| 82 std::map<std::string, DictionaryInfo> local_dictionary_info_; | |
| 83 size_t total_dictionary_bytes_; | 111 size_t total_dictionary_bytes_; |
| 84 | 112 |
| 85 scoped_ptr<base::Clock> clock_; | 113 scoped_ptr<base::Clock> clock_; |
| 86 | 114 |
| 87 size_t max_total_dictionary_size_; | 115 size_t max_total_dictionary_size_; |
| 88 size_t min_space_for_dictionary_fetch_; | 116 size_t min_space_for_dictionary_fetch_; |
| 89 | 117 |
| 118 // Dictionary persistence machinery. |
| 119 // * |in_memory_pref_store_| is created on construction and used in |
| 120 // the absence of any call to EnablePersistentStorage(). |
| 121 // * |external_pref_store_| holds the preference store specified |
| 122 // by EnablePersistentStorage() (if any), while it is being read in. |
| 123 // it is only set while the external pref store is being observed. |
| 124 // A non-null value here signals that the SdchOwner is observing |
| 125 // the pref store; when read-in completes and observation is no longer |
| 126 // needed, the pointer is set to null. This is to avoid lots of |
| 127 // extra irrelevant function calls; the only observer interface this |
| 128 // class is interested in is OnInitializationCompleted(). |
| 129 // * |pref_store_| holds an unowned pointer to the currently |
| 130 // active pref store (one of the preceding two). |
| 131 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_; |
| 132 PersistentPrefStore* external_pref_store_; |
| 133 |
| 134 WriteablePrefStore* pref_store_; |
| 135 |
| 90 base::MemoryPressureListener memory_pressure_listener_; | 136 base::MemoryPressureListener memory_pressure_listener_; |
| 91 | 137 |
| 92 DISALLOW_COPY_AND_ASSIGN(SdchOwner); | 138 DISALLOW_COPY_AND_ASSIGN(SdchOwner); |
| 93 }; | 139 }; |
| 94 | 140 |
| 95 } // namespace net | 141 } // namespace net |
| 96 | 142 |
| 97 #endif // NET_SDCH_SDCH_OWNER_H_ | 143 #endif // NET_SDCH_SDCH_OWNER_H_ |
| OLD | NEW |