| 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 // This file contains the SdchManager class and two nested classes | 5 // This file contains the SdchManager class and the DictionarySet |
| 6 // (Dictionary, DictionarySet). SdchManager::Dictionary contains all | 6 // nested class. The manager is responsible for storing all |
| 7 // of the information about an SDCH dictionary. The manager is | 7 // SdchDictionarys, and provides access to them through DictionarySet |
| 8 // responsible for storing those dictionaries, and provides access to | 8 // objects. A DictionarySet is an object whose lifetime is under the |
| 9 // them through DictionarySet objects. A DictionarySet is an object | 9 // control of the consumer. It is a reference to a set of |
| 10 // whose lifetime is under the control of the consumer. It is a | 10 // dictionaries, and guarantees that none of those dictionaries will |
| 11 // reference to a set of dictionaries, and guarantees that none of | 11 // be destroyed while the DictionarySet reference is alive. |
| 12 // those dictionaries will be destroyed while the DictionarySet | |
| 13 // reference is alive. | |
| 14 | 12 |
| 15 #ifndef NET_BASE_SDCH_MANAGER_H_ | 13 #ifndef NET_BASE_SDCH_MANAGER_H_ |
| 16 #define NET_BASE_SDCH_MANAGER_H_ | 14 #define NET_BASE_SDCH_MANAGER_H_ |
| 17 | 15 |
| 18 #include <map> | 16 #include <map> |
| 19 #include <set> | 17 #include <set> |
| 20 #include <string> | 18 #include <string> |
| 21 #include <vector> | 19 #include <vector> |
| 22 | 20 |
| 23 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
| 24 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 25 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
| 26 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
| 27 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
| 28 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
| 29 #include "base/time/time.h" | |
| 30 #include "net/base/net_export.h" | 27 #include "net/base/net_export.h" |
| 28 #include "net/base/sdch_dictionary.h" |
| 31 #include "net/base/sdch_problem_codes.h" | 29 #include "net/base/sdch_problem_codes.h" |
| 32 #include "url/gurl.h" | 30 |
| 31 class GURL; |
| 33 | 32 |
| 34 namespace base { | 33 namespace base { |
| 35 class Clock; | |
| 36 class Value; | 34 class Value; |
| 37 } | 35 } |
| 38 | 36 |
| 39 namespace net { | 37 namespace net { |
| 40 | 38 |
| 41 class SdchObserver; | 39 class SdchObserver; |
| 42 | 40 |
| 43 // Provides global database of differential decompression dictionaries for the | 41 // Provides global database of differential decompression dictionaries for the |
| 44 // SDCH filter (processes sdch enconded content). | 42 // SDCH filter (processes sdch enconded content). |
| 45 // | 43 // |
| 46 // The SdchManager maintains a collection of memory resident dictionaries. It | 44 // The SdchManager maintains a collection of memory resident dictionaries. It |
| 47 // can find a dictionary (based on a server specification of a hash), store a | 45 // can find a dictionary (based on a server specification of a hash), store a |
| 48 // dictionary, and make judgements about what URLs can use, set, etc. a | 46 // dictionary, and make judgements about what URLs can use, set, etc. a |
| 49 // dictionary. | 47 // dictionary. |
| 50 | 48 |
| 51 // These dictionaries are acquired over the net, and include a header | 49 // These dictionaries are acquired over the net, and include a header |
| 52 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF | 50 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF |
| 53 // module) to decompress data. | 51 // module) to decompress data. |
| 54 // | 52 // |
| 55 // A dictionary held by the manager may nonetheless outlive the manager if | 53 // A dictionary held by the manager may nonetheless outlive the manager if |
| 56 // a DictionarySet object refers to it; see below. | 54 // a DictionarySet object refers to it; see below. |
| 57 class NET_EXPORT SdchManager { | 55 class NET_EXPORT SdchManager { |
| 58 public: | 56 public: |
| 59 class Dictionary; | 57 typedef std::map<std::string, |
| 60 typedef std::map<std::string, scoped_refptr<base::RefCountedData<Dictionary>>> | 58 scoped_refptr<base::RefCountedData<SdchDictionary>>> |
| 61 DictionaryMap; | 59 DictionaryMap; |
| 62 | 60 |
| 63 class NET_EXPORT_PRIVATE Dictionary { | |
| 64 public: | |
| 65 // Construct a vc-diff usable dictionary from the dictionary_text starting | |
| 66 // at the given offset. The supplied client_hash should be used to | |
| 67 // advertise the dictionary's availability relative to the suppplied URL. | |
| 68 Dictionary(const std::string& dictionary_text, | |
| 69 size_t offset, | |
| 70 const std::string& client_hash, | |
| 71 const std::string& server_hash, | |
| 72 const GURL& url, | |
| 73 const std::string& domain, | |
| 74 const std::string& path, | |
| 75 const base::Time& expiration, | |
| 76 const std::set<int>& ports); | |
| 77 | |
| 78 ~Dictionary(); | |
| 79 | |
| 80 // Sdch filters can get our text to use in decoding compressed data. | |
| 81 const std::string& text() const { return text_; } | |
| 82 | |
| 83 const GURL& url() const { return url_; } | |
| 84 const std::string& client_hash() const { return client_hash_; } | |
| 85 const std::string& server_hash() const { return server_hash_; } | |
| 86 const std::string& domain() const { return domain_; } | |
| 87 const std::string& path() const { return path_; } | |
| 88 const base::Time& expiration() const { return expiration_; } | |
| 89 const std::set<int>& ports() const { return ports_; } | |
| 90 | |
| 91 // Security methods to check if we can establish a new dictionary with the | |
| 92 // given data, that arrived in response to get of dictionary_url. | |
| 93 static SdchProblemCode CanSet(const std::string& domain, | |
| 94 const std::string& path, | |
| 95 const std::set<int>& ports, | |
| 96 const GURL& dictionary_url); | |
| 97 | |
| 98 // Security method to check if we can use a dictionary to decompress a | |
| 99 // target that arrived with a reference to this dictionary. | |
| 100 SdchProblemCode CanUse(const GURL& referring_url) const; | |
| 101 | |
| 102 // Compare paths to see if they "match" for dictionary use. | |
| 103 static bool PathMatch(const std::string& path, | |
| 104 const std::string& restriction); | |
| 105 | |
| 106 // Compare domains to see if the "match" for dictionary use. | |
| 107 static bool DomainMatch(const GURL& url, const std::string& restriction); | |
| 108 | |
| 109 // Is this dictionary expired? | |
| 110 bool Expired() const; | |
| 111 | |
| 112 void SetClockForTesting(scoped_ptr<base::Clock> clock); | |
| 113 | |
| 114 private: | |
| 115 friend class base::RefCountedData<Dictionary>; | |
| 116 | |
| 117 // Private copy-constructor to support RefCountedData<>, which requires | |
| 118 // that an object stored in it be either DefaultConstructible or | |
| 119 // CopyConstructible | |
| 120 Dictionary(const Dictionary& rhs); | |
| 121 | |
| 122 // The actual text of the dictionary. | |
| 123 std::string text_; | |
| 124 | |
| 125 // Part of the hash of text_ that the client uses to advertise the fact that | |
| 126 // it has a specific dictionary pre-cached. | |
| 127 std::string client_hash_; | |
| 128 | |
| 129 // Part of the hash of text_ that the server uses to identify the | |
| 130 // dictionary it wants used for decoding. | |
| 131 std::string server_hash_; | |
| 132 | |
| 133 // The GURL that arrived with the text_ in a URL request to specify where | |
| 134 // this dictionary may be used. | |
| 135 const GURL url_; | |
| 136 | |
| 137 // Metadate "headers" in before dictionary text contained the following: | |
| 138 // Each dictionary payload consists of several headers, followed by the text | |
| 139 // of the dictionary. The following are the known headers. | |
| 140 const std::string domain_; | |
| 141 const std::string path_; | |
| 142 const base::Time expiration_; // Implied by max-age. | |
| 143 const std::set<int> ports_; | |
| 144 | |
| 145 scoped_ptr<base::Clock> clock_; | |
| 146 | |
| 147 void operator=(const Dictionary&) = delete; | |
| 148 }; | |
| 149 | |
| 150 // A handle for one or more dictionaries which will keep the dictionaries | 61 // A handle for one or more dictionaries which will keep the dictionaries |
| 151 // alive and accessible for the handle's lifetime. | 62 // alive and accessible for the handle's lifetime. |
| 152 class NET_EXPORT_PRIVATE DictionarySet { | 63 class NET_EXPORT_PRIVATE DictionarySet { |
| 153 public: | 64 public: |
| 154 ~DictionarySet(); | 65 ~DictionarySet(); |
| 155 | 66 |
| 156 // Return a comma separated list of client hashes. | 67 // Return a comma separated list of client hashes. |
| 157 std::string GetDictionaryClientHashList() const; | 68 std::string GetDictionaryClientHashList() const; |
| 158 | 69 |
| 159 // Lookup a given dictionary based on server hash. Returned pointer | 70 // Lookup a given dictionary based on server hash. Returned pointer |
| 160 // is guaranteed to be valid for the lifetime of the DictionarySet. | 71 // is guaranteed to be valid for the lifetime of the DictionarySet. |
| 161 // Returns NULL if hash is not a valid server hash for a dictionary | 72 // Returns NULL if hash is not a valid server hash for a dictionary |
| 162 // named by DictionarySet. | 73 // named by DictionarySet. |
| 163 const SdchManager::Dictionary* GetDictionary(const std::string& hash) const; | 74 const SdchDictionary* GetDictionary(const std::string& hash) const; |
| 164 | 75 |
| 165 bool Empty() const; | 76 bool Empty() const; |
| 166 | 77 |
| 167 private: | 78 private: |
| 168 // A DictionarySet may only be constructed by the SdchManager. | 79 // A DictionarySet may only be constructed by the SdchManager. |
| 169 friend class SdchManager; | 80 friend class SdchManager; |
| 170 | 81 |
| 171 DictionarySet(); | 82 DictionarySet(); |
| 172 void AddDictionary(const std::string& server_hash, | 83 void AddDictionary( |
| 173 const scoped_refptr<base::RefCountedData< | 84 const std::string& server_hash, |
| 174 SdchManager::Dictionary>>& dictionary); | 85 const scoped_refptr<base::RefCountedData<SdchDictionary>>& dictionary); |
| 175 | 86 |
| 176 DictionaryMap dictionaries_; | 87 DictionaryMap dictionaries_; |
| 177 | 88 |
| 178 DISALLOW_COPY_AND_ASSIGN(DictionarySet); | 89 DISALLOW_COPY_AND_ASSIGN(DictionarySet); |
| 179 }; | 90 }; |
| 180 | 91 |
| 181 SdchManager(); | 92 SdchManager(); |
| 182 ~SdchManager(); | 93 ~SdchManager(); |
| 183 | 94 |
| 184 // Clear data (for browser data removal). | 95 // Clear data (for browser data removal). |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 base::ThreadChecker thread_checker_; | 254 base::ThreadChecker thread_checker_; |
| 344 | 255 |
| 345 base::WeakPtrFactory<SdchManager> factory_; | 256 base::WeakPtrFactory<SdchManager> factory_; |
| 346 | 257 |
| 347 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 258 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
| 348 }; | 259 }; |
| 349 | 260 |
| 350 } // namespace net | 261 } // namespace net |
| 351 | 262 |
| 352 #endif // NET_BASE_SDCH_MANAGER_H_ | 263 #endif // NET_BASE_SDCH_MANAGER_H_ |
| OLD | NEW |