sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 1 | // Copyright 2015 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 | #include "net/dns/host_resolver_mojo.h" |
| 6 | |
| 7 | #include "net/base/address_list.h" |
| 8 | #include "net/base/net_errors.h" |
amistry | 7ec5811 | 2015-02-26 06:03:00 | [diff] [blame] | 9 | #include "net/dns/mojo_host_type_converters.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 10 | #include "net/log/net_log.h" |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 11 | #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 12 | |
| 13 | namespace net { |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // Default TTL for successful host resolutions. |
| 17 | const int kCacheEntryTTLSeconds = 5; |
| 18 | |
| 19 | // Default TTL for unsuccessful host resolutions. |
| 20 | const int kNegativeCacheEntryTTLSeconds = 0; |
| 21 | |
| 22 | HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) { |
| 23 | return HostCache::Key(info.hostname(), info.address_family(), |
| 24 | info.host_resolver_flags()); |
| 25 | } |
| 26 | |
| 27 | } // namespace |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 28 | |
amistry | 3923072 | 2015-07-03 00:24:39 | [diff] [blame] | 29 | class HostResolverMojo::Job : public interfaces::HostResolverRequestClient { |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 30 | public: |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 31 | Job(const HostCache::Key& key, |
| 32 | AddressList* addresses, |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 33 | const CompletionCallback& callback, |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 34 | mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, |
| 35 | base::WeakPtr<HostCache> host_cache); |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | // interfaces::HostResolverRequestClient override. |
| 39 | void ReportResult(int32_t error, |
| 40 | interfaces::AddressListPtr address_list) override; |
| 41 | |
amistry | 3923072 | 2015-07-03 00:24:39 | [diff] [blame] | 42 | // Mojo error handler. |
| 43 | void OnConnectionError(); |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 44 | |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 45 | const HostCache::Key key_; |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 46 | AddressList* addresses_; |
| 47 | CompletionCallback callback_; |
| 48 | mojo::Binding<interfaces::HostResolverRequestClient> binding_; |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 49 | base::WeakPtr<HostCache> host_cache_; |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 50 | }; |
| 51 | |
sammc | a3242c9 | 2015-07-10 02:38:51 | [diff] [blame^] | 52 | HostResolverMojo::HostResolverMojo(Impl* impl) |
| 53 | : impl_(impl), |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 54 | host_cache_(HostCache::CreateDefaultCache()), |
| 55 | host_cache_weak_factory_(host_cache_.get()) { |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | HostResolverMojo::~HostResolverMojo() = default; |
| 59 | |
| 60 | int HostResolverMojo::Resolve(const RequestInfo& info, |
| 61 | RequestPriority priority, |
| 62 | AddressList* addresses, |
| 63 | const CompletionCallback& callback, |
| 64 | RequestHandle* request_handle, |
| 65 | const BoundNetLog& source_net_log) { |
| 66 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 | DVLOG(1) << "Resolve " << info.host_port_pair().ToString(); |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 68 | |
| 69 | HostCache::Key key = CacheKeyForRequest(info); |
| 70 | int cached_result = ResolveFromCacheInternal(info, key, addresses); |
| 71 | if (cached_result != ERR_DNS_CACHE_MISS) { |
| 72 | DVLOG(1) << "Resolved " << info.host_port_pair().ToString() |
| 73 | << " from cache"; |
| 74 | return cached_result; |
| 75 | } |
| 76 | |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 77 | interfaces::HostResolverRequestClientPtr handle; |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 78 | *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle), |
| 79 | host_cache_weak_factory_.GetWeakPtr()); |
sammc | a3242c9 | 2015-07-10 02:38:51 | [diff] [blame^] | 80 | impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), |
| 81 | handle.Pass()); |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 82 | return ERR_IO_PENDING; |
| 83 | } |
| 84 | |
| 85 | int HostResolverMojo::ResolveFromCache(const RequestInfo& info, |
| 86 | AddressList* addresses, |
| 87 | const BoundNetLog& source_net_log) { |
| 88 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 | DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 90 | return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void HostResolverMojo::CancelRequest(RequestHandle req) { |
| 94 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 | // Deleting the Job closes the HostResolverRequestClient connection, |
| 96 | // signalling cancellation of the request. |
| 97 | delete static_cast<Job*>(req); |
| 98 | } |
| 99 | |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 100 | HostCache* HostResolverMojo::GetHostCache() { |
| 101 | return host_cache_.get(); |
| 102 | } |
| 103 | |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 104 | int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info, |
| 105 | const HostCache::Key& key, |
| 106 | AddressList* addresses) { |
| 107 | if (!info.allow_cached_response()) |
| 108 | return ERR_DNS_CACHE_MISS; |
| 109 | |
| 110 | const HostCache::Entry* entry = |
| 111 | host_cache_->Lookup(key, base::TimeTicks::Now()); |
| 112 | if (!entry) |
| 113 | return ERR_DNS_CACHE_MISS; |
| 114 | |
| 115 | *addresses = AddressList::CopyWithPort(entry->addrlist, info.port()); |
| 116 | return entry->error; |
| 117 | } |
| 118 | |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 119 | HostResolverMojo::Job::Job( |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 120 | const HostCache::Key& key, |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 121 | AddressList* addresses, |
| 122 | const CompletionCallback& callback, |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 123 | mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, |
| 124 | base::WeakPtr<HostCache> host_cache) |
| 125 | : key_(key), |
| 126 | addresses_(addresses), |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 127 | callback_(callback), |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 128 | binding_(this, request.Pass()), |
| 129 | host_cache_(host_cache) { |
amistry | 3923072 | 2015-07-03 00:24:39 | [diff] [blame] | 130 | binding_.set_connection_error_handler(base::Bind( |
| 131 | &HostResolverMojo::Job::OnConnectionError, base::Unretained(this))); |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void HostResolverMojo::Job::ReportResult( |
| 135 | int32_t error, |
| 136 | interfaces::AddressListPtr address_list) { |
| 137 | if (error == OK && address_list) |
| 138 | *addresses_ = address_list->To<AddressList>(); |
sammc | 6119a59c | 2015-04-01 06:56:52 | [diff] [blame] | 139 | if (host_cache_) { |
| 140 | base::TimeDelta ttl = base::TimeDelta::FromSeconds( |
| 141 | error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); |
| 142 | HostCache::Entry entry(error, *addresses_, ttl); |
| 143 | host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); |
| 144 | } |
sammc | 6ac3fe5 | 2015-02-25 06:00:28 | [diff] [blame] | 145 | callback_.Run(error); |
| 146 | delete this; |
| 147 | } |
| 148 | |
| 149 | void HostResolverMojo::Job::OnConnectionError() { |
| 150 | ReportResult(ERR_FAILED, interfaces::AddressListPtr()); |
| 151 | } |
| 152 | |
| 153 | } // namespace net |