Commit 92f0bcfc authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Add field trial name/group to hints request

Bug: 1112970
Change-Id: I6d8fbbb236a185bec828d11720407b66aee3f8c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2337287
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Reviewed-by: default avatarMichael Crouse <mcrouse@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795755}
parent 6562db31
......@@ -41,6 +41,7 @@
#include "components/optimization_guide/top_host_provider.h"
#include "components/prefs/pref_service.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/variations/hashing.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
......@@ -457,6 +458,12 @@ class HintsFetcherDisabledBrowserTest : public InProcessBrowserTest {
hosts_and_urls_requested.erase(host_or_url);
}
EXPECT_EQ(0u, hosts_and_urls_requested.size());
// We only expect 1 field trial to be allowed and sent up.
EXPECT_EQ(1, hints_request.active_field_trials_size());
EXPECT_EQ(variations::HashName(
"scoped_feature_list_trial_for_OptimizationHintsFetching"),
hints_request.active_field_trials(0).name_hash());
}
void TearDownOnMainThread() override {
......@@ -509,6 +516,9 @@ class HintsFetcherBrowserTest : public HintsFetcherDisabledBrowserTest {
{optimization_guide::features::kOptimizationHints, {}},
{optimization_guide::features::kRemoteOptimizationGuideFetching,
{{"max_concurrent_page_navigation_fetches", "2"}}},
{optimization_guide::features::kOptimizationHintsFieldTrials,
{{"allowed_field_trial_names",
"scoped_feature_list_trial_for_OptimizationHintsFetching"}}},
},
{});
// Call to inherited class to match same set up with feature flags added.
......
......@@ -17,6 +17,7 @@
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/variations/active_field_trials.h"
#include "components/variations/net/variations_http_headers.h"
#include "content/public/browser/network_service_instance.h"
#include "net/base/load_flags.h"
......@@ -90,6 +91,8 @@ HintsFetcher::HintsFetcher(
url_loader_factory_ = std::move(url_loader_factory);
CHECK(optimization_guide_service_url_.SchemeIs(url::kHttpsScheme));
DCHECK(features::IsRemoteFetchingEnabled());
allowed_field_trial_name_hashes_ =
features::FieldTrialNameHashesAllowedForFetch();
}
HintsFetcher::~HintsFetcher() {
......@@ -202,6 +205,30 @@ bool HintsFetcher::FetchOptimizationGuideServiceHints(
get_hints_request.set_context(request_context_);
if (!allowed_field_trial_name_hashes_.empty()) {
std::vector<variations::ActiveGroupId> active_field_trials;
variations::GetFieldTrialActiveGroupIds(/*suffix=*/"",
&active_field_trials);
for (const auto& active_field_trial : active_field_trials) {
if (static_cast<size_t>(get_hints_request.active_field_trials_size()) ==
allowed_field_trial_name_hashes_.size()) {
// We've found all the field trials that we are allowed to send to the
// server.
break;
}
if (allowed_field_trial_name_hashes_.find(active_field_trial.name) ==
allowed_field_trial_name_hashes_.end()) {
// Continue if we are not allowed to send the field trial to the server.
continue;
}
proto::FieldTrial* ft_proto = get_hints_request.add_active_field_trials();
ft_proto->set_name_hash(active_field_trial.name);
ft_proto->set_group_hash(active_field_trial.group);
}
}
for (const auto& url : valid_urls)
get_hints_request.add_urls()->set_url(url.spec());
......
......@@ -163,6 +163,10 @@ class HintsFetcher {
// retrieving hints from the remote Optimization Guide Service.
base::TimeTicks hints_fetch_start_time_;
// Field trial name hashes that are allowed to be sent up in the request to
// the remote Optimization Guide Service.
base::flat_set<uint32_t> allowed_field_trial_name_hashes_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(HintsFetcher);
......
......@@ -13,6 +13,7 @@
#include "build/build_config.h"
#include "components/optimization_guide/optimization_guide_constants.h"
#include "components/optimization_guide/optimization_guide_switches.h"
#include "components/variations/hashing.h"
#include "google_apis/google_api_keys.h"
#include "net/base/url_util.h"
......@@ -42,6 +43,11 @@ const base::Feature kOptimizationHints {
const base::Feature kOptimizationHintsExperiments{
"OptimizationHintsExperiments", base::FEATURE_DISABLED_BY_DEFAULT};
// Feature flag that contains a feature param that specifies the field trials
// that are allowed to be sent up to the Optimization Guide Server.
const base::Feature kOptimizationHintsFieldTrials{
"OptimizationHintsFieldTrials", base::FEATURE_DISABLED_BY_DEFAULT};
// Enables fetching from a remote Optimization Guide Service.
const base::Feature kRemoteOptimizationGuideFetching {
"OptimizationHintsFetching",
......@@ -292,6 +298,22 @@ base::flat_set<std::string> ExternalAppPackageNamesApprovedForFetch() {
app_packages_list.end());
}
base::flat_set<uint32_t> FieldTrialNameHashesAllowedForFetch() {
std::string value = base::GetFieldTrialParamValueByFeature(
kOptimizationHintsFieldTrials, "allowed_field_trial_names");
if (value.empty())
return {};
std::vector<std::string> allowed_field_trial_names = base::SplitString(
value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
base::flat_set<uint32_t> allowed_field_trial_name_hashes;
for (const auto& allowed_field_trial_name : allowed_field_trial_names) {
allowed_field_trial_name_hashes.insert(
variations::HashName(allowed_field_trial_name));
}
return allowed_field_trial_name_hashes;
}
bool ShouldUseMLServiceForPrediction() {
return base::FeatureList::IsEnabled(
kOptimizationTargetPredictionUsingMLService);
......
......@@ -21,6 +21,7 @@ namespace features {
extern const base::Feature kOptimizationHints;
extern const base::Feature kOptimizationHintsExperiments;
extern const base::Feature kOptimizationHintsFieldTrials;
constexpr char kOptimizationHintsExperimentNameParam[] = "experiment_name";
extern const base::Feature kRemoteOptimizationGuideFetching;
extern const base::Feature kRemoteOptimizationGuideFetchingAnonymousDataConsent;
......@@ -151,6 +152,11 @@ int PredictionModelFetchRandomMaxDelaySecs();
// approved for fetching from the remote Optimization Guide Service.
base::flat_set<std::string> ExternalAppPackageNamesApprovedForFetch();
// Returns a set of field trial name hashes that can be sent in the request to
// the remote Optimization Guide Service if the client is in one of the
// specified field trials.
base::flat_set<uint32_t> FieldTrialNameHashesAllowedForFetch();
// Whether out-of-process model evaluation via the ML Service is enabled.
bool ShouldUseMLServiceForPrediction();
......
......@@ -39,6 +39,13 @@ message UrlInfo {
optional string url = 1;
}
message FieldTrial {
// The hash of a field trial.
optional uint32 name_hash = 1;
// The hash of the active group within the field trial.
optional uint32 group_hash = 2;
}
// Request to return a set of hints that guide what optimizations to perform
// on those hosts.
message GetHintsRequest {
......@@ -64,6 +71,9 @@ message GetHintsRequest {
// Context in which this request is made.
optional RequestContext context = 3;
// The field trials that are currently active when this request is made.
repeated FieldTrial active_field_trials = 6;
}
// Response to the GetHints request.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment