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_
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"
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
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
{
56 typedef std::map
<std::string
,
57 scoped_refptr
<base::RefCountedData
<SdchDictionary
>>>
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
{
66 // Return a comma separated list of client hashes.
67 std::string
GetDictionaryClientHashList() 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
74 // The pointer is guaranteed to be valid as long as the DictionarySet
76 const std::string
* GetDictionaryText(const std::string
& server_hash
) const;
79 // A DictionarySet may only be constructed by the SdchManager.
80 friend class SdchManager
;
84 const std::string
& server_hash
,
85 const scoped_refptr
<base::RefCountedData
<SdchDictionary
>>& dictionary
);
87 DictionaryMap dictionaries_
;
89 DISALLOW_COPY_AND_ASSIGN(DictionarySet
);
95 // Clear data (for browser data removal).
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 // Enables or disables SDCH compression over secure connection.
107 static void EnableSecureSchemeSupport(bool enabled
);
109 static bool secure_scheme_supported() { return g_secure_scheme_supported_
; }
111 // Briefly prevent further advertising of SDCH on this domain (if SDCH is
112 // enabled). After enough calls to IsInSupportedDomain() the blacklisting
113 // will be removed. Additional blacklists take exponentially more calls
114 // to IsInSupportedDomain() before the blacklisting is undone.
115 // Used when filter errors are found from a given domain, but it is plausible
116 // that the cause is temporary (such as application startup, where cached
117 // entries are used, but a dictionary is not yet loaded).
118 void BlacklistDomain(const GURL
& url
, SdchProblemCode blacklist_reason
);
120 // Used when SEVERE filter errors are found from a given domain, to prevent
121 // further use of SDCH on that domain.
122 void BlacklistDomainForever(const GURL
& url
,
123 SdchProblemCode blacklist_reason
);
125 // Unit test only, this function resets enabling of sdch, and clears the
127 void ClearBlacklistings();
129 // Unit test only, this function resets the blacklisting count for a domain.
130 void ClearDomainBlacklisting(const std::string
& domain
);
132 // Unit test only: indicate how many more times a domain will be blacklisted.
133 int BlackListDomainCount(const std::string
& domain
);
135 // Unit test only: Indicate what current blacklist increment is for a domain.
136 int BlacklistDomainExponential(const std::string
& domain
);
138 // Check to see if SDCH is enabled (globally), and the given URL is in a
139 // supported domain (i.e., not blacklisted, and either the specific supported
140 // domain, or all domains were assumed supported). If it is blacklist, reduce
141 // by 1 the number of times it will be reported as blacklisted.
142 SdchProblemCode
IsInSupportedDomain(const GURL
& url
);
144 // Send out appropriate events notifying observers that a Get-Dictionary
145 // header has been seen.
146 SdchProblemCode
OnGetDictionary(const GURL
& request_url
,
147 const GURL
& dictionary_url
);
149 // Send out appropriate events notifying observers that a dictionary
150 // was successfully used to decode a request. Note that this can happen
151 // after a dictionary has been deleted from the SdchManager (because
152 // DictionarySets retain references to deleted dictionaries).
153 void OnDictionaryUsed(const std::string
& server_hash
);
155 // Get a handle to the available dictionaries that might be used
156 // for encoding responses for the given URL. The return set will not
157 // include expired dictionaries. If no dictionaries
158 // are appropriate to use with the target_url, NULL is returned.
159 scoped_ptr
<DictionarySet
> GetDictionarySet(const GURL
& target_url
);
161 // Get a handle to a specific dictionary, by its server hash, confirming
162 // that that specific dictionary is appropriate to use with |target_url|.
163 // Expired dictionaries will be returned. If no dictionary with that
164 // hash exists that is usable with |target_url|, NULL is returned.
165 // If there is a usability problem, |*error_code| is set to the
166 // appropriate problem code.
167 scoped_ptr
<DictionarySet
> GetDictionarySetByHash(
168 const GURL
& target_url
,
169 const std::string
& server_hash
,
170 SdchProblemCode
* problem_code
);
172 // Construct the pair of hashes for client and server to identify an SDCH
173 // dictionary. This is only made public to facilitate unit testing, but is
175 static void GenerateHash(const std::string
& dictionary_text
,
176 std::string
* client_hash
, std::string
* server_hash
);
178 // For Latency testing only, we need to know if we've succeeded in doing a
179 // round trip before starting our comparative tests. If ever we encounter
180 // problems with SDCH, we opt-out of the test unless/until we perform a
181 // complete SDCH decoding.
182 bool AllowLatencyExperiment(const GURL
& url
) const;
184 void SetAllowLatencyExperiment(const GURL
& url
, bool enable
);
186 scoped_ptr
<base::Value
> SdchInfoToValue() const;
188 // Add an SDCH dictionary to our list of availible
189 // dictionaries. This addition will fail if addition is illegal
190 // (data in the dictionary is not acceptable from the
191 // dictionary_url; dictionary already added, etc.).
192 // If |server_hash| is non-null, returns the server hash that may be
193 // used as an argument to GetDictionarySetByHash.
194 // Returns SDCH_OK if the addition was successfull, and corresponding error
196 SdchProblemCode
AddSdchDictionary(const std::string
& dictionary_text
,
197 const GURL
& dictionary_url
,
198 std::string
* server_hash_p
);
200 // Remove an SDCH dictionary
201 SdchProblemCode
RemoveSdchDictionary(const std::string
& server_hash
);
203 // Registration for events generated by the SDCH subsystem.
204 void AddObserver(SdchObserver
* observer
);
205 void RemoveObserver(SdchObserver
* observer
);
207 static scoped_ptr
<DictionarySet
> CreateEmptyDictionarySetForTesting();
210 struct BlacklistInfo
{
211 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK
) {}
213 int count
; // # of times to refuse SDCH advertisement.
214 int exponential_count
; // Current exponential backoff ratchet.
215 SdchProblemCode reason
; // Why domain was blacklisted.
218 typedef std::map
<std::string
, BlacklistInfo
> DomainBlacklistInfo
;
219 typedef std::set
<std::string
> ExperimentSet
;
221 // Determines whether a "Get-Dictionary" header is legal (dictionary
222 // url has appropriate relationship to referrer url) in the SDCH
223 // protocol. Return SDCH_OK if fetch is legal.
224 SdchProblemCode
CanFetchDictionary(const GURL
& referring_url
,
225 const GURL
& dictionary_url
) const;
227 // Support SDCH compression, by advertising in headers.
228 static bool g_sdch_enabled_
;
230 // Support SDCH compression for HTTPS requests and responses. When supported,
231 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS.
232 static bool g_secure_scheme_supported_
;
234 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
235 static void UrlSafeBase64Encode(const std::string
& input
,
236 std::string
* output
);
238 DictionaryMap dictionaries_
;
240 // List domains where decode failures have required disabling sdch.
241 DomainBlacklistInfo blacklisted_domains_
;
243 // List of hostnames for which a latency experiment is allowed (because a
244 // round trip test has recently passed).
245 ExperimentSet allow_latency_experiment_
;
247 // Observers that want to be notified of SDCH events.
248 // Assert list is empty on destruction since if there is an observer
249 // that hasn't removed itself from the list, that observer probably
250 // has a reference to the SdchManager.
251 base::ObserverList
<SdchObserver
, true> observers_
;
253 base::ThreadChecker thread_checker_
;
255 DISALLOW_COPY_AND_ASSIGN(SdchManager
);
260 #endif // NET_BASE_SDCH_MANAGER_H_