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 #ifndef NET_BASE_SDCH_MANAGER_H_
6 #define NET_BASE_SDCH_MANAGER_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/time.h"
18 #include "net/base/net_export.h"
19 #include "net/base/sdch_problem_codes.h"
30 // Provides global database of differential decompression dictionaries for the
31 // SDCH filter (processes sdch enconded content).
33 // The SdchManager maintains a collection of memory resident dictionaries. It
34 // can find a dictionary (based on a server specification of a hash), store a
35 // dictionary, and make judgements about what URLs can use, set, etc. a
38 // These dictionaries are acquired over the net, and include a header
39 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF
40 // module) to decompress data.
41 class NET_EXPORT SdchManager
{
43 // Use the following static limits to block DOS attacks until we implement
44 // a cached dictionary evicition strategy.
45 static const size_t kMaxDictionarySize
;
46 static const size_t kMaxDictionaryCount
;
48 // There is one instance of |Dictionary| for each memory-cached SDCH
50 class NET_EXPORT_PRIVATE Dictionary
: public base::RefCounted
<Dictionary
> {
52 // Sdch filters can get our text to use in decoding compressed data.
53 const std::string
& text() const { return text_
; }
56 friend class base::RefCounted
<Dictionary
>;
57 friend class SdchManager
; // Only manager can construct an instance.
58 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest
, PathMatch
);
60 // Construct a vc-diff usable dictionary from the dictionary_text starting
61 // at the given offset. The supplied client_hash should be used to
62 // advertise the dictionary's availability relative to the suppplied URL.
63 Dictionary(const std::string
& dictionary_text
,
65 const std::string
& client_hash
,
67 const std::string
& domain
,
68 const std::string
& path
,
69 const base::Time
& expiration
,
70 const std::set
<int>& ports
);
71 virtual ~Dictionary();
73 const GURL
& url() const { return url_
; }
74 const std::string
& client_hash() const { return client_hash_
; }
75 const std::string
& domain() const { return domain_
; }
76 const std::string
& path() const { return path_
; }
77 const base::Time
& expiration() const { return expiration_
; }
78 const std::set
<int>& ports() const { return ports_
; }
80 // Security method to check if we can advertise this dictionary for use
81 // if the |target_url| returns SDCH compressed data.
82 SdchProblemCode
CanAdvertise(const GURL
& target_url
) const;
84 // Security methods to check if we can establish a new dictionary with the
85 // given data, that arrived in response to get of dictionary_url.
86 static SdchProblemCode
CanSet(const std::string
& domain
,
87 const std::string
& path
,
88 const std::set
<int>& ports
,
89 const GURL
& dictionary_url
);
91 // Security method to check if we can use a dictionary to decompress a
92 // target that arrived with a reference to this dictionary.
93 SdchProblemCode
CanUse(const GURL
& referring_url
) const;
95 // Compare paths to see if they "match" for dictionary use.
96 static bool PathMatch(const std::string
& path
,
97 const std::string
& restriction
);
99 // Compare domains to see if the "match" for dictionary use.
100 static bool DomainMatch(const GURL
& url
, const std::string
& restriction
);
102 // The actual text of the dictionary.
105 // Part of the hash of text_ that the client uses to advertise the fact that
106 // it has a specific dictionary pre-cached.
107 std::string client_hash_
;
109 // The GURL that arrived with the text_ in a URL request to specify where
110 // this dictionary may be used.
113 // Metadate "headers" in before dictionary text contained the following:
114 // Each dictionary payload consists of several headers, followed by the text
115 // of the dictionary. The following are the known headers.
116 const std::string domain_
;
117 const std::string path_
;
118 const base::Time expiration_
; // Implied by max-age.
119 const std::set
<int> ports_
;
121 DISALLOW_COPY_AND_ASSIGN(Dictionary
);
127 // Clear data (for browser data removal).
130 // Record stats on various errors.
131 static void SdchErrorRecovery(SdchProblemCode problem
);
133 // Enables or disables SDCH compression.
134 static void EnableSdchSupport(bool enabled
);
136 static bool sdch_enabled() { return g_sdch_enabled_
; }
138 // Enables or disables SDCH compression over secure connection.
139 static void EnableSecureSchemeSupport(bool enabled
);
141 static bool secure_scheme_supported() { return g_secure_scheme_supported_
; }
143 // Briefly prevent further advertising of SDCH on this domain (if SDCH is
144 // enabled). After enough calls to IsInSupportedDomain() the blacklisting
145 // will be removed. Additional blacklists take exponentially more calls
146 // to IsInSupportedDomain() before the blacklisting is undone.
147 // Used when filter errors are found from a given domain, but it is plausible
148 // that the cause is temporary (such as application startup, where cached
149 // entries are used, but a dictionary is not yet loaded).
150 void BlacklistDomain(const GURL
& url
, SdchProblemCode blacklist_reason
);
152 // Used when SEVERE filter errors are found from a given domain, to prevent
153 // further use of SDCH on that domain.
154 void BlacklistDomainForever(const GURL
& url
,
155 SdchProblemCode blacklist_reason
);
157 // Unit test only, this function resets enabling of sdch, and clears the
159 void ClearBlacklistings();
161 // Unit test only, this function resets the blacklisting count for a domain.
162 void ClearDomainBlacklisting(const std::string
& domain
);
164 // Unit test only: indicate how many more times a domain will be blacklisted.
165 int BlackListDomainCount(const std::string
& domain
);
167 // Unit test only: Indicate what current blacklist increment is for a domain.
168 int BlacklistDomainExponential(const std::string
& domain
);
170 // Check to see if SDCH is enabled (globally), and the given URL is in a
171 // supported domain (i.e., not blacklisted, and either the specific supported
172 // domain, or all domains were assumed supported). If it is blacklist, reduce
173 // by 1 the number of times it will be reported as blacklisted.
174 SdchProblemCode
IsInSupportedDomain(const GURL
& url
);
176 // Send out appropriate events notifying observers that a Get-Dictionary
177 // header has been seen.
178 SdchProblemCode
OnGetDictionary(const GURL
& request_url
,
179 const GURL
& dictionary_url
);
181 // Find the vcdiff dictionary (the body of the sdch dictionary that appears
182 // after the meta-data headers like Domain:...) with the given |server_hash|
183 // to use to decompreses data that arrived as SDCH encoded content. Check to
184 // be sure the returned |dictionary| can be used for decoding content supplied
185 // in response to a request for |referring_url|.
186 // Return null in |dictionary| if there is no matching legal dictionary.
187 // Returns SDCH_OK if dictionary is not found, SDCH(-over-https) is disabled,
188 // or if matching legal dictionary exists. Otherwise returns the
189 // corresponding problem code.
190 SdchProblemCode
GetVcdiffDictionary(const std::string
& server_hash
,
191 const GURL
& referring_url
,
192 scoped_refptr
<Dictionary
>* dictionary
);
194 // Get list of available (pre-cached) dictionaries that we have already loaded
195 // into memory. The list is a comma separated list of (client) hashes per
197 void GetAvailDictionaryList(const GURL
& target_url
, std::string
* list
);
199 // Construct the pair of hashes for client and server to identify an SDCH
200 // dictionary. This is only made public to facilitate unit testing, but is
202 static void GenerateHash(const std::string
& dictionary_text
,
203 std::string
* client_hash
, std::string
* server_hash
);
205 // For Latency testing only, we need to know if we've succeeded in doing a
206 // round trip before starting our comparative tests. If ever we encounter
207 // problems with SDCH, we opt-out of the test unless/until we perform a
208 // complete SDCH decoding.
209 bool AllowLatencyExperiment(const GURL
& url
) const;
211 void SetAllowLatencyExperiment(const GURL
& url
, bool enable
);
213 base::Value
* SdchInfoToValue() const;
215 // Add an SDCH dictionary to our list of availible
216 // dictionaries. This addition will fail if addition is illegal
217 // (data in the dictionary is not acceptable from the
218 // dictionary_url; dictionary already added, etc.).
219 // Returns SDCH_OK if the addition was successfull, and corresponding error
221 SdchProblemCode
AddSdchDictionary(const std::string
& dictionary_text
,
222 const GURL
& dictionary_url
);
224 // Registration for events generated by the SDCH subsystem.
225 void AddObserver(SdchObserver
* observer
);
226 void RemoveObserver(SdchObserver
* observer
);
229 struct BlacklistInfo
{
230 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK
) {}
232 int count
; // # of times to refuse SDCH advertisement.
233 int exponential_count
; // Current exponential backoff ratchet.
234 SdchProblemCode reason
; // Why domain was blacklisted.
236 typedef std::map
<std::string
, BlacklistInfo
> DomainBlacklistInfo
;
237 typedef std::set
<std::string
> ExperimentSet
;
239 // Determines whether a "Get-Dictionary" header is legal (dictionary
240 // url has appropriate relationship to referrer url) in the SDCH
241 // protocol. Return SDCH_OK if fetch is legal.
242 SdchProblemCode
CanFetchDictionary(const GURL
& referring_url
,
243 const GURL
& dictionary_url
) const;
245 // A map of dictionaries info indexed by the hash that the server provides.
246 typedef std::map
<std::string
, scoped_refptr
<Dictionary
> > DictionaryMap
;
248 // Support SDCH compression, by advertising in headers.
249 static bool g_sdch_enabled_
;
251 // Support SDCH compression for HTTPS requests and responses. When supported,
252 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS.
253 static bool g_secure_scheme_supported_
;
255 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
256 static void UrlSafeBase64Encode(const std::string
& input
,
257 std::string
* output
);
259 DictionaryMap dictionaries_
;
261 // List domains where decode failures have required disabling sdch.
262 DomainBlacklistInfo blacklisted_domains_
;
264 // List of hostnames for which a latency experiment is allowed (because a
265 // round trip test has recently passed).
266 ExperimentSet allow_latency_experiment_
;
268 // Observers that want to be notified of SDCH events.
269 // Assert list is empty on destruction since if there is an observer
270 // that hasn't removed itself from the list, that observer probably
271 // has a reference to the SdchManager.
272 ObserverList
<SdchObserver
, true> observers_
;
274 base::ThreadChecker thread_checker_
;
276 DISALLOW_COPY_AND_ASSIGN(SdchManager
);
281 #endif // NET_BASE_SDCH_MANAGER_H_