Chromium Code Reviews| 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 "base/callback_helpers.h" | |
| 9 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
| 10 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/dns/mojo_host_type_converters.h" | 13 #include "net/dns/mojo_host_type_converters.h" |
| 13 #include "net/log/net_log.h" | 14 #include "net/log/net_log.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Default TTL for successful host resolutions. | 19 // Default TTL for successful host resolutions. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 44 // Mojo error handler. | 45 // Mojo error handler. |
| 45 void OnConnectionError(); | 46 void OnConnectionError(); |
| 46 | 47 |
| 47 const HostCache::Key key_; | 48 const HostCache::Key key_; |
| 48 AddressList* addresses_; | 49 AddressList* addresses_; |
| 49 CompletionCallback callback_; | 50 CompletionCallback callback_; |
| 50 mojo::Binding<interfaces::HostResolverRequestClient> binding_; | 51 mojo::Binding<interfaces::HostResolverRequestClient> binding_; |
| 51 base::WeakPtr<HostCache> host_cache_; | 52 base::WeakPtr<HostCache> host_cache_; |
| 52 }; | 53 }; |
| 53 | 54 |
| 55 class HostResolverMojo::RequestImpl : public HostResolver::Request { | |
| 56 public: | |
| 57 explicit RequestImpl(std::unique_ptr<Job> job) : job_(std::move(job)) {} | |
| 58 | |
| 59 ~RequestImpl() override { job_.reset(); } | |
|
mmenke
2016/07/19 19:03:56
You can just give this an empty body - job_ will b
maksims (do not use this acc)
2016/07/21 07:12:46
Done.
| |
| 60 | |
| 61 void ChangeRequestPriority(RequestPriority priority) override {} | |
| 62 | |
| 63 private: | |
| 64 std::unique_ptr<Job> job_; | |
| 65 }; | |
| 66 | |
| 54 HostResolverMojo::HostResolverMojo(Impl* impl) | 67 HostResolverMojo::HostResolverMojo(Impl* impl) |
| 55 : impl_(impl), | 68 : impl_(impl), |
| 56 host_cache_(HostCache::CreateDefaultCache()), | 69 host_cache_(HostCache::CreateDefaultCache()), |
| 57 host_cache_weak_factory_(host_cache_.get()) { | 70 host_cache_weak_factory_(host_cache_.get()) { |
| 58 } | 71 } |
| 59 | 72 |
| 60 HostResolverMojo::~HostResolverMojo() = default; | 73 HostResolverMojo::~HostResolverMojo() = default; |
| 61 | 74 |
| 62 int HostResolverMojo::Resolve(const RequestInfo& info, | 75 int HostResolverMojo::Resolve(const RequestInfo& info, |
| 63 RequestPriority priority, | 76 RequestPriority priority, |
| 64 AddressList* addresses, | 77 AddressList* addresses, |
| 65 const CompletionCallback& callback, | 78 const CompletionCallback& callback, |
| 66 RequestHandle* request_handle, | 79 std::unique_ptr<Request>* request, |
| 67 const BoundNetLog& source_net_log) { | 80 const BoundNetLog& source_net_log) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 81 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 DVLOG(1) << "Resolve " << info.host_port_pair().ToString(); | 82 DVLOG(1) << "Resolve " << info.host_port_pair().ToString(); |
| 70 | 83 |
| 71 HostCache::Key key = CacheKeyForRequest(info); | 84 HostCache::Key key = CacheKeyForRequest(info); |
| 72 int cached_result = ResolveFromCacheInternal(info, key, addresses); | 85 int cached_result = ResolveFromCacheInternal(info, key, addresses); |
| 73 if (cached_result != ERR_DNS_CACHE_MISS) { | 86 if (cached_result != ERR_DNS_CACHE_MISS) { |
| 74 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() | 87 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() |
| 75 << " from cache"; | 88 << " from cache"; |
| 76 return cached_result; | 89 return cached_result; |
| 77 } | 90 } |
| 78 | 91 |
| 79 interfaces::HostResolverRequestClientPtr handle; | 92 interfaces::HostResolverRequestClientPtr handle; |
| 80 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle), | 93 std::unique_ptr<Job> job(new Job(key, addresses, callback, |
| 81 host_cache_weak_factory_.GetWeakPtr()); | 94 mojo::GetProxy(&handle), |
| 95 host_cache_weak_factory_.GetWeakPtr())); | |
| 96 (*request).reset(new RequestImpl(std::move(job))); | |
|
mmenke
2016/07/19 19:03:56
nit: request->reset(new RequestImpl(std::move(job
maksims (do not use this acc)
2016/07/21 07:12:46
Done.
| |
| 97 | |
| 82 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), | 98 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), |
| 83 std::move(handle)); | 99 std::move(handle)); |
| 84 return ERR_IO_PENDING; | 100 return ERR_IO_PENDING; |
| 85 } | 101 } |
| 86 | 102 |
| 87 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, | 103 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, |
| 88 AddressList* addresses, | 104 AddressList* addresses, |
| 89 const BoundNetLog& source_net_log) { | 105 const BoundNetLog& source_net_log) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); | 107 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); |
| 92 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); | 108 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); |
| 93 } | 109 } |
| 94 | 110 |
| 95 void HostResolverMojo::ChangeRequestPriority(RequestHandle req, | |
| 96 RequestPriority priority) { | |
| 97 // Do nothing, since Resolve() discarded the priority anyway. | |
| 98 } | |
| 99 | |
| 100 void HostResolverMojo::CancelRequest(RequestHandle req) { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 // Deleting the Job closes the HostResolverRequestClient connection, | |
| 103 // signalling cancellation of the request. | |
| 104 delete static_cast<Job*>(req); | |
| 105 } | |
| 106 | |
| 107 HostCache* HostResolverMojo::GetHostCache() { | 111 HostCache* HostResolverMojo::GetHostCache() { |
| 108 return host_cache_.get(); | 112 return host_cache_.get(); |
| 109 } | 113 } |
| 110 | 114 |
| 111 int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info, | 115 int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info, |
| 112 const HostCache::Key& key, | 116 const HostCache::Key& key, |
| 113 AddressList* addresses) { | 117 AddressList* addresses) { |
| 114 if (!info.allow_cached_response()) | 118 if (!info.allow_cached_response()) |
| 115 return ERR_DNS_CACHE_MISS; | 119 return ERR_DNS_CACHE_MISS; |
| 116 | 120 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 142 int32_t error, | 146 int32_t error, |
| 143 interfaces::AddressListPtr address_list) { | 147 interfaces::AddressListPtr address_list) { |
| 144 if (error == OK && address_list) | 148 if (error == OK && address_list) |
| 145 *addresses_ = address_list->To<AddressList>(); | 149 *addresses_ = address_list->To<AddressList>(); |
| 146 if (host_cache_) { | 150 if (host_cache_) { |
| 147 base::TimeDelta ttl = base::TimeDelta::FromSeconds( | 151 base::TimeDelta ttl = base::TimeDelta::FromSeconds( |
| 148 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); | 152 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); |
| 149 HostCache::Entry entry(error, *addresses_, ttl); | 153 HostCache::Entry entry(error, *addresses_, ttl); |
| 150 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); | 154 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); |
| 151 } | 155 } |
| 152 callback_.Run(error); | 156 if (binding_.is_bound()) |
| 153 delete this; | 157 binding_.Close(); |
| 158 base::ResetAndReturn(&callback_).Run(error); | |
| 154 } | 159 } |
| 155 | 160 |
| 156 void HostResolverMojo::Job::OnConnectionError() { | 161 void HostResolverMojo::Job::OnConnectionError() { |
| 157 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); | 162 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); |
| 158 } | 163 } |
| 159 | 164 |
| 160 } // namespace net | 165 } // namespace net |
| OLD | NEW |