Correctly track texture cleared state for sharing
[chromium-blink-merge.git] / components / dns_prefetch / renderer / renderer_net_predictor.cc
blob273e3e1994a9fecfa4cea1b781831f456b5ab47e
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 "components/dns_prefetch/renderer/renderer_net_predictor.h"
9 #include <ctype.h>
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "components/dns_prefetch/common/prefetch_common.h"
15 #include "components/dns_prefetch/common/prefetch_messages.h"
16 #include "components/dns_prefetch/renderer/predictor_queue.h"
17 #include "content/public/renderer/render_thread.h"
19 using content::RenderThread;
21 namespace dns_prefetch {
23 RendererNetPredictor::RendererNetPredictor()
24 : c_string_queue_(1000),
25 weak_factory_(this) {
26 Reset();
29 RendererNetPredictor::~RendererNetPredictor() {
32 void RendererNetPredictor::Reset() {
33 domain_map_.clear();
34 c_string_queue_.Clear();
35 buffer_full_discard_count_ = 0;
36 numeric_ip_discard_count_ = 0;
37 new_name_count_ = 0;
40 // Push names into queue quickly!
41 void RendererNetPredictor::Resolve(const char* name, size_t length) {
42 if (!length)
43 return; // Don't store empty strings in buffer.
44 if (is_numeric_ip(name, length))
45 return; // Numeric IPs have no DNS lookup significance.
47 size_t old_size = c_string_queue_.Size();
48 DnsQueue::PushResult result = c_string_queue_.Push(name, length);
49 if (DnsQueue::SUCCESSFUL_PUSH == result) {
50 if (1 == c_string_queue_.Size()) {
51 DCHECK_EQ(old_size, 0u);
52 if (0 != old_size)
53 return; // Overkill safety net: Don't send too many InvokeLater's.
54 weak_factory_.InvalidateWeakPtrs();
55 RenderThread::Get()->GetTaskRunner()->PostDelayedTask(
56 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames,
57 weak_factory_.GetWeakPtr()),
58 base::TimeDelta::FromMilliseconds(10));
60 return;
62 if (DnsQueue::OVERFLOW_PUSH == result) {
63 ++buffer_full_discard_count_;
64 return;
66 DCHECK(DnsQueue::REDUNDANT_PUSH == result);
69 // Extract data from the Queue, and then send it off the the Browser process
70 // to be resolved.
71 void RendererNetPredictor::SubmitHostnames() {
72 // Get all names out of the C_string_queue (into our map)
73 ExtractBufferedNames();
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
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
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
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
82 // page.
84 // Don't overload the browser DNS lookup facility, or take too long here,
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.
87 DnsPrefetchNames(kMaxDnsHostnamesPerRequest);
88 if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) {
89 weak_factory_.InvalidateWeakPtrs();
90 RenderThread::Get()->GetTaskRunner()->PostDelayedTask(
91 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames,
92 weak_factory_.GetWeakPtr()),
93 base::TimeDelta::FromMilliseconds(10));
94 } else {
95 // TODO(JAR): Should we only clear the map when we navigate, or reload?
96 domain_map_.clear();
100 // Pull some hostnames from the queue, and add them to our map.
101 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) {
102 size_t count(0); // Number of entries to find (0 means find all).
103 if (size_goal > 0) {
104 if (size_goal <= domain_map_.size())
105 return; // Size goal was met.
106 count = size_goal - domain_map_.size();
109 std::string name;
110 while (c_string_queue_.Pop(&name)) {
111 DCHECK_NE(name.size(), 0u);
112 // We don't put numeric IP names into buffer.
113 DCHECK(!is_numeric_ip(name.c_str(), name.size()));
114 DomainUseMap::iterator it;
115 it = domain_map_.find(name);
116 if (domain_map_.end() == it) {
117 domain_map_[name] = kPending;
118 ++new_name_count_;
119 if (0 == count) continue; // Until buffer is empty.
120 if (1 == count) break; // We found size_goal.
121 DCHECK_GT(count, 1u);
122 --count;
123 } else {
124 DCHECK(kPending == it->second || kLookupRequested == it->second);
129 void RendererNetPredictor::DnsPrefetchNames(size_t max_count) {
130 // We are on the renderer thread, and just need to send things to the browser.
131 NameList names;
132 for (DomainUseMap::iterator it = domain_map_.begin();
133 it != domain_map_.end();
134 ++it) {
135 if (0 == (it->second & kLookupRequested)) {
136 it->second |= kLookupRequested;
137 names.push_back(it->first);
138 if (0 == max_count) continue; // Get all, independent of count.
139 if (1 == max_count) break;
140 --max_count;
141 DCHECK_GE(max_count, 1u);
144 DCHECK_GE(new_name_count_, names.size());
145 new_name_count_ -= names.size();
147 dns_prefetch::LookupRequest request;
148 request.hostname_list = names;
149 RenderThread::Get()->Send(new DnsPrefetchMsg_RequestPrefetch(request));
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;
162 return true;
165 } // namespcae predictor