Chromium Code Reviews| Index: net/base/sdch_manager.h |
| diff --git a/net/base/sdch_manager.h b/net/base/sdch_manager.h |
| index ff157e52a32e1cb04e955f467b3c524cba11b6a7..37ca746a82476db2facbf07405afde97330c3a8f 100644 |
| --- a/net/base/sdch_manager.h |
| +++ b/net/base/sdch_manager.h |
| @@ -8,6 +8,7 @@ |
| #include <map> |
| #include <set> |
| #include <string> |
| +#include <vector> |
| #include "base/gtest_prod_util.h" |
| #include "base/memory/ref_counted.h" |
| @@ -35,10 +36,12 @@ class SdchObserver; |
| // module) to decompress data. |
| class NET_EXPORT SdchManager { |
| public: |
| + class DictionarySet; |
| + |
| // A list of errors that appeared and were either resolved, or used to turn |
| // off sdch encoding. |
| enum ProblemCodes { |
| - MIN_PROBLEM_CODE, |
| + OK, |
| // Content-encoding correction problems. |
| ADDED_CONTENT_ENCODING = 1, |
| @@ -89,11 +92,14 @@ class NET_EXPORT SdchManager { |
| // Failsafe hack. |
| ATTEMPT_TO_DECODE_NON_HTTP_DATA = 40, |
| - |
| // Content-Encoding problems detected, with no action taken. |
| MULTIENCODING_FOR_NON_SDCH_REQUEST = 50, |
| SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST = 51, |
| + // A dictionary that wasn't advertised is being used for decoding. |
| + UNADVERTISED_SDCH_DICTIONARY_USED = 52, |
| + UNADVERTISED_SDCH_DICTIONARY_USED_CACHED = 52, |
|
Bence
2014/11/12 21:46:26
Last value should be 53 instead of 52.
Randy Smith (Not in Mondays)
2014/11/13 22:11:02
Whoops; thanks for the catch.
|
| + |
| // Dictionary manager issues. |
| DOMAIN_BLACKLIST_INCLUDES_TARGET = 61, |
| @@ -142,16 +148,8 @@ class NET_EXPORT SdchManager { |
| // There is one instance of |Dictionary| for each memory-cached SDCH |
| // dictionary. |
| - class NET_EXPORT_PRIVATE Dictionary : public base::RefCounted<Dictionary> { |
| + class NET_EXPORT_PRIVATE Dictionary { |
| public: |
| - // Sdch filters can get our text to use in decoding compressed data. |
| - const std::string& text() const { return text_; } |
| - |
| - private: |
| - friend class base::RefCounted<Dictionary>; |
| - friend class SdchManager; // Only manager can construct an instance. |
| - FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); |
| - |
| // Construct a vc-diff usable dictionary from the dictionary_text starting |
| // at the given offset. The supplied client_hash should be used to |
| // advertise the dictionary's availability relative to the suppplied URL. |
| @@ -165,21 +163,21 @@ class NET_EXPORT SdchManager { |
| const std::set<int>& ports); |
| virtual ~Dictionary(); |
| + // Sdch filters can get our text to use in decoding compressed data. |
| + const std::string& text() const { return text_; } |
| + |
| const GURL& url() const { return url_; } |
| const std::string& client_hash() const { return client_hash_; } |
| - // Security method to check if we can advertise this dictionary for use |
| - // if the |target_url| returns SDCH compressed data. |
| - bool CanAdvertise(const GURL& target_url); |
| - |
| // Security methods to check if we can establish a new dictionary with the |
| // given data, that arrived in response to get of dictionary_url. |
| static bool CanSet(const std::string& domain, const std::string& path, |
| const std::set<int>& ports, const GURL& dictionary_url); |
| - // Security method to check if we can use a dictionary to decompress a |
| - // target that arrived with a reference to this dictionary. |
| - bool CanUse(const GURL& referring_url); |
| + // Security method to check if a dictionary is appropriate to use |
| + // with the given URL. Does not check expiration (it's appropriate to |
| + // use dictionaries that were expired while a request is outstanding). |
| + SdchManager::ProblemCodes CanUse(const GURL& target_url); |
| // Compare paths to see if they "match" for dictionary use. |
| static bool PathMatch(const std::string& path, |
| @@ -188,7 +186,10 @@ class NET_EXPORT SdchManager { |
| // Compare domains to see if the "match" for dictionary use. |
| static bool DomainMatch(const GURL& url, const std::string& restriction); |
| + // Is this dictionary expired? |
| + bool Expired() const; |
| + private: |
| // The actual text of the dictionary. |
| std::string text_; |
| @@ -211,6 +212,59 @@ class NET_EXPORT SdchManager { |
| DISALLOW_COPY_AND_ASSIGN(Dictionary); |
| }; |
| + // Implementation type relevant for the private data members of |
| + // DictionarySet and SdchManager. This class should not be used |
|
Bence
2014/11/12 21:46:26
Two spaces should be enough between two sentences.
Randy Smith (Not in Mondays)
2014/11/13 22:11:02
Actually, Ryan's badgered me down to 1 :-}. Not s
|
| + // outside of sdch_manager.*. |
| + class DictionaryWrapper : public base::RefCounted<DictionaryWrapper> { |
| + public: |
| + typedef std::map<std::string, scoped_refptr<DictionaryWrapper> > |
| + DictionaryMap; |
| + |
| + explicit DictionaryWrapper(scoped_ptr<Dictionary> dictionary); |
| + Dictionary* dictionary() { return dictionary_.get(); } |
| + |
| + private: |
| + friend class base::RefCounted<DictionaryWrapper>; |
| + ~DictionaryWrapper(); |
| + |
| + scoped_ptr<SdchManager::Dictionary> dictionary_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DictionaryWrapper); |
| + }; |
| + |
| + // A handle for one or more dictionaries which will keep the dictionaries |
| + // alive and accessible for the handle's lifetime. |
| + class DictionarySet { |
| + public: |
| + ~DictionarySet(); |
| + |
| + // Append a comma separated list of client hashes onto the end |
| + // of |client_hashes|. |
| + void GetDictionaryClientHashList(std::string* client_hashes) const; |
| + |
| + // Lookup a given dictionary based on server hash. Returned pointer |
| + // is guaranteed to be valid for the lifetime of the DictionarySet. |
| + // Returns NULL if hash is not a valid server hash for a dictionary |
| + // named by DictionarySet. |
| + const SdchManager::Dictionary* Dictionary(const std::string& hash) const; |
| + |
| + bool Empty() const; |
| + |
| + private: |
| + // A DictionarySet may only be constructed by the SdchManager. |
| + friend class SdchManager; |
| + |
| + static const char* kSdchDictionariesHandleDataKey; |
| + |
| + DictionarySet(); |
| + void AddDictionary(const std::string& server_hash, |
| + scoped_refptr<DictionaryWrapper> dictionary); |
| + |
| + DictionaryWrapper::DictionaryMap dictionaries_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DictionarySet); |
| + }; |
| + |
| SdchManager(); |
| ~SdchManager(); |
| @@ -266,20 +320,19 @@ class NET_EXPORT SdchManager { |
| // header has been seen. |
| void OnGetDictionary(const GURL& request_url, const GURL& dictionary_url); |
| - // Find the vcdiff dictionary (the body of the sdch dictionary that appears |
| - // after the meta-data headers like Domain:...) with the given |server_hash| |
| - // to use to decompreses data that arrived as SDCH encoded content. Check to |
| - // be sure the returned |dictionary| can be used for decoding content supplied |
| - // in response to a request for |referring_url|. |
| - // Return null in |dictionary| if there is no matching legal dictionary. |
| - void GetVcdiffDictionary(const std::string& server_hash, |
| - const GURL& referring_url, |
| - scoped_refptr<Dictionary>* dictionary); |
| - |
| - // Get list of available (pre-cached) dictionaries that we have already loaded |
| - // into memory. The list is a comma separated list of (client) hashes per |
| - // the SDCH spec. |
| - void GetAvailDictionaryList(const GURL& target_url, std::string* list); |
| + // Get a handle to the available dictionaries that might be used |
| + // for encoding responses for the given URL. The return set will not |
| + // include expired dictionaries. If no dictionaries |
| + // are appropriate to use with the target_url, NULL is returned. |
| + scoped_ptr<DictionarySet> GetDictionarySet(const GURL& target_url); |
| + |
| + // Get a handle to a specific dictionary, by its server hash, confirming |
| + // that that specific dictionary is appropriate to use with |target_url|. |
| + // Expired dictionaries will be returned. If no dictionary with that |
| + // hash exists that is usable with |target_url|, NULL is returned. |
| + scoped_ptr<DictionarySet> GetDictionarySetByHash( |
| + const GURL& target_url, |
| + const std::string& server_hash); |
| // Construct the pair of hashes for client and server to identify an SDCH |
| // dictionary. This is only made public to facilitate unit testing, but is |
| @@ -306,18 +359,21 @@ class NET_EXPORT SdchManager { |
| void AddObserver(SdchObserver* observer); |
| void RemoveObserver(SdchObserver* observer); |
| + static scoped_ptr<DictionarySet> CreateNullDictionarySetForTesting(); |
| + |
| private: |
| struct BlacklistInfo { |
| BlacklistInfo() |
| : count(0), |
| exponential_count(0), |
| - reason(MIN_PROBLEM_CODE) {} |
| + reason(OK) {} |
| int count; // # of times to refuse SDCH advertisement. |
| int exponential_count; // Current exponential backoff ratchet. |
| ProblemCodes reason; // Why domain was blacklisted. |
| }; |
| + |
| typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; |
| typedef std::set<std::string> ExperimentSet; |
| @@ -327,9 +383,6 @@ class NET_EXPORT SdchManager { |
| bool CanFetchDictionary(const GURL& referring_url, |
| const GURL& dictionary_url) const; |
| - // A map of dictionaries info indexed by the hash that the server provides. |
| - typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; |
| - |
| // Support SDCH compression, by advertising in headers. |
| static bool g_sdch_enabled_; |
| @@ -340,7 +393,8 @@ class NET_EXPORT SdchManager { |
| // A simple implementation of a RFC 3548 "URL safe" base64 encoder. |
| static void UrlSafeBase64Encode(const std::string& input, |
| std::string* output); |
| - DictionaryMap dictionaries_; |
| + |
| + DictionaryWrapper::DictionaryMap dictionaries_; |
| // List domains where decode failures have required disabling sdch. |
| DomainBlacklistInfo blacklisted_domains_; |