Fix destruction order of PromoResourceService and NTPResourceCache
[chromium-blink-merge.git] / net / dns / host_resolver_mojo.cc
blobf478e08e9112e3c793a7af76f6a5d2c80eccf2d4
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/base/net_log.h"
10 #include "net/dns/mojo_host_type_converters.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 {
16 class HostResolverMojo::Job : public interfaces::HostResolverRequestClient,
17 public mojo::ErrorHandler {
18 public:
19 Job(AddressList* addresses,
20 const CompletionCallback& callback,
21 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request);
23 private:
24 // interfaces::HostResolverRequestClient override.
25 void ReportResult(int32_t error,
26 interfaces::AddressListPtr address_list) override;
28 // mojo::ErrorHandler override.
29 void OnConnectionError() override;
31 AddressList* addresses_;
32 CompletionCallback callback_;
33 mojo::Binding<interfaces::HostResolverRequestClient> binding_;
36 HostResolverMojo::HostResolverMojo(interfaces::HostResolverPtr resolver,
37 const base::Closure& disconnect_callback)
38 : resolver_(resolver.Pass()), disconnect_callback_(disconnect_callback) {
39 if (!disconnect_callback_.is_null())
40 resolver_.set_error_handler(this);
43 HostResolverMojo::~HostResolverMojo() = default;
45 int HostResolverMojo::Resolve(const RequestInfo& info,
46 RequestPriority priority,
47 AddressList* addresses,
48 const CompletionCallback& callback,
49 RequestHandle* request_handle,
50 const BoundNetLog& source_net_log) {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 DVLOG(1) << "Resolve " << info.host_port_pair().ToString();
53 interfaces::HostResolverRequestClientPtr handle;
54 *request_handle = new Job(addresses, callback, mojo::GetProxy(&handle));
55 resolver_->Resolve(interfaces::HostResolverRequestInfo::From(info),
56 handle.Pass());
57 return ERR_IO_PENDING;
60 int HostResolverMojo::ResolveFromCache(const RequestInfo& info,
61 AddressList* addresses,
62 const BoundNetLog& source_net_log) {
63 DCHECK(thread_checker_.CalledOnValidThread());
64 DVLOG(1) << "ResolveFromCache " << info.host_port_pair().ToString();
65 // TODO(sammc): Support caching.
66 return ERR_DNS_CACHE_MISS;
69 void HostResolverMojo::CancelRequest(RequestHandle req) {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 // Deleting the Job closes the HostResolverRequestClient connection,
72 // signalling cancellation of the request.
73 delete static_cast<Job*>(req);
76 void HostResolverMojo::OnConnectionError() {
77 DCHECK(!disconnect_callback_.is_null());
78 disconnect_callback_.Run();
81 HostResolverMojo::Job::Job(
82 AddressList* addresses,
83 const CompletionCallback& callback,
84 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> request)
85 : addresses_(addresses),
86 callback_(callback),
87 binding_(this, request.Pass()) {
88 binding_.set_error_handler(this);
91 void HostResolverMojo::Job::ReportResult(
92 int32_t error,
93 interfaces::AddressListPtr address_list) {
94 if (error == OK && address_list)
95 *addresses_ = address_list->To<AddressList>();
96 callback_.Run(error);
97 delete this;
100 void HostResolverMojo::Job::OnConnectionError() {
101 ReportResult(ERR_FAILED, interfaces::AddressListPtr());
104 } // namespace net