Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / sdch / sdch_owner.h
blob053eecd6d8868af984d3e1d62be665f3dc371c86
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/memory/weak_ptr.h"
14 #include "base/prefs/pref_store.h"
15 #include "net/base/sdch_observer.h"
16 #include "net/url_request/sdch_dictionary_fetcher.h"
18 class GURL;
19 class PersistentPrefStore;
20 class ValueMapPrefStore;
21 class WriteablePrefStore;
23 namespace base {
24 class Clock;
27 namespace net {
28 class SdchManager;
29 class URLRequestContext;
31 // This class owns the SDCH objects not owned as part of URLRequestContext, and
32 // exposes interface for setting SDCH policy. It should be instantiated by
33 // the net/ embedder.
34 // TODO(rdsmith): Implement dictionary prioritization.
35 class NET_EXPORT SdchOwner : public net::SdchObserver,
36 public PrefStore::Observer {
37 public:
38 static const size_t kMaxTotalDictionarySize;
39 static const size_t kMinSpaceForDictionaryFetch;
41 // Consumer must guarantee that |sdch_manager| and |context| outlive
42 // this object.
43 SdchOwner(net::SdchManager* sdch_manager, net::URLRequestContext* context);
44 ~SdchOwner() override;
46 // Enables use of pref persistence. Note that |pref_store| is owned
47 // by the caller, but must be guaranteed to outlive SdchOwner. The
48 // actual mechanisms by which the PersistentPrefStore are persisted
49 // are the responsibility of the caller. This routine may only be
50 // called once per SdchOwner instance.
51 void EnablePersistentStorage(PersistentPrefStore* pref_store);
53 // Defaults to kMaxTotalDictionarySize.
54 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size);
56 // Defaults to kMinSpaceForDictionaryFetch.
57 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch);
59 // SdchObserver implementation.
60 void OnDictionaryUsed(SdchManager* manager,
61 const std::string& server_hash) override;
62 void OnGetDictionary(net::SdchManager* manager,
63 const GURL& request_url,
64 const GURL& dictionary_url) override;
65 void OnClearDictionaries(net::SdchManager* manager) override;
67 // PrefStore::Observer implementation.
68 void OnPrefValueChanged(const std::string& key) override;
69 void OnInitializationCompleted(bool succeeded) override;
71 // Implementation detail--this is the function callback by the callback passed
72 // to the fetcher through which the fetcher informs the SdchOwner that it's
73 // gotten the dictionary. The first two arguments are bound locally.
74 // Public for testing.
75 void OnDictionaryFetched(base::Time last_used,
76 int use_count,
77 const std::string& dictionary_text,
78 const GURL& dictionary_url,
79 const net::BoundNetLog& net_log,
80 bool was_from_cache);
82 void SetClockForTesting(scoped_ptr<base::Clock> clock);
84 // Returns the total number of dictionaries loaded.
85 int GetDictionaryCountForTesting() const;
87 // Returns whether this SdchOwner has dictionary from |url| loaded.
88 bool HasDictionaryFromURLForTesting(const GURL& url) const;
90 void SetFetcherForTesting(scoped_ptr<SdchDictionaryFetcher> fetcher);
92 private:
93 // For each active dictionary, stores local info.
94 // Indexed by the server hash of the dictionary.
95 struct DictionaryInfo {
96 base::Time last_used;
97 int use_count;
98 size_t size;
100 DictionaryInfo() : use_count(0), size(0) {}
101 DictionaryInfo(const base::Time& last_used, size_t size)
102 : last_used(last_used), use_count(0), size(size) {}
103 DictionaryInfo(const DictionaryInfo& rhs) = default;
104 DictionaryInfo& operator=(const DictionaryInfo& rhs) = default;
107 void OnMemoryPressure(
108 base::MemoryPressureListener::MemoryPressureLevel level);
110 // Schedule loading of all dictionaries described in |persisted_info|.
111 // Returns false and does not schedule a load if |persisted_info| has an
112 // unsupported version or no dictionaries key. Skips any dictionaries that are
113 // malformed in |persisted_info|.
114 bool SchedulePersistedDictionaryLoads(
115 const base::DictionaryValue& persisted_info);
117 bool IsPersistingDictionaries() const;
119 // For investigation of http://crbug.com/454198; remove when resolved.
120 base::WeakPtr<net::SdchManager> manager_;
121 scoped_ptr<net::SdchDictionaryFetcher> fetcher_;
123 size_t total_dictionary_bytes_;
125 scoped_ptr<base::Clock> clock_;
127 size_t max_total_dictionary_size_;
128 size_t min_space_for_dictionary_fetch_;
130 #if defined(OS_CHROMEOS)
131 // For debugging http://crbug.com/454198; remove when resolved.
132 unsigned int destroyed_;
133 #endif
135 base::MemoryPressureListener memory_pressure_listener_;
137 // Dictionary persistence machinery.
138 // * |in_memory_pref_store_| is created on construction and used in
139 // the absence of any call to EnablePersistentStorage().
140 // * |external_pref_store_| holds the preference store specified
141 // by EnablePersistentStorage() (if any), while it is being read in.
142 // A non-null value here signals that the SdchOwner is observing
143 // the pref store; when read-in completes and observation is no longer
144 // needed, the pointer is set to null. This is to avoid lots of
145 // extra irrelevant function calls; the only observer interface this
146 // class is interested in is OnInitializationCompleted().
147 // * |pref_store_| holds an unowned pointer to the currently
148 // active pref store (one of the preceding two).
149 scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
150 PersistentPrefStore* external_pref_store_;
152 WriteablePrefStore* pref_store_;
154 // The use counts of dictionaries when they were loaded from the persistent
155 // store, keyed by server hash. These are stored to avoid generating
156 // misleading ever-increasing use counts for dictionaries that are persisted,
157 // since the UMA histogram for use counts is only supposed to be since last
158 // load.
159 std::map<std::string, int> use_counts_at_load_;
161 DISALLOW_COPY_AND_ASSIGN(SdchOwner);
164 } // namespace net
166 #endif // NET_SDCH_SDCH_OWNER_H_