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 // Briefly prevent further advertising of SDCH on this domain (if SDCH is
102 // enabled). After enough calls to IsInSupportedDomain() the blacklisting
103 // will be removed. Additional blacklists take exponentially more calls
104 // to IsInSupportedDomain() before the blacklisting is undone.
105 // Used when filter errors are found from a given domain, but it is plausible
106 // that the cause is temporary (such as application startup, where cached
107 // entries are used, but a dictionary is not yet loaded).
108 void BlacklistDomain(const GURL
& url
, SdchProblemCode blacklist_reason
);
110 // Used when SEVERE filter errors are found from a given domain, to prevent
111 // further use of SDCH on that domain.
112 void BlacklistDomainForever(const GURL
& url
,
113 SdchProblemCode blacklist_reason
);
115 // Unit test only, this function resets enabling of sdch, and clears the
117 void ClearBlacklistings();
119 // Unit test only, this function resets the blacklisting count for a domain.
120 void ClearDomainBlacklisting(const std::string
& domain
);
122 // Unit test only: indicate how many more times a domain will be blacklisted.
123 int BlackListDomainCount(const std::string
& domain
);
125 // Unit test only: Indicate what current blacklist increment is for a domain.
126 int BlacklistDomainExponential(const std::string
& domain
);
128 // Check to see if SDCH is enabled (globally), and the given URL is in a
129 // supported domain (i.e., not blacklisted, and either the specific supported
130 // domain, or all domains were assumed supported). If it is blacklist, reduce
131 // by 1 the number of times it will be reported as blacklisted.
132 SdchProblemCode
IsInSupportedDomain(const GURL
& url
);
134 // Send out appropriate events notifying observers that a Get-Dictionary
135 // header has been seen.
136 SdchProblemCode
OnGetDictionary(const GURL
& request_url
,
137 const GURL
& dictionary_url
);
139 // Send out appropriate events notifying observers that a dictionary
140 // was successfully used to decode a request. Note that this can happen
141 // after a dictionary has been deleted from the SdchManager (because
142 // DictionarySets retain references to deleted dictionaries).
143 void OnDictionaryUsed(const std::string
& server_hash
);
145 // Get a handle to the available dictionaries that might be used
146 // for encoding responses for the given URL. The return set will not
147 // include expired dictionaries. If no dictionaries
148 // are appropriate to use with the target_url, NULL is returned.
149 scoped_ptr
<DictionarySet
> GetDictionarySet(const GURL
& target_url
);
151 // Get a handle to a specific dictionary, by its server hash, confirming
152 // that that specific dictionary is appropriate to use with |target_url|.
153 // Expired dictionaries will be returned. If no dictionary with that
154 // hash exists that is usable with |target_url|, NULL is returned.
155 // If there is a usability problem, |*error_code| is set to the
156 // appropriate problem code.
157 scoped_ptr
<DictionarySet
> GetDictionarySetByHash(
158 const GURL
& target_url
,
159 const std::string
& server_hash
,
160 SdchProblemCode
* problem_code
);
162 // Construct the pair of hashes for client and server to identify an SDCH
163 // dictionary. This is only made public to facilitate unit testing, but is
165 static void GenerateHash(const std::string
& dictionary_text
,
166 std::string
* client_hash
, std::string
* server_hash
);
168 // For Latency testing only, we need to know if we've succeeded in doing a
169 // round trip before starting our comparative tests. If ever we encounter
170 // problems with SDCH, we opt-out of the test unless/until we perform a
171 // complete SDCH decoding.
172 bool AllowLatencyExperiment(const GURL
& url
) const;
174 void SetAllowLatencyExperiment(const GURL
& url
, bool enable
);
176 scoped_ptr
<base::Value
> SdchInfoToValue() const;
178 // Add an SDCH dictionary to our list of availible
179 // dictionaries. This addition will fail if addition is illegal
180 // (data in the dictionary is not acceptable from the
181 // dictionary_url; dictionary already added, etc.).
182 // If |server_hash| is non-null, returns the server hash that may be
183 // used as an argument to GetDictionarySetByHash.
184 // Returns SDCH_OK if the addition was successfull, and corresponding error
186 SdchProblemCode
AddSdchDictionary(const std::string
& dictionary_text
,
187 const GURL
& dictionary_url
,
188 std::string
* server_hash_p
);
190 // Remove an SDCH dictionary
191 SdchProblemCode
RemoveSdchDictionary(const std::string
& server_hash
);
193 // Registration for events generated by the SDCH subsystem.
194 void AddObserver(SdchObserver
* observer
);
195 void RemoveObserver(SdchObserver
* observer
);
197 static scoped_ptr
<DictionarySet
> CreateEmptyDictionarySetForTesting();
200 struct BlacklistInfo
{
201 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK
) {}
203 int count
; // # of times to refuse SDCH advertisement.
204 int exponential_count
; // Current exponential backoff ratchet.
205 SdchProblemCode reason
; // Why domain was blacklisted.
208 typedef std::map
<std::string
, BlacklistInfo
> DomainBlacklistInfo
;
209 typedef std::set
<std::string
> ExperimentSet
;
211 // Determines whether a "Get-Dictionary" header is legal (dictionary
212 // url has appropriate relationship to referrer url) in the SDCH
213 // protocol. Return SDCH_OK if fetch is legal.
214 SdchProblemCode
CanFetchDictionary(const GURL
& referring_url
,
215 const GURL
& dictionary_url
) const;
217 // Support SDCH compression, by advertising in headers.
218 static bool g_sdch_enabled_
;
220 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
221 static void UrlSafeBase64Encode(const std::string
& input
,
222 std::string
* output
);
224 DictionaryMap dictionaries_
;
226 // List domains where decode failures have required disabling sdch.
227 DomainBlacklistInfo blacklisted_domains_
;
229 // List of hostnames for which a latency experiment is allowed (because a
230 // round trip test has recently passed).
231 ExperimentSet allow_latency_experiment_
;
233 // Observers that want to be notified of SDCH events.
234 // Assert list is empty on destruction since if there is an observer
235 // that hasn't removed itself from the list, that observer probably
236 // has a reference to the SdchManager.
237 base::ObserverList
<SdchObserver
, true> observers_
;
239 base::ThreadChecker thread_checker_
;
241 DISALLOW_COPY_AND_ASSIGN(SdchManager
);
246 #endif // NET_BASE_SDCH_MANAGER_H_