Skip to content

Commit

Permalink
Added DownloadTask::Cancel() API
Browse files Browse the repository at this point in the history
Download Manager will use this API to cancel the download.

Bug: 791806
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I7f555dfbd64cad6beeceb3bb27f8880900787eea
Reviewed-on: https://chromium-review.googlesource.com/833237
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525046}
  • Loading branch information
Eugene But authored and Commit Bot committed Dec 19, 2017
1 parent 4f25de7 commit 410df8b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions ios/web/download/download_task_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DownloadTaskImpl : public DownloadTask {
// DownloadTask overrides:
DownloadTask::State GetState() const override;
void Start(std::unique_ptr<net::URLFetcherResponseWriter> writer) override;
void Cancel() override;
net::URLFetcherResponseWriter* GetResponseWriter() const override;
NSString* GetIndentifier() const override;
const GURL& GetOriginalUrl() const override;
Expand Down
10 changes: 9 additions & 1 deletion ios/web/download/download_task_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ - (void)URLSession:(NSURLSession*)session
weak_factory_.GetWeakPtr()));
}

void DownloadTaskImpl::Cancel() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
[session_task_ cancel];
session_task_ = nil;
state_ = State::kCancelled;
OnDownloadUpdated();
}

net::URLFetcherResponseWriter* DownloadTaskImpl::GetResponseWriter() const {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
return writer_.get();
Expand All @@ -194,7 +202,7 @@ - (void)URLSession:(NSURLSession*)session

bool DownloadTaskImpl::IsDone() const {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
return state_ == State::kComplete;
return state_ == State::kComplete || state_ == State::kCancelled;
}

int DownloadTaskImpl::GetErrorCode() const {
Expand Down
19 changes: 19 additions & 0 deletions ios/web/download/download_task_impl_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,25 @@ void SimulateDownloadCompletion(CRWFakeNSURLSessionTask* session_task,
EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}

// Tests cancelling the download task.
TEST_F(DownloadTaskImplTest, Cancelling) {
EXPECT_CALL(task_observer_, OnDownloadUpdated(task_.get()));
CRWFakeNSURLSessionTask* session_task = Start();
ASSERT_TRUE(session_task);
testing::Mock::VerifyAndClearExpectations(&task_observer_);

// Cancel the download.
EXPECT_CALL(task_observer_, OnDownloadUpdated(task_.get()));
task_->Cancel();
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForDownloadTimeout, ^{
return task_->IsDone();
}));
testing::Mock::VerifyAndClearExpectations(&task_observer_);
EXPECT_EQ(DownloadTask::State::kCancelled, task_->GetState());

EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}

// Tests sucessfull download of response with only one
// URLSession:dataTask:didReceiveData: callback.
TEST_F(DownloadTaskImplTest, SmallResponseDownload) {
Expand Down
6 changes: 6 additions & 0 deletions ios/web/public/download/download_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class DownloadTask {
// Download is actively progressing.
kInProgress,

// Download is cancelled.
kCancelled,

// Download is completely finished.
kComplete,
};
Expand All @@ -45,6 +48,9 @@ class DownloadTask {
// in-file downloads and must not be null. Start() can only be called once.
virtual void Start(std::unique_ptr<net::URLFetcherResponseWriter> writer) = 0;

// Cancels the download.
virtual void Cancel() = 0;

// Response writer, which was passed to Start(). Can be used to obtain the
// download data.
virtual net::URLFetcherResponseWriter* GetResponseWriter() const = 0;
Expand Down
1 change: 1 addition & 0 deletions ios/web/public/test/fakes/fake_download_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FakeDownloadTask : public DownloadTask {
// DownloadTask overrides:
DownloadTask::State GetState() const override;
void Start(std::unique_ptr<net::URLFetcherResponseWriter> writer) override;
void Cancel() override;
net::URLFetcherResponseWriter* GetResponseWriter() const override;
NSString* GetIndentifier() const override;
const GURL& GetOriginalUrl() const override;
Expand Down
5 changes: 5 additions & 0 deletions ios/web/public/test/fakes/fake_download_task.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
OnDownloadUpdated();
}

void FakeDownloadTask::Cancel() {
state_ = State::kCancelled;
OnDownloadUpdated();
}

net::URLFetcherResponseWriter* FakeDownloadTask::GetResponseWriter() const {
return writer_.get();
}
Expand Down

0 comments on commit 410df8b

Please sign in to comment.