| 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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient { | 31 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient { |
| 32 public: | 32 public: |
| 33 Job(const HostCache::Key& key, | 33 Job(const HostCache::Key& key, |
| 34 AddressList* addresses, | 34 AddressList* addresses, |
| 35 const CompletionCallback& callback, | 35 const CompletionCallback& callback, |
| 36 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, | 36 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request, |
| 37 base::WeakPtr<HostCache> host_cache); | 37 base::WeakPtr<HostCache> host_cache); |
| 38 | 38 |
| 39 void SetRequest(RequestImpl* req) { |
| 40 DCHECK(req); |
| 41 request_ = req; |
| 42 } |
| 43 |
| 39 private: | 44 private: |
| 40 // interfaces::HostResolverRequestClient override. | 45 // interfaces::HostResolverRequestClient override. |
| 41 void ReportResult(int32_t error, | 46 void ReportResult(int32_t error, |
| 42 interfaces::AddressListPtr address_list) override; | 47 interfaces::AddressListPtr address_list) override; |
| 43 | 48 |
| 44 // Mojo error handler. | 49 // Mojo error handler. |
| 45 void OnConnectionError(); | 50 void OnConnectionError(); |
| 46 | 51 |
| 47 const HostCache::Key key_; | 52 const HostCache::Key key_; |
| 48 AddressList* addresses_; | 53 AddressList* addresses_; |
| 49 CompletionCallback callback_; | 54 CompletionCallback callback_; |
| 50 mojo::Binding<interfaces::HostResolverRequestClient> binding_; | 55 mojo::Binding<interfaces::HostResolverRequestClient> binding_; |
| 51 base::WeakPtr<HostCache> host_cache_; | 56 base::WeakPtr<HostCache> host_cache_; |
| 57 RequestImpl* request_; |
| 58 }; |
| 59 |
| 60 class HostResolverMojo::RequestImpl : public HostResolver::Request { |
| 61 public: |
| 62 explicit RequestImpl(std::unique_ptr<Job> job) : job_(std::move(job)) { |
| 63 DCHECK(job_); |
| 64 job_->SetRequest(this); |
| 65 } |
| 66 |
| 67 ~RequestImpl() override {} |
| 68 |
| 69 void ChangeRequestPriority(RequestPriority priority) override {} |
| 70 |
| 71 void RemoveJob() { |
| 72 if (job_) |
| 73 job_.reset(); |
| 74 } |
| 75 |
| 76 private: |
| 77 std::unique_ptr<Job> job_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(RequestImpl); |
| 52 }; | 80 }; |
| 53 | 81 |
| 54 HostResolverMojo::HostResolverMojo(Impl* impl) | 82 HostResolverMojo::HostResolverMojo(Impl* impl) |
| 55 : impl_(impl), | 83 : impl_(impl), |
| 56 host_cache_(HostCache::CreateDefaultCache()), | 84 host_cache_(HostCache::CreateDefaultCache()), |
| 57 host_cache_weak_factory_(host_cache_.get()) { | 85 host_cache_weak_factory_(host_cache_.get()) { |
| 58 } | 86 } |
| 59 | 87 |
| 60 HostResolverMojo::~HostResolverMojo() = default; | 88 HostResolverMojo::~HostResolverMojo() = default; |
| 61 | 89 |
| 62 int HostResolverMojo::Resolve(const RequestInfo& info, | 90 int HostResolverMojo::Resolve(const RequestInfo& info, |
| 63 RequestPriority priority, | 91 RequestPriority priority, |
| 64 AddressList* addresses, | 92 AddressList* addresses, |
| 65 const CompletionCallback& callback, | 93 const CompletionCallback& callback, |
| 66 RequestHandle* request_handle, | 94 std::unique_ptr<Request>* request_handle, |
| 67 const BoundNetLog& source_net_log) { | 95 const BoundNetLog& source_net_log) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 96 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 DVLOG(1) << "Resolve " << info.host_port_pair().ToString(); | 97 DVLOG(1) << "Resolve " << info.host_port_pair().ToString(); |
| 70 | 98 |
| 71 HostCache::Key key = CacheKeyForRequest(info); | 99 HostCache::Key key = CacheKeyForRequest(info); |
| 72 int cached_result = ResolveFromCacheInternal(info, key, addresses); | 100 int cached_result = ResolveFromCacheInternal(info, key, addresses); |
| 73 if (cached_result != ERR_DNS_CACHE_MISS) { | 101 if (cached_result != ERR_DNS_CACHE_MISS) { |
| 74 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() | 102 DVLOG(1) << "Resolved " << info.host_port_pair().ToString() |
| 75 << " from cache"; | 103 << " from cache"; |
| 76 return cached_result; | 104 return cached_result; |
| 77 } | 105 } |
| 78 | 106 |
| 79 interfaces::HostResolverRequestClientPtr handle; | 107 interfaces::HostResolverRequestClientPtr handle; |
| 80 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle), | 108 std::unique_ptr<Job> job(new Job(key, addresses, callback, |
| 81 host_cache_weak_factory_.GetWeakPtr()); | 109 mojo::GetProxy(&handle), |
| 110 host_cache_weak_factory_.GetWeakPtr())); |
| 111 (*request_handle).reset(new RequestImpl(std::move(job))); |
| 112 |
| 82 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), | 113 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), |
| 83 std::move(handle)); | 114 std::move(handle)); |
| 84 return ERR_IO_PENDING; | 115 return ERR_IO_PENDING; |
| 85 } | 116 } |
| 86 | 117 |
| 87 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, | 118 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, |
| 88 AddressList* addresses, | 119 AddressList* addresses, |
| 89 const BoundNetLog& source_net_log) { | 120 const BoundNetLog& source_net_log) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); | 122 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); |
| 92 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); | 123 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); |
| 93 } | 124 } |
| 94 | 125 |
| 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() { | 126 HostCache* HostResolverMojo::GetHostCache() { |
| 108 return host_cache_.get(); | 127 return host_cache_.get(); |
| 109 } | 128 } |
| 110 | 129 |
| 111 int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info, | 130 int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info, |
| 112 const HostCache::Key& key, | 131 const HostCache::Key& key, |
| 113 AddressList* addresses) { | 132 AddressList* addresses) { |
| 114 if (!info.allow_cached_response()) | 133 if (!info.allow_cached_response()) |
| 115 return ERR_DNS_CACHE_MISS; | 134 return ERR_DNS_CACHE_MISS; |
| 116 | 135 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 142 int32_t error, | 161 int32_t error, |
| 143 interfaces::AddressListPtr address_list) { | 162 interfaces::AddressListPtr address_list) { |
| 144 if (error == OK && address_list) | 163 if (error == OK && address_list) |
| 145 *addresses_ = address_list->To<AddressList>(); | 164 *addresses_ = address_list->To<AddressList>(); |
| 146 if (host_cache_) { | 165 if (host_cache_) { |
| 147 base::TimeDelta ttl = base::TimeDelta::FromSeconds( | 166 base::TimeDelta ttl = base::TimeDelta::FromSeconds( |
| 148 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); | 167 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); |
| 149 HostCache::Entry entry(error, *addresses_, ttl); | 168 HostCache::Entry entry(error, *addresses_, ttl); |
| 150 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); | 169 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); |
| 151 } | 170 } |
| 152 callback_.Run(error); | 171 |
| 153 delete this; | 172 CompletionCallback cb = callback_; |
| 173 callback_.Reset(); |
| 174 |
| 175 DCHECK(request_); |
| 176 request_->RemoveJob(); |
| 177 |
| 178 if (!cb.is_null()) |
| 179 cb.Run(error); |
| 154 } | 180 } |
| 155 | 181 |
| 156 void HostResolverMojo::Job::OnConnectionError() { | 182 void HostResolverMojo::Job::OnConnectionError() { |
| 157 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); | 183 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); |
| 158 } | 184 } |
| 159 | 185 |
| 160 } // namespace net | 186 } // namespace net |
| OLD | NEW |