0
Files
src/content/browser/field_trial_synchronizer.cc
Dan Harrington 42d5c83e1a Revert^2 "Add code supporting new field-trial-internals page"
This reverts commit 269aa9d493.

Reason for revert: including fix for test

Original change's description:
> Revert "Add code supporting new field-trial-internals page"
>
> This reverts commit b8e462bc4b.
>
> Reason for revert: This CL is likely the cause of test failure CfmBrowserServiceTest.GetVariationsData on https://ci.chromium.org/ui/p/chromium/builders/ci/linux-cfm-rel starting from https://ci.chromium.org/ui/b/8763776059209925809. Error message: Expected equality of these values:
>   field_trial_states
>     Which is: "*Baz/Qux/Foo/Bar/"
>   states
>     Which is: "*Baz/Qux/Foo/Bar"
>
> Original change's description:
> > Add code supporting new field-trial-internals page
> >
> > This CL lays the groundwork for a future
> > field-trial-internals page, with the following changes:
> >
> > * Update base::FieldTrial to include an 'overridden' flag,
> >   which is used to change variation group hashes so
> >   overridden trial hashes are distinct.
> > * I pulled out code to build field trial strings
> >   into AppendFieldTrialGroupToString, to be reused. In some places, we
> >   were adding a trailing '/', and other places we weren't. Now we omit
> >   the trailing slash everywhere. We can still parse the string
> >   with or without the trailing slash.
> >
> > See go/field-trial-internals-dd for more information.
> >
> > Bug: b:284986126
> > Change-Id: I58b86c558cd7973bb6a493322353b52ebf9f619f
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4620752
> > Reviewed-by: Richard (Torne) Coles <torne@chromium.org>
> > Reviewed-by: Matt Menke <mmenke@chromium.org>
> > Reviewed-by: Mike Dougherty <michaeldo@chromium.org>
> > Reviewed-by: Robert Kaplow <rkaplow@chromium.org>
> > Commit-Queue: Dan H <harringtond@chromium.org>
> > Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
> > Reviewed-by: Erik Chen <erikchen@chromium.org>
> > Reviewed-by: Luc Nguyen <lucnguyen@google.com>
> > Cr-Commit-Position: refs/heads/main@{#1227624}
>
> Bug: b:284986126
> Change-Id: I18f84ccf307f6265da24b3603b82344e1bf42c20
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5052200
> Owners-Override: Maggie Cai <mxcai@chromium.org>
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Commit-Queue: Maggie Cai <mxcai@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1227726}

Bug: b:284986126
Change-Id: Id00f514233c08aa439caaa7f4d6bdaa6c5fc38d5
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5055166
Reviewed-by: Luc Nguyen <lucnguyen@google.com>
Reviewed-by: Erik Chen <erikchen@chromium.org>
Commit-Queue: Dan H <harringtond@chromium.org>
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Reviewed-by: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: Paul Moy <pmoy@chromium.org>
Reviewed-by: Richard (Torne) Coles <torne@chromium.org>
Reviewed-by: Matt Menke <mmenke@chromium.org>
Reviewed-by: Robert Kaplow <rkaplow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1229416}
2023-11-27 18:10:16 +00:00

159 lines
5.8 KiB
C++

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/field_trial_synchronizer.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_list_including_low_anonymity.h"
#include "base/strings/strcat.h"
#include "base/threading/thread.h"
#include "components/metrics/persistent_system_profile.h"
#include "components/variations/active_field_trials.h"
#include "components/variations/variations_client.h"
#include "content/common/renderer_variations_configuration.mojom.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "ipc/ipc_channel_proxy.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
namespace content {
namespace {
FieldTrialSynchronizer* g_instance = nullptr;
// Notifies all renderer processes about the |group_name| that is finalized for
// the given field trail (|field_trial_name|). This is called on UI thread.
void NotifyAllRenderersOfFieldTrial(const std::string& field_trial_name,
const std::string& group_name,
bool is_low_anonymity,
bool is_overridden) {
// To iterate over RenderProcessHosts, or to send messages to the hosts, we
// need to be on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Low anonymity or overridden field trials must not be written to persistent
// data, otherwise they might end up being logged in metrics.
//
// TODO(crbug.com/1431156): split this out into a separate class that
// registers using |FieldTrialList::AddObserver()| (and so doesn't get told
// about low anonymity trials at all).
if (!is_low_anonymity) {
// Note this in the persistent profile as it will take a while for a new
// "complete" profile to be generated.
metrics::GlobalPersistentSystemProfile::GetInstance()->AddFieldTrial(
field_trial_name,
is_overridden ? base::StrCat({group_name, variations::kOverrideSuffix})
: group_name);
}
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd(); it.Advance()) {
auto* host = it.GetCurrentValue();
IPC::ChannelProxy* channel = host->GetChannel();
// channel might be null in tests.
if (host->IsInitializedAndNotDead() && channel) {
mojo::AssociatedRemote<mojom::RendererVariationsConfiguration>
renderer_variations_configuration;
channel->GetRemoteAssociatedInterface(&renderer_variations_configuration);
renderer_variations_configuration->SetFieldTrialGroup(field_trial_name,
group_name);
}
}
}
} // namespace
// static
void FieldTrialSynchronizer::CreateInstance() {
// Only 1 instance is allowed per process.
DCHECK(!g_instance);
g_instance = new FieldTrialSynchronizer();
}
FieldTrialSynchronizer::FieldTrialSynchronizer() {
// TODO(crbug.com/1431156): consider whether there is a need to exclude low
// anonymity field trials from non-browser processes (or to plumb through the
// anonymity property for more fine-grained access).
bool success = base::FieldTrialListIncludingLowAnonymity::AddObserver(this);
// Ensure the observer was actually registered.
DCHECK(success);
variations::VariationsIdsProvider::GetInstance()->AddObserver(this);
NotifyAllRenderersOfVariationsHeader();
}
void FieldTrialSynchronizer::OnFieldTrialGroupFinalized(
const base::FieldTrial& trial,
const std::string& group_name) {
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
NotifyAllRenderersOfFieldTrial(trial.trial_name(), group_name,
trial.is_low_anonymity(),
trial.IsOverridden());
} else {
// Note that in some tests, `trial` may not be alive when the posted task is
// called.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&NotifyAllRenderersOfFieldTrial, trial.trial_name(),
group_name, trial.is_low_anonymity(),
trial.IsOverridden()));
}
}
// static
void FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader() {
// To iterate over RenderProcessHosts, or to send messages to the hosts, we
// need to be on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd(); it.Advance()) {
UpdateRendererVariationsHeader(it.GetCurrentValue());
}
}
// static
void FieldTrialSynchronizer::UpdateRendererVariationsHeader(
RenderProcessHost* host) {
if (!host->IsInitializedAndNotDead())
return;
IPC::ChannelProxy* channel = host->GetChannel();
// |channel| might be null in tests.
if (!channel)
return;
variations::VariationsClient* client =
host->GetBrowserContext()->GetVariationsClient();
// |client| might be null in tests.
if (!client || client->IsOffTheRecord())
return;
mojo::AssociatedRemote<mojom::RendererVariationsConfiguration>
renderer_variations_configuration;
channel->GetRemoteAssociatedInterface(&renderer_variations_configuration);
renderer_variations_configuration->SetVariationsHeaders(
client->GetVariationsHeaders());
}
void FieldTrialSynchronizer::VariationIdsHeaderUpdated() {
// PostTask to avoid recursive lock.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader));
}
FieldTrialSynchronizer::~FieldTrialSynchronizer() {
NOTREACHED();
}
} // namespace content