[go: up one dir, main page]

WebAppInstallTask: Use weak pointers

We replace calls to base::Unretained(this) with GetWeakPtr() in
WebAppInstallTask and WebAppInstallManager.

(cherry picked from commit 25030ef24ece304403cc88cb4fc8c5e8936e0098)

Bug: 1267661
Change-Id: I1ac0d79657d8980e8edf7cc3a2db5f8ece9c9b58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3275867
Auto-Submit: Eric Willigers <ericwilligers@chromium.org>
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: Chase Phillips <cmp@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#941240}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3307742
Commit-Queue: Daniel Murphy <dmurph@chromium.org>
Auto-Submit: Chase Phillips <cmp@chromium.org>
Reviewed-by: Daniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/branch-heads/4664@{#1181}
Cr-Branched-From: 24dc4ee75e01a29d390d43c9c264372a169273a7-refs/heads/main@{#929512}
diff --git a/chrome/browser/web_applications/web_app_install_manager.cc b/chrome/browser/web_applications/web_app_install_manager.cc
index b75e45b..d933b672 100644
--- a/chrome/browser/web_applications/web_app_install_manager.cc
+++ b/chrome/browser/web_applications/web_app_install_manager.cc
@@ -82,7 +82,7 @@
       web_app_url, install_source, url_loader_.get(),
       base::BindOnce(
           &WebAppInstallManager::OnLoadWebAppAndCheckManifestCompleted,
-          base::Unretained(this), task.get(), std::move(callback)));
+          GetWeakPtr(), task.get(), std::move(callback)));
 
   tasks_.insert(std::move(task));
 }
@@ -102,7 +102,7 @@
       contents, bypass_service_worker_check, install_source,
       std::move(dialog_callback),
       base::BindOnce(&WebAppInstallManager::OnInstallTaskCompleted,
-                     base::Unretained(this), task.get(), std::move(callback)));
+                     GetWeakPtr(), task.get(), std::move(callback)));
 
   tasks_.insert(std::move(task));
 }
@@ -121,7 +121,7 @@
   task->InstallWebAppFromManifestWithFallback(
       contents, force_shortcut_app, install_source, std::move(dialog_callback),
       base::BindOnce(&WebAppInstallManager::OnInstallTaskCompleted,
-                     base::Unretained(this), task.get(), std::move(callback)));
+                     GetWeakPtr(), task.get(), std::move(callback)));
 
   tasks_.insert(std::move(task));
 }
@@ -156,7 +156,7 @@
       std::move(web_application_info), overwrite_existing_manifest_fields,
       for_installable_site, install_source,
       base::BindOnce(&WebAppInstallManager::OnInstallTaskCompleted,
-                     base::Unretained(this), task.get(), std::move(callback)));
+                     GetWeakPtr(), task.get(), std::move(callback)));
 
   tasks_.insert(std::move(task));
 }
@@ -174,11 +174,15 @@
   task->InstallWebAppWithParams(
       web_contents, install_params, install_source,
       base::BindOnce(&WebAppInstallManager::OnInstallTaskCompleted,
-                     base::Unretained(this), task.get(), std::move(callback)));
+                     GetWeakPtr(), task.get(), std::move(callback)));
 
   tasks_.insert(std::move(task));
 }
 
+base::WeakPtr<WebAppInstallManager> WebAppInstallManager::GetWeakPtr() {
+  return weak_ptr_factory_.GetWeakPtr();
+}
+
 void WebAppInstallManager::EnqueueInstallAppFromSync(
     const AppId& sync_app_id,
     std::unique_ptr<WebApplicationInfo> web_application_info,
@@ -225,16 +229,15 @@
   OnceInstallCallback task_completed_callback = base::BindOnce(
       &WebAppInstallManager::
           LoadAndInstallWebAppFromManifestWithFallbackCompleted_ForAppSync,
-      base::Unretained(this), sync_app_id, std::move(web_application_info),
+      GetWeakPtr(), sync_app_id, std::move(web_application_info),
       std::move(callback));
 
   base::OnceClosure start_task = base::BindOnce(
       &WebAppInstallTask::LoadAndInstallWebAppFromManifestWithFallback,
       task->GetWeakPtr(), start_url, EnsureWebContentsCreated(),
       base::Unretained(url_loader_.get()), webapps::WebappInstallSource::SYNC,
-      base::BindOnce(&WebAppInstallManager::OnQueuedTaskCompleted,
-                     base::Unretained(this), task.get(),
-                     std::move(task_completed_callback)));
+      base::BindOnce(&WebAppInstallManager::OnQueuedTaskCompleted, GetWeakPtr(),
+                     task.get(), std::move(task_completed_callback)));
 
   EnqueueTask(std::move(task), std::move(start_task));
 }
@@ -374,8 +377,8 @@
       &WebAppInstallTask::InstallWebAppFromInfoRetrieveIcons,
       task->GetWeakPtr(), EnsureWebContentsCreated(),
       std::move(web_application_info), finalize_options,
-      base::BindOnce(&WebAppInstallManager::OnQueuedTaskCompleted,
-                     base::Unretained(this), task.get(), std::move(callback)));
+      base::BindOnce(&WebAppInstallManager::OnQueuedTaskCompleted, GetWeakPtr(),
+                     task.get(), std::move(callback)));
 
   EnqueueTask(std::move(task), std::move(start_task));
 }
