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