#include "content/browser/background_sync/background_sync_scheduler.h"
#include <algorithm>
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "content/browser/browser_context_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/background_sync_controller.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
namespace content {
DelayedProcessingInfoMap;
BackgroundSyncScheduler* BackgroundSyncScheduler::GetFor(
BrowserContext* browser_context) { … }
BackgroundSyncScheduler::BackgroundSyncScheduler() = default;
BackgroundSyncScheduler::~BackgroundSyncScheduler() { … }
void BackgroundSyncScheduler::ScheduleDelayedProcessing(
StoragePartitionImpl* storage_partition,
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) { … }
void BackgroundSyncScheduler::CancelDelayedProcessing(
StoragePartitionImpl* storage_partition,
blink::mojom::BackgroundSyncType sync_type) { … }
DelayedProcessingInfoMap& BackgroundSyncScheduler::GetDelayedProcessingInfoMap(
blink::mojom::BackgroundSyncType sync_type) { … }
void BackgroundSyncScheduler::RunDelayedTaskAndPruneInfoMap(
blink::mojom::BackgroundSyncType sync_type,
StoragePartitionImpl* storage_partition,
base::OnceClosure delayed_task) { … }
#if BUILDFLAG(IS_ANDROID)
void BackgroundSyncScheduler::ScheduleOrCancelBrowserWakeupForSyncType(
blink::mojom::BackgroundSyncType sync_type,
StoragePartitionImpl* storage_partition) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto* browser_context = storage_partition->browser_context();
DCHECK(browser_context);
auto* controller = browser_context->GetBackgroundSyncController();
DCHECK(controller);
auto& delayed_processing_info = GetDelayedProcessingInfoMap(sync_type);
if (delayed_processing_info.empty()) {
scheduled_wakeup_time_[sync_type] = base::TimeTicks::Max();
controller->CancelBrowserWakeup(sync_type);
return;
}
auto& min_info = *std::min_element(
delayed_processing_info.begin(), delayed_processing_info.end(),
[](auto& lhs, auto& rhs) {
return (lhs.second->desired_run_time() - base::TimeTicks::Now()) <
(rhs.second->desired_run_time() - base::TimeTicks::Now());
});
base::TimeTicks next_time = min_info.second->desired_run_time();
if (next_time >= scheduled_wakeup_time_[sync_type]) {
return;
}
scheduled_wakeup_time_[sync_type] = next_time;
controller->ScheduleBrowserWakeUpWithDelay(
sync_type, min_info.second->desired_run_time() - base::TimeTicks::Now());
}
#endif
}