OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See header file for description of RendererNetPredictor class | 5 // See header file for description of RendererDnsPrefetch class |
6 | 6 |
7 #include "components/dns_prefetch/renderer/renderer_net_predictor.h" | 7 #include "components/network_hints/renderer/renderer_dns_prefetch.h" |
8 | 8 |
9 #include <ctype.h> | 9 #include <ctype.h> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "components/dns_prefetch/common/prefetch_common.h" | 14 #include "components/network_hints/common/network_hints_common.h" |
15 #include "components/dns_prefetch/common/prefetch_messages.h" | 15 #include "components/network_hints/common/network_hints_messages.h" |
16 #include "components/dns_prefetch/renderer/predictor_queue.h" | 16 #include "components/network_hints/renderer/dns_prefetch_queue.h" |
17 #include "content/public/renderer/render_thread.h" | 17 #include "content/public/renderer/render_thread.h" |
18 | 18 |
19 using content::RenderThread; | 19 using content::RenderThread; |
20 | 20 |
21 namespace dns_prefetch { | 21 namespace network_hints { |
22 | 22 |
23 RendererNetPredictor::RendererNetPredictor() | 23 RendererDnsPrefetch::RendererDnsPrefetch() |
24 : c_string_queue_(1000), | 24 : c_string_queue_(1000), |
25 weak_factory_(this) { | 25 weak_factory_(this) { |
26 Reset(); | 26 Reset(); |
27 } | 27 } |
28 | 28 |
29 RendererNetPredictor::~RendererNetPredictor() { | 29 RendererDnsPrefetch::~RendererDnsPrefetch() { |
30 } | 30 } |
31 | 31 |
32 void RendererNetPredictor::Reset() { | 32 void RendererDnsPrefetch::Reset() { |
33 domain_map_.clear(); | 33 domain_map_.clear(); |
34 c_string_queue_.Clear(); | 34 c_string_queue_.Clear(); |
35 buffer_full_discard_count_ = 0; | 35 buffer_full_discard_count_ = 0; |
36 numeric_ip_discard_count_ = 0; | 36 numeric_ip_discard_count_ = 0; |
37 new_name_count_ = 0; | 37 new_name_count_ = 0; |
38 } | 38 } |
39 | 39 |
40 // Push names into queue quickly! | 40 // Push names into queue quickly! |
41 void RendererNetPredictor::Resolve(const char* name, size_t length) { | 41 void RendererDnsPrefetch::Resolve(const char* name, size_t length) { |
42 if (!length) | 42 if (!length) |
43 return; // Don't store empty strings in buffer. | 43 return; // Don't store empty strings in buffer. |
44 if (is_numeric_ip(name, length)) | 44 if (is_numeric_ip(name, length)) |
45 return; // Numeric IPs have no DNS lookup significance. | 45 return; // Numeric IPs have no DNS lookup significance. |
46 | 46 |
47 size_t old_size = c_string_queue_.Size(); | 47 size_t old_size = c_string_queue_.Size(); |
48 DnsQueue::PushResult result = c_string_queue_.Push(name, length); | 48 DnsQueue::PushResult result = c_string_queue_.Push(name, length); |
49 if (DnsQueue::SUCCESSFUL_PUSH == result) { | 49 if (DnsQueue::SUCCESSFUL_PUSH == result) { |
50 if (1 == c_string_queue_.Size()) { | 50 if (1 == c_string_queue_.Size()) { |
51 DCHECK_EQ(old_size, 0u); | 51 DCHECK_EQ(old_size, 0u); |
52 if (0 != old_size) | 52 if (0 != old_size) |
53 return; // Overkill safety net: Don't send too many InvokeLater's. | 53 return; // Overkill safety net: Don't send too many InvokeLater's. |
54 weak_factory_.InvalidateWeakPtrs(); | 54 weak_factory_.InvalidateWeakPtrs(); |
55 RenderThread::Get()->GetTaskRunner()->PostDelayedTask( | 55 RenderThread::Get()->GetTaskRunner()->PostDelayedTask( |
56 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames, | 56 FROM_HERE, base::Bind(&RendererDnsPrefetch::SubmitHostnames, |
57 weak_factory_.GetWeakPtr()), | 57 weak_factory_.GetWeakPtr()), |
58 base::TimeDelta::FromMilliseconds(10)); | 58 base::TimeDelta::FromMilliseconds(10)); |
59 } | 59 } |
60 return; | 60 return; |
61 } | 61 } |
62 if (DnsQueue::OVERFLOW_PUSH == result) { | 62 if (DnsQueue::OVERFLOW_PUSH == result) { |
63 ++buffer_full_discard_count_; | 63 ++buffer_full_discard_count_; |
64 return; | 64 return; |
65 } | 65 } |
66 DCHECK(DnsQueue::REDUNDANT_PUSH == result); | 66 DCHECK(DnsQueue::REDUNDANT_PUSH == result); |
67 } | 67 } |
68 | 68 |
69 // Extract data from the Queue, and then send it off the the Browser process | 69 // Extract data from the Queue, and then send it off the the Browser process |
70 // to be resolved. | 70 // to be resolved. |
71 void RendererNetPredictor::SubmitHostnames() { | 71 void RendererDnsPrefetch::SubmitHostnames() { |
72 // Get all names out of the C_string_queue (into our map) | 72 // Get all names out of the C_string_queue (into our map) |
73 ExtractBufferedNames(); | 73 ExtractBufferedNames(); |
74 // TBD: IT could be that we should only extract about as many names as we are | 74 // TBD: IT could be that we should only extract about as many names as we are |
75 // going to send to the browser. That would cause a "silly" page with a TON | 75 // going to send to the browser. That would cause a "silly" page with a TON |
76 // of URLs to start to overrun the DnsQueue, which will cause the names to | 76 // of URLs to start to overrun the DnsQueue, which will cause the names to |
77 // be dropped (not stored in the queue). By fetching ALL names, we are | 77 // be dropped (not stored in the queue). By fetching ALL names, we are |
78 // taking on a lot of work, which may take a long time to process... perhaps | 78 // taking on a lot of work, which may take a long time to process... perhaps |
79 // longer than the page may be visible!?!?! If we implement a better | 79 // longer than the page may be visible!?!?! If we implement a better |
80 // mechanism for doing domain_map.clear() (see end of this method), then | 80 // mechanism for doing domain_map.clear() (see end of this method), then |
81 // we'd automatically flush such pending work from a ridiculously link-filled | 81 // we'd automatically flush such pending work from a ridiculously link-filled |
82 // page. | 82 // page. |
83 | 83 |
84 // Don't overload the browser DNS lookup facility, or take too long here, | 84 // Don't overload the browser DNS lookup facility, or take too long here, |
85 // by only sending off kMaxDnsHostnamesPerRequest names to the Browser. | 85 // by only sending off kMaxDnsHostnamesPerRequest names to the Browser. |
86 // This will help to avoid overloads when a page has a TON of links. | 86 // This will help to avoid overloads when a page has a TON of links. |
87 DnsPrefetchNames(kMaxDnsHostnamesPerRequest); | 87 DnsPrefetchNames(kMaxDnsHostnamesPerRequest); |
88 if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) { | 88 if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) { |
89 weak_factory_.InvalidateWeakPtrs(); | 89 weak_factory_.InvalidateWeakPtrs(); |
90 RenderThread::Get()->GetTaskRunner()->PostDelayedTask( | 90 RenderThread::Get()->GetTaskRunner()->PostDelayedTask( |
91 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames, | 91 FROM_HERE, base::Bind(&RendererDnsPrefetch::SubmitHostnames, |
92 weak_factory_.GetWeakPtr()), | 92 weak_factory_.GetWeakPtr()), |
93 base::TimeDelta::FromMilliseconds(10)); | 93 base::TimeDelta::FromMilliseconds(10)); |
94 } else { | 94 } else { |
95 // TODO(JAR): Should we only clear the map when we navigate, or reload? | 95 // TODO(JAR): Should we only clear the map when we navigate, or reload? |
96 domain_map_.clear(); | 96 domain_map_.clear(); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 // Pull some hostnames from the queue, and add them to our map. | 100 // Pull some hostnames from the queue, and add them to our map. |
101 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) { | 101 void RendererDnsPrefetch::ExtractBufferedNames(size_t size_goal) { |
102 size_t count(0); // Number of entries to find (0 means find all). | 102 size_t count(0); // Number of entries to find (0 means find all). |
103 if (size_goal > 0) { | 103 if (size_goal > 0) { |
104 if (size_goal <= domain_map_.size()) | 104 if (size_goal <= domain_map_.size()) |
105 return; // Size goal was met. | 105 return; // Size goal was met. |
106 count = size_goal - domain_map_.size(); | 106 count = size_goal - domain_map_.size(); |
107 } | 107 } |
108 | 108 |
109 std::string name; | 109 std::string name; |
110 while (c_string_queue_.Pop(&name)) { | 110 while (c_string_queue_.Pop(&name)) { |
111 DCHECK_NE(name.size(), 0u); | 111 DCHECK_NE(name.size(), 0u); |
112 // We don't put numeric IP names into buffer. | 112 // We don't put numeric IP names into buffer. |
113 DCHECK(!is_numeric_ip(name.c_str(), name.size())); | 113 DCHECK(!is_numeric_ip(name.c_str(), name.size())); |
114 DomainUseMap::iterator it; | 114 DomainUseMap::iterator it; |
115 it = domain_map_.find(name); | 115 it = domain_map_.find(name); |
116 if (domain_map_.end() == it) { | 116 if (domain_map_.end() == it) { |
117 domain_map_[name] = kPending; | 117 domain_map_[name] = kPending; |
118 ++new_name_count_; | 118 ++new_name_count_; |
119 if (0 == count) continue; // Until buffer is empty. | 119 if (0 == count) continue; // Until buffer is empty. |
120 if (1 == count) break; // We found size_goal. | 120 if (1 == count) break; // We found size_goal. |
121 DCHECK_GT(count, 1u); | 121 DCHECK_GT(count, 1u); |
122 --count; | 122 --count; |
123 } else { | 123 } else { |
124 DCHECK(kPending == it->second || kLookupRequested == it->second); | 124 DCHECK(kPending == it->second || kLookupRequested == it->second); |
125 } | 125 } |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 void RendererNetPredictor::DnsPrefetchNames(size_t max_count) { | 129 void RendererDnsPrefetch::DnsPrefetchNames(size_t max_count) { |
130 // We are on the renderer thread, and just need to send things to the browser. | 130 // We are on the renderer thread, and just need to send things to the browser. |
131 NameList names; | 131 NameList names; |
132 for (DomainUseMap::iterator it = domain_map_.begin(); | 132 for (DomainUseMap::iterator it = domain_map_.begin(); |
133 it != domain_map_.end(); | 133 it != domain_map_.end(); |
134 ++it) { | 134 ++it) { |
135 if (0 == (it->second & kLookupRequested)) { | 135 if (0 == (it->second & kLookupRequested)) { |
136 it->second |= kLookupRequested; | 136 it->second |= kLookupRequested; |
137 names.push_back(it->first); | 137 names.push_back(it->first); |
138 if (0 == max_count) continue; // Get all, independent of count. | 138 if (0 == max_count) continue; // Get all, independent of count. |
139 if (1 == max_count) break; | 139 if (1 == max_count) break; |
140 --max_count; | 140 --max_count; |
141 DCHECK_GE(max_count, 1u); | 141 DCHECK_GE(max_count, 1u); |
142 } | 142 } |
143 } | 143 } |
144 DCHECK_GE(new_name_count_, names.size()); | 144 DCHECK_GE(new_name_count_, names.size()); |
145 new_name_count_ -= names.size(); | 145 new_name_count_ -= names.size(); |
146 | 146 |
147 dns_prefetch::LookupRequest request; | 147 network_hints::LookupRequest request; |
148 request.hostname_list = names; | 148 request.hostname_list = names; |
149 RenderThread::Get()->Send(new DnsPrefetchMsg_RequestPrefetch(request)); | 149 RenderThread::Get()->Send(new DnsPrefetchMsg_RequestPrefetch(request)); |
150 } | 150 } |
151 | 151 |
152 // is_numeric_ip() checks to see if all characters in name are either numeric, | 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 | 153 // or dots. Such a name will not actually be passed to DNS, as it is an IP |
154 // address. | 154 // address. |
155 bool RendererNetPredictor::is_numeric_ip(const char* name, size_t length) { | 155 bool RendererDnsPrefetch::is_numeric_ip(const char* name, size_t length) { |
156 // Scan for a character outside our lookup list. | 156 // Scan for a character outside our lookup list. |
157 while (length-- > 0) { | 157 while (length-- > 0) { |
158 if (!isdigit(*name) && '.' != *name) | 158 if (!isdigit(*name) && '.' != *name) |
159 return false; | 159 return false; |
160 ++name; | 160 ++name; |
161 } | 161 } |
162 return true; | 162 return true; |
163 } | 163 } |
164 | 164 |
165 } // namespcae predictor | 165 } // namespcae predictor |
OLD | NEW |