File: refresh_task_scheduler_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (80 lines) | stat: -rw-r--r-- 2,776 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/feed/android/refresh_task_scheduler_impl.h"

#include <utility>

#include "components/background_task_scheduler/background_task_scheduler.h"
#include "components/background_task_scheduler/task_ids.h"
#include "components/background_task_scheduler/task_info.h"
#include "components/feed/core/v2/config.h"
#include "components/feed/core/v2/public/feed_api.h"
#include "components/feed/core/v2/public/feed_service.h"

namespace feed {

namespace {
background_task::TaskIds ToBackgroundTaskId(RefreshTaskId id) {
  switch (id) {
    case RefreshTaskId::kRefreshForYouFeed:
      return background_task::TaskIds::FEEDV2_REFRESH_JOB_ID;
    case RefreshTaskId::kRefreshWebFeed:
      return background_task::TaskIds::WEBFEEDS_REFRESH_JOB_ID;
  }
}
}  // namespace

RefreshTaskSchedulerImpl::RefreshTaskSchedulerImpl(
    background_task::BackgroundTaskScheduler* scheduler)
    : scheduler_(scheduler) {
  DCHECK(scheduler_);
}
RefreshTaskSchedulerImpl::~RefreshTaskSchedulerImpl() = default;

void RefreshTaskSchedulerImpl::Run(RefreshTaskId task_id,
                                   FeedService* service,
                                   base::OnceClosure task_complete) {
  TaskCallback(task_id) = std::move(task_complete);
  service->GetStream()->ExecuteRefreshTask(task_id);
}

void RefreshTaskSchedulerImpl::EnsureScheduled(RefreshTaskId task_id,
                                               base::TimeDelta delay) {
  background_task::OneOffInfo one_off;
  one_off.window_start_time_ms = delay.InMilliseconds();
  one_off.window_end_time_ms =
      delay.InMilliseconds() +
      GetFeedConfig().background_refresh_window_length.InMilliseconds();
  one_off.expires_after_window_end_time = true;
  background_task::TaskInfo task_info(
      static_cast<int>(ToBackgroundTaskId(task_id)), one_off);
  task_info.is_persisted = true;
  task_info.update_current = true;
  task_info.network_type = background_task::TaskInfo::ANY;
  scheduler_->Schedule(task_info);
}

void RefreshTaskSchedulerImpl::Cancel(RefreshTaskId task_id) {
  scheduler_->Cancel(static_cast<int>(ToBackgroundTaskId(task_id)));
}

void RefreshTaskSchedulerImpl::RefreshTaskComplete(RefreshTaskId task_id) {
  base::OnceClosure& callback = TaskCallback(task_id);
  if (callback) {
    std::move(callback).Run();
  }
}

base::OnceClosure& RefreshTaskSchedulerImpl::TaskCallback(
    RefreshTaskId task_id) {
  switch (task_id) {
    case RefreshTaskId::kRefreshForYouFeed:
      return for_you_task_complete_callback_;
    case RefreshTaskId::kRefreshWebFeed:
      return web_feeds_task_complete_callback_;
  }
}

}  // namespace feed