download_task_scheduler_impl.h 1.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TASK_SCHEDULER_IMPL_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TASK_SCHEDULER_IMPL_H_

#include <map>

#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/download/public/task_scheduler.h"

namespace content {
class BrowserContext;
}  // namespace content

// A TaskScheduler implementation that doesn't do anything but posts the task
// after the specified delay.
// If Chrome is shut down, the implementation will not automatically restart it.
class DownloadTaskSchedulerImpl : public download::TaskScheduler {
 public:
  explicit DownloadTaskSchedulerImpl(content::BrowserContext* context);
25
  ~DownloadTaskSchedulerImpl() override;
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

  // TaskScheduler implementation.
  void ScheduleTask(download::DownloadTaskType task_type,
                    bool require_unmetered_network,
                    bool require_charging,
                    long window_start_time_seconds,
                    long window_end_time_seconds) override;
  void CancelTask(download::DownloadTaskType task_type) override;

 private:
  void RunScheduledTask(download::DownloadTaskType task_type);
  void OnTaskFinished(bool reschedule);

  content::BrowserContext* context_;

  // Keeps track of scheduled tasks so that they can be cancelled.
  std::map<download::DownloadTaskType, base::CancelableClosure>
      scheduled_tasks_;

  base::WeakPtrFactory<DownloadTaskSchedulerImpl> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(DownloadTaskSchedulerImpl);
};

#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TASK_SCHEDULER_IMPL_H_