This source file includes following definitions.
- MaybeShuffle
- IsDontShowOpenURLsEnabled
- RemoveItemsMatchingOpenTabs
- RemovePageValuesMatchingOpenTabs
- LogInHistogram
- ShouldRemoveURL
#include "chrome/browser/history/most_visited_tiles_experiment.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "chrome/common/instant_types.h"
namespace history {
namespace {
const char kMostVisitedFieldTrialName[] = "MostVisitedTilePlacement";
const char kMostVisitedExperimentHistogramName[] =
"NewTabPage.MostVisitedTilePlacementExperiment";
const char kOneEightGroupPrefix[] = "OneEight";
const char kOneFourGroupPrefix[] = "OneFour";
const char kFlippedSuffix[] = "Flipped";
const char kDontShowOpenURLsGroupName[] = "DontShowOpenTabs";
const size_t kMinUrlSuggestions = 8;
}
void MostVisitedTilesExperiment::MaybeShuffle(MostVisitedURLList* data) {
const std::string group_name =
base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName);
if (!EndsWith(group_name, kFlippedSuffix, true))
return;
size_t index_to_flip = 0;
if (StartsWithASCII(group_name, kOneEightGroupPrefix, true)) {
if (data->size() < 8) {
LogInHistogram(NTP_TILE_EXPERIMENT_ACTION_TOO_FEW_URLS_TILES_1_8);
return;
}
index_to_flip = 7;
} else if (StartsWithASCII(group_name, kOneFourGroupPrefix, true)) {
if (data->size() < 4) {
LogInHistogram(NTP_TILE_EXPERIMENT_ACTION_TOO_FEW_URLS_TILES_1_4);
return;
}
index_to_flip = 3;
}
std::swap((*data)[0], (*data)[index_to_flip]);
}
bool MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled() {
return base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName) ==
kDontShowOpenURLsGroupName;
}
void MostVisitedTilesExperiment::RemoveItemsMatchingOpenTabs(
const std::set<std::string>& open_urls,
std::vector<InstantMostVisitedItem>* items) {
for (size_t i = 0; i < items->size(); ) {
const std::string& url = (*items)[i].url.spec();
if (ShouldRemoveURL(open_urls, url, items->size()))
items->erase(items->begin() + i);
else
++i;
}
}
void MostVisitedTilesExperiment::RemovePageValuesMatchingOpenTabs(
const std::set<std::string>& open_urls,
base::ListValue* pages_value) {
for (size_t i = 0; i < pages_value->GetSize(); ) {
base::DictionaryValue* page_value;
std::string url;
if (pages_value->GetDictionary(i, &page_value) &&
page_value->GetString("url", &url) &&
ShouldRemoveURL(open_urls, url, pages_value->GetSize())) {
pages_value->Remove(*page_value, &i);
} else {
++i;
}
}
}
void MostVisitedTilesExperiment::LogInHistogram(
NtpTileExperimentActions action) {
UMA_HISTOGRAM_ENUMERATION(kMostVisitedExperimentHistogramName,
action,
NUM_NTP_TILE_EXPERIMENT_ACTIONS);
}
bool MostVisitedTilesExperiment::ShouldRemoveURL(
const std::set<std::string>& open_urls,
const std::string& url,
const size_t size) {
if (open_urls.count(url) == 0)
return false;
if (size <= kMinUrlSuggestions) {
LogInHistogram(NTP_TILE_EXPERIMENT_ACTION_DID_NOT_REMOVE_URL);
return false;
}
LogInHistogram(NTP_TILE_EXPERIMENT_ACTION_REMOVED_URL);
return true;
}
}