| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/dns/host_resolver_mojo.h" | 5 #include "net/dns/host_resolver_mojo.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "mojo/public/cpp/bindings/binding.h" | 9 #include "mojo/public/cpp/bindings/binding.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/dns/mojo_host_type_converters.h" | |
| 13 #include "net/log/net_log.h" | 12 #include "net/log/net_log.h" |
| 14 | 13 |
| 15 namespace net { | 14 namespace net { |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 // Default TTL for successful host resolutions. | 17 // Default TTL for successful host resolutions. |
| 19 const int kCacheEntryTTLSeconds = 5; | 18 const int kCacheEntryTTLSeconds = 5; |
| 20 | 19 |
| 21 // Default TTL for unsuccessful host resolutions. | 20 // Default TTL for unsuccessful host resolutions. |
| 22 const int kNegativeCacheEntryTTLSeconds = 0; | 21 const int kNegativeCacheEntryTTLSeconds = 0; |
| 23 | 22 |
| 24 HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) { | 23 HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) { |
| 25 return HostCache::Key(info.hostname(), info.address_family(), | 24 return HostCache::Key(info.hostname(), info.address_family(), |
| 26 info.host_resolver_flags()); | 25 info.host_resolver_flags()); |
| 27 } | 26 } |
| 28 | 27 |
| 29 } // namespace | 28 } // namespace |
| 30 | 29 |
| 31 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient { | 30 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient { |
| 32 public: | 31 public: |
| 33 Job(const HostCache::Key& key, | 32 Job(const HostCache::Key& key, |
| 34 AddressList* addresses, | 33 AddressList* addresses, |
| 35 const CompletionCallback& callback, | 34 const CompletionCallback& callback, |
| 36 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, | 35 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, |
| 37 base::WeakPtr<HostCache> host_cache); | 36 base::WeakPtr<HostCache> host_cache); |
| 38 | 37 |
| 39 private: | 38 private: |
| 40 // interfaces::HostResolverRequestClient override. | 39 // interfaces::HostResolverRequestClient override. |
| 41 void ReportResult(int32_t error, | 40 void ReportResult(int32_t error, const AddressList& address_list) override; |
| 42 interfaces::AddressListPtr address_list) override; | |
| 43 | 41 |
| 44 // Mojo error handler. | 42 // Mojo error handler. |
| 45 void OnConnectionError(); | 43 void OnConnectionError(); |
| 46 | 44 |
| 47 const HostCache::Key key_; | 45 const HostCache::Key key_; |
| 48 AddressList* addresses_; | 46 AddressList* addresses_; |
| 49 CompletionCallback callback_; | 47 CompletionCallback callback_; |
| 50 mojo::Binding<interfaces::HostResolverRequestClient> binding_; | 48 mojo::Binding<interfaces::HostResolverRequestClient> binding_; |
| 51 base::WeakPtr<HostCache> host_cache_; | 49 base::WeakPtr<HostCache> host_cache_; |
| 52 }; | 50 }; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 int cached_result = ResolveFromCacheInternal(info, key, addresses); | 70 int cached_result = ResolveFromCacheInternal(info, key, addresses); |
| 73 if (cached_result != ERR_DNS_CACHE_MISS) { | 71 if (cached_result != ERR_DNS_CACHE_MISS) { |
| 74 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() | 72 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() |
| 75 << " from cache"; | 73 << " from cache"; |
| 76 return cached_result; | 74 return cached_result; |
| 77 } | 75 } |
| 78 | 76 |
| 79 interfaces::HostResolverRequestClientPtr handle; | 77 interfaces::HostResolverRequestClientPtr handle; |
| 80 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle), | 78 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle), |
| 81 host_cache_weak_factory_.GetWeakPtr()); | 79 host_cache_weak_factory_.GetWeakPtr()); |
| 82 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), | 80 impl_->ResolveDns(base::MakeUnique<HostResolver::RequestInfo>(info), |
| 83 std::move(handle)); | 81 std::move(handle)); |
| 84 return ERR_IO_PENDING; | 82 return ERR_IO_PENDING; |
| 85 } | 83 } |
| 86 | 84 |
| 87 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, | 85 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, |
| 88 AddressList* addresses, | 86 AddressList* addresses, |
| 89 const BoundNetLog& source_net_log) { | 87 const BoundNetLog& source_net_log) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); | 89 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); |
| 92 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); | 90 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 base::WeakPtr<HostCache> host_cache) | 129 base::WeakPtr<HostCache> host_cache) |
| 132 : key_(key), | 130 : key_(key), |
| 133 addresses_(addresses), | 131 addresses_(addresses), |
| 134 callback_(callback), | 132 callback_(callback), |
| 135 binding_(this, std::move(request)), | 133 binding_(this, std::move(request)), |
| 136 host_cache_(host_cache) { | 134 host_cache_(host_cache) { |
| 137 binding_.set_connection_error_handler(base::Bind( | 135 binding_.set_connection_error_handler(base::Bind( |
| 138 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this))); | 136 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this))); |
| 139 } | 137 } |
| 140 | 138 |
| 141 void HostResolverMojo::Job::ReportResult( | 139 void HostResolverMojo::Job::ReportResult(int32_t error, |
| 142 int32_t error, | 140 const AddressList& address_list) { |
| 143 interfaces::AddressListPtr address_list) { | 141 if (error == OK) |
| 144 if (error == OK && address_list) | 142 *addresses_ = address_list; |
| 145 *addresses_ = address_list->To<AddressList>(); | |
| 146 if (host_cache_) { | 143 if (host_cache_) { |
| 147 base::TimeDelta ttl = base::TimeDelta::FromSeconds( | 144 base::TimeDelta ttl = base::TimeDelta::FromSeconds( |
| 148 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); | 145 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); |
| 149 HostCache::Entry entry(error, *addresses_, ttl); | 146 HostCache::Entry entry(error, *addresses_, ttl); |
| 150 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); | 147 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); |
| 151 } | 148 } |
| 152 callback_.Run(error); | 149 callback_.Run(error); |
| 153 delete this; | 150 delete this; |
| 154 } | 151 } |
| 155 | 152 |
| 156 void HostResolverMojo::Job::OnConnectionError() { | 153 void HostResolverMojo::Job::OnConnectionError() { |
| 157 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); | 154 ReportResult(ERR_FAILED, AddressList()); |
| 158 } | 155 } |
| 159 | 156 |
| 160 } // namespace net | 157 } // namespace net |
| OLD | NEW |