This source file includes following definitions.
- weak_factory_
- Reset
- Resolve
- SubmitHostnames
- ExtractBufferedNames
- DnsPrefetchNames
- is_numeric_ip
#include "chrome/renderer/net/renderer_net_predictor.h"
#include <ctype.h>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "chrome/common/net/predictor_common.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/net/predictor_queue.h"
#include "content/public/renderer/render_thread.h"
using content::RenderThread;
static const size_t kMAX_SUBMISSION_PER_TASK = 30;
RendererNetPredictor::RendererNetPredictor()
: c_string_queue_(1000),
weak_factory_(this) {
Reset();
}
RendererNetPredictor::~RendererNetPredictor() {
}
void RendererNetPredictor::Reset() {
domain_map_.clear();
c_string_queue_.Clear();
buffer_full_discard_count_ = 0;
numeric_ip_discard_count_ = 0;
new_name_count_ = 0;
}
void RendererNetPredictor::Resolve(const char* name, size_t length) {
if (!length)
return;
if (is_numeric_ip(name, length))
return;
size_t old_size = c_string_queue_.Size();
DnsQueue::PushResult result = c_string_queue_.Push(name, length);
if (DnsQueue::SUCCESSFUL_PUSH == result) {
if (1 == c_string_queue_.Size()) {
DCHECK_EQ(old_size, 0u);
if (0 != old_size)
return;
weak_factory_.InvalidateWeakPtrs();
RenderThread::Get()->GetMessageLoop()->PostDelayedTask(
FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(10));
}
return;
}
if (DnsQueue::OVERFLOW_PUSH == result) {
++buffer_full_discard_count_;
return;
}
DCHECK(DnsQueue::REDUNDANT_PUSH == result);
}
void RendererNetPredictor::SubmitHostnames() {
ExtractBufferedNames();
DnsPrefetchNames(kMAX_SUBMISSION_PER_TASK);
if (new_name_count_ > 0 || 0 < c_string_queue_.Size()) {
weak_factory_.InvalidateWeakPtrs();
RenderThread::Get()->GetMessageLoop()->PostDelayedTask(
FROM_HERE, base::Bind(&RendererNetPredictor::SubmitHostnames,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(10));
} else {
domain_map_.clear();
}
}
void RendererNetPredictor::ExtractBufferedNames(size_t size_goal) {
size_t count(0);
if (size_goal > 0) {
if (size_goal <= domain_map_.size())
return;
count = size_goal - domain_map_.size();
}
std::string name;
while (c_string_queue_.Pop(&name)) {
DCHECK_NE(name.size(), 0u);
DCHECK(!is_numeric_ip(name.c_str(), name.size()));
DomainUseMap::iterator it;
it = domain_map_.find(name);
if (domain_map_.end() == it) {
domain_map_[name] = kPending;
++new_name_count_;
if (0 == count) continue;
if (1 == count) break;
DCHECK_GT(count, 1u);
--count;
} else {
DCHECK(kPending == it->second || kLookupRequested == it->second);
}
}
}
void RendererNetPredictor::DnsPrefetchNames(size_t max_count) {
chrome_common_net::NameList names;
for (DomainUseMap::iterator it = domain_map_.begin();
it != domain_map_.end();
++it) {
if (0 == (it->second & kLookupRequested)) {
it->second |= kLookupRequested;
names.push_back(it->first);
if (0 == max_count) continue;
if (1 == max_count) break;
--max_count;
DCHECK_GE(max_count, 1u);
}
}
DCHECK_GE(new_name_count_, names.size());
new_name_count_ -= names.size();
RenderThread::Get()->Send(new ChromeViewHostMsg_DnsPrefetch(names));
}
bool RendererNetPredictor::is_numeric_ip(const char* name, size_t length) {
while (length-- > 0) {
if (!isdigit(*name) && '.' != *name)
return false;
++name;
}
return true;
}