Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / net / base / sdch_manager.h
blobc8d0a847047e0d55439c66846fe70434479e61f6
1 // Copyright (c) 2011 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 // This file contains the SdchManager class and the DictionarySet
6 // nested class. The manager is responsible for storing all
7 // SdchDictionarys, and provides access to them through DictionarySet
8 // objects. A DictionarySet is an object whose lifetime is under the
9 // control of the consumer. It is a reference to a set of
10 // dictionaries, and guarantees that none of those dictionaries will
11 // be destroyed while the DictionarySet reference is alive.
13 #ifndef NET_BASE_SDCH_MANAGER_H_
14 #define NET_BASE_SDCH_MANAGER_H_
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <vector>
21 #include "base/gtest_prod_util.h"
22 #include "base/memory/ref_counted.h"
23 #include "base/memory/scoped_ptr.h"
24 #include "base/observer_list.h"
25 #include "base/threading/thread_checker.h"
26 #include "net/base/net_export.h"
27 #include "net/base/sdch_dictionary.h"
28 #include "net/base/sdch_problem_codes.h"
30 class GURL;
32 namespace base {
33 class Value;
36 namespace net {
38 class SdchObserver;
40 // Provides global database of differential decompression dictionaries for the
41 // SDCH filter (processes sdch enconded content).
43 // The SdchManager maintains a collection of memory resident dictionaries. It
44 // can find a dictionary (based on a server specification of a hash), store a
45 // dictionary, and make judgements about what URLs can use, set, etc. a
46 // dictionary.
48 // These dictionaries are acquired over the net, and include a header
49 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF
50 // module) to decompress data.
52 // A dictionary held by the manager may nonetheless outlive the manager if
53 // a DictionarySet object refers to it; see below.
54 class NET_EXPORT SdchManager {
55 public:
56 typedef std::map<std::string,
57 scoped_refptr<base::RefCountedData<SdchDictionary>>>
58 DictionaryMap;
60 // A handle for one or more dictionaries which will keep the dictionaries
61 // alive and accessible for the handle's lifetime.
62 class NET_EXPORT_PRIVATE DictionarySet {
63 public:
64 ~DictionarySet();
66 // Return a comma separated list of client hashes.
67 std::string GetDictionaryClientHashList() const;
69 bool Empty() const;
71 // Lookup the dictionary contents based on the server hash. Returns
72 // a null pointer if the specified hash is not present in the dictionary
73 // set.
74 // The pointer is guaranteed to be valid as long as the DictionarySet
75 // is alive.
76 const std::string* GetDictionaryText(const std::string& server_hash) const;
78 private:
79 // A DictionarySet may only be constructed by the SdchManager.
80 friend class SdchManager;
82 DictionarySet();
83 void AddDictionary(
84 const std::string& server_hash,
85 const scoped_refptr<base::RefCountedData<SdchDictionary>>& dictionary);
87 DictionaryMap dictionaries_;
89 DISALLOW_COPY_AND_ASSIGN(DictionarySet);
92 SdchManager();
93 ~SdchManager();
95 // Clear data (for browser data removal).
96 void ClearData();
98 // Record stats on various errors.
99 static void SdchErrorRecovery(SdchProblemCode problem);
101 // Enables or disables SDCH compression.
102 static void EnableSdchSupport(bool enabled);
104 static bool sdch_enabled() { return g_sdch_enabled_; }
106 // Briefly prevent further advertising of SDCH on this domain (if SDCH is
107 // enabled). After enough calls to IsInSupportedDomain() the blacklisting
108 // will be removed. Additional blacklists take exponentially more calls
109 // to IsInSupportedDomain() before the blacklisting is undone.
110 // Used when filter errors are found from a given domain, but it is plausible
111 // that the cause is temporary (such as application startup, where cached
112 // entries are used, but a dictionary is not yet loaded).
113 void BlacklistDomain(const GURL& url, SdchProblemCode blacklist_reason);
115 // Used when SEVERE filter errors are found from a given domain, to prevent
116 // further use of SDCH on that domain.
117 void BlacklistDomainForever(const GURL& url,
118 SdchProblemCode blacklist_reason);
120 // Unit test only, this function resets enabling of sdch, and clears the
121 // blacklist.
122 void ClearBlacklistings();
124 // Unit test only, this function resets the blacklisting count for a domain.
125 void ClearDomainBlacklisting(const std::string& domain);
127 // Unit test only: indicate how many more times a domain will be blacklisted.
128 int BlackListDomainCount(const std::string& domain);
130 // Unit test only: Indicate what current blacklist increment is for a domain.
131 int BlacklistDomainExponential(const std::string& domain);
133 // Check to see if SDCH is enabled (globally), and the given URL is in a
134 // supported domain (i.e., not blacklisted, and either the specific supported
135 // domain, or all domains were assumed supported). If it is blacklist, reduce
136 // by 1 the number of times it will be reported as blacklisted.
137 SdchProblemCode IsInSupportedDomain(const GURL& url);
139 // Send out appropriate events notifying observers that a Get-Dictionary
140 // header has been seen.
141 SdchProblemCode OnGetDictionary(const GURL& request_url,
142 const GURL& dictionary_url);
144 // Send out appropriate events notifying observers that a dictionary
145 // was successfully used to decode a request. Note that this can happen
146 // after a dictionary has been deleted from the SdchManager (because
147 // DictionarySets retain references to deleted dictionaries).
148 void OnDictionaryUsed(const std::string& server_hash);
150 // Get a handle to the available dictionaries that might be used
151 // for encoding responses for the given URL. The return set will not
152 // include expired dictionaries. If no dictionaries
153 // are appropriate to use with the target_url, NULL is returned.
154 scoped_ptr<DictionarySet> GetDictionarySet(const GURL& target_url);
156 // Get a handle to a specific dictionary, by its server hash, confirming
157 // that that specific dictionary is appropriate to use with |target_url|.
158 // Expired dictionaries will be returned. If no dictionary with that
159 // hash exists that is usable with |target_url|, NULL is returned.
160 // If there is a usability problem, |*error_code| is set to the
161 // appropriate problem code.
162 scoped_ptr<DictionarySet> GetDictionarySetByHash(
163 const GURL& target_url,
164 const std::string& server_hash,
165 SdchProblemCode* problem_code);
167 // Construct the pair of hashes for client and server to identify an SDCH
168 // dictionary. This is only made public to facilitate unit testing, but is
169 // otherwise private
170 static void GenerateHash(const std::string& dictionary_text,
171 std::string* client_hash, std::string* server_hash);
173 // For Latency testing only, we need to know if we've succeeded in doing a
174 // round trip before starting our comparative tests. If ever we encounter
175 // problems with SDCH, we opt-out of the test unless/until we perform a
176 // complete SDCH decoding.
177 bool AllowLatencyExperiment(const GURL& url) const;
179 void SetAllowLatencyExperiment(const GURL& url, bool enable);
181 scoped_ptr<base::Value> SdchInfoToValue() const;
183 // Add an SDCH dictionary to our list of availible
184 // dictionaries. This addition will fail if addition is illegal
185 // (data in the dictionary is not acceptable from the
186 // dictionary_url; dictionary already added, etc.).
187 // If |server_hash| is non-null, returns the server hash that may be
188 // used as an argument to GetDictionarySetByHash.
189 // Returns SDCH_OK if the addition was successfull, and corresponding error
190 // code otherwise.
191 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text,
192 const GURL& dictionary_url,
193 std::string* server_hash_p);
195 // Remove an SDCH dictionary
196 SdchProblemCode RemoveSdchDictionary(const std::string& server_hash);
198 // Registration for events generated by the SDCH subsystem.
199 void AddObserver(SdchObserver* observer);
200 void RemoveObserver(SdchObserver* observer);
202 static scoped_ptr<DictionarySet> CreateEmptyDictionarySetForTesting();
204 private:
205 struct BlacklistInfo {
206 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {}
208 int count; // # of times to refuse SDCH advertisement.
209 int exponential_count; // Current exponential backoff ratchet.
210 SdchProblemCode reason; // Why domain was blacklisted.
213 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo;
214 typedef std::set<std::string> ExperimentSet;
216 // Determines whether a "Get-Dictionary" header is legal (dictionary
217 // url has appropriate relationship to referrer url) in the SDCH
218 // protocol. Return SDCH_OK if fetch is legal.
219 SdchProblemCode CanFetchDictionary(const GURL& referring_url,
220 const GURL& dictionary_url) const;
222 // Support SDCH compression, by advertising in headers.
223 static bool g_sdch_enabled_;
225 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
226 static void UrlSafeBase64Encode(const std::string& input,
227 std::string* output);
229 DictionaryMap dictionaries_;
231 // List domains where decode failures have required disabling sdch.
232 DomainBlacklistInfo blacklisted_domains_;
234 // List of hostnames for which a latency experiment is allowed (because a
235 // round trip test has recently passed).
236 ExperimentSet allow_latency_experiment_;
238 // Observers that want to be notified of SDCH events.
239 // Assert list is empty on destruction since if there is an observer
240 // that hasn't removed itself from the list, that observer probably
241 // has a reference to the SdchManager.
242 base::ObserverList<SdchObserver, true> observers_;
244 base::ThreadChecker thread_checker_;
246 DISALLOW_COPY_AND_ASSIGN(SdchManager);
249 } // namespace net
251 #endif // NET_BASE_SDCH_MANAGER_H_