Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: chrome/renderer/net/render_dns_master.cc

Issue 2866026: Rename Dns prefetching files to Predictor files... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2006-2010 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 // See header file for description of RendererNetPredictor class
6
7
8 #include "chrome/renderer/net/render_dns_master.h"
9
10 #include <ctype.h>
11
12 #include "base/logging.h"
13 #include "base/message_loop.h"
14 #include "chrome/common/net/dns.h"
15 #include "chrome/common/render_messages.h"
16 #include "chrome/renderer/net/render_dns_queue.h"
17 #include "chrome/renderer/render_thread.h"
18
19 // This API is used in the render process by renderer_glue.cc.
20 // IF you are in the render process, you MUST be on the renderer thread to call.
21 void DnsPrefetchCString(const char* hostname, size_t length) {
22 RenderThread::current()->Resolve(hostname, length);
23 }
24
25 // The number of hostnames submitted to Browser DNS resolver per call to
26 // SubmitHostsnames() (which reads names from our queue).
27 static const size_t kMAX_SUBMISSION_PER_TASK = 30;
28
29 RendererNetPredictor::RendererNetPredictor()
30 : c_string_queue_(1000),
31 ALLOW_THIS_IN_INITIALIZER_LIST(renderer_predictor_factory_(this)) {
32 Reset();
33 }
34
35 void RendererNetPredictor::Reset() {
36 domain_map_.clear();
37 c_string_queue_.Clear();
38 buffer_full_discard_count_ = 0;
39 numeric_ip_discard_count_ = 0;
40 new_name_count_ = 0;
41 }
42
43 // Push names into queue quickly!
44 void RendererNetPredictor::Resolve(const char* name, size_t length) {
45 if (!length)
46 return; // Don't store empty strings in buffer.
47 if (is_numeric_ip(name, length))
48 return; // Numeric IPs have no DNS lookup significance.
49
50 size_t old_size = c_string_queue_.Size();
51 DnsQueue::PushResult result = c_string_queue_.Push(name, length);
52 if (DnsQueue::SUCCESSFUL_PUSH == result) {
53 if (1 == c_string_queue_.Size()) {
54 DCHECK_EQ(old_size, 0u);
55 if (0 != old_size)
56 return; // Overkill safety net: Don't send too many InvokeLater's.
57 renderer_predictor_factory_.RevokeAll();
58 RenderThread::current()->message_loop()->PostDelayedTask(FROM_HERE,
59 renderer_predictor_factory_.NewRunnableMethod(
60 &RendererNetPredictor::SubmitHostnames), 10);
61 }
62 return;
63 }
64 if (DnsQueue::OVERFLOW_PUSH == result) {
65 ++buffer_full_discard_count_;
66 return;
67 }
68 DCHECK(DnsQueue::REDUNDANT_PUSH == result);
69 }
70
71 // Extract data from the Queue, and then send it off the the Browser process
72 // to be resolved.
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
84 // page.
85
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 renderer_predictor_factory_.RevokeAll();
92 RenderThread::current()->message_loop()->PostDelayedTask(FROM_HERE,
93 renderer_predictor_factory_.NewRunnableMethod(
94 &RendererNetPredictor::SubmitHostnames), 10);
95 } else {
96 // TODO(JAR): Should we only clear the map when we navigate, or reload?
97 domain_map_.clear();
98 }
99 }
100
101 // Pull some hostnames from the queue, and add them to our map.
102 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) {
103 size_t count(0); // Number of entries to find (0 means find all).
104 if (size_goal > 0) {
105 if (size_goal <= domain_map_.size())
106 return; // Size goal was met.
107 count = size_goal - domain_map_.size();
108 }
109
110 std::string name;
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;
119 ++new_name_count_;
120 if (0 == count) continue; // Until buffer is empty.
121 if (1 == count) break; // We found size_goal.
122 DCHECK_GT(count, 1u);
123 --count;
124 } else {
125 DCHECK(kPending == it->second || kLookupRequested == it->second);
126 }
127 }
128 }
129
130 void RendererNetPredictor::DnsPrefetchNames(size_t max_count) {
131 // We are on the renderer thread, and just need to send things to the browser.
132 chrome_common_net::NameList names;
133 for (DomainUseMap::iterator it = domain_map_.begin();
134 it != domain_map_.end();
135 ++it) {
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;
141 --max_count;
142 DCHECK_GE(max_count, 1u);
143 }
144 }
145 DCHECK_GE(new_name_count_, names.size());
146 new_name_count_ -= names.size();
147
148 RenderThread::current()->Send(new ViewHostMsg_DnsPrefetch(names));
149 }
150
151 // is_numeric_ip() checks to see if all characters in name are either numeric,
152 // or dots. Such a name will not actually be passed to DNS, as it is an IP
153 // address.
154 bool RendererNetPredictor::is_numeric_ip(const char* name, size_t length) {
155 // Scan for a character outside our lookup list.
156 while (length-- > 0) {
157 if (!isdigit(*name) && '.' != *name)
158 return false;
159 ++name;
160 }
161 return true;
162 }
OLDNEW
« no previous file with comments | « chrome/renderer/net/render_dns_master.h ('k') | chrome/renderer/net/render_dns_master_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698