QUIC - enable LargePostSmallBandwidthLargeBuffer unittest.
[chromium-blink-merge.git] / net / dns / host_resolver_mojo.cc
blobd4e00ee70d15374a684ca0262fdfccfdc38428b2
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
3 // found in the LICENSE file.
5 #include "net/dns/host_resolver_mojo.h"
7 #include "net/base/address_list.h"
8 #include "net/base/net_errors.h"
9 #include "net/dns/mojo_host_type_converters.h"
10 #include "net/log/net_log.h"
11 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
14 namespace net {
15 namespace {
17 // Default TTL for successful host resolutions.
18 const int kCacheEntryTTLSeconds = 5;
20 // Default TTL for unsuccessful host resolutions.
21 const int kNegativeCacheEntryTTLSeconds = 0;
23 HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) {
24 return HostCache::Key(info.hostname(), info.address_family(),
25 info.host_resolver_flags());
28 } // namespace
30 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient,
31 public mojo::ErrorHandler {
32 public:
33 Job(const HostCache::Key& key,
34 AddressList* addresses,
35 const CompletionCallback& callback,
36 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
37 base::WeakPtr<HostCache> host_cache);
39 private:
40 // interfaces::HostResolverRequestClient override.
41 void ReportResult(int32_t error,
42 interfaces::AddressListPtr address_list) override;
44 // mojo::ErrorHandler override.
45 void OnConnectionError() override;
47 const HostCache::Key key_;
48 AddressList* addresses_;
49 CompletionCallback callback_;
50 mojo::Binding<interfaces::HostResolverRequestClient> binding_;
51 base::WeakPtr<HostCache> host_cache_;
54 HostResolverMojo::HostResolverMojo(interfaces::HostResolverPtr resolver,
55 const base::Closure& disconnect_callback)
56 : resolver_(resolver.Pass()),
57 disconnect_callback_(disconnect_callback),
58 host_cache_(HostCache::CreateDefaultCache()),
59 host_cache_weak_factory_(host_cache_.get()) {
60 if (!disconnect_callback_.is_null())
61 resolver_.set_error_handler(this);
64 HostResolverMojo::~HostResolverMojo() = default;
66 int HostResolverMojo::Resolve(const RequestInfo& info,
67 RequestPriority priority,
68 AddressList* addresses,
69 const CompletionCallback& callback,
70 RequestHandle* request_handle,
71 const BoundNetLog& source_net_log) {
72 DCHECK(thread_checker_.CalledOnValidThread());
73 DVLOG(1) << "Resolve " << info.host_port_pair().ToString();
75 HostCache::Key key = CacheKeyForRequest(info);
76 int cached_result = ResolveFromCacheInternal(info, key, addresses);
77 if (cached_result != ERR_DNS_CACHE_MISS) {
78 DVLOG(1) << "Resolved " << info.host_port_pair().ToString()
79 << " from cache";
80 return cached_result;
83 interfaces::HostResolverRequestClientPtr handle;
84 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle),
85 host_cache_weak_factory_.GetWeakPtr());
86 resolver_->Resolve(interfaces::HostResolverRequestInfo::From(info),
87 handle.Pass());
88 return ERR_IO_PENDING;
91 int HostResolverMojo::ResolveFromCache(const RequestInfo& info,
92 AddressList* addresses,
93 const BoundNetLog& source_net_log) {
94 DCHECK(thread_checker_.CalledOnValidThread());
95 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString();
96 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses);
99 void HostResolverMojo::CancelRequest(RequestHandle req) {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 // Deleting the Job closes the HostResolverRequestClient connection,
102 // signalling cancellation of the request.
103 delete static_cast<Job*>(req);
106 HostCache* HostResolverMojo::GetHostCache() {
107 return host_cache_.get();
110 void HostResolverMojo::OnConnectionError() {
111 DCHECK(!disconnect_callback_.is_null());
112 disconnect_callback_.Run();
115 int HostResolverMojo::ResolveFromCacheInternal(const RequestInfo& info,
116 const HostCache::Key& key,
117 AddressList* addresses) {
118 if (!info.allow_cached_response())
119 return ERR_DNS_CACHE_MISS;
121 const HostCache::Entry* entry =
122 host_cache_->Lookup(key, base::TimeTicks::Now());
123 if (!entry)
124 return ERR_DNS_CACHE_MISS;
126 *addresses = AddressList::CopyWithPort(entry->addrlist, info.port());
127 return entry->error;
130 HostResolverMojo::Job::Job(
131 const HostCache::Key& key,
132 AddressList* addresses,
133 const CompletionCallback& callback,
134 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
135 base::WeakPtr<HostCache> host_cache)
136 : key_(key),
137 addresses_(addresses),
138 callback_(callback),
139 binding_(this, request.Pass()),
140 host_cache_(host_cache) {
141 binding_.set_error_handler(this);
144 void HostResolverMojo::Job::ReportResult(
145 int32_t error,
146 interfaces::AddressListPtr address_list) {
147 if (error == OK && address_list)
148 *addresses_ = address_list->To<AddressList>();
149 if (host_cache_) {
150 base::TimeDelta ttl = base::TimeDelta::FromSeconds(
151 error == OK ? kCacheEntryTTLSeconds : kNegativeCacheEntryTTLSeconds);
152 HostCache::Entry entry(error, *addresses_, ttl);
153 host_cache_->Set(key_, entry, base::TimeTicks::Now(), ttl);
155 callback_.Run(error);
156 delete this;
159 void HostResolverMojo::Job::OnConnectionError() {
160 ReportResult(ERR_FAILED, interfaces::AddressListPtr());
163 } // namespace net