Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #ifndef NET_DNS_MDNS_CACHE_H_ | |
| 6 #define NET_DNS_MDNS_CACHE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/time.h" | |
| 15 #include "net/base/net_export.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 class ParsedDnsRecord; | |
| 20 class RecordParsed; | |
| 21 | |
| 22 // MDNS CACHE | |
| 23 // This is a cache of mDNS records. It keeps track of expiration times and is | |
| 24 // guaranteed not to return expired records. It also has facilities for timely | |
| 25 // record expiration. | |
| 26 | |
| 27 class NET_EXPORT_PRIVATE MDnsCache { | |
| 28 public: | |
| 29 typedef base::Callback<void(const RecordParsed*)> RecordRemovedCallback; | |
| 30 | |
| 31 enum UpdateType { | |
| 32 RecordAdded, | |
| 33 RecordChanged, | |
| 34 NoChange | |
| 35 }; | |
| 36 | |
| 37 MDnsCache(); | |
| 38 ~MDnsCache(); | |
| 39 | |
| 40 // Return value indicates whether the record was added, changed | |
| 41 // (existed previously with different value) or not changed (existed | |
| 42 // previously with same value). | |
| 43 UpdateType UpdateDnsRecord(scoped_ptr<const RecordParsed> record); | |
| 44 | |
| 45 // Return records with type |type| and name |name|. Expired records will not | |
| 46 // be returned. If |name| is empty, return all records with type |type|. | |
| 47 void FindDnsRecords(unsigned type, | |
| 48 const std::string& name, | |
| 49 std::vector<const RecordParsed*>* records, | |
| 50 base::Time now); | |
| 51 | |
| 52 // Remove expired records, call |record_removed_callback| for every removed | |
| 53 // record. | |
| 54 void CleanupRecords(base::Time now, | |
| 55 const RecordRemovedCallback& record_removed_callback); | |
| 56 | |
| 57 // Returns a time less than or equal to the next time a record will expire. | |
| 58 // Is updated when CleanupRecords or UpdateDnsRecord are called. Returns | |
| 59 // base::Time when the cache is empty. | |
| 60 base::Time next_expiration() const { return next_expiration_; } | |
| 61 | |
| 62 void Clear(); | |
| 63 | |
| 64 private: | |
| 65 // Key type for the record map. It is a 3-tuple of type, name and optional | |
| 66 // value ordered by type, then name, then optional value. This allows us to | |
| 67 // query for all records of a certain type and name, while also allowing us | |
| 68 // to set records of a certain type, name and optionally value as unique. | |
| 69 class DnsRecordCacheKey { | |
|
szym
2013/05/20 19:35:15
I'd just call this |Key|.
Noam Samuel
2013/05/20 21:07:02
Done.
| |
| 70 public: | |
| 71 DnsRecordCacheKey(unsigned type, | |
| 72 const std::string& name, | |
| 73 const std::string& optional); | |
| 74 DnsRecordCacheKey(const DnsRecordCacheKey&); | |
| 75 DnsRecordCacheKey& operator=(const DnsRecordCacheKey&); | |
| 76 ~DnsRecordCacheKey(); | |
| 77 bool operator<(const DnsRecordCacheKey& key) const; | |
| 78 bool operator==(const DnsRecordCacheKey& key) const; | |
| 79 | |
| 80 unsigned type() const { return type_; } | |
| 81 const std::string& name() const { return name_; } | |
| 82 const std::string& optional() const { return optional_; } | |
| 83 | |
| 84 private: | |
| 85 unsigned type_; | |
| 86 std::string name_; | |
| 87 std::string optional_; | |
| 88 }; | |
| 89 | |
| 90 typedef std::map<DnsRecordCacheKey, const RecordParsed*> | |
| 91 MDnsCacheMap; | |
|
szym
2013/05/20 19:35:15
I'd just call this |RecordMap|.
Noam Samuel
2013/05/20 21:07:02
Done.
| |
| 92 | |
| 93 // Get the effective expiration of a cache entry, based on its creation time | |
| 94 // and TTL. Does adjustments so entries with a TTL of zero will have a | |
| 95 // nonzero TTL, as explained in RFC 6762 Section 10.1. | |
| 96 base::Time GetEffectiveExpiration(const RecordParsed* entry); | |
| 97 | |
| 98 // Get optional part of the DNS key for shared records. For example, in PTR | |
| 99 // records this is the pointed domain, since multiple PTR records may exist | |
| 100 // for the same name. | |
| 101 std::string GetOptionalFieldForRecord(const RecordParsed* record); | |
| 102 | |
| 103 MDnsCacheMap mdns_cache_; | |
| 104 | |
| 105 base::Time next_expiration_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(MDnsCache); | |
| 108 }; | |
| 109 | |
| 110 } // namespace net | |
| 111 | |
| 112 #endif // NET_DNS_MDNS_CACHE_H_ | |
| OLD | NEW |