0
Files
src/content/browser/field_trial_synchronizer.cc
Matt Falkenhagen 439e1a3b0d content: Remove remaining uses of content::RunOrPostTaskOnThread.
This helper was created in
commit 7b509e56dc to expedite the UI/IO
thread simplification effort, and particularly the service worker thread
migration which is now complete. It was not intended as a long-term
function for general use, as it is a strange layer over the normal
threading and tasks functions in base/ and encourages a typically
unrecommended pattern of "maybe async, maybe sync". There are just a
few targeted uses of it today, and these can directly use the usual
functions without this helper.

Bug: 1138155, 1187753
Change-Id: I7c82f0909656a802ec103b2af40bceb436e26e57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3173372
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Martin Šrámek <msramek@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/main@{#924711}
2021-09-24 10:30:16 +00:00

133 lines
4.5 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/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 {
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) {
// To iterate over RenderProcessHosts, or to send messages to the hosts, we
// need to be on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// 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);
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() {
bool success = base::FieldTrialList::AddObserver(this);
// Ensure the observer was actually registered.
DCHECK(success);
variations::VariationsIdsProvider::GetInstance()->AddObserver(this);
NotifyAllRenderersOfVariationsHeader();
}
void FieldTrialSynchronizer::OnFieldTrialGroupFinalized(
const std::string& field_trial_name,
const std::string& group_name) {
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
NotifyAllRenderersOfFieldTrial(field_trial_name, group_name);
} else {
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&NotifyAllRenderersOfFieldTrial,
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.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader));
}
FieldTrialSynchronizer::~FieldTrialSynchronizer() {
NOTREACHED();
}
} // namespace content