| 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 RendererDnsPrefetch class | 5 // See header file for description of RendererDnsPrefetch class |
| 6 | 6 |
| 7 #include "components/network_hints/renderer/renderer_dns_prefetch.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/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/network_hints/common/network_hints_common.h" | 16 #include "components/network_hints/common/network_hints_common.h" |
| 17 #include "components/network_hints/common/network_hints_messages.h" | |
| 18 #include "components/network_hints/renderer/dns_prefetch_queue.h" | 17 #include "components/network_hints/renderer/dns_prefetch_queue.h" |
| 19 #include "content/public/renderer/render_thread.h" | 18 #include "content/public/renderer/render_thread.h" |
| 19 #include "services/shell/public/cpp/interface_provider.h" |
| 20 | 20 |
| 21 using content::RenderThread; | 21 using content::RenderThread; |
| 22 | 22 |
| 23 namespace network_hints { | 23 namespace network_hints { |
| 24 | 24 |
| 25 RendererDnsPrefetch::RendererDnsPrefetch() | 25 RendererDnsPrefetch::RendererDnsPrefetch() |
| 26 : c_string_queue_(1000), | 26 : c_string_queue_(1000), |
| 27 weak_factory_(this) { | 27 weak_factory_(this) { |
| 28 Reset(); | 28 Reset(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 RendererDnsPrefetch::~RendererDnsPrefetch() { | 31 RendererDnsPrefetch::~RendererDnsPrefetch() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 void RendererDnsPrefetch::Reset() { | 34 void RendererDnsPrefetch::Reset() { |
| 35 domain_map_.clear(); | 35 domain_map_.clear(); |
| 36 c_string_queue_.Clear(); | 36 c_string_queue_.Clear(); |
| 37 buffer_full_discard_count_ = 0; | 37 buffer_full_discard_count_ = 0; |
| 38 numeric_ip_discard_count_ = 0; | 38 numeric_ip_discard_count_ = 0; |
| 39 new_name_count_ = 0; | 39 new_name_count_ = 0; |
| 40 } | 40 } |
| 41 | 41 |
| 42 mojom::NetworkHints& RendererDnsPrefetch::GetNetworkHints() { |
| 43 DCHECK(content::RenderThread::Get()); |
| 44 if (!network_hints_) { |
| 45 RenderThread::Get()->GetRemoteInterfaces()->GetInterface( |
| 46 mojo::GetProxy(&network_hints_)); |
| 47 } |
| 48 return *network_hints_; |
| 49 } |
| 50 |
| 42 // Push names into queue quickly! | 51 // Push names into queue quickly! |
| 43 void RendererDnsPrefetch::Resolve(const char* name, size_t length) { | 52 void RendererDnsPrefetch::Resolve(const char* name, size_t length) { |
| 44 DCHECK(content::RenderThread::Get()); | 53 DCHECK(content::RenderThread::Get()); |
| 45 if (!length) | 54 if (!length) |
| 46 return; // Don't store empty strings in buffer. | 55 return; // Don't store empty strings in buffer. |
| 47 if (is_numeric_ip(name, length)) | 56 if (is_numeric_ip(name, length)) |
| 48 return; // Numeric IPs have no DNS lookup significance. | 57 return; // Numeric IPs have no DNS lookup significance. |
| 49 | 58 |
| 50 size_t old_size = c_string_queue_.Size(); | 59 size_t old_size = c_string_queue_.Size(); |
| 51 DnsQueue::PushResult result = c_string_queue_.Push(name, length); | 60 DnsQueue::PushResult result = c_string_queue_.Push(name, length); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 if (1 == max_count) break; | 155 if (1 == max_count) break; |
| 147 --max_count; | 156 --max_count; |
| 148 DCHECK_GE(max_count, 1u); | 157 DCHECK_GE(max_count, 1u); |
| 149 } | 158 } |
| 150 } | 159 } |
| 151 DCHECK_GE(new_name_count_, domains_handled); | 160 DCHECK_GE(new_name_count_, domains_handled); |
| 152 new_name_count_ -= domains_handled; | 161 new_name_count_ -= domains_handled; |
| 153 | 162 |
| 154 network_hints::LookupRequest request; | 163 network_hints::LookupRequest request; |
| 155 request.hostname_list = names; | 164 request.hostname_list = names; |
| 156 RenderThread::Get()->Send(new NetworkHintsMsg_DNSPrefetch(request)); | 165 GetNetworkHints().DNSPrefetch(request); |
| 157 } | 166 } |
| 158 | 167 |
| 159 // is_numeric_ip() checks to see if all characters in name are either numeric, | 168 // is_numeric_ip() checks to see if all characters in name are either numeric, |
| 160 // or dots. Such a name will not actually be passed to DNS, as it is an IP | 169 // or dots. Such a name will not actually be passed to DNS, as it is an IP |
| 161 // address. | 170 // address. |
| 162 bool RendererDnsPrefetch::is_numeric_ip(const char* name, size_t length) { | 171 bool RendererDnsPrefetch::is_numeric_ip(const char* name, size_t length) { |
| 163 // Scan for a character outside our lookup list. | 172 // Scan for a character outside our lookup list. |
| 164 while (length-- > 0) { | 173 while (length-- > 0) { |
| 165 if (!isdigit(*name) && '.' != *name) | 174 if (!isdigit(*name) && '.' != *name) |
| 166 return false; | 175 return false; |
| 167 ++name; | 176 ++name; |
| 168 } | 177 } |
| 169 return true; | 178 return true; |
| 170 } | 179 } |
| 171 | 180 |
| 172 } // namespace predictor | 181 } // namespace predictor |
| OLD | NEW |