OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/network_hints/browser/network_hints_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "components/network_hints/common/network_hints_common.h" | |
10 #include "ipc/ipc_message_macros.h" | |
11 #include "net/base/address_list.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "net/dns/host_resolver.h" | |
14 #include "net/dns/single_request_host_resolver.h" | |
15 #include "url/gurl.h" | |
16 | |
17 namespace network_hints { | |
18 | |
19 namespace { | |
20 | |
21 const int kDefaultPort = 80; | |
22 | |
23 class DnsLookupRequest { | |
24 public: | |
25 DnsLookupRequest(net::HostResolver* host_resolver, | |
26 const std::string& hostname) | |
27 : hostname_(hostname), | |
28 resolver_(host_resolver) { | |
29 } | |
30 | |
31 // Return underlying network resolver status. | |
32 // net::OK ==> Host was found synchronously. | |
33 // net:ERR_IO_PENDING ==> Network will callback later with result. | |
34 // anything else ==> Host was not found synchronously. | |
35 int Start() { | |
36 net::HostResolver::RequestInfo resolve_info( | |
37 net::HostPortPair(hostname_, kDefaultPort)); | |
38 | |
39 // Make a note that this is a speculative resolve request. This allows | |
40 // separating it from real navigations in the observer's callback, and | |
41 // lets the HostResolver know it can be de-prioritized. | |
42 resolve_info.set_is_speculative(true); | |
43 return resolver_.Resolve( | |
44 resolve_info, | |
45 net::DEFAULT_PRIORITY, | |
46 &addresses_, | |
47 base::Bind(&DnsLookupRequest::OnLookupFinished, base::Owned(this)), | |
48 net::BoundNetLog()); | |
49 } | |
50 | |
51 private: | |
52 void OnLookupFinished(int result) { | |
53 VLOG(2) << __FUNCTION__ << ": " << hostname_ << ", result=" << result; | |
54 } | |
55 | |
56 const std::string hostname_; | |
57 net::SingleRequestHostResolver resolver_; | |
58 net::AddressList addresses_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(DnsLookupRequest); | |
61 }; | |
62 | |
63 } // namespace | |
64 | |
65 void NetworkHintsImpl::Bind(mojom::NetworkHintsRequest request) { | |
66 bindings_.AddBinding(this, std::move(request)); | |
67 } | |
68 | |
69 NetworkHintsImpl::NetworkHintsImpl(net::HostResolver* host_resolver) | |
70 : host_resolver_(host_resolver) { | |
71 DCHECK(host_resolver_); | |
72 } | |
73 | |
74 NetworkHintsImpl::~NetworkHintsImpl() {} | |
75 | |
76 void NetworkHintsImpl::DNSPrefetch(const LookupRequest& lookup_request) { | |
77 DCHECK(host_resolver_); | |
78 for (const std::string& hostname : lookup_request.hostname_list) { | |
79 DnsLookupRequest* request = new DnsLookupRequest(host_resolver_, hostname); | |
80 // Note: DnsLookupRequest will be freed by the base::Owned call when | |
81 // resolving has completed. | |
82 request->Start(); | |
83 } | |
84 } | |
85 | |
86 } // namespace network_hints | |
OLD | NEW |