Added DownloadTask::Cancel() API

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 <[email protected]>
Reviewed-by: Sylvain Defresne <[email protected]>
Cr-Commit-Position: refs/heads/master@{#525046}
diff --git a/ios/web/download/download_task_impl.h b/ios/web/download/download_task_impl.h
index 8a5948e..c785c05f 100644
--- a/ios/web/download/download_task_impl.h
+++ b/ios/web/download/download_task_impl.h
@@ -57,6 +57,7 @@
   // 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;
diff --git a/ios/web/download/download_task_impl.mm b/ios/web/download/download_task_impl.mm
index 7bee9bc..f3e38f3 100644
--- a/ios/web/download/download_task_impl.mm
+++ b/ios/web/download/download_task_impl.mm
@@ -177,6 +177,14 @@
                         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();
@@ -194,7 +202,7 @@
 
 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 {
diff --git a/ios/web/download/download_task_impl_unittest.mm b/ios/web/download/download_task_impl_unittest.mm
index 52a9b3e..b83dc651 100644
--- a/ios/web/download/download_task_impl_unittest.mm
+++ b/ios/web/download/download_task_impl_unittest.mm
@@ -231,6 +231,25 @@
   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) {
diff --git a/ios/web/public/download/download_task.h b/ios/web/public/download/download_task.h
index 6472ce0..2fbc852 100644
--- a/ios/web/public/download/download_task.h
+++ b/ios/web/public/download/download_task.h
@@ -34,6 +34,9 @@
     // Download is actively progressing.
     kInProgress,
 
+    // Download is cancelled.
+    kCancelled,
+
     // Download is completely finished.
     kComplete,
   };
@@ -45,6 +48,9 @@
   // 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;
diff --git a/ios/web/public/test/fakes/fake_download_task.h b/ios/web/public/test/fakes/fake_download_task.h
index 30f94dff..2388c00 100644
--- a/ios/web/public/test/fakes/fake_download_task.h
+++ b/ios/web/public/test/fakes/fake_download_task.h
@@ -23,6 +23,7 @@
   // 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;
diff --git a/ios/web/public/test/fakes/fake_download_task.mm b/ios/web/public/test/fakes/fake_download_task.mm
index 57082b6..9c483b1 100644
--- a/ios/web/public/test/fakes/fake_download_task.mm
+++ b/ios/web/public/test/fakes/fake_download_task.mm
@@ -29,6 +29,11 @@
   OnDownloadUpdated();
 }
 
+void FakeDownloadTask::Cancel() {
+  state_ = State::kCancelled;
+  OnDownloadUpdated();
+}
+
 net::URLFetcherResponseWriter* FakeDownloadTask::GetResponseWriter() const {
   return writer_.get();
 }