Skip to content

Commit

Permalink
[ios] Fix crash related to download and prerendering
Browse files Browse the repository at this point in the history
When a WebState is removed from the WebStateList while a download is
pending/started/complete, it needs to inform the delegate so that it
can be stopped.

This requires remembering in DownloadManagerTabHelper whether the
delegate has been started, which happens from `DidCreateDownload()`
if the WebState is visible by that point or `WasShown()`.

(cherry picked from commit 794decd)

Fixed: 1317293
Change-Id: I72dedbf8c03107405edb6b1fc4abf4e150dee951
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3592976
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: Harry Souders <harrysouders@google.com>
Commit-Queue: Harry Souders <harrysouders@google.com>
Cr-Original-Commit-Position: refs/heads/main@{#993753}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3594442
Reviewed-by: Krishna Govind <govind@chromium.org>
Commit-Queue: Krishna Govind <govind@chromium.org>
Owners-Override: Krishna Govind <govind@chromium.org>
Cr-Commit-Position: refs/branch-heads/5005@{#33}
Cr-Branched-From: 5b4d945-refs/heads/main@{#992738}
  • Loading branch information
sdefresne authored and Chromium LUCI CQ committed Apr 19, 2022
1 parent 16e7d39 commit 75da76c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions ios/chrome/browser/download/download_manager_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class DownloadManagerTabHelper
web::WebState* web_state_ = nullptr;
__weak id<DownloadManagerTabHelperDelegate> delegate_ = nil;
std::unique_ptr<web::DownloadTask> task_;
bool delegate_started_ = false;

WEB_STATE_USER_DATA_KEY_DECL();
};
Expand Down
22 changes: 17 additions & 5 deletions ios/chrome/browser/download/download_manager_tab_helper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@

void DownloadManagerTabHelper::SetDelegate(
id<DownloadManagerTabHelperDelegate> delegate) {
if (delegate == delegate_)
return;

if (delegate_ && task_ && delegate_started_)
[delegate_ downloadManagerTabHelper:this didHideDownload:task_.get()];

delegate_started_ = false;
delegate_ = delegate;
}

void DownloadManagerTabHelper::WasShown(web::WebState* web_state) {
if (task_) {
if (task_ && delegate_) {
delegate_started_ = true;
[delegate_ downloadManagerTabHelper:this didShowDownload:task_.get()];
}
}

void DownloadManagerTabHelper::WasHidden(web::WebState* web_state) {
if (task_) {
if (task_ && delegate_) {
delegate_started_ = false;
[delegate_ downloadManagerTabHelper:this didHideDownload:task_.get()];
}
}
Expand Down Expand Up @@ -104,9 +113,12 @@
}
task_ = std::move(task);
task_->AddObserver(this);
[delegate_ downloadManagerTabHelper:this
didCreateDownload:task_.get()
webStateIsVisible:web_state_->IsVisible()];
if (web_state_->IsVisible() && delegate_) {
delegate_started_ = true;
[delegate_ downloadManagerTabHelper:this
didCreateDownload:task_.get()
webStateIsVisible:true];
}
}

WEB_STATE_USER_DATA_KEY_IMPL(DownloadManagerTabHelper)

0 comments on commit 75da76c

Please sign in to comment.