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_
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"
19 class PersistentPrefStore
;
20 class ValueMapPrefStore
;
21 class WriteablePrefStore
;
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
34 // TODO(rdsmith): Implement dictionary prioritization.
35 class NET_EXPORT SdchOwner
: public SdchObserver
, public PrefStore::Observer
{
37 static const size_t kMaxTotalDictionarySize
;
38 static const size_t kMinSpaceForDictionaryFetch
;
40 // Consumer must guarantee that |sdch_manager| and |context| outlive
42 SdchOwner(SdchManager
* sdch_manager
, URLRequestContext
* context
);
43 ~SdchOwner() override
;
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
);
52 // Defaults to kMaxTotalDictionarySize.
53 void SetMaxTotalDictionarySize(size_t max_total_dictionary_size
);
55 // Defaults to kMinSpaceForDictionaryFetch.
56 void SetMinSpaceForDictionaryFetch(size_t min_space_for_dictionary_fetch
);
58 // SdchObserver implementation.
59 void OnDictionaryUsed(SdchManager
* manager
,
60 const std::string
& server_hash
) override
;
61 void OnGetDictionary(SdchManager
* manager
,
62 const GURL
& request_url
,
63 const GURL
& dictionary_url
) override
;
64 void OnClearDictionaries(SdchManager
* manager
) 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
,
76 const std::string
& dictionary_text
,
77 const GURL
& dictionary_url
,
78 const BoundNetLog
& net_log
,
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
);
92 // For each active dictionary, stores local info.
93 // Indexed by the server hash of the dictionary.
94 struct DictionaryInfo
{
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 // For investigation of http://crbug.com/454198; remove when resolved.
119 base::WeakPtr
<SdchManager
> manager_
;
120 scoped_ptr
<SdchDictionaryFetcher
> fetcher_
;
122 size_t total_dictionary_bytes_
;
124 scoped_ptr
<base::Clock
> clock_
;
126 size_t max_total_dictionary_size_
;
127 size_t min_space_for_dictionary_fetch_
;
129 #if defined(OS_CHROMEOS)
130 // For debugging http://crbug.com/454198; remove when resolved.
131 unsigned int destroyed_
;
134 base::MemoryPressureListener memory_pressure_listener_
;
136 // Dictionary persistence machinery.
137 // * |in_memory_pref_store_| is created on construction and used in
138 // the absence of any call to EnablePersistentStorage().
139 // * |external_pref_store_| holds the preference store specified
140 // by EnablePersistentStorage() (if any), while it is being read in.
141 // A non-null value here signals that the SdchOwner is observing
142 // the pref store; when read-in completes and observation is no longer
143 // needed, the pointer is set to null. This is to avoid lots of
144 // extra irrelevant function calls; the only observer interface this
145 // class is interested in is OnInitializationCompleted().
146 // * |pref_store_| holds an unowned pointer to the currently
147 // active pref store (one of the preceding two).
148 scoped_refptr
<ValueMapPrefStore
> in_memory_pref_store_
;
149 PersistentPrefStore
* external_pref_store_
;
151 WriteablePrefStore
* pref_store_
;
153 // The use counts of dictionaries when they were loaded from the persistent
154 // store, keyed by server hash. These are stored to avoid generating
155 // misleading ever-increasing use counts for dictionaries that are persisted,
156 // since the UMA histogram for use counts is only supposed to be since last
158 std::map
<std::string
, int> use_counts_at_load_
;
160 DISALLOW_COPY_AND_ASSIGN(SdchOwner
);
165 #endif // NET_SDCH_SDCH_OWNER_H_