Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / net / base / sdch_manager.h
blob19dc7103dedf4c987afc69d4eaf66c506588beb1
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 two nested classes
6 // (Dictionary, DictionarySet). SdchManager::Dictionary contains all
7 // of the information about an SDCH dictionary. The manager is
8 // responsible for storing those dictionaries, and provides access to
9 // them through DictionarySet objects. A DictionarySet is an object
10 // whose lifetime is under the control of the consumer. It is a
11 // reference to a set of dictionaries, and guarantees that none of
12 // those dictionaries will be destroyed while the DictionarySet
13 // reference is alive.
15 #ifndef NET_BASE_SDCH_MANAGER_H_
16 #define NET_BASE_SDCH_MANAGER_H_
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <vector>
23 #include "base/gtest_prod_util.h"
24 #include "base/memory/ref_counted.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/weak_ptr.h"
27 #include "base/observer_list.h"
28 #include "base/threading/thread_checker.h"
29 #include "base/time/time.h"
30 #include "net/base/net_export.h"
31 #include "net/base/sdch_problem_codes.h"
32 #include "url/gurl.h"
34 namespace base {
35 class Clock;
36 class Value;
39 namespace net {
41 class SdchObserver;
43 // Provides global database of differential decompression dictionaries for the
44 // SDCH filter (processes sdch enconded content).
46 // The SdchManager maintains a collection of memory resident dictionaries. It
47 // can find a dictionary (based on a server specification of a hash), store a
48 // dictionary, and make judgements about what URLs can use, set, etc. a
49 // dictionary.
51 // These dictionaries are acquired over the net, and include a header
52 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF
53 // module) to decompress data.
55 // A dictionary held by the manager may nonetheless outlive the manager if
56 // a DictionarySet object refers to it; see below.
57 class NET_EXPORT SdchManager {
58 public:
59 class Dictionary;
60 typedef std::map<std::string, scoped_refptr<base::RefCountedData<Dictionary>>>
61 DictionaryMap;
63 class NET_EXPORT_PRIVATE Dictionary {
64 public:
65 // Construct a vc-diff usable dictionary from the dictionary_text starting
66 // at the given offset. The supplied client_hash should be used to
67 // advertise the dictionary's availability relative to the suppplied URL.
68 Dictionary(const std::string& dictionary_text,
69 size_t offset,
70 const std::string& client_hash,
71 const std::string& server_hash,
72 const GURL& url,
73 const std::string& domain,
74 const std::string& path,
75 const base::Time& expiration,
76 const std::set<int>& ports);
78 ~Dictionary();
80 // Sdch filters can get our text to use in decoding compressed data.
81 const std::string& text() const { return text_; }
83 const GURL& url() const { return url_; }
84 const std::string& client_hash() const { return client_hash_; }
85 const std::string& server_hash() const { return server_hash_; }
86 const std::string& domain() const { return domain_; }
87 const std::string& path() const { return path_; }
88 const base::Time& expiration() const { return expiration_; }
89 const std::set<int>& ports() const { return ports_; }
91 // Security methods to check if we can establish a new dictionary with the
92 // given data, that arrived in response to get of dictionary_url.
93 static SdchProblemCode CanSet(const std::string& domain,
94 const std::string& path,
95 const std::set<int>& ports,
96 const GURL& dictionary_url);
98 // Security method to check if we can use a dictionary to decompress a
99 // target that arrived with a reference to this dictionary.
100 SdchProblemCode CanUse(const GURL& referring_url) const;
102 // Compare paths to see if they "match" for dictionary use.
103 static bool PathMatch(const std::string& path,
104 const std::string& restriction);
106 // Compare domains to see if the "match" for dictionary use.
107 static bool DomainMatch(const GURL& url, const std::string& restriction);
109 // Is this dictionary expired?
110 bool Expired() const;
112 void SetClockForTesting(scoped_ptr<base::Clock> clock);
114 private:
115 friend class base::RefCountedData<Dictionary>;
117 // Private copy-constructor to support RefCountedData<>, which requires
118 // that an object stored in it be either DefaultConstructible or
119 // CopyConstructible
120 Dictionary(const Dictionary& rhs);
122 // The actual text of the dictionary.
123 std::string text_;
125 // Part of the hash of text_ that the client uses to advertise the fact that
126 // it has a specific dictionary pre-cached.
127 std::string client_hash_;
129 // Part of the hash of text_ that the server uses to identify the
130 // dictionary it wants used for decoding.
131 std::string server_hash_;
133 // The GURL that arrived with the text_ in a URL request to specify where
134 // this dictionary may be used.
135 const GURL url_;
137 // Metadate "headers" in before dictionary text contained the following:
138 // Each dictionary payload consists of several headers, followed by the text
139 // of the dictionary. The following are the known headers.
140 const std::string domain_;
141 const std::string path_;
142 const base::Time expiration_; // Implied by max-age.
143 const std::set<int> ports_;
145 scoped_ptr<base::Clock> clock_;
147 void operator=(const Dictionary&) = delete;
150 // A handle for one or more dictionaries which will keep the dictionaries
151 // alive and accessible for the handle's lifetime.
152 class NET_EXPORT_PRIVATE DictionarySet {
153 public:
154 ~DictionarySet();
156 // Return a comma separated list of client hashes.
157 std::string GetDictionaryClientHashList() const;
159 // Lookup a given dictionary based on server hash. Returned pointer
160 // is guaranteed to be valid for the lifetime of the DictionarySet.
161 // Returns NULL if hash is not a valid server hash for a dictionary
162 // named by DictionarySet.
163 const SdchManager::Dictionary* GetDictionary(const std::string& hash) const;
165 bool Empty() const;
167 private:
168 // A DictionarySet may only be constructed by the SdchManager.
169 friend class SdchManager;
171 DictionarySet();
172 void AddDictionary(const std::string& server_hash,
173 const scoped_refptr<base::RefCountedData<
174 SdchManager::Dictionary>>& dictionary);
176 DictionaryMap dictionaries_;
178 DISALLOW_COPY_AND_ASSIGN(DictionarySet);
181 SdchManager();
182 ~SdchManager();
184 // Clear data (for browser data removal).
185 void ClearData();
187 // Record stats on various errors.
188 static void SdchErrorRecovery(SdchProblemCode problem);
190 // Enables or disables SDCH compression.
191 static void EnableSdchSupport(bool enabled);
193 static bool sdch_enabled() { return g_sdch_enabled_; }
195 // Enables or disables SDCH compression over secure connection.
196 static void EnableSecureSchemeSupport(bool enabled);
198 static bool secure_scheme_supported() { return g_secure_scheme_supported_; }
200 // Briefly prevent further advertising of SDCH on this domain (if SDCH is
201 // enabled). After enough calls to IsInSupportedDomain() the blacklisting
202 // will be removed. Additional blacklists take exponentially more calls
203 // to IsInSupportedDomain() before the blacklisting is undone.
204 // Used when filter errors are found from a given domain, but it is plausible
205 // that the cause is temporary (such as application startup, where cached
206 // entries are used, but a dictionary is not yet loaded).
207 void BlacklistDomain(const GURL& url, SdchProblemCode blacklist_reason);
209 // Used when SEVERE filter errors are found from a given domain, to prevent
210 // further use of SDCH on that domain.
211 void BlacklistDomainForever(const GURL& url,
212 SdchProblemCode blacklist_reason);
214 // Unit test only, this function resets enabling of sdch, and clears the
215 // blacklist.
216 void ClearBlacklistings();
218 // Unit test only, this function resets the blacklisting count for a domain.
219 void ClearDomainBlacklisting(const std::string& domain);
221 // Unit test only: indicate how many more times a domain will be blacklisted.
222 int BlackListDomainCount(const std::string& domain);
224 // Unit test only: Indicate what current blacklist increment is for a domain.
225 int BlacklistDomainExponential(const std::string& domain);
227 // Check to see if SDCH is enabled (globally), and the given URL is in a
228 // supported domain (i.e., not blacklisted, and either the specific supported
229 // domain, or all domains were assumed supported). If it is blacklist, reduce
230 // by 1 the number of times it will be reported as blacklisted.
231 SdchProblemCode IsInSupportedDomain(const GURL& url);
233 // Send out appropriate events notifying observers that a Get-Dictionary
234 // header has been seen.
235 SdchProblemCode OnGetDictionary(const GURL& request_url,
236 const GURL& dictionary_url);
238 // Send out appropriate events notifying observers that a dictionary
239 // was successfully used to decode a request. Note that this can happen
240 // after a dictionary has been deleted from the SdchManager (because
241 // DictionarySets retain references to deleted dictionaries).
242 void OnDictionaryUsed(const std::string& server_hash);
244 // Get a handle to the available dictionaries that might be used
245 // for encoding responses for the given URL. The return set will not
246 // include expired dictionaries. If no dictionaries
247 // are appropriate to use with the target_url, NULL is returned.
248 scoped_ptr<DictionarySet> GetDictionarySet(const GURL& target_url);
250 // Get a handle to a specific dictionary, by its server hash, confirming
251 // that that specific dictionary is appropriate to use with |target_url|.
252 // Expired dictionaries will be returned. If no dictionary with that
253 // hash exists that is usable with |target_url|, NULL is returned.
254 // If there is a usability problem, |*error_code| is set to the
255 // appropriate problem code.
256 scoped_ptr<DictionarySet> GetDictionarySetByHash(
257 const GURL& target_url,
258 const std::string& server_hash,
259 SdchProblemCode* problem_code);
261 // Construct the pair of hashes for client and server to identify an SDCH
262 // dictionary. This is only made public to facilitate unit testing, but is
263 // otherwise private
264 static void GenerateHash(const std::string& dictionary_text,
265 std::string* client_hash, std::string* server_hash);
267 // For Latency testing only, we need to know if we've succeeded in doing a
268 // round trip before starting our comparative tests. If ever we encounter
269 // problems with SDCH, we opt-out of the test unless/until we perform a
270 // complete SDCH decoding.
271 bool AllowLatencyExperiment(const GURL& url) const;
273 void SetAllowLatencyExperiment(const GURL& url, bool enable);
275 base::Value* SdchInfoToValue() const;
277 // Add an SDCH dictionary to our list of availible
278 // dictionaries. This addition will fail if addition is illegal
279 // (data in the dictionary is not acceptable from the
280 // dictionary_url; dictionary already added, etc.).
281 // If |server_hash| is non-null, returns the server hash that may be
282 // used as an argument to GetDictionarySetByHash.
283 // Returns SDCH_OK if the addition was successfull, and corresponding error
284 // code otherwise.
285 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text,
286 const GURL& dictionary_url,
287 std::string* server_hash_p);
289 // Remove an SDCH dictionary
290 SdchProblemCode RemoveSdchDictionary(const std::string& server_hash);
292 // Registration for events generated by the SDCH subsystem.
293 void AddObserver(SdchObserver* observer);
294 void RemoveObserver(SdchObserver* observer);
296 static scoped_ptr<DictionarySet> CreateEmptyDictionarySetForTesting();
298 // For investigation of http://crbug.com/454198; remove when resolved.
299 base::WeakPtr<SdchManager> GetWeakPtr();
301 private:
302 struct BlacklistInfo {
303 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {}
305 int count; // # of times to refuse SDCH advertisement.
306 int exponential_count; // Current exponential backoff ratchet.
307 SdchProblemCode reason; // Why domain was blacklisted.
310 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo;
311 typedef std::set<std::string> ExperimentSet;
313 // Determines whether a "Get-Dictionary" header is legal (dictionary
314 // url has appropriate relationship to referrer url) in the SDCH
315 // protocol. Return SDCH_OK if fetch is legal.
316 SdchProblemCode CanFetchDictionary(const GURL& referring_url,
317 const GURL& dictionary_url) const;
319 // Support SDCH compression, by advertising in headers.
320 static bool g_sdch_enabled_;
322 // Support SDCH compression for HTTPS requests and responses. When supported,
323 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS.
324 static bool g_secure_scheme_supported_;
326 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
327 static void UrlSafeBase64Encode(const std::string& input,
328 std::string* output);
330 DictionaryMap dictionaries_;
332 // List domains where decode failures have required disabling sdch.
333 DomainBlacklistInfo blacklisted_domains_;
335 // List of hostnames for which a latency experiment is allowed (because a
336 // round trip test has recently passed).
337 ExperimentSet allow_latency_experiment_;
339 // Observers that want to be notified of SDCH events.
340 // Assert list is empty on destruction since if there is an observer
341 // that hasn't removed itself from the list, that observer probably
342 // has a reference to the SdchManager.
343 ObserverList<SdchObserver, true> observers_;
345 base::ThreadChecker thread_checker_;
347 base::WeakPtrFactory<SdchManager> factory_;
349 DISALLOW_COPY_AND_ASSIGN(SdchManager);
352 } // namespace net
354 #endif // NET_BASE_SDCH_MANAGER_H_