1 // Copyright (c) 2012 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 // See header file for description of RendererDnsPrefetch class
7 #include "components/network_hints/renderer/renderer_dns_prefetch.h"
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/message_loop/message_loop.h"
15 #include "components/network_hints/common/network_hints_common.h"
16 #include "components/network_hints/common/network_hints_messages.h"
17 #include "components/network_hints/renderer/dns_prefetch_queue.h"
18 #include "content/public/renderer/render_thread.h"
20 using content::RenderThread
;
22 namespace network_hints
{
24 RendererDnsPrefetch::RendererDnsPrefetch()
25 : c_string_queue_(1000),
30 RendererDnsPrefetch::~RendererDnsPrefetch() {
33 void RendererDnsPrefetch::Reset() {
35 c_string_queue_
.Clear();
36 buffer_full_discard_count_
= 0;
37 numeric_ip_discard_count_
= 0;
41 // Push names into queue quickly!
42 void RendererDnsPrefetch::Resolve(const char* name
, size_t length
) {
43 DCHECK(content::RenderThread::Get());
45 return; // Don't store empty strings in buffer.
46 if (is_numeric_ip(name
, length
))
47 return; // Numeric IPs have no DNS lookup significance.
49 size_t old_size
= c_string_queue_
.Size();
50 DnsQueue::PushResult result
= c_string_queue_
.Push(name
, length
);
51 if (DnsQueue::SUCCESSFUL_PUSH
== result
) {
52 if (1 == c_string_queue_
.Size()) {
53 DCHECK_EQ(old_size
, 0u);
55 return; // Overkill safety net: Don't send too many InvokeLater's.
56 weak_factory_
.InvalidateWeakPtrs();
57 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
58 FROM_HERE
, base::Bind(&RendererDnsPrefetch::SubmitHostnames
,
59 weak_factory_
.GetWeakPtr()),
60 base::TimeDelta::FromMilliseconds(10));
64 if (DnsQueue::OVERFLOW_PUSH
== result
) {
65 ++buffer_full_discard_count_
;
68 DCHECK(DnsQueue::REDUNDANT_PUSH
== result
);
71 // Extract data from the Queue, and then send it off the the Browser process
73 void RendererDnsPrefetch::SubmitHostnames() {
74 DCHECK(content::RenderThread::Get());
75 // Get all names out of the C_string_queue (into our map)
76 ExtractBufferedNames();
77 // TBD: IT could be that we should only extract about as many names as we are
78 // going to send to the browser. That would cause a "silly" page with a TON
79 // of URLs to start to overrun the DnsQueue, which will cause the names to
80 // be dropped (not stored in the queue). By fetching ALL names, we are
81 // taking on a lot of work, which may take a long time to process... perhaps
82 // longer than the page may be visible!?!?! If we implement a better
83 // mechanism for doing domain_map.clear() (see end of this method), then
84 // we'd automatically flush such pending work from a ridiculously link-filled
87 // Don't overload the browser DNS lookup facility, or take too long here,
88 // by only sending off kMaxDnsHostnamesPerRequest names to the Browser.
89 // This will help to avoid overloads when a page has a TON of links.
90 DnsPrefetchNames(kMaxDnsHostnamesPerRequest
);
91 if (new_name_count_
> 0 || 0 < c_string_queue_
.Size()) {
92 weak_factory_
.InvalidateWeakPtrs();
93 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
94 FROM_HERE
, base::Bind(&RendererDnsPrefetch::SubmitHostnames
,
95 weak_factory_
.GetWeakPtr()),
96 base::TimeDelta::FromMilliseconds(10));
98 // TODO(JAR): Should we only clear the map when we navigate, or reload?
103 // Pull some hostnames from the queue, and add them to our map.
104 void RendererDnsPrefetch::ExtractBufferedNames(size_t size_goal
) {
105 size_t count(0); // Number of entries to find (0 means find all).
107 if (size_goal
<= domain_map_
.size())
108 return; // Size goal was met.
109 count
= size_goal
- domain_map_
.size();
113 while (c_string_queue_
.Pop(&name
)) {
114 DCHECK_NE(name
.size(), 0u);
115 // We don't put numeric IP names into buffer.
116 DCHECK(!is_numeric_ip(name
.c_str(), name
.size()));
117 DomainUseMap::iterator it
;
118 it
= domain_map_
.find(name
);
119 if (domain_map_
.end() == it
) {
120 domain_map_
[name
] = kPending
;
122 if (0 == count
) continue; // Until buffer is empty.
123 if (1 == count
) break; // We found size_goal.
124 DCHECK_GT(count
, 1u);
127 DCHECK(kPending
== it
->second
|| kLookupRequested
== it
->second
);
132 void RendererDnsPrefetch::DnsPrefetchNames(size_t max_count
) {
133 // We are on the renderer thread, and just need to send things to the browser.
135 size_t domains_handled
= 0;
136 for (DomainUseMap::iterator it
= domain_map_
.begin();
137 it
!= domain_map_
.end();
139 if (0 == (it
->second
& kLookupRequested
)) {
140 it
->second
|= kLookupRequested
;
142 if (it
->first
.length() <= network_hints::kMaxDnsHostnameLength
)
143 names
.push_back(it
->first
);
144 if (0 == max_count
) continue; // Get all, independent of count.
145 if (1 == max_count
) break;
147 DCHECK_GE(max_count
, 1u);
150 DCHECK_GE(new_name_count_
, domains_handled
);
151 new_name_count_
-= domains_handled
;
153 network_hints::LookupRequest request
;
154 request
.hostname_list
= names
;
155 RenderThread::Get()->Send(new NetworkHintsMsg_DNSPrefetch(request
));
158 // is_numeric_ip() checks to see if all characters in name are either numeric,
159 // or dots. Such a name will not actually be passed to DNS, as it is an IP
161 bool RendererDnsPrefetch::is_numeric_ip(const char* name
, size_t length
) {
162 // Scan for a character outside our lookup list.
163 while (length
-- > 0) {
164 if (!isdigit(*name
) && '.' != *name
)
171 } // namespace predictor