@@ -410,7 +413,7 @@
       GURL("about:blank"), web_contents_.get(),
       WebAppUrlLoader::UrlComparison::kExact,
       base::BindOnce(&WebAppInstallManager::OnWebContentsReadyRunTask,
-                     weak_ptr_factory_.GetWeakPtr(), std::move(pending_task)));
+                     GetWeakPtr(), std::move(pending_task)));
 }
 
 void WebAppInstallManager::DeleteTask(WebAppInstallTask* task) {
diff --git a/chrome/browser/web_applications/web_app_install_manager.h b/chrome/browser/web_applications/web_app_install_manager.h
index ba77098..2b01e63 100644
--- a/chrome/browser/web_applications/web_app_install_manager.h
+++ b/chrome/browser/web_applications/web_app_install_manager.h
@@ -143,6 +143,8 @@
   FRIEND_TEST_ALL_PREFIXES(WebAppInstallManagerTest,
                            TaskQueueWebContentsReadyRace);
 
+  base::WeakPtr<WebAppInstallManager> GetWeakPtr();
+
   void EnqueueInstallAppFromSync(
       const AppId& sync_app_id,
       std::unique_ptr<WebApplicationInfo> web_application_info,
diff --git a/chrome/browser/web_applications/web_app_install_task.cc b/chrome/browser/web_applications/web_app_install_task.cc
index 4faad776..9a2d05d 100644
--- a/chrome/browser/web_applications/web_app_install_task.cc
+++ b/chrome/browser/web_applications/web_app_install_task.cc
@@ -127,7 +127,7 @@
       WebAppUrlLoader::UrlComparison::kIgnoreQueryParamsAndRef,
       base::BindOnce(
           &WebAppInstallTask::OnWebAppUrlLoadedCheckAndRetrieveManifest,
-          base::Unretained(this), web_contents_ptr));
+          GetWeakPtr(), web_contents_ptr));
 }
 
 void WebAppInstallTask::InstallWebAppFromManifest(
@@ -152,7 +152,7 @@
   data_retriever_->CheckInstallabilityAndRetrieveManifest(
       web_contents(), bypass_service_worker_check,
       base::BindOnce(&WebAppInstallTask::OnDidPerformInstallableCheck,
-                     base::Unretained(this), std::move(web_app_info),
+                     GetWeakPtr(), std::move(web_app_info),
                      /*force_shortcut_app=*/false));
 }
 
@@ -172,8 +172,8 @@
 
   data_retriever_->GetWebApplicationInfo(
       web_contents(),
-      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo,
-                     base::Unretained(this), force_shortcut_app));
+      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo, GetWeakPtr(),
+                     force_shortcut_app));
 }
 
 void WebAppInstallTask::LoadAndInstallWebAppFromManifestWithFallback(
@@ -268,8 +268,8 @@
 
   data_retriever_->GetWebApplicationInfo(
       web_contents(),
-      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo,
-                     base::Unretained(this), /*force_shortcut_app=*/false));
+      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo, GetWeakPtr(),
+                     /*force_shortcut_app=*/false));
 }
 
 void WebAppInstallTask::UpdateWebAppFromInfo(
@@ -408,8 +408,8 @@
 
   data_retriever_->GetWebApplicationInfo(
       web_contents(),
-      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo,
-                     base::Unretained(this), /*force_shortcut_app*/ false));
+      base::BindOnce(&WebAppInstallTask::OnGetWebApplicationInfo, GetWeakPtr(),
+                     /*force_shortcut_app*/ false));
 }
 
 void WebAppInstallTask::OnWebAppUrlLoadedCheckAndRetrieveManifest(
@@ -437,7 +437,7 @@
       web_contents,
       /*bypass_service_worker_check=*/true,
       base::BindOnce(&WebAppInstallTask::OnWebAppInstallabilityChecked,
-                     base::Unretained(this)));
+                     GetWeakPtr()));
 }
 
 void WebAppInstallTask::OnWebAppInstallabilityChecked(
@@ -488,7 +488,7 @@
   data_retriever_->CheckInstallabilityAndRetrieveManifest(
       web_contents(), bypass_service_worker_check,
       base::BindOnce(&WebAppInstallTask::OnDidPerformInstallableCheck,
-                     base::Unretained(this), std::move(web_app_info),
+                     GetWeakPtr(), std::move(web_app_info),
                      force_shortcut_app));
 }
 
@@ -657,7 +657,7 @@
           ? WebAppIconDownloader::Histogram::kForSync
           : WebAppIconDownloader::Histogram::kForCreate,
       base::BindOnce(&WebAppInstallTask::OnIconsRetrievedShowDialog,
-                     base::Unretained(this), std::move(web_app_info),
+                     GetWeakPtr(), std::move(web_app_info),
                      for_installable_site));
 }
 
@@ -685,9 +685,8 @@
       install_source_ == webapps::WebappInstallSource::SYNC
           ? WebAppIconDownloader::Histogram::kForSync
           : WebAppIconDownloader::Histogram::kForCreate,
-      base::BindOnce(&WebAppInstallTask::OnIconsRetrieved,
-                     base::Unretained(this), std::move(web_application_info),
-                     finalize_options));
+      base::BindOnce(&WebAppInstallTask::OnIconsRetrieved, GetWeakPtr(),
+                     std::move(web_application_info), finalize_options));
 }
 
 void WebAppInstallTask::OnIconsRetrieved(