Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / dns / host_resolver_mojo.cc
blob161f586f61a45f045f4d31fff8b0e0ee350bf935
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"
13 namespace net {
14 namespace {
16 // Default TTL for successful host resolutions.
17 const int kCacheEntryTTLSeconds = 5;
19 // Default TTL for unsuccessful host resolutions.
20 const int kNegativeCacheEntryTTLSeconds = 0;
22 HostCache::Key CacheKeyForRequest(const HostResolver::RequestInfo& info) {
23 return HostCache::Key(info.hostname(), info.address_family(),
24 info.host_resolver_flags());
27 } // namespace
29 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient {
30 public:
31 Job(const HostCache::Key& key,
32 AddressList* addresses,
33 const CompletionCallback& callback,
34 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
35 base::WeakPtr<HostCache> host_cache);
37 private:
38 // interfaces::HostResolverRequestClient override.
39 void ReportResult(int32_t error,
40 interfaces::AddressListPtr address_list) override;
42 // Mojo error handler.
43 void OnConnectionError();
45 const HostCache::Key key_;
46 AddressList* addresses_;
47 CompletionCallback callback_;
48 mojo::Binding<interfaces::HostResolverRequestClient> binding_;
49 base::WeakPtr<HostCache> host_cache_;
52 HostResolverMojo::HostResolverMojo(Impl* impl)
53 : impl_(impl),
54 host_cache_(HostCache::CreateDefaultCache()),
55 host_cache_weak_factory_(host_cache_.get()) {
58 HostResolverMojo::~HostResolverMojo() = default;
60 int 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();
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;
77 interfaces::HostResolverRequestClientPtr handle;
78 *request_handle = new Job(key, addresses, callback, mojo::GetProxy(&handle),
79 host_cache_weak_factory_.GetWeakPtr());
80 impl_->ResolveDns(interfaces::HostResolverRequestInfo::From(info),
81 handle.Pass());
82 return ERR_IO_PENDING;
85 int 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();
90 return ResolveFromCacheInternal(info, CacheKeyForRequest(info), addresses);
93 void 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);
100 HostCache* HostResolverMojo::GetHostCache() {
101 return host_cache_.get();
104 int 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;
110 const HostCache::Entry* entry =
111 host_cache_->Lookup(key, base::TimeTicks::Now());
112 if (!entry)
113 return ERR_DNS_CACHE_MISS;
115 *addresses = AddressList::CopyWithPort(entry->addrlist, info.port());
116 return entry->error;
119 HostResolverMojo::Job::Job(
120 const HostCache::Key& key,
121 AddressList* addresses,
122 const CompletionCallback& callback,
123 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request,
124 base::WeakPtr<HostCache> host_cache)
125 : key_(key),
126 addresses_(addresses),
127 callback_(callback),
128 binding_(this, request.Pass()),
129 host_cache_(host_cache) {
130 binding_.set_connection_error_handler(base::Bind(
131 &HostResolverMojo::Job::OnConnectionError, base::Unretained(this)));
134 void HostResolverMojo::Job::ReportResult(
135 int32_t error,
136 interfaces::AddressListPtr address_list) {
137 if (error == OK && address_list)
138 *addresses_ = address_list->To<AddressList>();
139 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);
145 callback_.Run(error);
146 delete this;
149 void HostResolverMojo::Job::OnConnectionError() {
150 ReportResult(ERR_FAILED, interfaces::AddressListPtr());
153 } // namespace net