1 // Copyright 2018 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 #ifndef SERVICES_NETWORK_RESOLVE_HOST_REQUEST_H_
6 #define SERVICES_NETWORK_RESOLVE_HOST_REQUEST_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/optional.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "mojo/public/cpp/bindings/pending_remote.h"
14 #include "mojo/public/cpp/bindings/receiver.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "net/base/completion_once_callback.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/dns/public/resolve_error_info.h"
19 #include "services/network/public/mojom/host_resolver.mojom.h"
20 
21 namespace net {
22 class HostPortPair;
23 class NetLog;
24 class NetworkIsolationKey;
25 }  // namespace net
26 
27 namespace network {
28 
29 // Manager of a single Mojo request to NetworkContext::ResolveHost(). Binds
30 // itself as the implementation of the control handle, and manages request
31 // lifetime and cancellation.
32 class ResolveHostRequest : public mojom::ResolveHostHandle {
33  public:
34   ResolveHostRequest(
35       net::HostResolver* resolver,
36       const net::HostPortPair& host,
37       const net::NetworkIsolationKey& network_isolation_key,
38       const base::Optional<net::HostResolver::ResolveHostParameters>&
39           optional_parameters,
40       net::NetLog* net_log);
41   ~ResolveHostRequest() override;
42 
43   int Start(
44       mojo::PendingReceiver<mojom::ResolveHostHandle> control_handle_request,
45       mojo::PendingRemote<mojom::ResolveHostClient> pending_response_client,
46       net::CompletionOnceCallback callback);
47 
48   // ResolveHostHandle overrides.
49   void Cancel(int error) override;
50 
51  private:
52   void OnComplete(int error);
53   net::ResolveErrorInfo GetResolveErrorInfo() const;
54   const base::Optional<net::AddressList>& GetAddressResults() const;
55   void SignalNonAddressResults();
56 
57   std::unique_ptr<net::HostResolver::ResolveHostRequest> internal_request_;
58 
59   mojo::Receiver<mojom::ResolveHostHandle> control_handle_receiver_{this};
60   mojo::Remote<mojom::ResolveHostClient> response_client_;
61   net::CompletionOnceCallback callback_;
62   bool cancelled_ = false;
63   // Error info for a cancelled request.
64   net::ResolveErrorInfo resolve_error_info_;
65 
66   DISALLOW_COPY_AND_ASSIGN(ResolveHostRequest);
67 };
68 
69 }  // namespace network
70 
71 #endif  // SERVICES_NETWORK_RESOLVE_HOST_REQUEST_H_
72