OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 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 | 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 RendererNetPredictor class |
6 | 6 |
7 #include "chrome/renderer/net/renderer_net_predictor.h" | 7 #include "chrome/renderer/net/renderer_net_predictor.h" |
8 | 8 |
9 #include <ctype.h> | 9 #include <ctype.h> |
10 | 10 |
| 11 #include "base/bind.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
13 #include "chrome/common/net/predictor_common.h" | 14 #include "chrome/common/net/predictor_common.h" |
14 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
15 #include "chrome/renderer/net/predictor_queue.h" | 16 #include "chrome/renderer/net/predictor_queue.h" |
16 #include "content/public/renderer/render_thread.h" | 17 #include "content/public/renderer/render_thread.h" |
17 | 18 |
18 using content::RenderThread; | 19 using content::RenderThread; |
19 | 20 |
20 // The number of hostnames submitted to Browser DNS resolver per call to | 21 // The number of hostnames submitted to Browser DNS resolver per call to |
21 // SubmitHostsnames() (which reads names from our queue). | 22 // SubmitHostsnames() (which reads names from our queue). |
22 static const size_t kMAX_SUBMISSION_PER_TASK = 30; | 23 static const size_t kMAX_SUBMISSION_PER_TASK = 30; |
23 | 24 |
24 RendererNetPredictor::RendererNetPredictor() | 25 RendererNetPredictor::RendererNetPredictor() |
25 : c_string_queue_(1000), | 26 : c_string_queue_(1000), |
26 ALLOW_THIS_IN_INITIALIZER_LIST(renderer_predictor_factory_(this)) { | 27 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
27 Reset(); | 28 Reset(); |
28 } | 29 } |
29 | 30 |
30 RendererNetPredictor::~RendererNetPredictor() { | 31 RendererNetPredictor::~RendererNetPredictor() { |
31 } | 32 } |
32 | 33 |
33 void RendererNetPredictor::Reset() { | 34 void RendererNetPredictor::Reset() { |
34 domain_map_.clear(); | 35 domain_map_.clear(); |
35 c_string_queue_.Clear(); | 36 c_string_queue_.Clear(); |
36 buffer_full_discard_count_ = 0; | 37 buffer_full_discard_count_ = 0; |
37 numeric_ip_discard_count_ = 0; | 38 numeric_ip_discard_count_ = 0; |
38 new_name_count_ = 0; | 39 new_name_count_ = 0; |
39 } | 40 } |
40 | 41 |
41 // Push names into queue quickly! | 42 // Push names into queue quickly! |
42 void RendererNetPredictor::Resolve(const char* name, size_t length) { | 43 void RendererNetPredictor::Resolve(const char* name, size_t length) { |
43 if (!length) | 44 if (!length) |
44 return; // Don't store empty strings in buffer. | 45 return; // Don't store empty strings in buffer. |
45 if (is_numeric_ip(name, length)) | 46 if (is_numeric_ip(name, length)) |
46 return; // Numeric IPs have no DNS lookup significance. | 47 return; // Numeric IPs have no DNS lookup significance. |
47 | 48 |
48 size_t old_size = c_string_queue_.Size(); | 49 size_t old_size = c_string_queue_.Size(); |
49 DnsQueue::PushResult result = c_string_queue_.Push(name, length); | 50 DnsQueue::PushResult result = c_string_queue_.Push(name, length); |
50 if (DnsQueue::SUCCESSFUL_PUSH == result) { | 51 if (DnsQueue::SUCCESSFUL_PUSH == result) { |
51 if (1 == c_string_queue_.Size()) { | 52 if (1 == c_string_queue_.Size()) { |
52 DCHECK_EQ(old_size, 0u); | 53 DCHECK_EQ(old_size, 0u); |
53 if (0 != old_size) | 54 if (0 != old_size) |
54 return; // Overkill safety net: Don't send too many InvokeLater's. | 55 return; // Overkill safety net: Don't send too many InvokeLater's. |
55 renderer_predictor_factory_.RevokeAll(); | 56 weak_factory_.InvalidateWeakPtrs(); |
56 RenderThread::Get()->GetMessageLoop()->PostDelayedTask(FROM_HERE, | 57 RenderThread::Get()->GetMessageLoop()->PostDelayedTask( |
57 renderer_predictor_factory_.NewRunnableMethod( | 58 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames, |
58 &RendererNetPredictor::SubmitHostnames), 10); | 59 weak_factory_.GetWeakPtr()), |
| 60 10); |
59 } | 61 } |
60 return; | 62 return; |
61 } | 63 } |
62 if (DnsQueue::OVERFLOW_PUSH == result) { | 64 if (DnsQueue::OVERFLOW_PUSH == result) { |
63 ++buffer_full_discard_count_; | 65 ++buffer_full_discard_count_; |
64 return; | 66 return; |
65 } | 67 } |
66 DCHECK(DnsQueue::REDUNDANT_PUSH == result); | 68 DCHECK(DnsQueue::REDUNDANT_PUSH == result); |
67 } | 69 } |
68 | 70 |
(...skipping 10 matching lines...) Expand all Loading... |
79 // longer than the page may be visible!?!?! If we implement a better | 81 // 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 | 82 // 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 | 83 // we'd automatically flush such pending work from a ridiculously link-filled |
82 // page. | 84 // page. |
83 | 85 |
84 // Don't overload the browser DNS lookup facility, or take too long here, | 86 // Don't overload the browser DNS lookup facility, or take too long here, |
85 // by only sending off kMAX_SUBMISSION_PER_TASK names to the Browser. | 87 // by only sending off kMAX_SUBMISSION_PER_TASK names to the Browser. |
86 // This will help to avoid overloads when a page has a TON of links. | 88 // This will help to avoid overloads when a page has a TON of links. |
87 DnsPrefetchNames(kMAX_SUBMISSION_PER_TASK); | 89 DnsPrefetchNames(kMAX_SUBMISSION_PER_TASK); |
88 if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) { | 90 if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) { |
89 renderer_predictor_factory_.RevokeAll(); | 91 weak_factory_.InvalidateWeakPtrs(); |
90 RenderThread::Get()->GetMessageLoop()->PostDelayedTask(FROM_HERE, | 92 RenderThread::Get()->GetMessageLoop()->PostDelayedTask( |
91 renderer_predictor_factory_.NewRunnableMethod( | 93 FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames, |
92 &RendererNetPredictor::SubmitHostnames), 10); | 94 weak_factory_.GetWeakPtr()), |
| 95 10); |
93 } else { | 96 } else { |
94 // TODO(JAR): Should we only clear the map when we navigate, or reload? | 97 // TODO(JAR): Should we only clear the map when we navigate, or reload? |
95 domain_map_.clear(); | 98 domain_map_.clear(); |
96 } | 99 } |
97 } | 100 } |
98 | 101 |
99 // Pull some hostnames from the queue, and add them to our map. | 102 // Pull some hostnames from the queue, and add them to our map. |
100 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) { | 103 void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) { |
101 size_t count(0); // Number of entries to find (0 means find all). | 104 size_t count(0); // Number of entries to find (0 means find all). |
102 if (size_goal > 0) { | 105 if (size_goal > 0) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // address. | 154 // address. |
152 bool RendererNetPredictor::is_numeric_ip(const char* name, size_t length) { | 155 bool RendererNetPredictor::is_numeric_ip(const char* name, size_t length) { |
153 // Scan for a character outside our lookup list. | 156 // Scan for a character outside our lookup list. |
154 while (length-- > 0) { | 157 while (length-- > 0) { |
155 if (!isdigit(*name) && '.' != *name) | 158 if (!isdigit(*name) && '.' != *name) |
156 return false; | 159 return false; |
157 ++name; | 160 ++name; |
158 } | 161 } |
159 return true; | 162 return true; |
160 } | 163 } |
OLD | NEW |