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. 4 5 // See header file for description of RendererNetPredictor class 6 7 #include "chrome/renderer/net/renderer_net_predictor.h" 8 9 #include <ctype.h> 10 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" 18 19 using content::RenderThread; 20 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; 24 25 RendererNetPredictor::RendererNetPredictor() 26 : c_string_queue_(1000), 27 weak_factory_(this) { 28 Reset(); 29 } 30 31 RendererNetPredictor::~RendererNetPredictor() { 32 } 33 34 void RendererNetPredictor::Reset() { 35 domain_map_.clear(); 36 c_string_queue_.Clear(); 37 buffer_full_discard_count_ = 0; 38 numeric_ip_discard_count_ = 0; 39 new_name_count_ = 0; 40 } 41 42 // Push names into queue quickly! 43 void RendererNetPredictor::Resolve(const char* name, size_t length) { 44 if (!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. 48 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); 54 if (0 != old_size) 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)); 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 weak_factory_.InvalidateWeakPtrs(); 92 RenderThread::Get()->GetMessageLoop()->PostDelayedTask( 93 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames, 94 weak_factory_.GetWeakPtr()), 95 base::TimeDelta::FromMilliseconds(10)); 96 } else { 97 // TODO(JAR): Should we only clear the map when we navigate, or reload? 98 domain_map_.clear(); 99 } 100 } 101 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). 105 if (size_goal > 0) { 106 if (size_goal <= domain_map_.size()) 107 return; // Size goal was met. 108 count = size_goal - domain_map_.size(); 109 } 110 111 std::string name; 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; 120 ++new_name_count_; 121 if (0 == count) continue; // Until buffer is empty. 122 if (1 == count) break; // We found size_goal. 123 DCHECK_GT(count, 1u); 124 --count; 125 } else { 126 DCHECK(kPending == it->second || kLookupRequested == it->second); 127 } 128 } 129 } 130 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(); 136 ++it) { 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; 142 --max_count; 143 DCHECK_GE(max_count, 1u); 144 } 145 } 146 DCHECK_GE(new_name_count_, names.size()); 147 new_name_count_ -= names.size(); 148 149 RenderThread::Get()->Send(new ChromeViewHostMsg_DnsPrefetch(names)); 150 } 151 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 154 // address. 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) 159 return false; 160 ++name; 161 } 162 return true; 163 } 164