Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: net/sdch/sdch_owner.h

Issue 901303002: Make SDCH dictionaries persistent across browser restart. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for rdsmith Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 GetDictionaryCount() const;
Randy Smith (Not in Mondays) 2015/02/17 20:57:42 Hmmm. This is only used for testing, and the info
mmenke 2015/02/19 20:14:35 +1
Elly Fong-Jones 2015/02/20 20:35:25 Done.
Elly Fong-Jones 2015/02/20 20:35:25 Done.
85
86 // Returns whether this SdchOwner has dictionary from |url| loaded.
87 bool HasDictionaryFrom(const GURL& url) const;
Randy Smith (Not in Mondays) 2015/02/17 20:57:42 Same here (only used for testing, information is m
mmenke 2015/02/19 20:14:35 +1
Elly Fong-Jones 2015/02/20 20:35:25 Done.
Elly Fong-Jones 2015/02/20 20:35:25 Done.
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
128 // Dictionary persistence machinery.
129 // * |in_memory_pref_store_| is created on construction and used in
130 // the absence of any call to EnablePersistentStorage().
131 // * |external_pref_store_| holds the preference store specified
132 // by EnablePersistentStorage() (if any), while it is being read in.
133 // A non-null value here signals that the SdchOwner is observing
134 // the pref store; when read-in completes and observation is no longer
135 // needed, the pointer is set to null. This is to avoid lots of
136 // extra irrelevant function calls; the only observer interface this
137 // class is interested in is OnInitializationCompleted().
138 // * |pref_store_| holds an unowned pointer to the currently
139 // active pref store (one of the preceding two).
140 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
141 PersistentPrefStore* external_pref_store_;
142
143 WriteablePrefStore* pref_store_;
144
90 base::MemoryPressureListener memory_pressure_listener_; 145 base::MemoryPressureListener memory_pressure_listener_;
91 146
92 DISALLOW_COPY_AND_ASSIGN(SdchOwner); 147 DISALLOW_COPY_AND_ASSIGN(SdchOwner);
93 }; 148 };
94 149
95 } // namespace net 150 } // namespace net
96 151
97 #endif // NET_SDCH_SDCH_OWNER_H_ 152 #endif // NET_SDCH_SDCH_OWNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698