Commit 9030db5d authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download auto resumption: Ignore downloads started long time ago.

This CL adds an expiration internal to check whether the download can
be auto resumed.

Download later downloads are not affected.

Bug: 1090983
Change-Id: I3bc00e35af6b0e6853465f0ed4e3cc26fc957ecc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2422804Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809122}
parent 06e4c0d1
...@@ -39,6 +39,11 @@ const base::TimeDelta kBatchDownloadUpdatesInterval = ...@@ -39,6 +39,11 @@ const base::TimeDelta kBatchDownloadUpdatesInterval =
const base::TimeDelta kDownloadImmediateRetryDelay = const base::TimeDelta kDownloadImmediateRetryDelay =
base::TimeDelta::FromSeconds(1); base::TimeDelta::FromSeconds(1);
// Any downloads started before this interval will be ignored. User scheduled
// download will not be affected.
const base::TimeDelta kAutoResumptionExpireInterval =
base::TimeDelta::FromDays(7);
// The task type to use for scheduling a task. // The task type to use for scheduling a task.
const download::DownloadTaskType kResumptionTaskType = const download::DownloadTaskType kResumptionTaskType =
download::DownloadTaskType::DOWNLOAD_AUTO_RESUMPTION_TASK; download::DownloadTaskType::DOWNLOAD_AUTO_RESUMPTION_TASK;
...@@ -309,6 +314,13 @@ bool AutoResumptionHandler::IsAutoResumableDownload( ...@@ -309,6 +314,13 @@ bool AutoResumptionHandler::IsAutoResumableDownload(
if (!item || item->IsDangerous()) if (!item || item->IsDangerous())
return false; return false;
// Ignore downloads started a while ago. This doesn't include user scheduled
// downloads.
if (!item->GetDownloadSchedule().has_value() &&
(clock_->Now() - item->GetStartTime() > kAutoResumptionExpireInterval)) {
return false;
}
switch (item->GetState()) { switch (item->GetState()) {
case download::DownloadItem::IN_PROGRESS: case download::DownloadItem::IN_PROGRESS:
return !item->IsPaused(); return !item->IsPaused();
......
...@@ -108,6 +108,10 @@ class AutoResumptionHandlerTest : public testing::Test { ...@@ -108,6 +108,10 @@ class AutoResumptionHandlerTest : public testing::Test {
ON_CALL(*download, GetLastReason()).WillByDefault(Return(last_reason)); ON_CALL(*download, GetLastReason()).WillByDefault(Return(last_reason));
ON_CALL(*download, GetDownloadSchedule()) ON_CALL(*download, GetDownloadSchedule())
.WillByDefault(ReturnRefOfCopy(base::Optional<DownloadSchedule>())); .WillByDefault(ReturnRefOfCopy(base::Optional<DownloadSchedule>()));
// Make sure the item won't be expired and ignored.
ON_CALL(*download, GetStartTime())
.WillByDefault(Return(GetNow() - base::TimeDelta::FromDays(1)));
} }
void SetNetworkConnectionType(ConnectionType connection_type) { void SetNetworkConnectionType(ConnectionType connection_type) {
...@@ -280,6 +284,39 @@ TEST_F(AutoResumptionHandlerTest, ...@@ -280,6 +284,39 @@ TEST_F(AutoResumptionHandlerTest,
task_runner_->FastForwardUntilNoTasksRemain(); task_runner_->FastForwardUntilNoTasksRemain();
} }
TEST_F(AutoResumptionHandlerTest, ExpiredDownloadNotAutoResumed) {
SetNetworkConnectionType(ConnectionType::CONNECTION_WIFI);
// Create a normal expired download.
base::Time expired_start_time = GetNow() - base::TimeDelta::FromDays(100);
auto item0 = std::make_unique<NiceMock<MockDownloadItem>>();
SetDownloadState(item0.get(), DownloadItem::INTERRUPTED, false, false);
ON_CALL(*item0.get(), GetStartTime())
.WillByDefault(Return(expired_start_time));
// Create an expired user scheduled download.
auto item1 = std::make_unique<NiceMock<MockDownloadItem>>();
SetDownloadState(item1.get(), DownloadItem::INTERRUPTED, false, false);
SetDownloadSchedule(item1.get(),
DownloadSchedule(true /*only_on_wifi*/, base::nullopt));
ON_CALL(*item1.get(), GetStartTime())
.WillByDefault(Return(expired_start_time));
auto_resumption_handler_->OnDownloadStarted(item0.get());
auto_resumption_handler_->OnDownloadStarted(item1.get());
task_runner_->FastForwardUntilNoTasksRemain();
// Expired downoad |item0| won't be resumed. Expired user scheduled download
// |item1| will still be resumed.
EXPECT_CALL(*item0.get(), Resume(_)).Times(0);
EXPECT_CALL(*item1.get(), Resume(_)).Times(1);
TaskFinishedCallback callback;
auto_resumption_handler_->OnStartScheduledTask(
DownloadTaskType::DOWNLOAD_AUTO_RESUMPTION_TASK, std::move(callback));
task_runner_->FastForwardUntilNoTasksRemain();
}
TEST_F(AutoResumptionHandlerTest, DownloadWithoutTargetPathNotAutoResumed) { TEST_F(AutoResumptionHandlerTest, DownloadWithoutTargetPathNotAutoResumed) {
SetNetworkConnectionType(ConnectionType::CONNECTION_WIFI); SetNetworkConnectionType(ConnectionType::CONNECTION_WIFI);
auto item = std::make_unique<NiceMock<MockDownloadItem>>(); auto item = std::make_unique<NiceMock<MockDownloadItem>>();
......
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