Commit b43687e1 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

Migrate TaskFinishedCallback to OnceCallback.

Also updated DownloadServiceImpl's pending tasks and actions to use
OnceClosure and fixed lint errors in modified files.

Bug: 714018
Change-Id: I3efdae52913657519dfca2bd1aa7b46aa98ae45d
Reviewed-on: https://chromium-review.googlesource.com/1117311Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarCarlos Knippschild <carlosk@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Sky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#571573}
parent d5296cb7
......@@ -32,13 +32,13 @@ void JNI_DownloadBackgroundTask_StartBackgroundTask(
DCHECK(profile);
TaskFinishedCallback finish_callback =
base::Bind(&CallTaskFinishedCallback,
base::android::ScopedJavaGlobalRef<jobject>(jcallback));
base::BindOnce(&CallTaskFinishedCallback,
base::android::ScopedJavaGlobalRef<jobject>(jcallback));
DownloadService* download_service =
DownloadServiceFactory::GetForBrowserContext(profile);
download_service->OnStartScheduledTask(
static_cast<DownloadTaskType>(task_type), finish_callback);
static_cast<DownloadTaskType>(task_type), std::move(finish_callback));
}
// static
......
......@@ -18,8 +18,8 @@ void DownloadTaskScheduler::ScheduleTask(DownloadTaskType task_type,
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) {
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_DownloadTaskScheduler_scheduleTask(
env, static_cast<jint>(task_type), require_unmetered_network,
......
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_ANDROID_DOWNLOAD_SERVICE_DOWNLOAD_TASK_SCHEDULER_H_
#include <jni.h>
#include <stdint.h>
#include <memory>
#include "base/macros.h"
......@@ -27,8 +28,8 @@ class DownloadTaskScheduler : public TaskScheduler {
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) override;
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) override;
void CancelTask(DownloadTaskType task_type) override;
private:
......
......@@ -26,8 +26,8 @@ void DownloadTaskSchedulerImpl::ScheduleTask(
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) {
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) {
// We only rely on this for cleanup tasks. Since this doesn't restart Chrome,
// for download tasks it doesn't do much and we handle them outside of task
// scheduler.
......@@ -52,8 +52,8 @@ void DownloadTaskSchedulerImpl::RunScheduledTask(
download::DownloadService* download_service =
DownloadServiceFactory::GetForBrowserContext(context_);
download_service->OnStartScheduledTask(
task_type, base::Bind(&DownloadTaskSchedulerImpl::OnTaskFinished,
weak_factory_.GetWeakPtr()));
task_type, base::BindOnce(&DownloadTaskSchedulerImpl::OnTaskFinished,
weak_factory_.GetWeakPtr()));
}
void DownloadTaskSchedulerImpl::OnTaskFinished(bool reschedule) {
......
......@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TASK_SCHEDULER_IMPL_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TASK_SCHEDULER_IMPL_H_
#include <stdint.h>
#include <map>
#include "base/cancelable_callback.h"
......@@ -29,8 +30,8 @@ class DownloadTaskSchedulerImpl : public download::TaskScheduler {
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) override;
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) override;
void CancelTask(download::DownloadTaskType task_type) override;
private:
......
......@@ -102,7 +102,7 @@ class Controller {
// See DownloadService::OnStartScheduledTask.
virtual void OnStartScheduledTask(DownloadTaskType task_type,
const TaskFinishedCallback& callback) = 0;
TaskFinishedCallback callback) = 0;
// See DownloadService::OnStopScheduledTask.
virtual bool OnStopScheduledTask(DownloadTaskType task_type) = 0;
......
......@@ -5,9 +5,8 @@
#include "components/download/internal/background_service/controller_impl.h"
#include <inttypes.h>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
......@@ -304,10 +303,9 @@ DownloadClient ControllerImpl::GetOwnerOfDownload(const std::string& guid) {
return entry ? entry->client : DownloadClient::INVALID;
}
void ControllerImpl::OnStartScheduledTask(
DownloadTaskType task_type,
const TaskFinishedCallback& callback) {
task_finished_callbacks_[task_type] = callback;
void ControllerImpl::OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) {
task_finished_callbacks_[task_type] = std::move(callback);
switch (controller_state_) {
case State::READY:
......
......@@ -8,6 +8,8 @@
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/cancelable_callback.h"
#include "base/macros.h"
......@@ -78,7 +80,7 @@ class ControllerImpl : public Controller,
const SchedulingParams& params) override;
DownloadClient GetOwnerOfDownload(const std::string& guid) override;
void OnStartScheduledTask(DownloadTaskType task_type,
const TaskFinishedCallback& callback) override;
TaskFinishedCallback callback) override;
bool OnStopScheduledTask(DownloadTaskType task_type) override;
private:
......
......@@ -4,8 +4,10 @@
#include "components/download/internal/background_service/controller_impl.h"
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/guid.h"
......@@ -104,7 +106,7 @@ class MockTaskScheduler : public TaskScheduler {
// TaskScheduler implementation.
MOCK_METHOD6(ScheduleTask,
void(DownloadTaskType, bool, bool, int, long, long));
void(DownloadTaskType, bool, bool, int, int64_t, int64_t));
MOCK_METHOD1(CancelTask, void(DownloadTaskType));
};
......@@ -496,7 +498,7 @@ TEST_F(DownloadServiceControllerImplTest,
store_->TriggerInit(true, std::make_unique<std::vector<Entry>>(entries));
file_monitor_->TriggerInit(true);
controller_->OnStartScheduledTask(DownloadTaskType::CLEANUP_TASK,
base::Bind(&NotifyTaskFinished));
base::BindOnce(&NotifyTaskFinished));
task_runner_->RunUntilIdle();
controller_->OnStopScheduledTask(DownloadTaskType::CLEANUP_TASK);
......@@ -1025,7 +1027,7 @@ TEST_F(DownloadServiceControllerImplTest, OnDownloadSucceeded) {
base::Time now = base::Time::Now();
done_dentry.completion_time = now;
long start_time = 0;
int64_t start_time = 0;
EXPECT_CALL(*task_scheduler_,
ScheduleTask(DownloadTaskType::CLEANUP_TASK, _, _, _, _, _))
.WillOnce(SaveArg<4>(&start_time));
......@@ -1889,7 +1891,7 @@ TEST_F(DownloadServiceControllerImplTest, DownloadTaskQueuesAfterFinish) {
// Simulate a task start, which should limit our calls to Reschedule() because
// we are in a task.
controller_->OnStartScheduledTask(DownloadTaskType::DOWNLOAD_TASK,
base::Bind(&NotifyTaskFinished));
base::BindOnce(&NotifyTaskFinished));
// Set up new expectations to start a new download.
ON_CALL(*scheduler_, Next(_, _))
......@@ -1954,7 +1956,7 @@ TEST_F(DownloadServiceControllerImplTest, CleanupTaskQueuesAfterFinish) {
ScheduleTask(DownloadTaskType::CLEANUP_TASK, _, _, _, _, _))
.Times(0);
controller_->OnStartScheduledTask(DownloadTaskType::CLEANUP_TASK,
base::Bind(&NotifyTaskFinished));
base::BindOnce(&NotifyTaskFinished));
// Trigger download succeed events, which should not schedule a cleanup until
// the existing cleanup has finished.
......
......@@ -4,6 +4,8 @@
#include "components/download/internal/background_service/download_service_impl.h"
#include <utility>
#include "base/bind.h"
#include "base/strings/string_util.h"
#include "components/download/internal/background_service/controller.h"
......@@ -31,17 +33,16 @@ const ServiceConfig& DownloadServiceImpl::GetConfig() {
return service_config_;
}
void DownloadServiceImpl::OnStartScheduledTask(
DownloadTaskType task_type,
const TaskFinishedCallback& callback) {
void DownloadServiceImpl::OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) {
if (startup_completed_) {
controller_->OnStartScheduledTask(task_type, callback);
controller_->OnStartScheduledTask(task_type, std::move(callback));
return;
}
pending_tasks_[task_type] =
base::Bind(&Controller::OnStartScheduledTask,
base::Unretained(controller_.get()), task_type, callback);
pending_tasks_[task_type] = base::BindOnce(
&Controller::OnStartScheduledTask, base::Unretained(controller_.get()),
task_type, base::Passed(&callback));
}
bool DownloadServiceImpl::OnStopScheduledTask(DownloadTaskType task_type) {
......@@ -53,7 +54,7 @@ bool DownloadServiceImpl::OnStopScheduledTask(DownloadTaskType task_type) {
if (iter != pending_tasks_.end()) {
// We still need to run the callback in order to properly cleanup and notify
// the system by running the respective task finished callbacks.
iter->second.Run();
std::move(iter->second).Run();
pending_tasks_.erase(iter);
}
......@@ -81,9 +82,9 @@ void DownloadServiceImpl::StartDownload(const DownloadParams& download_params) {
if (startup_completed_) {
controller_->StartDownload(download_params);
} else {
pending_actions_.push_back(base::Bind(&Controller::StartDownload,
base::Unretained(controller_.get()),
download_params));
pending_actions_.push_back(
base::BindOnce(&Controller::StartDownload,
base::Unretained(controller_.get()), download_params));
}
}
......@@ -93,7 +94,7 @@ void DownloadServiceImpl::PauseDownload(const std::string& guid) {
if (startup_completed_) {
controller_->PauseDownload(guid);
} else {
pending_actions_.push_back(base::Bind(
pending_actions_.push_back(base::BindOnce(
&Controller::PauseDownload, base::Unretained(controller_.get()), guid));
}
}
......@@ -104,9 +105,9 @@ void DownloadServiceImpl::ResumeDownload(const std::string& guid) {
if (startup_completed_) {
controller_->ResumeDownload(guid);
} else {
pending_actions_.push_back(base::Bind(&Controller::ResumeDownload,
base::Unretained(controller_.get()),
guid));
pending_actions_.push_back(
base::BindOnce(&Controller::ResumeDownload,
base::Unretained(controller_.get()), guid));
}
}
......@@ -116,9 +117,9 @@ void DownloadServiceImpl::CancelDownload(const std::string& guid) {
if (startup_completed_) {
controller_->CancelDownload(guid);
} else {
pending_actions_.push_back(base::Bind(&Controller::CancelDownload,
base::Unretained(controller_.get()),
guid));
pending_actions_.push_back(
base::BindOnce(&Controller::CancelDownload,
base::Unretained(controller_.get()), guid));
}
}
......@@ -130,9 +131,9 @@ void DownloadServiceImpl::ChangeDownloadCriteria(
if (startup_completed_) {
controller_->ChangeDownloadCriteria(guid, params);
} else {
pending_actions_.push_back(base::Bind(&Controller::ChangeDownloadCriteria,
base::Unretained(controller_.get()),
guid, params));
pending_actions_.push_back(
base::BindOnce(&Controller::ChangeDownloadCriteria,
base::Unretained(controller_.get()), guid, params));
}
}
......@@ -142,14 +143,14 @@ Logger* DownloadServiceImpl::GetLogger() {
void DownloadServiceImpl::OnControllerInitialized() {
while (!pending_actions_.empty()) {
auto callback = pending_actions_.front();
callback.Run();
auto callback = std::move(pending_actions_.front());
pending_actions_.pop_front();
std::move(callback).Run();
}
while (!pending_tasks_.empty()) {
auto iter = pending_tasks_.begin();
iter->second.Run();
std::move(iter->second).Run();
pending_tasks_.erase(iter);
}
......
......@@ -34,7 +34,7 @@ class DownloadServiceImpl : public DownloadService {
// DownloadService implementation.
const ServiceConfig& GetConfig() override;
void OnStartScheduledTask(DownloadTaskType task_type,
const TaskFinishedCallback& callback) override;
TaskFinishedCallback callback) override;
bool OnStopScheduledTask(DownloadTaskType task_type) override;
ServiceStatus GetStatus() override;
void StartDownload(const DownloadParams& download_params) override;
......@@ -56,8 +56,8 @@ class DownloadServiceImpl : public DownloadService {
std::unique_ptr<Controller> controller_;
ServiceConfigImpl service_config_;
base::circular_deque<base::Closure> pending_actions_;
std::map<DownloadTaskType, base::Closure> pending_tasks_;
base::circular_deque<base::OnceClosure> pending_actions_;
std::map<DownloadTaskType, base::OnceClosure> pending_tasks_;
bool startup_completed_;
DISALLOW_COPY_AND_ASSIGN(DownloadServiceImpl);
......
......@@ -14,8 +14,8 @@ void EmptyTaskScheduler::ScheduleTask(DownloadTaskType task_type,
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) {}
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) {}
void EmptyTaskScheduler::CancelTask(DownloadTaskType task_type) {}
......
......@@ -5,9 +5,10 @@
#ifndef COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_EMPTY_TASK_SCHEDULER_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_EMPTY_TASK_SCHEDULER_H_
#include "components/download/public/background_service/task_scheduler.h"
#include <stdint.h>
#include "base/macros.h"
#include "components/download/public/background_service/task_scheduler.h"
namespace download {
......@@ -23,8 +24,8 @@ class EmptyTaskScheduler : public TaskScheduler {
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) override;
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) override;
void CancelTask(DownloadTaskType task_type) override;
DISALLOW_COPY_AND_ASSIGN(EmptyTaskScheduler);
......
......@@ -4,6 +4,7 @@
#include "components/download/internal/background_service/scheduler/scheduler_impl.h"
#include <stdint.h>
#include <memory>
#include "base/strings/string_number_conversions.h"
......@@ -26,7 +27,7 @@ class MockTaskScheduler : public TaskScheduler {
~MockTaskScheduler() override = default;
MOCK_METHOD6(ScheduleTask,
void(DownloadTaskType, bool, bool, int, long, long));
void(DownloadTaskType, bool, bool, int, int64_t, int64_t));
MOCK_METHOD1(CancelTask, void(DownloadTaskType));
};
......
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_TEST_MOCK_CONTROLLER_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_TEST_MOCK_CONTROLLER_H_
#include <string>
#include "base/macros.h"
#include "components/download/internal/background_service/controller.h"
#include "components/download/internal/background_service/startup_status.h"
......@@ -30,7 +32,7 @@ class MockController : public Controller {
void(const std::string&, const SchedulingParams&));
MOCK_METHOD1(GetOwnerOfDownload, DownloadClient(const std::string&));
MOCK_METHOD2(OnStartScheduledTask,
void(DownloadTaskType, const TaskFinishedCallback&));
void(DownloadTaskType, TaskFinishedCallback));
MOCK_METHOD1(OnStopScheduledTask, bool(DownloadTaskType task_type));
void TriggerInitCompleted();
......
......@@ -25,7 +25,7 @@ class ServiceConfig;
struct DownloadParams;
struct SchedulingParams;
using TaskFinishedCallback = base::Callback<void(bool)>;
using TaskFinishedCallback = base::OnceCallback<void(bool)>;
// A service responsible for helping facilitate the scheduling and downloading
// of file content from the web. See |DownloadParams| for more details on the
......@@ -62,7 +62,7 @@ class DownloadService : public KeyedService {
// or OnStopScheduledTask is invoked by the system. Do not call this method
// directly.
virtual void OnStartScheduledTask(DownloadTaskType task_type,
const TaskFinishedCallback& callback) = 0;
TaskFinishedCallback callback) = 0;
// Callback method to run by the service if the system decides to stop the
// task. Returns true if the task needs to be rescheduled. Any pending
......
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_TASK_SCHEDULER_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_TASK_SCHEDULER_H_
#include <stdint.h>
#include "components/download/public/background_service/download_task_types.h"
namespace download {
......@@ -23,8 +25,8 @@ class TaskScheduler {
bool require_unmetered_network,
bool require_charging,
int optimal_battery_percentage,
long window_start_time_seconds,
long window_end_time_seconds) = 0;
int64_t window_start_time_seconds,
int64_t window_end_time_seconds) = 0;
// Cancels a pre-scheduled task of type |task_type|.
virtual void CancelTask(DownloadTaskType task_type) = 0;
......
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_TEST_MOCK_DOWNLOAD_SERVICE_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_TEST_MOCK_DOWNLOAD_SERVICE_H_
#include <string>
#include "base/macros.h"
#include "components/download/public/background_service/download_params.h"
#include "components/download/public/background_service/download_service.h"
......@@ -22,8 +24,7 @@ class MockDownloadService : public DownloadService {
// DownloadService implementation.
MOCK_METHOD0(GetConfig, const ServiceConfig&());
MOCK_METHOD2(OnStartScheduledTask,
void(DownloadTaskType task_type,
const TaskFinishedCallback& callback));
void(DownloadTaskType task_type, TaskFinishedCallback callback));
MOCK_METHOD1(OnStopScheduledTask, bool(DownloadTaskType task_type));
MOCK_METHOD0(GetStatus, ServiceStatus());
MOCK_METHOD1(StartDownload, void(const DownloadParams& download_params));
......
......@@ -4,8 +4,6 @@
#include "components/download/public/background_service/test/test_download_service.h"
#include <memory>
#include "base/bind.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/download/public/background_service/client.h"
......@@ -55,9 +53,8 @@ const ServiceConfig& TestDownloadService::GetConfig() {
return *service_config_;
}
void TestDownloadService::OnStartScheduledTask(
DownloadTaskType task_type,
const TaskFinishedCallback& callback) {}
void TestDownloadService::OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) {}
bool TestDownloadService::OnStopScheduledTask(DownloadTaskType task_type) {
return true;
......
......@@ -6,6 +6,8 @@
#define COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_TEST_TEST_DOWNLOAD_SERVICE_H_
#include <list>
#include <memory>
#include <string>
#include "base/optional.h"
#include "components/download/public/background_service/client.h"
......@@ -21,13 +23,13 @@ namespace test {
// Implementation of DownloadService used for testing.
class TestDownloadService : public DownloadService {
public:
explicit TestDownloadService();
TestDownloadService();
~TestDownloadService() override;
// DownloadService implementation.
const ServiceConfig& GetConfig() override;
void OnStartScheduledTask(DownloadTaskType task_type,
const TaskFinishedCallback& callback) override;
TaskFinishedCallback callback) override;
bool OnStopScheduledTask(DownloadTaskType task_type) override;
DownloadService::ServiceStatus GetStatus() override;
void StartDownload(const DownloadParams& download_params) override;
......
......@@ -78,7 +78,7 @@ void TestDownloadService::SetTestFileData(const std::string& data) {
void TestDownloadService::OnStartScheduledTask(
download::DownloadTaskType task_type,
const download::TaskFinishedCallback& callback) {
download::TaskFinishedCallback callback) {
NOTIMPLEMENTED();
}
bool TestDownloadService::OnStopScheduledTask(
......
......@@ -6,6 +6,7 @@
#define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_TEST_DOWNLOAD_SERVICE_H_
#include <list>
#include <string>
#include "base/files/scoped_temp_dir.h"
#include "base/optional.h"
......@@ -19,14 +20,13 @@ namespace offline_pages {
// Implementation of DownloadService used for testing.
class TestDownloadService : public download::DownloadService {
public:
explicit TestDownloadService();
TestDownloadService();
~TestDownloadService() override;
// DownloadService implementation.
const download::ServiceConfig& GetConfig() override;
void OnStartScheduledTask(
download::DownloadTaskType task_type,
const download::TaskFinishedCallback& callback) override;
void OnStartScheduledTask(download::DownloadTaskType task_type,
download::TaskFinishedCallback callback) override;
bool OnStopScheduledTask(download::DownloadTaskType task_type) override;
DownloadService::ServiceStatus GetStatus() override;
void StartDownload(const download::DownloadParams& download_params) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment