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 "base/callback_helpers.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/dns/mojo_host_type_converters.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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 << " from cache"; | 86 << " from cache"; |
89 return cached_result; | 87 return cached_result; |
90 } | 88 } |
91 | 89 |
92 interfaces::HostResolverRequestClientPtr handle; | 90 interfaces::HostResolverRequestClientPtr handle; |
93 std::unique_ptr<Job> job(new Job(key, addresses, callback, | 91 std::unique_ptr<Job> job(new Job(key, addresses, callback, |
94 mojo::GetProxy(&handle), | 92 mojo::GetProxy(&handle), |
95 host_cache_weak_factory_.GetWeakPtr())); | 93 host_cache_weak_factory_.GetWeakPtr())); |
96 request->reset(new RequestImpl(std::move(job))); | 94 request->reset(new RequestImpl(std::move(job))); |
97 | 95 |
98 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info), | 96 impl_->ResolveDns(base::MakeUnique<HostResolver::RequestInfo>(info), |
99 std::move(handle)); | 97 std::move(handle)); |
100 return ERR_IO_PENDING; | 98 return ERR_IO_PENDING; |
101 } | 99 } |
102 | 100 |
103 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, | 101 int HostResolverMojo::ResolveFromCache(const RequestInfo& info, |
104 AddressList* addresses, | 102 AddressList* addresses, |
105 const NetLogWithSource& source_net_log) { | 103 const NetLogWithSource& source_net_log) { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 104 DCHECK(thread_checker_.CalledOnValidThread()); |
107 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); | 105 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString(); |
108 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); | 106 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses); |
(...skipping 26 matching lines...) Expand all Loading... |
135 base::WeakPtr<HostCache> host_cache) | 133 base::WeakPtr<HostCache> host_cache) |
136 : key_(key), | 134 : key_(key), |
137 addresses_(addresses), | 135 addresses_(addresses), |
138 callback_(callback), | 136 callback_(callback), |
139 binding_(this, std::move(request)), | 137 binding_(this, std::move(request)), |
140 host_cache_(host_cache) { | 138 host_cache_(host_cache) { |
141 binding_.set_connection_error_handler(base::Bind( | 139 binding_.set_connection_error_handler(base::Bind( |
142 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this))); | 140 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this))); |
143 } | 141 } |
144 | 142 |
145 void HostResolverMojo::Job::ReportResult( | 143 void HostResolverMojo::Job::ReportResult(int32_t error, |
146 int32_t error, | 144 const AddressList& address_list) { |
147 interfaces::AddressListPtr address_list) { | 145 if (error == OK) |
148 if (error == OK && address_list) | 146 *addresses_ = address_list; |
149 *addresses_ = address_list->To<AddressList>(); | |
150 if (host_cache_) { | 147 if (host_cache_) { |
151 base::TimeDelta ttl = base::TimeDelta::FromSeconds( | 148 base::TimeDelta ttl = base::TimeDelta::FromSeconds( |
152 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); | 149 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds); |
153 HostCache::Entry entry(error, *addresses_, ttl); | 150 HostCache::Entry entry(error, *addresses_, ttl); |
154 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); | 151 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl); |
155 } | 152 } |
156 if (binding_.is_bound()) | 153 if (binding_.is_bound()) |
157 binding_.Close(); | 154 binding_.Close(); |
158 base::ResetAndReturn(&callback_).Run(error); | 155 base::ResetAndReturn(&callback_).Run(error); |
159 } | 156 } |
160 | 157 |
161 void HostResolverMojo::Job::OnConnectionError() { | 158 void HostResolverMojo::Job::OnConnectionError() { |
162 ReportResult(ERR_FAILED, interfaces::AddressListPtr()); | 159 ReportResult(ERR_FAILED, AddressList()); |
163 } | 160 } |
164 | 161 |
165 } // namespace net | 162 } // namespace net |
OLD | NEW |