Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 5 | #include "content/browser/field_trial_synchronizer.h" |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 6 | |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 7 | #include "base/check_op.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 8 | #include "base/functional/bind.h" |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
| 10 | #include "base/metrics/field_trial_list_including_low_anonymity.h" |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 11 | #include "base/strings/strcat.h" |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
bcwhite | c1ddec3 | 2017-06-29 14:13:04 | [diff] [blame] | 13 | #include "components/metrics/persistent_system_profile.h" |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 14 | #include "components/variations/active_field_trials.h" |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 15 | #include "components/variations/variations_client.h" |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 16 | #include "content/common/renderer_variations_configuration.mojom.h" |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 17 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 18 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 20 | #include "content/public/browser/render_process_host.h" |
Lei Zhang | 487cb3cf | 2022-02-28 01:11:59 | [diff] [blame] | 21 | #include "ipc/ipc_channel_proxy.h" |
Julie Jeongeun Kim | 68fde7a | 2019-09-30 10:39:38 | [diff] [blame] | 22 | #include "mojo/public/cpp/bindings/associated_remote.h" |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 23 | |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 24 | namespace content { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 25 | |
Xi Han | 095f3934 | 2019-08-08 20:00:54 | [diff] [blame] | 26 | namespace { |
| 27 | |
Francois Doray | da23db5 | 2021-05-06 22:34:36 | [diff] [blame] | 28 | FieldTrialSynchronizer* g_instance = nullptr; |
Francois Doray | 9985b63 | 2021-05-05 14:45:37 | [diff] [blame] | 29 | |
Francois Doray | da23db5 | 2021-05-06 22:34:36 | [diff] [blame] | 30 | // Notifies all renderer processes about the |group_name| that is finalized for |
| 31 | // the given field trail (|field_trial_name|). This is called on UI thread. |
| 32 | void NotifyAllRenderersOfFieldTrial(const std::string& field_trial_name, |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 33 | const std::string& group_name, |
| 34 | bool is_low_anonymity, |
| 35 | bool is_overridden) { |
Francois Doray | 9985b63 | 2021-05-05 14:45:37 | [diff] [blame] | 36 | // To iterate over RenderProcessHosts, or to send messages to the hosts, we |
| 37 | // need to be on the UI thread. |
| 38 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 39 | |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 40 | // Low anonymity or overridden field trials must not be written to persistent |
| 41 | // data, otherwise they might end up being logged in metrics. |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 42 | // |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 43 | // TODO(crbug.com/40263398): split this out into a separate class that |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 44 | // registers using |FieldTrialList::AddObserver()| (and so doesn't get told |
| 45 | // about low anonymity trials at all). |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 46 | if (!is_low_anonymity) { |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 47 | // Note this in the persistent profile as it will take a while for a new |
| 48 | // "complete" profile to be generated. |
| 49 | metrics::GlobalPersistentSystemProfile::GetInstance()->AddFieldTrial( |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 50 | field_trial_name, |
| 51 | is_overridden ? base::StrCat({group_name, variations::kOverrideSuffix}) |
| 52 | : group_name); |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 53 | } |
bcwhite | c1ddec3 | 2017-06-29 14:13:04 | [diff] [blame] | 54 | |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 55 | for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 56 | !it.IsAtEnd(); it.Advance()) { |
Alexander Yashkin | 69b731c | 2019-01-17 10:14:15 | [diff] [blame] | 57 | auto* host = it.GetCurrentValue(); |
| 58 | IPC::ChannelProxy* channel = host->GetChannel(); |
nigeltao | f007b70 | 2017-02-04 00:53:25 | [diff] [blame] | 59 | // channel might be null in tests. |
Alexander Yashkin | 69b731c | 2019-01-17 10:14:15 | [diff] [blame] | 60 | if (host->IsInitializedAndNotDead() && channel) { |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 61 | mojo::AssociatedRemote<mojom::RendererVariationsConfiguration> |
| 62 | renderer_variations_configuration; |
| 63 | channel->GetRemoteAssociatedInterface(&renderer_variations_configuration); |
| 64 | renderer_variations_configuration->SetFieldTrialGroup(field_trial_name, |
| 65 | group_name); |
nigeltao | f007b70 | 2017-02-04 00:53:25 | [diff] [blame] | 66 | } |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Francois Doray | da23db5 | 2021-05-06 22:34:36 | [diff] [blame] | 70 | } // namespace |
| 71 | |
| 72 | // static |
| 73 | void FieldTrialSynchronizer::CreateInstance() { |
| 74 | // Only 1 instance is allowed per process. |
| 75 | DCHECK(!g_instance); |
| 76 | g_instance = new FieldTrialSynchronizer(); |
| 77 | } |
| 78 | |
| 79 | FieldTrialSynchronizer::FieldTrialSynchronizer() { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 80 | // TODO(crbug.com/40263398): consider whether there is a need to exclude low |
James Lee | 01bb2e9 | 2023-04-15 19:46:40 | [diff] [blame] | 81 | // anonymity field trials from non-browser processes (or to plumb through the |
| 82 | // anonymity property for more fine-grained access). |
| 83 | bool success = base::FieldTrialListIncludingLowAnonymity::AddObserver(this); |
Francois Doray | da23db5 | 2021-05-06 22:34:36 | [diff] [blame] | 84 | // Ensure the observer was actually registered. |
| 85 | DCHECK(success); |
| 86 | |
| 87 | variations::VariationsIdsProvider::GetInstance()->AddObserver(this); |
| 88 | NotifyAllRenderersOfVariationsHeader(); |
| 89 | } |
| 90 | |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 91 | void FieldTrialSynchronizer::OnFieldTrialGroupFinalized( |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 92 | const base::FieldTrial& trial, |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 93 | const std::string& group_name) { |
Matt Falkenhagen | 439e1a3 | 2021-09-24 10:30:16 | [diff] [blame] | 94 | if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 95 | NotifyAllRenderersOfFieldTrial(trial.trial_name(), group_name, |
| 96 | trial.is_low_anonymity(), |
| 97 | trial.IsOverridden()); |
Matt Falkenhagen | 439e1a3 | 2021-09-24 10:30:16 | [diff] [blame] | 98 | } else { |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 99 | // Note that in some tests, `trial` may not be alive when the posted task is |
| 100 | // called. |
Matt Falkenhagen | 439e1a3 | 2021-09-24 10:30:16 | [diff] [blame] | 101 | GetUIThreadTaskRunner({})->PostTask( |
Dan Harrington | 42d5c83 | 2023-11-27 18:10:16 | [diff] [blame] | 102 | FROM_HERE, |
| 103 | base::BindOnce(&NotifyAllRenderersOfFieldTrial, trial.trial_name(), |
| 104 | group_name, trial.is_low_anonymity(), |
| 105 | trial.IsOverridden())); |
Matt Falkenhagen | 439e1a3 | 2021-09-24 10:30:16 | [diff] [blame] | 106 | } |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // static |
| 110 | void FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader() { |
| 111 | // To iterate over RenderProcessHosts, or to send messages to the hosts, we |
| 112 | // need to be on the UI thread. |
| 113 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 | for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 115 | !it.IsAtEnd(); it.Advance()) { |
| 116 | UpdateRendererVariationsHeader(it.GetCurrentValue()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // static |
| 121 | void FieldTrialSynchronizer::UpdateRendererVariationsHeader( |
| 122 | RenderProcessHost* host) { |
| 123 | if (!host->IsInitializedAndNotDead()) |
| 124 | return; |
| 125 | |
| 126 | IPC::ChannelProxy* channel = host->GetChannel(); |
| 127 | |
| 128 | // |channel| might be null in tests. |
| 129 | if (!channel) |
| 130 | return; |
| 131 | |
| 132 | variations::VariationsClient* client = |
| 133 | host->GetBrowserContext()->GetVariationsClient(); |
| 134 | |
| 135 | // |client| might be null in tests. |
Ramin Halavati | 15a1021 | 2020-05-18 05:26:13 | [diff] [blame] | 136 | if (!client || client->IsOffTheRecord()) |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | mojo::AssociatedRemote<mojom::RendererVariationsConfiguration> |
| 140 | renderer_variations_configuration; |
| 141 | channel->GetRemoteAssociatedInterface(&renderer_variations_configuration); |
Caitlin Fischer | b756877 | 2020-09-23 14:32:00 | [diff] [blame] | 142 | renderer_variations_configuration->SetVariationsHeaders( |
| 143 | client->GetVariationsHeaders()); |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void FieldTrialSynchronizer::VariationIdsHeaderUpdated() { |
| 147 | // PostTask to avoid recursive lock. |
Gabriel Charette | c40dd9c | 2021-03-30 11:00:11 | [diff] [blame] | 148 | GetUIThreadTaskRunner({})->PostTask( |
| 149 | FROM_HERE, |
Alex Clarke | 3ebd977 | 2020-02-28 10:50:27 | [diff] [blame] | 150 | base::BindOnce( |
| 151 | &FieldTrialSynchronizer::NotifyAllRenderersOfVariationsHeader)); |
[email protected] | 63a8ba1 | 2011-04-29 05:42:22 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 649d1c0 | 2012-04-27 02:56:21 | [diff] [blame] | 154 | FieldTrialSynchronizer::~FieldTrialSynchronizer() { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 155 | NOTREACHED(); |
[email protected] | 649d1c0 | 2012-04-27 02:56:21 | [diff] [blame] | 156 | } |
Alex Clarke | 9fc39b9b | 2020-01-31 17:29:47 | [diff] [blame] | 157 | |
| 158 | } // namespace content |