OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_SDCH_MANAGER_H_ | 5 #ifndef NET_BASE_SDCH_MANAGER_H_ |
6 #define NET_BASE_SDCH_MANAGER_H_ | 6 #define NET_BASE_SDCH_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | |
11 | 12 |
12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
19 #include "net/base/sdch_problem_codes.h" | 20 #include "net/base/sdch_problem_codes.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
24 class Clock; | |
23 class Value; | 25 class Value; |
24 } | 26 } |
25 | 27 |
26 namespace net { | 28 namespace net { |
27 | 29 |
28 class SdchObserver; | 30 class SdchObserver; |
29 | 31 |
30 // Provides global database of differential decompression dictionaries for the | 32 // Provides global database of differential decompression dictionaries for the |
31 // SDCH filter (processes sdch enconded content). | 33 // SDCH filter (processes sdch enconded content). |
32 // | 34 // |
33 // The SdchManager maintains a collection of memory resident dictionaries. It | 35 // 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 | 36 // 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 | 37 // dictionary, and make judgements about what URLs can use, set, etc. a |
36 // dictionary. | 38 // dictionary. |
37 | 39 |
38 // These dictionaries are acquired over the net, and include a header | 40 // 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 | 41 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF |
40 // module) to decompress data. | 42 // module) to decompress data. |
41 class NET_EXPORT SdchManager { | 43 class NET_EXPORT SdchManager { |
42 public: | 44 public: |
45 class DictionarySet; | |
46 | |
43 // Use the following static limits to block DOS attacks until we implement | 47 // Use the following static limits to block DOS attacks until we implement |
44 // a cached dictionary evicition strategy. | 48 // a cached dictionary evicition strategy. |
45 static const size_t kMaxDictionarySize; | 49 static const size_t kMaxDictionarySize; |
46 static const size_t kMaxDictionaryCount; | 50 static const size_t kMaxDictionaryCount; |
47 | 51 |
48 // There is one instance of |Dictionary| for each memory-cached SDCH | 52 class NET_EXPORT_PRIVATE Dictionary { |
49 // dictionary. | |
50 class NET_EXPORT_PRIVATE Dictionary : public base::RefCounted<Dictionary> { | |
51 public: | 53 public: |
52 // Sdch filters can get our text to use in decoding compressed data. | |
53 const std::string& text() const { return text_; } | |
54 | |
55 private: | |
56 friend class base::RefCounted<Dictionary>; | |
57 friend class SdchManager; // Only manager can construct an instance. | |
58 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); | |
59 | |
60 // Construct a vc-diff usable dictionary from the dictionary_text starting | 54 // Construct a vc-diff usable dictionary from the dictionary_text starting |
61 // at the given offset. The supplied client_hash should be used to | 55 // at the given offset. The supplied client_hash should be used to |
62 // advertise the dictionary's availability relative to the suppplied URL. | 56 // advertise the dictionary's availability relative to the suppplied URL. |
63 Dictionary(const std::string& dictionary_text, | 57 Dictionary(const std::string& dictionary_text, |
64 size_t offset, | 58 size_t offset, |
65 const std::string& client_hash, | 59 const std::string& client_hash, |
66 const GURL& url, | 60 const GURL& url, |
67 const std::string& domain, | 61 const std::string& domain, |
68 const std::string& path, | 62 const std::string& path, |
69 const base::Time& expiration, | 63 const base::Time& expiration, |
70 const std::set<int>& ports); | 64 const std::set<int>& ports); |
71 virtual ~Dictionary(); | 65 |
66 ~Dictionary(); | |
67 | |
68 // Sdch filters can get our text to use in decoding compressed data. | |
69 const std::string& text() const { return text_; } | |
72 | 70 |
73 const GURL& url() const { return url_; } | 71 const GURL& url() const { return url_; } |
74 const std::string& client_hash() const { return client_hash_; } | 72 const std::string& client_hash() const { return client_hash_; } |
75 const std::string& domain() const { return domain_; } | 73 const std::string& domain() const { return domain_; } |
76 const std::string& path() const { return path_; } | 74 const std::string& path() const { return path_; } |
77 const base::Time& expiration() const { return expiration_; } | 75 const base::Time& expiration() const { return expiration_; } |
78 const std::set<int>& ports() const { return ports_; } | 76 const std::set<int>& ports() const { return ports_; } |
79 | 77 |
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; | |
83 | |
84 // Security methods to check if we can establish a new dictionary with the | 78 // 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. | 79 // given data, that arrived in response to get of dictionary_url. |
86 static SdchProblemCode CanSet(const std::string& domain, | 80 static SdchProblemCode CanSet(const std::string& domain, |
87 const std::string& path, | 81 const std::string& path, |
88 const std::set<int>& ports, | 82 const std::set<int>& ports, |
89 const GURL& dictionary_url); | 83 const GURL& dictionary_url); |
90 | 84 |
91 // Security method to check if we can use a dictionary to decompress a | 85 // Security method to check if we can use a dictionary to decompress a |
92 // target that arrived with a reference to this dictionary. | 86 // target that arrived with a reference to this dictionary. |
93 SdchProblemCode CanUse(const GURL& referring_url) const; | 87 SdchProblemCode CanUse(const GURL& referring_url) const; |
94 | 88 |
95 // Compare paths to see if they "match" for dictionary use. | 89 // Compare paths to see if they "match" for dictionary use. |
96 static bool PathMatch(const std::string& path, | 90 static bool PathMatch(const std::string& path, |
97 const std::string& restriction); | 91 const std::string& restriction); |
98 | 92 |
99 // Compare domains to see if the "match" for dictionary use. | 93 // Compare domains to see if the "match" for dictionary use. |
100 static bool DomainMatch(const GURL& url, const std::string& restriction); | 94 static bool DomainMatch(const GURL& url, const std::string& restriction); |
101 | 95 |
96 // Is this dictionary expired? | |
97 bool Expired() const; | |
98 | |
99 void SetClockForTesting(scoped_ptr<base::Clock> clock); | |
100 | |
101 private: | |
102 friend class base::RefCountedData<Dictionary>; | |
103 Dictionary(const Dictionary& rhs); | |
104 | |
102 // The actual text of the dictionary. | 105 // The actual text of the dictionary. |
103 std::string text_; | 106 std::string text_; |
104 | 107 |
105 // Part of the hash of text_ that the client uses to advertise the fact that | 108 // Part of the hash of text_ that the client uses to advertise the fact that |
106 // it has a specific dictionary pre-cached. | 109 // it has a specific dictionary pre-cached. |
107 std::string client_hash_; | 110 std::string client_hash_; |
108 | 111 |
109 // The GURL that arrived with the text_ in a URL request to specify where | 112 // The GURL that arrived with the text_ in a URL request to specify where |
110 // this dictionary may be used. | 113 // this dictionary may be used. |
111 const GURL url_; | 114 const GURL url_; |
112 | 115 |
113 // Metadate "headers" in before dictionary text contained the following: | 116 // Metadate "headers" in before dictionary text contained the following: |
114 // Each dictionary payload consists of several headers, followed by the text | 117 // Each dictionary payload consists of several headers, followed by the text |
115 // of the dictionary. The following are the known headers. | 118 // of the dictionary. The following are the known headers. |
116 const std::string domain_; | 119 const std::string domain_; |
117 const std::string path_; | 120 const std::string path_; |
118 const base::Time expiration_; // Implied by max-age. | 121 const base::Time expiration_; // Implied by max-age. |
119 const std::set<int> ports_; | 122 const std::set<int> ports_; |
120 | 123 |
121 DISALLOW_COPY_AND_ASSIGN(Dictionary); | 124 scoped_ptr<base::Clock> clock_; |
125 | |
126 // DISALLOW_COPY_AND_ASSIGN(Dictionary) by hand since the copy constructor | |
Bence
2014/11/17 20:38:04
Well, then you don't disallow COPY after all, only
Bence
2014/11/17 20:38:04
For clarity, consider writing
Dictionary(const Dic
Randy Smith (Not in Mondays)
2014/11/18 22:22:42
I don't think that that will work with the scoped_
Randy Smith (Not in Mondays)
2014/11/18 22:22:42
So as far as any public user of this class is conc
Bence
2014/11/19 14:26:50
Hm okay. I wouldn't have guessed that it would no
Bence
2014/11/19 14:26:50
Of course, these are private members, you're right
Randy Smith (Not in Mondays)
2014/11/19 14:45:05
The issue is the scoped_ptr, which, when you assig
Bence
2014/11/19 15:40:32
Sorry, I didn't realize that you are actually defi
| |
127 // is needed by RefCountedData<>. | |
128 void operator=(const Dictionary&); | |
Bence
2014/11/17 20:38:04
Consider writing
void operator=(const Dictionary&)
Randy Smith (Not in Mondays)
2014/11/18 22:22:42
Nice. Thank you. Done.
| |
129 }; | |
130 | |
131 // Implementation type relevant for the private data members of | |
132 // DictionarySet and SdchManager. This class should not be used | |
133 // outside of sdch_manager.*. | |
134 class DictionaryWrapper : public base::RefCounted<DictionaryWrapper> { | |
135 public: | |
136 typedef std::map<std::string, scoped_refptr<DictionaryWrapper> > | |
137 DictionaryMap; | |
138 | |
139 explicit DictionaryWrapper(scoped_ptr<Dictionary> dictionary); | |
140 Dictionary* dictionary() { return dictionary_.get(); } | |
141 | |
142 private: | |
143 friend class base::RefCounted<DictionaryWrapper>; | |
144 ~DictionaryWrapper(); | |
145 | |
146 scoped_ptr<SdchManager::Dictionary> dictionary_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(DictionaryWrapper); | |
149 }; | |
150 typedef std::map<std::string, scoped_refptr<base::RefCountedData<Dictionary>>> | |
151 DictionaryMap; | |
152 | |
153 // A handle for one or more dictionaries which will keep the dictionaries | |
154 // alive and accessible for the handle's lifetime. | |
155 class NET_EXPORT_PRIVATE DictionarySet { | |
156 public: | |
157 ~DictionarySet(); | |
158 | |
159 // Return a comma separated list of client hashes. | |
160 std::string GetDictionaryClientHashList() const; | |
161 | |
162 // Lookup a given dictionary based on server hash. Returned pointer | |
163 // is guaranteed to be valid for the lifetime of the DictionarySet. | |
164 // Returns NULL if hash is not a valid server hash for a dictionary | |
165 // named by DictionarySet. | |
166 const SdchManager::Dictionary* Dictionary(const std::string& hash) const; | |
167 | |
168 bool Empty() const; | |
169 | |
170 private: | |
171 // A DictionarySet may only be constructed by the SdchManager. | |
172 friend class SdchManager; | |
173 | |
174 DictionarySet(); | |
175 void AddDictionary(const std::string& server_hash, | |
176 scoped_refptr<base::RefCountedData< | |
177 SdchManager::Dictionary>> dictionary); | |
178 | |
179 DictionaryMap dictionaries_; | |
180 | |
181 DISALLOW_COPY_AND_ASSIGN(DictionarySet); | |
122 }; | 182 }; |
123 | 183 |
124 SdchManager(); | 184 SdchManager(); |
125 ~SdchManager(); | 185 ~SdchManager(); |
126 | 186 |
127 // Clear data (for browser data removal). | 187 // Clear data (for browser data removal). |
128 void ClearData(); | 188 void ClearData(); |
129 | 189 |
130 // Record stats on various errors. | 190 // Record stats on various errors. |
131 static void SdchErrorRecovery(SdchProblemCode problem); | 191 static void SdchErrorRecovery(SdchProblemCode problem); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 // supported domain (i.e., not blacklisted, and either the specific supported | 231 // supported domain (i.e., not blacklisted, and either the specific supported |
172 // domain, or all domains were assumed supported). If it is blacklist, reduce | 232 // 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. | 233 // by 1 the number of times it will be reported as blacklisted. |
174 SdchProblemCode IsInSupportedDomain(const GURL& url); | 234 SdchProblemCode IsInSupportedDomain(const GURL& url); |
175 | 235 |
176 // Send out appropriate events notifying observers that a Get-Dictionary | 236 // Send out appropriate events notifying observers that a Get-Dictionary |
177 // header has been seen. | 237 // header has been seen. |
178 SdchProblemCode OnGetDictionary(const GURL& request_url, | 238 SdchProblemCode OnGetDictionary(const GURL& request_url, |
179 const GURL& dictionary_url); | 239 const GURL& dictionary_url); |
180 | 240 |
181 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 241 // Get a handle to the available dictionaries that might be used |
182 // after the meta-data headers like Domain:...) with the given |server_hash| | 242 // for encoding responses for the given URL. The return set will not |
183 // to use to decompreses data that arrived as SDCH encoded content. Check to | 243 // include expired dictionaries. If no dictionaries |
184 // be sure the returned |dictionary| can be used for decoding content supplied | 244 // are appropriate to use with the target_url, NULL is returned. |
185 // in response to a request for |referring_url|. | 245 scoped_ptr<DictionarySet> GetDictionarySet(const GURL& target_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); | |
193 | 246 |
194 // Get list of available (pre-cached) dictionaries that we have already loaded | 247 // Get a handle to a specific dictionary, by its server hash, confirming |
195 // into memory. The list is a comma separated list of (client) hashes per | 248 // that that specific dictionary is appropriate to use with |target_url|. |
196 // the SDCH spec. | 249 // Expired dictionaries will be returned. If no dictionary with that |
197 void GetAvailDictionaryList(const GURL& target_url, std::string* list); | 250 // hash exists that is usable with |target_url|, NULL is returned. |
251 // If there is a usability problem, |*error_code| is set to the | |
252 // appropriate problem code. | |
253 scoped_ptr<DictionarySet> GetDictionarySetByHash( | |
254 const GURL& target_url, | |
255 const std::string& server_hash, | |
256 SdchProblemCode* problem_code); | |
198 | 257 |
199 // Construct the pair of hashes for client and server to identify an SDCH | 258 // 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 | 259 // dictionary. This is only made public to facilitate unit testing, but is |
201 // otherwise private | 260 // otherwise private |
202 static void GenerateHash(const std::string& dictionary_text, | 261 static void GenerateHash(const std::string& dictionary_text, |
203 std::string* client_hash, std::string* server_hash); | 262 std::string* client_hash, std::string* server_hash); |
204 | 263 |
205 // For Latency testing only, we need to know if we've succeeded in doing a | 264 // 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 | 265 // 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 | 266 // problems with SDCH, we opt-out of the test unless/until we perform a |
(...skipping 10 matching lines...) Expand all Loading... | |
218 // dictionary_url; dictionary already added, etc.). | 277 // dictionary_url; dictionary already added, etc.). |
219 // Returns SDCH_OK if the addition was successfull, and corresponding error | 278 // Returns SDCH_OK if the addition was successfull, and corresponding error |
220 // code otherwise. | 279 // code otherwise. |
221 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text, | 280 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text, |
222 const GURL& dictionary_url); | 281 const GURL& dictionary_url); |
223 | 282 |
224 // Registration for events generated by the SDCH subsystem. | 283 // Registration for events generated by the SDCH subsystem. |
225 void AddObserver(SdchObserver* observer); | 284 void AddObserver(SdchObserver* observer); |
226 void RemoveObserver(SdchObserver* observer); | 285 void RemoveObserver(SdchObserver* observer); |
227 | 286 |
287 static scoped_ptr<DictionarySet> CreateEmptyDictionarySetForTesting(); | |
288 | |
228 private: | 289 private: |
229 struct BlacklistInfo { | 290 struct BlacklistInfo { |
230 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {} | 291 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {} |
231 | 292 |
232 int count; // # of times to refuse SDCH advertisement. | 293 int count; // # of times to refuse SDCH advertisement. |
233 int exponential_count; // Current exponential backoff ratchet. | 294 int exponential_count; // Current exponential backoff ratchet. |
234 SdchProblemCode reason; // Why domain was blacklisted. | 295 SdchProblemCode reason; // Why domain was blacklisted. |
235 }; | 296 }; |
297 | |
236 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; | 298 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; |
237 typedef std::set<std::string> ExperimentSet; | 299 typedef std::set<std::string> ExperimentSet; |
238 | 300 |
239 // Determines whether a "Get-Dictionary" header is legal (dictionary | 301 // Determines whether a "Get-Dictionary" header is legal (dictionary |
240 // url has appropriate relationship to referrer url) in the SDCH | 302 // url has appropriate relationship to referrer url) in the SDCH |
241 // protocol. Return SDCH_OK if fetch is legal. | 303 // protocol. Return SDCH_OK if fetch is legal. |
242 SdchProblemCode CanFetchDictionary(const GURL& referring_url, | 304 SdchProblemCode CanFetchDictionary(const GURL& referring_url, |
243 const GURL& dictionary_url) const; | 305 const GURL& dictionary_url) const; |
244 | 306 |
245 // A map of dictionaries info indexed by the hash that the server provides. | |
246 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; | |
247 | |
248 // Support SDCH compression, by advertising in headers. | 307 // Support SDCH compression, by advertising in headers. |
249 static bool g_sdch_enabled_; | 308 static bool g_sdch_enabled_; |
250 | 309 |
251 // Support SDCH compression for HTTPS requests and responses. When supported, | 310 // Support SDCH compression for HTTPS requests and responses. When supported, |
252 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. | 311 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. |
253 static bool g_secure_scheme_supported_; | 312 static bool g_secure_scheme_supported_; |
254 | 313 |
255 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. | 314 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. |
256 static void UrlSafeBase64Encode(const std::string& input, | 315 static void UrlSafeBase64Encode(const std::string& input, |
257 std::string* output); | 316 std::string* output); |
(...skipping 14 matching lines...) Expand all Loading... | |
272 ObserverList<SdchObserver, true> observers_; | 331 ObserverList<SdchObserver, true> observers_; |
273 | 332 |
274 base::ThreadChecker thread_checker_; | 333 base::ThreadChecker thread_checker_; |
275 | 334 |
276 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 335 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
277 }; | 336 }; |
278 | 337 |
279 } // namespace net | 338 } // namespace net |
280 | 339 |
281 #endif // NET_BASE_SDCH_MANAGER_H_ | 340 #endif // NET_BASE_SDCH_MANAGER_H_ |
OLD | NEW |