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
) {
44 return; // Don't store empty strings in buffer.
45 if (is_numeric_ip(name
, length
))
46 return; // Numeric IPs have no DNS lookup significance.
48 size_t old_size
= c_string_queue_
.Size();
49 DnsQueue::PushResult result
= c_string_queue_
.Push(name
, length
);
50 if (DnsQueue::SUCCESSFUL_PUSH
== result
) {
51 if (1 == c_string_queue_
.Size()) {
52 DCHECK_EQ(old_size
, 0u);
54 return; // Overkill safety net: Don't send too many InvokeLater's.
55 weak_factory_
.InvalidateWeakPtrs();
56 RenderThread::Get()->GetTaskRunner()->PostDelayedTask(
57 FROM_HERE
, base::Bind(&RendererDnsPrefetch::SubmitHostnames
,
58 weak_factory_
.GetWeakPtr()),
59 base::TimeDelta::FromMilliseconds(10));
63 if (DnsQueue::OVERFLOW_PUSH
== result
) {
64 ++buffer_full_discard_count_
;
67 DCHECK(DnsQueue::REDUNDANT_PUSH
== result
);
70 // Extract data from the Queue, and then send it off the the Browser process
72 void RendererDnsPrefetch::SubmitHostnames() {
73 // Get all names out of the C_string_queue (into our map)
74 ExtractBufferedNames();
75 // TBD: IT could be that we should only extract about as many names as we are
76 // going to send to the browser. That would cause a "silly" page with a TON
77 // of URLs to start to overrun the DnsQueue, which will cause the names to
78 // be dropped (not stored in the queue). By fetching ALL names, we are
79 // taking on a lot of work, which may take a long time to process... perhaps
80 // longer than the page may be visible!?!?! If we implement a better
81 // mechanism for doing domain_map.clear() (see end of this method), then
82 // we'd automatically flush such pending work from a ridiculously link-filled
85 // Don't overload the browser DNS lookup facility, or take too long here,
86 // by only sending off kMaxDnsHostnamesPerRequest names to the Browser.
87 // This will help to avoid overloads when a page has a TON of links.
88 DnsPrefetchNames(kMaxDnsHostnamesPerRequest
);
89 if (new_name_count_
> 0 || 0 < c_string_queue_
.Size()) {
90 weak_factory_
.InvalidateWeakPtrs();
91 RenderThread::Get()->GetTaskRunner()->PostDelayedTask(
92 FROM_HERE
, base::Bind(&RendererDnsPrefetch::SubmitHostnames
,
93 weak_factory_
.GetWeakPtr()),
94 base::TimeDelta::FromMilliseconds(10));
96 // TODO(JAR): Should we only clear the map when we navigate, or reload?
101 // Pull some hostnames from the queue, and add them to our map.
102 void RendererDnsPrefetch::ExtractBufferedNames(size_t size_goal
) {
103 size_t count(0); // Number of entries to find (0 means find all).
105 if (size_goal
<= domain_map_
.size())
106 return; // Size goal was met.
107 count
= size_goal
- domain_map_
.size();
111 while (c_string_queue_
.Pop(&name
)) {
112 DCHECK_NE(name
.size(), 0u);
113 // We don't put numeric IP names into buffer.
114 DCHECK(!is_numeric_ip(name
.c_str(), name
.size()));
115 DomainUseMap::iterator it
;
116 it
= domain_map_
.find(name
);
117 if (domain_map_
.end() == it
) {
118 domain_map_
[name
] = kPending
;
120 if (0 == count
) continue; // Until buffer is empty.
121 if (1 == count
) break; // We found size_goal.
122 DCHECK_GT(count
, 1u);
125 DCHECK(kPending
== it
->second
|| kLookupRequested
== it
->second
);
130 void RendererDnsPrefetch::DnsPrefetchNames(size_t max_count
) {
131 // We are on the renderer thread, and just need to send things to the browser.
133 for (DomainUseMap::iterator it
= domain_map_
.begin();
134 it
!= domain_map_
.end();
136 if (0 == (it
->second
& kLookupRequested
)) {
137 it
->second
|= kLookupRequested
;
138 names
.push_back(it
->first
);
139 if (0 == max_count
) continue; // Get all, independent of count.
140 if (1 == max_count
) break;
142 DCHECK_GE(max_count
, 1u);
145 DCHECK_GE(new_name_count_
, names
.size());
146 new_name_count_
-= names
.size();
148 network_hints::LookupRequest request
;
149 request
.hostname_list
= names
;
150 RenderThread::Get()->Send(new NetworkHintsMsg_DNSPrefetch(request
));
153 // is_numeric_ip() checks to see if all characters in name are either numeric,
154 // or dots. Such a name will not actually be passed to DNS, as it is an IP
156 bool RendererDnsPrefetch::is_numeric_ip(const char* name
, size_t length
) {
157 // Scan for a character outside our lookup list.
158 while (length
-- > 0) {
159 if (!isdigit(*name
) && '.' != *name
)
166 } // namespace predictor