Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / net / sdch / sdch_owner.h
blob3d6b59b76618a819943914759ddb04d8bb382eaa
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
3 // found in the LICENSE file.
5 #ifndef NET_SDCH_SDCH_OWNER_H_
6 #define NET_SDCH_SDCH_OWNER_H_
8 #include <map>
9 #include <string>
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/prefs/pref_store.h"
14 #include "net/base/sdch_observer.h"
15 #include "net/url_request/sdch_dictionary_fetcher.h"
17 class GURL;
18 class PersistentPrefStore;
19 class ValueMapPrefStore;
20 class WriteablePrefStore;
22 namespace base {
23 class Clock;
26 namespace net {
27 class SdchManager;
28 class URLRequestContext;
30 // This class owns the SDCH objects not owned as part of URLRequestContext, and
31 // exposes interface for setting SDCH policy. It should be instantiated by
32 // the net/ embedder.
33 // TODO(rdsmith): Implement dictionary prioritization.
34 class NET_EXPORT SdchOwner : public SdchObserver, public PrefStore::Observer {
35 public:
36 static const size_t kMaxTotalDictionarySize;
37 static const size_t kMinSpaceForDictionaryFetch;
39 // Consumer must guarantee that |sdch_manager| and |context| outlive
40 // this object.
41 SdchOwner(SdchManager* sdch_manager, URLRequestContext* context);
42 ~SdchOwner() override;
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 PersistentPrefStore 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);
51 // Defaults to kMaxTotalDictionarySize.
52 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size);
54 // Defaults to kMinSpaceForDictionaryFetch.
55 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch);
57 // SdchObserver implementation.
58 void OnDictionaryAdded(const GURL& dictionary_url,
59 const std::string& server_hash) override;
60 void OnDictionaryRemoved(const std::string& server_hash) override;
61 void OnDictionaryUsed(const std::string& server_hash) override;
62 void OnGetDictionary(const GURL& request_url,
63 const GURL& dictionary_url) override;
64 void OnClearDictionaries() override;
66 // PrefStore::Observer implementation.
67 void OnPrefValueChanged(const std::string& key) override;
68 void OnInitializationCompleted(bool succeeded) override;
70 // Implementation detail--this is the function callback by the callback passed
71 // to the fetcher through which the fetcher informs the SdchOwner that it's
72 // gotten the dictionary. The first two arguments are bound locally.
73 // Public for testing.
74 void OnDictionaryFetched(base::Time last_used,
75 int use_count,
76 const std::string& dictionary_text,
77 const GURL& dictionary_url,
78 const BoundNetLog& net_log,
79 bool was_from_cache);
81 void SetClockForTesting(scoped_ptr<base::Clock> clock);
83 // Returns the total number of dictionaries loaded.
84 int GetDictionaryCountForTesting() const;
86 // Returns whether this SdchOwner has dictionary from |url| loaded.
87 bool HasDictionaryFromURLForTesting(const GURL& url) const;
89 void SetFetcherForTesting(scoped_ptr<SdchDictionaryFetcher> fetcher);
91 private:
92 // For each active dictionary, stores local info.
93 // Indexed by the server hash of the dictionary.
94 struct DictionaryInfo {
95 base::Time last_used;
96 int use_count;
97 size_t size;
99 DictionaryInfo() : use_count(0), size(0) {}
100 DictionaryInfo(const base::Time& last_used, size_t size)
101 : last_used(last_used), use_count(0), size(size) {}
102 DictionaryInfo(const DictionaryInfo& rhs) = default;
103 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default;
106 void OnMemoryPressure(
107 base::MemoryPressureListener::MemoryPressureLevel level);
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);
116 bool IsPersistingDictionaries() const;
118 enum DictionaryFate {
119 // A Get-Dictionary header wasn't acted on.
120 DICTIONARY_FATE_GET_IGNORED = 1,
122 // A fetch was attempted, but failed.
123 // TODO(rdsmith): Actually record this case.
124 DICTIONARY_FATE_FETCH_FAILED = 2,
126 // A successful fetch was dropped on the floor, no space.
127 DICTIONARY_FATE_FETCH_IGNORED_NO_SPACE = 3,
129 // A successful fetch was refused by the SdchManager.
130 DICTIONARY_FATE_FETCH_MANAGER_REFUSED = 4,
132 // A dictionary was successfully added based on
133 // a Get-Dictionary header in a response.
134 DICTIONARY_FATE_ADD_RESPONSE_TRIGGERED = 5,
136 // A dictionary was evicted by an incoming dict.
137 DICTIONARY_FATE_EVICT_FOR_DICT = 6,
139 // A dictionary was evicted by memory pressure.
140 DICTIONARY_FATE_EVICT_FOR_MEMORY = 7,
142 // A dictionary was evicted on destruction.
143 DICTIONARY_FATE_EVICT_FOR_DESTRUCTION = 8,
145 // A dictionary was successfully added based on
146 // persistence from a previous browser revision.
147 DICTIONARY_FATE_ADD_PERSISTENCE_TRIGGERED = 9,
149 // A dictionary was unloaded on destruction, but is still present on disk.
150 DICTIONARY_FATE_UNLOAD_FOR_DESTRUCTION = 10,
152 DICTIONARY_FATE_MAX = 11
155 void RecordDictionaryFate(DictionaryFate fate);
157 // Record the lifetime memory use of a specified dictionary, identified by
158 // server hash.
159 void RecordDictionaryEvictionOrUnload(
160 const std::string& server_hash,
161 size_t size,
162 int use_count, DictionaryFate fate);
164 net::SdchManager* manager_;
165 scoped_ptr<net::SdchDictionaryFetcher> fetcher_;
167 size_t total_dictionary_bytes_;
169 scoped_ptr<base::Clock> clock_;
171 size_t max_total_dictionary_size_;
172 size_t min_space_for_dictionary_fetch_;
174 base::MemoryPressureListener memory_pressure_listener_;
176 // Dictionary persistence machinery.
177 // * |in_memory_pref_store_| is created on construction and used in
178 // the absence of any call to EnablePersistentStorage().
179 // * |external_pref_store_| holds the preference store specified
180 // by EnablePersistentStorage() (if any), while it is being read in.
181 // A non-null value here signals that the SdchOwner is observing
182 // the pref store; when read-in completes and observation is no longer
183 // needed, the pointer is set to null. This is to avoid lots of
184 // extra irrelevant function calls; the only observer interface this
185 // class is interested in is OnInitializationCompleted().
186 // * |pref_store_| holds an unowned pointer to the currently
187 // active pref store (one of the preceding two).
188 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
189 PersistentPrefStore* external_pref_store_;
191 WriteablePrefStore* pref_store_;
193 // The use counts of dictionaries when they were loaded from the persistent
194 // store, keyed by server hash. These are stored to avoid generating
195 // misleading ever-increasing use counts for dictionaries that are persisted,
196 // since the UMA histogram for use counts is only supposed to be since last
197 // load.
198 std::map<std::string, int> use_counts_at_load_;
200 // Load times for loaded dictionaries, keyed by server hash. These are used to
201 // track the durations that dictionaries are in memory.
202 std::map<std::string, base::Time> load_times_;
204 // Byte-seconds consumed by dictionaries that have been unloaded. These are
205 // stored for later uploading in the SdchOwner destructor.
206 std::vector<int64> consumed_byte_seconds_;
208 // Creation time for this SdchOwner object, used for reporting temporal memory
209 // pressure.
210 base::Time creation_time_;
212 DISALLOW_COPY_AND_ASSIGN(SdchOwner);
215 } // namespace net
217 #endif // NET_SDCH_SDCH_OWNER_H_