0
Files
src/content/browser/field_trial_synchronizer.cc
Caitlin Fischer b756877dfc Support FIRST_PARTY variations IDs.
This implements the new GOOGLE_WEB_PROPERTIES_FIRST_PARTY
and GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY variations
types, which allow us to limit the set of experiment IDs included in
X-Client-Data headers sent in third-party contexts.

Adding support for this involved sending a map of headers rather than a
single header to the renderer. A map is used because the request context
is unknown until the process of sending the request has started. So, the
renderer needs to know the appropriate headers to send in first- and
third-party contexts.

Bug: 1094303
Change-Id: I447219b1cc80b0dc8552b440e6ad07b2fdb15d8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2411489
Reviewed-by: Zhongyi Shi <zhongyi@chromium.org>
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Jesse Doherty <jwd@chromium.org>
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Auto-Submit: Caitlin Fischer <caitlinfischer@google.com>
Commit-Queue: Caitlin Fischer <caitlinfischer@google.com>
Cr-Commit-Position: refs/heads/master@{#809756}
2020-09-23 14:32:00 +00:00

137 lines
4.9 KiB
C++

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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/bind.h"
#include "base/check_op.h"
#include "base/task/post_task.h"
#include "base/threading/thread.h"
#include "components/metrics/persistent_system_profile.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 "mojo/public/cpp/bindings/associated_remote.h"
namespace content {
namespace {
void AddFieldTrialToPersistentSystemProfile(const std::string& field_trial_name,
const std::string& group_name) {
// 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, group_name);
}
} // namespace
FieldTrialSynchronizer::FieldTrialSynchronizer() {
bool success = base::FieldTrialList::AddObserver(this);
// Ensure the observer was actually registered.
DCHECK(success);
variations::VariationsIdsProvider::GetInstance()->AddObserver(this);
NotifyAllRenderersOfVariationsHeader();
}
void FieldTrialSynchronizer::NotifyAllRenderersOfFieldTrial(
const std::string& field_trial_name,
const std::string& group_name) {
// To iterate over RenderProcessHosts, or to send messages to the hosts, we
// need to be on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
AddFieldTrialToPersistentSystemProfile(field_trial_name, 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);
}
}
}
void FieldTrialSynchronizer::OnFieldTrialGroupFinalized(
const std::string& field_trial_name,
const std::string& group_name) {
// The FieldTrialSynchronizer may have been created before any BrowserThread
// is created, so we don't need to synchronize with child processes in which
// case there are no child processes to notify yet. But we want to update the
// persistent system profile, thus the histogram data recorded in the reduced
// mode will be tagged to its corresponding field trial experiment.
if (!BrowserThread::IsThreadInitialized(BrowserThread::UI)) {
AddFieldTrialToPersistentSystemProfile(field_trial_name, group_name);
return;
}
RunOrPostTaskOnThread(
FROM_HERE, BrowserThread::UI,
base::BindOnce(&FieldTrialSynchronizer::NotifyAllRenderersOfFieldTrial,
this, field_trial_name, group_name));
}
// 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.
base::PostTask(
FROM_HERE, BrowserThread::UI,
base::BindOnce(
&FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader));
}
FieldTrialSynchronizer::~FieldTrialSynchronizer() {
base::FieldTrialList::RemoveObserver(this);
variations::VariationsIdsProvider::GetInstance()->RemoveObserver(this);
}
} // namespace content