This source file includes following definitions.
- GetInstance
- AppendHeaders
- SetDefaultVariationIds
- OnFieldTrialGroupFinalized
- InitVariationIDsCacheIfNeeded
- UpdateVariationIDsHeaderValue
- ShouldAppendHeaders
#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
#include <vector>
#include "base/base64.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/common/metrics/proto/chrome_experiments.pb.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/http/http_request_headers.h"
#include "url/gurl.h"
namespace chrome_variations {
VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() {
return Singleton<VariationsHttpHeaderProvider>::get();
}
void VariationsHttpHeaderProvider::AppendHeaders(
const GURL& url,
bool incognito,
bool uma_enabled,
net::HttpRequestHeaders* headers) {
if (incognito || !ShouldAppendHeaders(url))
return;
if (uma_enabled)
headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1");
InitVariationIDsCacheIfNeeded();
std::string variation_ids_header_copy;
{
base::AutoLock scoped_lock(lock_);
variation_ids_header_copy = variation_ids_header_;
}
if (!variation_ids_header_copy.empty()) {
headers->SetHeaderIfMissing("X-Client-Data",
variation_ids_header_copy);
}
}
bool VariationsHttpHeaderProvider::SetDefaultVariationIds(
const std::string& variation_ids) {
default_variation_ids_set_.clear();
default_trigger_id_set_.clear();
std::vector<std::string> entries;
base::SplitString(variation_ids, ',', &entries);
for (std::vector<std::string>::const_iterator it = entries.begin();
it != entries.end(); ++it) {
if (it->empty()) {
default_variation_ids_set_.clear();
default_trigger_id_set_.clear();
return false;
}
bool trigger_id = StartsWithASCII(*it, "t", true);
std::string entry = trigger_id ? it->substr(1) : *it;
int variation_id = 0;
if (!base::StringToInt(entry, &variation_id)) {
default_variation_ids_set_.clear();
default_trigger_id_set_.clear();
return false;
}
if (trigger_id)
default_trigger_id_set_.insert(variation_id);
else
default_variation_ids_set_.insert(variation_id);
}
return true;
}
VariationsHttpHeaderProvider::VariationsHttpHeaderProvider()
: variation_ids_cache_initialized_(false) {
}
VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {
}
void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized(
const std::string& trial_name,
const std::string& group_name) {
VariationID new_id =
GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name);
if (new_id == EMPTY_ID)
return;
base::AutoLock scoped_lock(lock_);
variation_ids_set_.insert(new_id);
UpdateVariationIDsHeaderValue();
}
void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() {
base::AutoLock scoped_lock(lock_);
if (variation_ids_cache_initialized_)
return;
DCHECK(base::MessageLoop::current());
base::FieldTrialList::AddObserver(this);
base::TimeTicks before_time = base::TimeTicks::Now();
base::FieldTrial::ActiveGroups initial_groups;
base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
for (base::FieldTrial::ActiveGroups::const_iterator it =
initial_groups.begin();
it != initial_groups.end(); ++it) {
const VariationID id =
GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, it->trial_name,
it->group_name);
if (id != EMPTY_ID)
variation_ids_set_.insert(id);
}
UpdateVariationIDsHeaderValue();
UMA_HISTOGRAM_CUSTOM_COUNTS(
"Variations.HeaderConstructionTime",
(base::TimeTicks::Now() - before_time).InMicroseconds(),
0,
base::TimeDelta::FromSeconds(1).InMicroseconds(),
50);
variation_ids_cache_initialized_ = true;
}
void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() {
lock_.AssertAcquired();
variation_ids_header_.clear();
if (variation_ids_set_.empty() && default_variation_ids_set_.empty() &&
default_trigger_id_set_.empty()) {
return;
}
DCHECK_LE(variation_ids_set_.size(), 10U);
if (variation_ids_set_.size() > 20)
return;
std::set<VariationID> all_variation_ids_set = default_variation_ids_set_;
for (std::set<VariationID>::const_iterator it = variation_ids_set_.begin();
it != variation_ids_set_.end(); ++it) {
all_variation_ids_set.insert(*it);
}
metrics::ChromeVariations proto;
for (std::set<VariationID>::const_iterator it = all_variation_ids_set.begin();
it != all_variation_ids_set.end(); ++it) {
proto.add_variation_id(*it);
}
for (std::set<VariationID>::const_iterator it =
default_trigger_id_set_.begin();
it != default_trigger_id_set_.end(); ++it) {
proto.add_trigger_variation_id(*it);
}
std::string serialized;
proto.SerializeToString(&serialized);
std::string hashed;
base::Base64Encode(serialized, &hashed);
variation_ids_header_ = hashed;
}
bool VariationsHttpHeaderProvider::ShouldAppendHeaders(const GURL& url) {
if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS)) {
return true;
}
if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS())
return false;
const std::string host = url.host();
if (EndsWith(host, ".doubleclick.net", false) ||
EndsWith(host, ".googlesyndication.com", false) ||
LowerCaseEqualsASCII(host, "www.googleadservices.com")) {
return true;
}
const size_t tld_length = net::registry_controlled_domains::GetRegistryLength(
host,
net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
if ((tld_length == 0) || (tld_length == std::string::npos))
return false;
const std::string host_minus_tld(host, 0, host.length() - tld_length);
return LowerCaseEqualsASCII(host_minus_tld, "youtube.") ||
EndsWith(host_minus_tld, ".youtube.", false);
}
}