Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: net/base/sdch_manager.h

Issue 711753003: Pin dictionaries from being deleted while request is outstanding. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporated comments. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/base/sdch_manager.cc » ('j') | net/base/sdch_manager.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
Ryan Sleevi 2014/11/20 22:50:29 So this is probably where a file-level comment exp
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 Done. (Side note: DictionaryWrapper doesn't exist
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;
Ryan Sleevi 2014/11/20 22:50:29 You don't actually need to forward declare this he
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 Done.
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_;
Ryan Sleevi 2014/11/20 22:50:30 Do you want each dictionary to carry a Clock? Shou
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 If there's a contract to that effect, I need to re
125
126 // Copy and assignment of this class are forbidden for any public
127 // users, but the class implements a private copy constructor
128 // (declared above) for use by RefCountedData<>. That implementation
129 // means that use of the DISALLOW_COPY_AND_ASSIGN() macro will break.
130 // The declaration below is the assignment disallow copied from that
131 // macro.
Ryan Sleevi 2014/11/20 22:50:29 Rather than this longer comment, which describes t
Randy Smith (Not in Mondays) 2014/11/20 23:44:29 Done. Having said that, I think it's a little con
132 void operator=(const Dictionary&) = delete;
133 };
134
135 // Implementation type relevant for the private data members of
136 // DictionarySet and SdchManager. This class should not be used
137 // outside of sdch_manager.*.
138 class DictionaryWrapper : public base::RefCounted<DictionaryWrapper> {
139 public:
140 typedef std::map<std::string, scoped_refptr<DictionaryWrapper> >
141 DictionaryMap;
142
143 explicit DictionaryWrapper(scoped_ptr<Dictionary> dictionary);
144 Dictionary* dictionary() { return dictionary_.get(); }
145
146 private:
147 friend class base::RefCounted<DictionaryWrapper>;
148 ~DictionaryWrapper();
149
150 scoped_ptr<SdchManager::Dictionary> dictionary_;
151
152 DISALLOW_COPY_AND_ASSIGN(DictionaryWrapper);
153 };
154 typedef std::map<std::string, scoped_refptr<base::RefCountedData<Dictionary>>>
Ryan Sleevi 2014/11/20 22:50:29 typedefs go up top - http://google-styleguide.goog
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 Done.
155 DictionaryMap;
156
157 // A handle for one or more dictionaries which will keep the dictionaries
158 // alive and accessible for the handle's lifetime.
159 class NET_EXPORT_PRIVATE DictionarySet {
160 public:
161 ~DictionarySet();
162
163 // Return a comma separated list of client hashes.
164 std::string GetDictionaryClientHashList() const;
165
166 // Lookup a given dictionary based on server hash. Returned pointer
167 // is guaranteed to be valid for the lifetime of the DictionarySet.
168 // Returns NULL if hash is not a valid server hash for a dictionary
169 // named by DictionarySet.
170 const SdchManager::Dictionary* Dictionary(const std::string& hash) const;
Ryan Sleevi 2014/11/20 22:50:29 nit: Naming - FindDictionary, LookupDictionary, Ge
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 *nod* Changed to GetDictionary().
171
172 bool Empty() const;
173
174 private:
175 // A DictionarySet may only be constructed by the SdchManager.
176 friend class SdchManager;
177
178 DictionarySet();
179 void AddDictionary(const std::string& server_hash,
180 scoped_refptr<base::RefCountedData<
181 SdchManager::Dictionary>> dictionary);
Ryan Sleevi 2014/11/20 22:50:29 const scoped_refptr<>& only scoped_ptr<>'s get th
Randy Smith (Not in Mondays) 2014/11/20 23:44:28 Done.
182
183 DictionaryMap dictionaries_;
184
185 DISALLOW_COPY_AND_ASSIGN(DictionarySet);
122 }; 186 };
123 187
124 SdchManager(); 188 SdchManager();
125 ~SdchManager(); 189 ~SdchManager();
126 190
127 // Clear data (for browser data removal). 191 // Clear data (for browser data removal).
128 void ClearData(); 192 void ClearData();
129 193
130 // Record stats on various errors. 194 // Record stats on various errors.
131 static void SdchErrorRecovery(SdchProblemCode problem); 195 static void SdchErrorRecovery(SdchProblemCode problem);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // supported domain (i.e., not blacklisted, and either the specific supported 235 // supported domain (i.e., not blacklisted, and either the specific supported
172 // domain, or all domains were assumed supported). If it is blacklist, reduce 236 // 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. 237 // by 1 the number of times it will be reported as blacklisted.
174 SdchProblemCode IsInSupportedDomain(const GURL& url); 238 SdchProblemCode IsInSupportedDomain(const GURL& url);
175 239
176 // Send out appropriate events notifying observers that a Get-Dictionary 240 // Send out appropriate events notifying observers that a Get-Dictionary
177 // header has been seen. 241 // header has been seen.
178 SdchProblemCode OnGetDictionary(const GURL& request_url, 242 SdchProblemCode OnGetDictionary(const GURL& request_url,
179 const GURL& dictionary_url); 243 const GURL& dictionary_url);
180 244
181 // Find the vcdiff dictionary (the body of the sdch dictionary that appears 245 // Get a handle to the available dictionaries that might be used
182 // after the meta-data headers like Domain:...) with the given |server_hash| 246 // 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 247 // include expired dictionaries. If no dictionaries
184 // be sure the returned |dictionary| can be used for decoding content supplied 248 // are appropriate to use with the target_url, NULL is returned.
185 // in response to a request for |referring_url|. 249 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 250
194 // Get list of available (pre-cached) dictionaries that we have already loaded 251 // 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 252 // that that specific dictionary is appropriate to use with |target_url|.
196 // the SDCH spec. 253 // Expired dictionaries will be returned. If no dictionary with that
197 void GetAvailDictionaryList(const GURL& target_url, std::string* list); 254 // hash exists that is usable with |target_url|, NULL is returned.
255 // If there is a usability problem, |*error_code| is set to the
256 // appropriate problem code.
257 scoped_ptr<DictionarySet> GetDictionarySetByHash(
258 const GURL& target_url,
259 const std::string& server_hash,
260 SdchProblemCode* problem_code);
198 261
199 // Construct the pair of hashes for client and server to identify an SDCH 262 // 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 263 // dictionary. This is only made public to facilitate unit testing, but is
201 // otherwise private 264 // otherwise private
202 static void GenerateHash(const std::string& dictionary_text, 265 static void GenerateHash(const std::string& dictionary_text,
203 std::string* client_hash, std::string* server_hash); 266 std::string* client_hash, std::string* server_hash);
204 267
205 // For Latency testing only, we need to know if we've succeeded in doing a 268 // 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 269 // 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 270 // problems with SDCH, we opt-out of the test unless/until we perform a
(...skipping 10 matching lines...) Expand all
218 // dictionary_url; dictionary already added, etc.). 281 // dictionary_url; dictionary already added, etc.).
219 // Returns SDCH_OK if the addition was successfull, and corresponding error 282 // Returns SDCH_OK if the addition was successfull, and corresponding error
220 // code otherwise. 283 // code otherwise.
221 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text, 284 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text,
222 const GURL& dictionary_url); 285 const GURL& dictionary_url);
223 286
224 // Registration for events generated by the SDCH subsystem. 287 // Registration for events generated by the SDCH subsystem.
225 void AddObserver(SdchObserver* observer); 288 void AddObserver(SdchObserver* observer);
226 void RemoveObserver(SdchObserver* observer); 289 void RemoveObserver(SdchObserver* observer);
227 290
291 static scoped_ptr<DictionarySet> CreateEmptyDictionarySetForTesting();
292
228 private: 293 private:
229 struct BlacklistInfo { 294 struct BlacklistInfo {
230 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {} 295 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {}
231 296
232 int count; // # of times to refuse SDCH advertisement. 297 int count; // # of times to refuse SDCH advertisement.
233 int exponential_count; // Current exponential backoff ratchet. 298 int exponential_count; // Current exponential backoff ratchet.
234 SdchProblemCode reason; // Why domain was blacklisted. 299 SdchProblemCode reason; // Why domain was blacklisted.
235 }; 300 };
301
236 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; 302 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo;
237 typedef std::set<std::string> ExperimentSet; 303 typedef std::set<std::string> ExperimentSet;
238 304
239 // Determines whether a "Get-Dictionary" header is legal (dictionary 305 // Determines whether a "Get-Dictionary" header is legal (dictionary
240 // url has appropriate relationship to referrer url) in the SDCH 306 // url has appropriate relationship to referrer url) in the SDCH
241 // protocol. Return SDCH_OK if fetch is legal. 307 // protocol. Return SDCH_OK if fetch is legal.
242 SdchProblemCode CanFetchDictionary(const GURL& referring_url, 308 SdchProblemCode CanFetchDictionary(const GURL& referring_url,
243 const GURL& dictionary_url) const; 309 const GURL& dictionary_url) const;
244 310
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. 311 // Support SDCH compression, by advertising in headers.
249 static bool g_sdch_enabled_; 312 static bool g_sdch_enabled_;
250 313
251 // Support SDCH compression for HTTPS requests and responses. When supported, 314 // Support SDCH compression for HTTPS requests and responses. When supported,
252 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. 315 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS.
253 static bool g_secure_scheme_supported_; 316 static bool g_secure_scheme_supported_;
254 317
255 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. 318 // A simple implementation of a RFC 3548 "URL safe" base64 encoder.
256 static void UrlSafeBase64Encode(const std::string& input, 319 static void UrlSafeBase64Encode(const std::string& input,
257 std::string* output); 320 std::string* output);
(...skipping 14 matching lines...) Expand all
272 ObserverList<SdchObserver, true> observers_; 335 ObserverList<SdchObserver, true> observers_;
273 336
274 base::ThreadChecker thread_checker_; 337 base::ThreadChecker thread_checker_;
275 338
276 DISALLOW_COPY_AND_ASSIGN(SdchManager); 339 DISALLOW_COPY_AND_ASSIGN(SdchManager);
277 }; 340 };
278 341
279 } // namespace net 342 } // namespace net
280 343
281 #endif // NET_BASE_SDCH_MANAGER_H_ 344 #endif // NET_BASE_SDCH_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | net/base/sdch_manager.cc » ('j') | net/base/sdch_manager.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698