#include "content/browser/renderer_host/frame_token_message_queue.h"
#include "base/functional/bind.h"
namespace content {
FrameTokenMessageQueue::FrameTokenMessageQueue() = default;
FrameTokenMessageQueue::~FrameTokenMessageQueue() = default;
void FrameTokenMessageQueue::Init(Client* client) {
client_ = client;
}
void FrameTokenMessageQueue::DidProcessFrame(uint32_t frame_token,
base::TimeTicks activation_time) {
if (callback_map_.empty()) {
last_received_frame_token_ = frame_token;
last_received_activation_time_ = activation_time;
return;
}
if ((frame_token <= last_received_frame_token_) &&
!(last_received_frame_token_reset_ &&
last_received_frame_token_reset_ != frame_token)) {
client_->OnInvalidFrameToken(frame_token);
return;
}
last_received_frame_token_ = frame_token;
last_received_activation_time_ = activation_time;
auto upper_bound = callback_map_.upper_bound(frame_token);
for (auto it = callback_map_.begin(); it != upper_bound; ++it)
std::move(it->second).Run(activation_time);
callback_map_.erase(callback_map_.begin(), upper_bound);
}
void FrameTokenMessageQueue::EnqueueOrRunFrameTokenCallback(
uint32_t frame_token,
base::OnceCallback<void(base::TimeTicks)> callback) {
if (frame_token <= last_received_frame_token_) {
std::move(callback).Run(last_received_activation_time_);
return;
}
callback_map_.insert(std::make_pair(frame_token, std::move(callback)));
}
void FrameTokenMessageQueue::Reset() {
last_received_frame_token_reset_ = last_received_frame_token_;
last_received_frame_token_ = 0;
callback_map_.clear();
}
}