Files
cdesouza-chromium 5cf18451d5 [CodeHealth] Use base::DictValue/base::ListValue - Part VI (#33697)
These classes were hoisted and renamed. This has been replaced in
Chromium as well. This is a mechanical change for Brave, done with the
following script.

```
git grep -lw 'Value::List' | xargs sed -i 's/\bValue::List\b/ListValue/g'
git grep -lw 'Value::Dict' | xargs sed -i 's/\bValue::Dict\b/DictValue/g'
git cl format
```

Chromium changes:
https://chromium.googlesource.com/chromium/src/+/6bc468d481835992696083e99e556516fb7f5f80

```
commit 6bc468d481835992696083e99e556516fb7f5f80
Author: Avi Drissman <avi@chromium.org>
Date:   Thu Jan 29 22:14:50 2026 -0800

    Remove aliases for base::DictValue and base::ListValue

    This removes a few last stragglers as well.

    Fixed: 478100525
    Cq-Include-Trybots: luci.chromium.try:win-official,mac-official,linux-official,android-official,android-desktop-x64-official
    Change-Id: If92142b8ab0562a82c609b71c6b2a7665cea6ec6
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7513889
    Auto-Submit: Avi Drissman <avi@chromium.org>
    Commit-Queue: Daniel Cheng <dcheng@chromium.org>
    Owners-Override: Daniel Cheng <dcheng@chromium.org>
    Reviewed-by: Daniel Cheng <dcheng@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#1577038}
```

Issue: https://github.com/brave/brave-browser/issues/52435
2026-02-05 17:09:26 -03:00

122 lines
3.8 KiB
C++

/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "brave/components/web_discovery/browser/request_queue.h"
#include <utility>
#include "base/json/values_util.h"
#include "base/rand_util.h"
#include "brave/components/web_discovery/browser/util.h"
#include "components/prefs/scoped_user_pref_update.h"
namespace web_discovery {
namespace {
constexpr char kRequestTimeKey[] = "request_time";
constexpr char kRetriesKey[] = "retries";
constexpr char kDataKey[] = "data";
} // namespace
RequestQueue::RequestQueue(
PrefService* profile_prefs,
RequestQueuePrefName list_pref_name,
base::TimeDelta request_max_age,
base::TimeDelta min_request_interval,
base::TimeDelta max_request_interval,
size_t max_retries,
base::RepeatingCallback<void(const base::Value&)> start_request_callback)
: profile_prefs_(profile_prefs),
list_pref_name_(list_pref_name),
backoff_entry_(&kBackoffPolicy),
request_max_age_(request_max_age),
min_request_interval_(min_request_interval),
max_request_interval_(max_request_interval),
max_retries_(max_retries),
start_request_callback_(start_request_callback) {
StartFetchTimer(false);
}
RequestQueue::~RequestQueue() = default;
void RequestQueue::ScheduleRequest(base::DictValue request_data) {
base::DictValue fetch_dict;
fetch_dict.Set(kDataKey, std::move(request_data));
fetch_dict.Set(kRequestTimeKey, base::TimeToValue(base::Time::Now()));
ScopedListPrefUpdate update(profile_prefs_, list_pref_name_.value());
update->Append(std::move(fetch_dict));
if (!fetch_timer_.IsRunning()) {
StartFetchTimer(false);
}
}
std::optional<base::Value> RequestQueue::NotifyRequestComplete(bool success) {
backoff_entry_.InformOfRequest(success);
ScopedListPrefUpdate update(profile_prefs_, list_pref_name_.value());
auto& request_dict = update->front().GetDict();
std::optional<base::Value> removed_value;
bool use_backoff_delta = false;
bool should_remove = success;
if (!success) {
use_backoff_delta = true;
auto retries = request_dict.FindInt(kRetriesKey);
if (retries && static_cast<size_t>(*retries + 1) >= max_retries_) {
should_remove = true;
} else {
request_dict.Set(kRetriesKey, retries.value_or(0) + 1);
}
}
if (should_remove) {
auto* data = request_dict.Find(kDataKey);
removed_value = data ? data->Clone() : base::Value();
update->erase(update->begin());
}
StartFetchTimer(use_backoff_delta);
return removed_value;
}
void RequestQueue::OnFetchTimer() {
ScopedListPrefUpdate update(profile_prefs_, list_pref_name_.value());
for (auto it = update->begin(); it != update->end();) {
const auto* fetch_dict = it->GetIfDict();
const auto* request_time_value =
fetch_dict ? fetch_dict->Find(kRequestTimeKey) : nullptr;
const auto request_time = request_time_value
? base::ValueToTime(*request_time_value)
: std::nullopt;
const auto* data = fetch_dict ? fetch_dict->Find(kDataKey) : nullptr;
if (!request_time ||
(base::Time::Now() - *request_time) > request_max_age_ || !data) {
it = update->erase(it);
continue;
}
start_request_callback_.Run(*data);
return;
}
}
void RequestQueue::StartFetchTimer(bool use_backoff_delta) {
base::TimeDelta delta;
if (use_backoff_delta) {
delta = backoff_entry_.GetTimeUntilRelease();
} else {
delta = base::RandTimeDelta(min_request_interval_, max_request_interval_);
}
fetch_timer_.Start(
FROM_HERE, delta,
base::BindOnce(&RequestQueue::OnFetchTimer, base::Unretained(this)));
}
} // namespace web_discovery