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 RendererNetPredictor class
7 #include "chrome/renderer/net/renderer_net_predictor.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "chrome/common/net/predictor_common.h"
15 #include "chrome/common/render_messages.h"
16 #include "chrome/renderer/net/predictor_queue.h"
17 #include "content/public/renderer/render_thread.h"
19 using content::RenderThread
;
21 // The number of hostnames submitted to Browser DNS resolver per call to
22 // SubmitHostsnames() (which reads names from our queue).
23 static const size_t kMAX_SUBMISSION_PER_TASK
= 30;
25 RendererNetPredictor::RendererNetPredictor()
26 : c_string_queue_(1000),
31 RendererNetPredictor::~RendererNetPredictor() {
34 void RendererNetPredictor::Reset() {
36 c_string_queue_
.Clear();
37 buffer_full_discard_count_
= 0;
38 numeric_ip_discard_count_
= 0;
42 // Push names into queue quickly!
43 void RendererNetPredictor::Resolve(const char* name
, size_t length
) {
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 RenderThread::Get()->GetMessageLoop()->PostDelayedTask(
58 FROM_HERE
, base::Bind(&RendererNetPredictor::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 RendererNetPredictor::SubmitHostnames() {
74 // Get all names out of the C_string_queue (into our map)
75 ExtractBufferedNames();
76 // TBD: IT could be that we should only extract about as many names as we are
77 // going to send to the browser. That would cause a "silly" page with a TON
78 // of URLs to start to overrun the DnsQueue, which will cause the names to
79 // be dropped (not stored in the queue). By fetching ALL names, we are
80 // taking on a lot of work, which may take a long time to process... perhaps
81 // longer than the page may be visible!?!?! If we implement a better
82 // mechanism for doing domain_map.clear() (see end of this method), then
83 // we'd automatically flush such pending work from a ridiculously link-filled
86 // Don't overload the browser DNS lookup facility, or take too long here,
87 // by only sending off kMAX_SUBMISSION_PER_TASK names to the Browser.
88 // This will help to avoid overloads when a page has a TON of links.
89 DnsPrefetchNames(kMAX_SUBMISSION_PER_TASK
);
90 if (new_name_count_
> 0 || 0 < c_string_queue_
.Size()) {
91 weak_factory_
.InvalidateWeakPtrs();
92 RenderThread::Get()->GetMessageLoop()->PostDelayedTask(
93 FROM_HERE
, base::Bind(&RendererNetPredictor::SubmitHostnames
,
94 weak_factory_
.GetWeakPtr()),
95 base::TimeDelta::FromMilliseconds(10));
97 // TODO(JAR): Should we only clear the map when we navigate, or reload?
102 // Pull some hostnames from the queue, and add them to our map.
103 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal
) {
104 size_t count(0); // Number of entries to find (0 means find all).
106 if (size_goal
<= domain_map_
.size())
107 return; // Size goal was met.
108 count
= size_goal
- domain_map_
.size();
112 while (c_string_queue_
.Pop(&name
)) {
113 DCHECK_NE(name
.size(), 0u);
114 // We don't put numeric IP names into buffer.
115 DCHECK(!is_numeric_ip(name
.c_str(), name
.size()));
116 DomainUseMap::iterator it
;
117 it
= domain_map_
.find(name
);
118 if (domain_map_
.end() == it
) {
119 domain_map_
[name
] = kPending
;
121 if (0 == count
) continue; // Until buffer is empty.
122 if (1 == count
) break; // We found size_goal.
123 DCHECK_GT(count
, 1u);
126 DCHECK(kPending
== it
->second
|| kLookupRequested
== it
->second
);
131 void RendererNetPredictor::DnsPrefetchNames(size_t max_count
) {
132 // We are on the renderer thread, and just need to send things to the browser.
133 chrome_common_net::NameList names
;
134 for (DomainUseMap::iterator it
= domain_map_
.begin();
135 it
!= domain_map_
.end();
137 if (0 == (it
->second
& kLookupRequested
)) {
138 it
->second
|= kLookupRequested
;
139 names
.push_back(it
->first
);
140 if (0 == max_count
) continue; // Get all, independent of count.
141 if (1 == max_count
) break;
143 DCHECK_GE(max_count
, 1u);
146 DCHECK_GE(new_name_count_
, names
.size());
147 new_name_count_
-= names
.size();
149 RenderThread::Get()->Send(new ChromeViewHostMsg_DnsPrefetch(names
));
152 // is_numeric_ip() checks to see if all characters in name are either numeric,
153 // or dots. Such a name will not actually be passed to DNS, as it is an IP
155 bool RendererNetPredictor::is_numeric_ip(const char* name
, size_t length
) {
156 // Scan for a character outside our lookup list.
157 while (length
-- > 0) {
158 if (!isdigit(*name
) && '.' != *name
)