This source file includes following definitions.
- SuggestHost
- DeleteLeastUseful
- Trim
- Trim
- Deserialize
- Serialize
- subresource_use_rate_
- SubresourceIsNeeded
- ReferrerWasObserved
#include "chrome/browser/net/referrer.h"
#include <limits.h>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "chrome/browser/net/predictor.h"
namespace chrome_browser_net {
static const double kWeightingForOldConnectsExpectedValue = 0.66;
static const double kInitialConnectsExpectedValue = 2.0;
Referrer::Referrer() : use_count_(1) {}
void Referrer::SuggestHost(const GURL& url) {
static const size_t kMaxSuggestions = 10;
if (!url.has_host())
return;
DCHECK(url == url.GetWithEmptyPath());
SubresourceMap::iterator it = find(url);
if (it != end()) {
it->second.SubresourceIsNeeded();
return;
}
if (kMaxSuggestions <= size()) {
DeleteLeastUseful();
DCHECK(kMaxSuggestions > size());
}
(*this)[url].SubresourceIsNeeded();
}
void Referrer::DeleteLeastUseful() {
GURL least_useful_url;
double lowest_rate_seen = 0.0;
int64 least_useful_lifetime = 0;
const base::Time kNow(base::Time::Now());
for (SubresourceMap::iterator it = begin(); it != end(); ++it) {
int64 lifetime = (kNow - it->second.birth_time()).InMilliseconds();
double rate = it->second.subresource_use_rate();
if (least_useful_url.has_host()) {
if (rate > lowest_rate_seen)
continue;
if (lifetime <= least_useful_lifetime)
continue;
}
least_useful_url = it->first;
lowest_rate_seen = rate;
least_useful_lifetime = lifetime;
}
if (least_useful_url.has_host())
erase(least_useful_url);
}
bool Referrer::Trim(double reduce_rate, double threshold) {
std::vector<GURL> discarded_urls;
for (SubresourceMap::iterator it = begin(); it != end(); ++it) {
if (!it->second.Trim(reduce_rate, threshold))
discarded_urls.push_back(it->first);
}
for (size_t i = 0; i < discarded_urls.size(); ++i)
erase(discarded_urls[i]);
return size() > 0;
}
bool ReferrerValue::Trim(double reduce_rate, double threshold) {
subresource_use_rate_ *= reduce_rate;
return subresource_use_rate_ > threshold;
}
void Referrer::Deserialize(const base::Value& value) {
if (value.GetType() != base::Value::TYPE_LIST)
return;
const base::ListValue* subresource_list(
static_cast<const base::ListValue*>(&value));
size_t index = 0;
while (true) {
std::string url_spec;
if (!subresource_list->GetString(index++, &url_spec))
return;
double rate;
if (!subresource_list->GetDouble(index++, &rate))
return;
GURL url(url_spec);
SuggestHost(url);
(*this)[url].SetSubresourceUseRate(rate);
}
}
base::Value* Referrer::Serialize() const {
base::ListValue* subresource_list(new base::ListValue);
for (const_iterator it = begin(); it != end(); ++it) {
base::StringValue* url_spec(new base::StringValue(it->first.spec()));
base::FundamentalValue* rate(new base::FundamentalValue(
it->second.subresource_use_rate()));
subresource_list->Append(url_spec);
subresource_list->Append(rate);
}
return subresource_list;
}
ReferrerValue::ReferrerValue()
: birth_time_(base::Time::Now()),
navigation_count_(0),
preconnection_count_(0),
preresolution_count_(0),
subresource_use_rate_(kInitialConnectsExpectedValue) {
}
void ReferrerValue::SubresourceIsNeeded() {
DCHECK_GE(kWeightingForOldConnectsExpectedValue, 0);
DCHECK_LE(kWeightingForOldConnectsExpectedValue, 1.0);
++navigation_count_;
subresource_use_rate_ += 1 - kWeightingForOldConnectsExpectedValue;
}
void ReferrerValue::ReferrerWasObserved() {
subresource_use_rate_ *= kWeightingForOldConnectsExpectedValue;
}
}