blob: 161f586f61a45f045f4d31fff8b0e0ee350bf935 [file] [log] [blame]
sammc6ac3fe52015-02-25 06:00:281// 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"
amistry7ec58112015-02-26 06:03:009#include "net/dns/mojo_host_type_converters.h"
eroman87c53d62015-04-02 06:51:0710#include "net/log/net_log.h"
sammc6ac3fe52015-02-25 06:00:2811#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
sammc6ac3fe52015-02-25 06:00:2812
13namespace net {
sammc6119a59c2015-04-01 06:56:5214namespace {
15
16// Default TTL for successful host resolutions.
17const int kCacheEntryTTLSeconds = 5;
18
19// Default TTL for unsuccessful host resolutions.
20const int kNegativeCacheEntryTTLSeconds = 0;
21
22HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) {
23 return HostCache::Key(info.hostname(), info.address_family(),
24 info.host_resolver_flags());
25}
26
27} // namespace
sammc6ac3fe52015-02-25 06:00:2828
amistry39230722015-07-03 00:24:3929class HostResolverMojo::Job : public interfaces::HostResolverRequestClient {
sammc6ac3fe52015-02-25 06:00:2830 public:
sammc6119a59c2015-04-01 06:56:5231 Job(const HostCache::Key& key,
32 AddressList* addresses,
sammc6ac3fe52015-02-25 06:00:2833 const CompletionCallback& callback,
sammc6119a59c2015-04-01 06:56:5234 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
35 base::WeakPtr<HostCache> host_cache);
sammc6ac3fe52015-02-25 06:00:2836
37 private:
38 // interfaces::HostResolverRequestClient override.
39 void ReportResult(int32_t error,
40 interfaces::AddressListPtr address_list) override;
41
amistry39230722015-07-03 00:24:3942 // Mojo error handler.
43 void OnConnectionError();
sammc6ac3fe52015-02-25 06:00:2844
sammc6119a59c2015-04-01 06:56:5245 const HostCache::Key key_;
sammc6ac3fe52015-02-25 06:00:2846 AddressList* addresses_;
47 CompletionCallback callback_;
48 mojo::Binding<interfaces::HostResolverRequestClient> binding_;
sammc6119a59c2015-04-01 06:56:5249 base::WeakPtr<HostCache> host_cache_;
sammc6ac3fe52015-02-25 06:00:2850};
51
sammca3242c92015-07-10 02:38:5152HostResolverMojo::HostResolverMojo(Impl* impl)
53 : impl_(impl),
sammc6119a59c2015-04-01 06:56:5254 host_cache_(HostCache::CreateDefaultCache()),
55 host_cache_weak_factory_(host_cache_.get()) {
sammc6ac3fe52015-02-25 06:00:2856}
57
58HostResolverMojo::~HostResolverMojo() = default;
59
60int 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();
sammc6119a59c2015-04-01 06:56:5268
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
sammc6ac3fe52015-02-25 06:00:2877 interfaces::HostResolverRequestClientPtr handle;
sammc6119a59c2015-04-01 06:56:5278 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle),
79 host_cache_weak_factory_.GetWeakPtr());
sammca3242c92015-07-10 02:38:5180 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info),
81 handle.Pass());
sammc6ac3fe52015-02-25 06:00:2882 return ERR_IO_PENDING;
83}
84
85int 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();
sammc6119a59c2015-04-01 06:56:5290 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses);
sammc6ac3fe52015-02-25 06:00:2891}
92
93void 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
sammc6119a59c2015-04-01 06:56:52100HostCache* HostResolverMojo::GetHostCache() {
101 return host_cache_.get();
102}
103
sammc6119a59c2015-04-01 06:56:52104int 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
sammc6ac3fe52015-02-25 06:00:28119HostResolverMojo::Job::Job(
sammc6119a59c2015-04-01 06:56:52120 const HostCache::Key& key,
sammc6ac3fe52015-02-25 06:00:28121 AddressList* addresses,
122 const CompletionCallback& callback,
sammc6119a59c2015-04-01 06:56:52123 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
124 base::WeakPtr<HostCache> host_cache)
125 : key_(key),
126 addresses_(addresses),
sammc6ac3fe52015-02-25 06:00:28127 callback_(callback),
sammc6119a59c2015-04-01 06:56:52128 binding_(this, request.Pass()),
129 host_cache_(host_cache) {
amistry39230722015-07-03 00:24:39130 binding_.set_connection_error_handler(base::Bind(
131 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this)));
sammc6ac3fe52015-02-25 06:00:28132}
133
134void HostResolverMojo::Job::ReportResult(
135 int32_t error,
136 interfaces::AddressListPtr address_list) {
137 if (error == OK && address_list)
138 *addresses_ = address_list->To<AddressList>();
sammc6119a59c2015-04-01 06:56:52139 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 }
sammc6ac3fe52015-02-25 06:00:28145 callback_.Run(error);
146 delete this;
147}
148
149void HostResolverMojo::Job::OnConnectionError() {
150 ReportResult(ERR_FAILED, interfaces::AddressListPtr());
151}
152
153} // namespace net