1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/variations/variations_ids_provider.h"
#include <algorithm>
#include "base/base64.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/observer_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "components/variations/proto/client_variations.pb.h"
#include "components/variations/variations_associated_data.h"
#include "components/variations/variations_client.h"
#include "components/variations/variations_features.h"
namespace variations {
namespace {
VariationsIdsProvider* g_instance = nullptr;
base::Lock& GetInstanceLock() {
static base::NoDestructor<base::Lock> lock;
return *lock;
}
// Sorts and removes duplicates from the given container. This is useful after
// building ID collections by merging IDs from different sources, as the source
// ID sets may intersect, yielding duplicate and out-of-order IDs.
template <typename Container>
void MakeSortedAndUnique(Container* container) {
std::sort(container->begin(), container->end());
container->erase(std::unique(container->begin(), container->end()),
container->end());
}
} // namespace
bool VariationsHeaderKey::operator<(const VariationsHeaderKey& other) const {
if (is_signed_in != other.is_signed_in) {
return is_signed_in < other.is_signed_in;
}
return web_visibility < other.web_visibility;
}
// Adding/removing headers is implemented by request consumers, and how it is
// implemented depends on the request type.
// There are three cases:
// 1. Subresources request in renderer, it is implemented by
// URLLoader::Context::Start() by adding a VariationsURLLoaderThrottle to a
// content::URLLoaderThrottle vector.
// 2. Navigations/Downloads request in browser, it is implemented in
// ChromeContentBrowserClient::CreateURLLoaderThrottles() which calls
// CreateContentBrowserURLLoaderThrottles which also adds a
// VariationsURLLoaderThrottle to a content::URLLoaderThrottle vector.
// 3. SimpleURLLoader in browser, it is implemented in a SimpleURLLoader wrapper
// function variations::CreateSimpleURLLoaderWithVariationsHeader().
// static
VariationsIdsProvider* VariationsIdsProvider::Create(Mode mode) {
base::AutoLock lock(GetInstanceLock());
DCHECK(!g_instance);
g_instance = new VariationsIdsProvider(mode);
return g_instance;
}
// static
VariationsIdsProvider* VariationsIdsProvider::GetInstance() {
base::AutoLock lock(GetInstanceLock());
DCHECK(g_instance);
return g_instance;
}
void VariationsIdsProvider::SetClockFunc(
VariationsIdsProvider::ClockFunction clock_func) {
base::AutoLock lock(lock_);
clock_func_ = std::move(clock_func);
}
variations::mojom::VariationsHeadersPtr
VariationsIdsProvider::GetClientDataHeaders(bool is_signed_in) {
if (mode_ == Mode::kIgnoreSignedInState) {
is_signed_in = true;
} else if (mode_ == Mode::kDontSendSignedInVariations) {
is_signed_in = false;
}
std::string first_party_header_copy;
std::string any_context_header_copy;
{
base::AutoLock lock(lock_);
MaybeUpdateVariationIDsAndHeaders();
first_party_header_copy = GetClientDataHeader(
is_signed_in, Study_GoogleWebVisibility_FIRST_PARTY);
any_context_header_copy =
GetClientDataHeader(is_signed_in, Study_GoogleWebVisibility_ANY);
}
if (first_party_header_copy.empty() && any_context_header_copy.empty()) {
return nullptr;
}
base::flat_map<variations::mojom::GoogleWebVisibility, std::string> headers =
{{variations::mojom::GoogleWebVisibility::FIRST_PARTY,
std::move(first_party_header_copy)},
{variations::mojom::GoogleWebVisibility::ANY,
std::move(any_context_header_copy)}};
return variations::mojom::VariationsHeaders::New(std::move(headers));
}
std::string VariationsIdsProvider::GetGoogleAppVariationsString() {
return GetVariationsString({GOOGLE_APP});
}
std::string VariationsIdsProvider::GetTriggerVariationsString() {
return GetVariationsString({GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT,
GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY});
}
std::string VariationsIdsProvider::GetVariationsString() {
return GetVariationsString(
{GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, GOOGLE_WEB_PROPERTIES_FIRST_PARTY});
}
std::vector<VariationID> VariationsIdsProvider::GetVariationsVector(
const std::set<IDCollectionKey>& keys) {
return GetVariationsVectorImpl(keys);
}
std::vector<VariationID>
VariationsIdsProvider::GetVariationsVectorForWebPropertiesKeys() {
const std::set<IDCollectionKey> web_properties_keys{
GOOGLE_WEB_PROPERTIES_ANY_CONTEXT,
GOOGLE_WEB_PROPERTIES_FIRST_PARTY,
GOOGLE_WEB_PROPERTIES_SIGNED_IN,
GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT,
GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY,
};
return GetVariationsVectorImpl(web_properties_keys);
}
void VariationsIdsProvider::SetLowEntropySourceValue(
std::optional<int> low_entropy_source_value) {
// The low entropy source value is an integer that is between 0 and 7999,
// inclusive. See components/metrics/metrics_state_manager.cc for the logic to
// generate it.
if (low_entropy_source_value) {
DCHECK_GE(low_entropy_source_value.value(), 0);
DCHECK_LE(low_entropy_source_value.value(), 7999);
}
base::AutoLock scoped_lock(lock_);
low_entropy_source_value_ = low_entropy_source_value;
}
VariationsIdsProvider::ForceIdsResult VariationsIdsProvider::ForceVariationIds(
const std::vector<std::string>& variation_ids,
const std::string& command_line_variation_ids) {
base::AutoLock scoped_lock(lock_);
force_enabled_ids_set_.clear();
ResetLastUpdateTime();
if (!AddVariationIdsToSet(variation_ids, /*should_dedupe=*/true,
&force_enabled_ids_set_)) {
return ForceIdsResult::INVALID_VECTOR_ENTRY;
}
if (!ParseVariationIdsParameter(command_line_variation_ids,
/*should_dedupe=*/true,
&force_enabled_ids_set_)) {
return ForceIdsResult::INVALID_SWITCH_ENTRY;
}
return ForceIdsResult::SUCCESS;
}
bool VariationsIdsProvider::ForceDisableVariationIds(
const std::string& command_line_variation_ids) {
base::AutoLock scoped_lock(lock_);
force_disabled_ids_set_.clear();
ResetLastUpdateTime();
// |should_dedupe| is false here in order to add the IDs specified in
// |command_line_variation_ids| to |force_disabled_ids_set_| even if they were
// defined before. The IDs are not marked as active; they are marked as
// disabled.
if (!ParseVariationIdsParameter(command_line_variation_ids,
/*should_dedupe=*/false,
&force_disabled_ids_set_)) {
return false;
}
// When disabling a variation ID through the command line, ensure it is
// disabled in every contexts.
static_assert(
ID_COLLECTION_COUNT == 6,
"If you add a new collection key, make sure it can be disabled here.");
VariationIDEntrySet additional_disabled_ids;
for (const auto& entry : force_disabled_ids_set_) {
if (entry.second == GOOGLE_WEB_PROPERTIES_ANY_CONTEXT) {
additional_disabled_ids.insert(
VariationIDEntry(entry.first, GOOGLE_WEB_PROPERTIES_SIGNED_IN));
additional_disabled_ids.insert(
VariationIDEntry(entry.first, GOOGLE_WEB_PROPERTIES_FIRST_PARTY));
} else if (entry.second == GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT) {
additional_disabled_ids.insert(VariationIDEntry(
entry.first, GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY));
}
}
force_disabled_ids_set_.merge(additional_disabled_ids);
return true;
}
void VariationsIdsProvider::AddObserver(Observer* observer) {
base::AutoLock scoped_lock(lock_);
CHECK(!base::Contains(observer_list_, observer));
observer_list_.push_back(observer);
}
void VariationsIdsProvider::RemoveObserver(Observer* observer) {
base::AutoLock scoped_lock(lock_);
std::erase(observer_list_, observer);
}
void VariationsIdsProvider::ResetForTesting() {
base::AutoLock scoped_lock(lock_);
// Stop observing field trials so that it can be restarted when this is
// re-initialized. Note: This is a no-op if this is not currently observing.
base::FieldTrialList::RemoveObserver(this);
is_subscribed_to_field_trial_list_ = false;
// Reset the remaining cached state.
low_entropy_source_value_ = std::nullopt;
last_update_time_ = base::Time::Min();
next_update_time_ = base::Time::Min();
active_variation_ids_set_.clear();
force_enabled_ids_set_.clear();
synthetic_variation_ids_set_.clear();
force_disabled_ids_set_.clear();
variations_headers_map_.clear();
observer_list_.clear();
clock_func_.Reset();
}
VariationsIdsProvider::VariationsIdsProvider(Mode mode) : mode_(mode) {}
VariationsIdsProvider::~VariationsIdsProvider() {
base::FieldTrialList::RemoveObserver(this);
}
// static
void VariationsIdsProvider::CreateInstanceForTesting(Mode mode) {
base::AutoLock lock(GetInstanceLock());
delete g_instance;
g_instance = new VariationsIdsProvider(mode);
}
// static
void VariationsIdsProvider::DestroyInstanceForTesting() {
base::AutoLock lock(GetInstanceLock());
delete g_instance;
g_instance = nullptr;
}
std::string VariationsIdsProvider::GetVariationsString(
const std::set<IDCollectionKey>& keys) {
// Construct a space-separated string with leading and trailing spaces from
// the VariationIDs set. The IDs in the string are unique and in sorted order.
std::string ids_string = " ";
for (const VariationID& id : GetVariationsVector(keys)) {
ids_string.append(base::NumberToString(id));
ids_string.push_back(' ');
}
return ids_string;
}
void VariationsIdsProvider::OnFieldTrialGroupFinalized(
const base::FieldTrial& trial,
const std::string& group_name) {
base::AutoLock scoped_lock(lock_);
// The finalized field trial may have caused variation IDs to be added to the
// active set. Reset the last update time to force an update on the next call
// to `MaybeUpdateVariationIDsAndHeaders()`.
ResetLastUpdateTime();
}
void VariationsIdsProvider::OnSyntheticTrialsChanged(
const std::vector<SyntheticTrialGroup>& trials_updated,
const std::vector<SyntheticTrialGroup>& trials_removed,
const std::vector<SyntheticTrialGroup>& groups) {
base::AutoLock scoped_lock(lock_);
synthetic_variation_ids_set_.clear();
ResetLastUpdateTime();
for (const SyntheticTrialGroup& group : groups) {
VariationID id =
GetGoogleVariationID(GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, group.id());
// TODO(crbug.com/40214121): Handle duplicated IDs in such a way that is
// visible to developers, but non-intrusive to users. See
// crrev/c/3628020/comments/e278cd12_2bb863ef for discussions.
if (id != EMPTY_ID) {
synthetic_variation_ids_set_.insert(
VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_ANY_CONTEXT));
}
id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES_SIGNED_IN, group.id());
if (id != EMPTY_ID) {
synthetic_variation_ids_set_.insert(
VariationIDEntry(id, GOOGLE_WEB_PROPERTIES_SIGNED_IN));
}
// Google App IDs omitted because they should never be defined
// synthetically.
}
}
void VariationsIdsProvider::MaybeUpdateVariationIDsAndHeaders() {
lock_.AssertAcquired();
// Check if an update is needed. If not, return early (the currently cached
// values are still valid), otherwise, update the cached values and notify
// observers. See `UpdateIsNeeded()` for more details.
const base::Time current_time = GetCurrentTime();
if (!UpdateIsNeeded(current_time)) {
return;
}
SetLastUpdateTime(current_time);
SetNextUpdateTime(GetNextTimeWindowEvent(current_time));
// Subscribe to field trials if not already subscribed.
if (!is_subscribed_to_field_trial_list_) {
const bool success = base::FieldTrialList::AddObserver(this);
CHECK(success);
is_subscribed_to_field_trial_list_ = true;
}
// Clear the active variation IDs set before adding new ones.
active_variation_ids_set_.clear();
// Populate the active variation IDs.
AddActiveVariationIds(current_time);
AddForceEnabledVariationIds();
AddSyntheticVariationIds();
AddLowEntropySourceValue();
RemoveForceDisabledVariationIds();
// Generate the header values.
for (bool is_signed_in : {false, true}) {
for (auto context : {Study_GoogleWebVisibility_ANY,
Study_GoogleWebVisibility_FIRST_PARTY}) {
variations_headers_map_[VariationsHeaderKey{is_signed_in, context}] =
GenerateBase64EncodedProto(is_signed_in, context);
}
}
// Notify observers that the variation IDs header has been updated.
for (auto* observer : observer_list_) {
observer->VariationIdsHeaderUpdated();
}
}
void VariationsIdsProvider::AddActiveVariationIds(base::Time current_time) {
lock_.AssertAcquired();
base::FieldTrial::ActiveGroups active_groups;
base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
// Add all IDs for the non-forced, timebox-active, groups.
for (const auto& entry : active_groups) {
const auto active_group_id =
MakeActiveGroupId(entry.trial_name, entry.group_name);
for (int i = 0; i < ID_COLLECTION_COUNT; ++i) {
const IDCollectionKey key = static_cast<IDCollectionKey>(i);
const VariationID id =
GetGoogleVariationID(key, active_group_id, current_time);
if (id != EMPTY_ID) {
active_variation_ids_set_.emplace(id, key);
}
}
}
}
void VariationsIdsProvider::AddLowEntropySourceValue() {
lock_.AssertAcquired();
// Add the low entropy source value, if it exists, which has one of
// 8000 possible values (between kLowEntropySourceVariationIdRange[Min/Max],
// ~13 bits). This is the value that has been used for deriving the variation
// ids included in the X-Client-Data header and therefore does not reveal
// additional information about the client when there are more than 13
// variations. A typical Chrome client has more than 13 variation ids
// reported.
//
// The entropy source value is used for retrospective A/A tests to validate
// that there's no existing bias between two randomized groups of clients for
// a later A/B study.
if (low_entropy_source_value_.has_value()) {
const int source_value = low_entropy_source_value_.value() +
internal::kLowEntropySourceVariationIdRangeMin;
DCHECK_GE(source_value, internal::kLowEntropySourceVariationIdRangeMin);
DCHECK_LE(source_value, internal::kLowEntropySourceVariationIdRangeMax);
active_variation_ids_set_.emplace(source_value,
GOOGLE_WEB_PROPERTIES_ANY_CONTEXT);
}
}
void VariationsIdsProvider::AddForceEnabledVariationIds() {
lock_.AssertAcquired();
active_variation_ids_set_.insert(force_enabled_ids_set_.begin(),
force_enabled_ids_set_.end());
}
void VariationsIdsProvider::AddSyntheticVariationIds() {
lock_.AssertAcquired();
active_variation_ids_set_.insert(synthetic_variation_ids_set_.begin(),
synthetic_variation_ids_set_.end());
}
void VariationsIdsProvider::RemoveForceDisabledVariationIds() {
lock_.AssertAcquired();
for (const auto& entry : force_disabled_ids_set_) {
active_variation_ids_set_.erase(entry);
}
}
std::string VariationsIdsProvider::GenerateBase64EncodedProto(
bool is_signed_in,
Study_GoogleWebVisibility context) {
lock_.AssertAcquired();
const bool is_first_party_context =
context == Study_GoogleWebVisibility_FIRST_PARTY;
ClientVariations proto;
for (const VariationIDEntry& entry : active_variation_ids_set_) {
switch (entry.second) {
case GOOGLE_WEB_PROPERTIES_SIGNED_IN:
if (is_signed_in) {
proto.add_variation_id(entry.first);
}
break;
case GOOGLE_WEB_PROPERTIES_ANY_CONTEXT:
proto.add_variation_id(entry.first);
break;
case GOOGLE_WEB_PROPERTIES_FIRST_PARTY:
if (is_first_party_context) {
proto.add_variation_id(entry.first);
}
break;
case GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT:
proto.add_trigger_variation_id(entry.first);
break;
case GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY:
if (is_first_party_context) {
proto.add_trigger_variation_id(entry.first);
}
break;
case GOOGLE_APP:
// These IDs should not be added into Google Web headers.
break;
case ID_COLLECTION_COUNT:
// This case included to get full enum coverage for switch, so that
// new enums introduce compiler warnings. Nothing to do for this.
break;
}
}
// Sort the ids and remove duplicates. This makes the representation of any
// given combination of variation ids deterministic.
MakeSortedAndUnique(proto.mutable_variation_id());
MakeSortedAndUnique(proto.mutable_trigger_variation_id());
const size_t total_id_count =
proto.variation_id_size() + proto.trigger_variation_id_size();
if (total_id_count == 0) {
return std::string();
}
// This is the bottleneck for the creation of the header, so validate the size
// here. Force a hard maximum on the ID count in case the Variations server
// returns too many IDs and DOSs receiving servers with large requests.
DCHECK_LE(total_id_count, 75U);
UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount",
total_id_count);
if (total_id_count > 100) {
return std::string();
}
std::string serialized;
proto.SerializeToString(&serialized);
return base::Base64Encode(serialized);
}
bool VariationsIdsProvider::AddVariationIdsToSet(
const std::vector<std::string>& variation_id_strings,
bool should_dedupe,
VariationIDEntrySet* target_set) {
lock_.AssertAcquired();
for (const std::string& entry_string : variation_id_strings) {
std::string_view entry = entry_string;
if (entry.empty()) {
target_set->clear();
return false;
}
const bool is_trigger_id =
base::StartsWith(entry, "t", base::CompareCase::SENSITIVE);
// Remove the "t" prefix if it's there.
std::string_view trimmed_entry = is_trigger_id ? entry.substr(1) : entry;
int variation_id = 0;
if (!base::StringToInt(trimmed_entry, &variation_id)) {
target_set->clear();
return false;
}
if (should_dedupe && IsDuplicateId(variation_id)) {
DVLOG(1) << "Invalid variation ID specified: " << entry
<< " (it is already in use)";
target_set->clear();
return false;
}
target_set->emplace(
variation_id, is_trigger_id ? GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT
: GOOGLE_WEB_PROPERTIES_ANY_CONTEXT);
}
return true;
}
bool VariationsIdsProvider::ParseVariationIdsParameter(
const std::string& command_line_variation_ids,
bool should_dedupe,
VariationIDEntrySet* target_set) {
lock_.AssertAcquired();
if (command_line_variation_ids.empty()) {
return true;
}
return AddVariationIdsToSet(
base::SplitString(command_line_variation_ids, ",", base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL),
should_dedupe, target_set);
}
std::string VariationsIdsProvider::GetClientDataHeader(
bool is_signed_in,
Study_GoogleWebVisibility web_visibility) {
lock_.AssertAcquired();
auto it = variations_headers_map_.find(
VariationsHeaderKey{is_signed_in, web_visibility});
if (it == variations_headers_map_.end()) {
return std::string();
}
// Deliberately return a copy.
return it->second;
}
std::vector<VariationID> VariationsIdsProvider::GetVariationsVectorImpl(
const std::set<IDCollectionKey>& keys) {
// Get all the active variation ids while holding the lock.
std::vector<VariationID> result;
{
base::AutoLock scoped_lock(lock_);
MaybeUpdateVariationIDsAndHeaders();
// Copy the requested variations to the output vector.
result.reserve(active_variation_ids_set_.size());
for (const auto& entry : active_variation_ids_set_) {
if (keys.find(entry.second) != keys.end()) {
result.push_back(entry.first);
}
}
}
// Sort the ids and remove duplicates. This makes the representation of any
// given combination of variation ids deterministic.
MakeSortedAndUnique(&result);
return result;
}
bool VariationsIdsProvider::IsDuplicateId(VariationID id) {
lock_.AssertAcquired();
for (int i = 0; i < ID_COLLECTION_COUNT; ++i) {
const IDCollectionKey key = static_cast<IDCollectionKey>(i);
// GOOGLE_APP ids may be duplicated. Further validation is done in
// GroupMapAccessor::ValidateID().
if (key == GOOGLE_APP) {
continue;
}
const VariationIDEntry entry(id, key);
if (base::Contains(force_enabled_ids_set_, entry) ||
base::Contains(synthetic_variation_ids_set_, entry)) {
return true;
}
}
return false;
}
base::Time VariationsIdsProvider::GetCurrentTime() const {
lock_.AssertAcquired();
return clock_func_ ? clock_func_.Run() : base::Time::Now();
}
void VariationsIdsProvider::SetLastUpdateTime(base::Time time) {
lock_.AssertAcquired();
last_update_time_ = time;
}
void VariationsIdsProvider::ResetLastUpdateTime() {
SetLastUpdateTime(base::Time::Min());
}
void VariationsIdsProvider::SetNextUpdateTime(base::Time time) {
lock_.AssertAcquired();
next_update_time_ = time;
}
bool VariationsIdsProvider::UpdateIsNeeded(base::Time current_time) const {
// The `last_update_time_` and prospective `next_update_time_` are cached
// whenever an update is performed. See `MaybeUpdateVariationIDsAndHeaders()`.
//
// When a new field trial is activated, `last_update_time_` is reset to
// `base::Time::Min()`. See `OnFieldTrialGroupFinalized()`. Similarly, if the
// force-enabled, force-disabled or synthetic trial ids are updated,
// `last_update_time_` is reset to `base::Time::Min()`. This forces a
// re-calculation of the cached header values. We do this because any new
// ids may be time-boxed with a window that starts not only before the
// `current_time`, but also before the `last_update_time_`.
//
// Otherwise, the active field trials have not changed, and we can rely on
// the cached `next_update_time_` to determine whether an update is needed.
lock_.AssertAcquired();
return last_update_time_ == base::Time::Min() ||
current_time >= next_update_time_;
}
} // namespace variations
|