Commit 1cf10167 authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions Callback Cleanup] Webstore Installers

Do the following clean-ups in the webstore installer classes and its
callers:
- Convert base::Callback to base::RepeatingCallback/base::OnceCallback
- Convert base::Bind to base::BindRepeating/base::BindOnce
- Pass callbacks by value when an instance is retained

Bug: 714018

Change-Id: Ia55a04264ac528625c3f8664fd30607036945c24
Reviewed-on: https://chromium-review.googlesource.com/c/1391867Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619844}
parent 5aa7731c
...@@ -36,10 +36,11 @@ AppInstaller::AppInstaller(content::WebContents* web_contents, ...@@ -36,10 +36,11 @@ AppInstaller::AppInstaller(content::WebContents* web_contents,
const std::string& item_id, const std::string& item_id,
Profile* profile, Profile* profile,
bool silent_installation, bool silent_installation,
const Callback& callback) Callback callback)
: extensions::WebstoreStandaloneInstaller(item_id, profile, callback), : extensions::WebstoreStandaloneInstaller(item_id,
profile,
std::move(callback)),
silent_installation_(silent_installation), silent_installation_(silent_installation),
callback_(callback),
web_contents_(web_contents), web_contents_(web_contents),
web_contents_observer_(new WebContentsObserver(web_contents, this)) { web_contents_observer_(new WebContentsObserver(web_contents, this)) {
DCHECK(web_contents_); DCHECK(web_contents_);
...@@ -79,7 +80,7 @@ content::WebContents* AppInstaller::GetWebContents() const { ...@@ -79,7 +80,7 @@ content::WebContents* AppInstaller::GetWebContents() const {
} }
void AppInstaller::OnWebContentsDestroyed(content::WebContents* web_contents) { void AppInstaller::OnWebContentsDestroyed(content::WebContents* web_contents) {
callback_.Run(false, kWebContentsDestroyedError, RunCallback(false, kWebContentsDestroyedError,
extensions::webstore_install::OTHER_ERROR); extensions::webstore_install::OTHER_ERROR);
AbortInstall(); AbortInstall();
} }
......
...@@ -26,7 +26,7 @@ class AppInstaller : public extensions::WebstoreStandaloneInstaller { ...@@ -26,7 +26,7 @@ class AppInstaller : public extensions::WebstoreStandaloneInstaller {
const std::string& item_id, const std::string& item_id,
Profile* profile, Profile* profile,
bool silent_installation, bool silent_installation,
const Callback& callback); Callback callback);
protected: protected:
friend class base::RefCountedThreadSafe<AppInstaller>; friend class base::RefCountedThreadSafe<AppInstaller>;
...@@ -47,7 +47,6 @@ class AppInstaller : public extensions::WebstoreStandaloneInstaller { ...@@ -47,7 +47,6 @@ class AppInstaller : public extensions::WebstoreStandaloneInstaller {
class WebContentsObserver; class WebContentsObserver;
bool silent_installation_; bool silent_installation_;
Callback callback_;
content::WebContents* web_contents_; content::WebContents* web_contents_;
std::unique_ptr<WebContentsObserver> web_contents_observer_; std::unique_ptr<WebContentsObserver> web_contents_observer_;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "base/bind.h"
#include "chrome/browser/apps/platform_apps/api/webstore_widget_private/app_installer.h" #include "chrome/browser/apps/platform_apps/api/webstore_widget_private/app_installer.h"
#include "chrome/browser/chromeos/file_manager/app_id.h" #include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -44,20 +45,19 @@ WebstoreWidgetPrivateInstallWebstoreItemFunction::Run() { ...@@ -44,20 +45,19 @@ WebstoreWidgetPrivateInstallWebstoreItemFunction::Run() {
if (params->silent_installation && !allow_silent_install) if (params->silent_installation && !allow_silent_install)
return RespondNow(Error("Silent installation not allowed.")); return RespondNow(Error("Silent installation not allowed."));
const extensions::WebstoreStandaloneInstaller::Callback callback = base::Bind(
&WebstoreWidgetPrivateInstallWebstoreItemFunction::OnInstallComplete,
this);
content::WebContents* web_contents = GetSenderWebContents(); content::WebContents* web_contents = GetSenderWebContents();
if (!web_contents) { if (!web_contents) {
return RespondNow( return RespondNow(
Error(extensions::function_constants::kCouldNotFindSenderWebContents)); Error(extensions::function_constants::kCouldNotFindSenderWebContents));
} }
scoped_refptr<webstore_widget::AppInstaller> installer(
new webstore_widget::AppInstaller( auto installer = base::MakeRefCounted<webstore_widget::AppInstaller>(
web_contents, params->item_id, web_contents, params->item_id,
Profile::FromBrowserContext(browser_context()), Profile::FromBrowserContext(browser_context()),
params->silent_installation, callback)); params->silent_installation,
base::BindOnce(
&WebstoreWidgetPrivateInstallWebstoreItemFunction::OnInstallComplete,
this));
// installer will be AddRef()'d in BeginInstall(). // installer will be AddRef()'d in BeginInstall().
installer->BeginInstall(); installer->BeginInstall();
......
...@@ -1889,9 +1889,9 @@ DeveloperPrivateRepairExtensionFunction::Run() { ...@@ -1889,9 +1889,9 @@ DeveloperPrivateRepairExtensionFunction::Run() {
return RespondNow(Error(kCouldNotFindWebContentsError)); return RespondNow(Error(kCouldNotFindWebContentsError));
scoped_refptr<WebstoreReinstaller> reinstaller(new WebstoreReinstaller( scoped_refptr<WebstoreReinstaller> reinstaller(new WebstoreReinstaller(
web_contents, web_contents, params->extension_id,
params->extension_id, base::BindOnce(
base::Bind(&DeveloperPrivateRepairExtensionFunction::OnReinstallComplete, &DeveloperPrivateRepairExtensionFunction::OnReinstallComplete,
this))); this)));
reinstaller->BeginReinstall(); reinstaller->BeginReinstall();
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/extensions/webstore_install_with_prompt.h" #include "chrome/browser/extensions/webstore_install_with_prompt.h"
#include <utility>
#include "chrome/browser/extensions/webstore_installer.h" #include "chrome/browser/extensions/webstore_installer.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
...@@ -15,8 +17,10 @@ namespace extensions { ...@@ -15,8 +17,10 @@ namespace extensions {
WebstoreInstallWithPrompt::WebstoreInstallWithPrompt( WebstoreInstallWithPrompt::WebstoreInstallWithPrompt(
const std::string& webstore_item_id, const std::string& webstore_item_id,
Profile* profile, Profile* profile,
const Callback& callback) Callback callback)
: WebstoreStandaloneInstaller(webstore_item_id, profile, callback), : WebstoreStandaloneInstaller(webstore_item_id,
profile,
std::move(callback)),
show_post_install_ui_(true), show_post_install_ui_(true),
dummy_web_contents_( dummy_web_contents_(
WebContents::Create(WebContents::CreateParams(profile))), WebContents::Create(WebContents::CreateParams(profile))),
...@@ -28,8 +32,10 @@ WebstoreInstallWithPrompt::WebstoreInstallWithPrompt( ...@@ -28,8 +32,10 @@ WebstoreInstallWithPrompt::WebstoreInstallWithPrompt(
const std::string& webstore_item_id, const std::string& webstore_item_id,
Profile* profile, Profile* profile,
gfx::NativeWindow parent_window, gfx::NativeWindow parent_window,
const Callback& callback) Callback callback)
: WebstoreStandaloneInstaller(webstore_item_id, profile, callback), : WebstoreStandaloneInstaller(webstore_item_id,
profile,
std::move(callback)),
show_post_install_ui_(true), show_post_install_ui_(true),
dummy_web_contents_( dummy_web_contents_(
WebContents::Create(WebContents::CreateParams(profile))), WebContents::Create(WebContents::CreateParams(profile))),
......
...@@ -35,14 +35,14 @@ class WebstoreInstallWithPrompt : public WebstoreStandaloneInstaller { ...@@ -35,14 +35,14 @@ class WebstoreInstallWithPrompt : public WebstoreStandaloneInstaller {
// will be centered on the screen. // will be centered on the screen.
WebstoreInstallWithPrompt(const std::string& webstore_item_id, WebstoreInstallWithPrompt(const std::string& webstore_item_id,
Profile* profile, Profile* profile,
const Callback& callback); Callback callback);
// If this constructor is used, the parent of the install dialog will be // If this constructor is used, the parent of the install dialog will be
// |parent_window|. // |parent_window|.
WebstoreInstallWithPrompt(const std::string& webstore_item_id, WebstoreInstallWithPrompt(const std::string& webstore_item_id,
Profile* profile, Profile* profile,
gfx::NativeWindow parent_window, gfx::NativeWindow parent_window,
const Callback& callback); Callback callback);
protected: protected:
friend class base::RefCountedThreadSafe<WebstoreInstallWithPrompt>; friend class base::RefCountedThreadSafe<WebstoreInstallWithPrompt>;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/extensions/webstore_reinstaller.h" #include "chrome/browser/extensions/webstore_reinstaller.h"
#include <utility>
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/extension_install_prompt.h" #include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
...@@ -21,11 +23,11 @@ const char kTabClosed[] = "Tab was closed."; ...@@ -21,11 +23,11 @@ const char kTabClosed[] = "Tab was closed.";
WebstoreReinstaller::WebstoreReinstaller( WebstoreReinstaller::WebstoreReinstaller(
content::WebContents* web_contents, content::WebContents* web_contents,
const std::string& extension_id, const std::string& extension_id,
const WebstoreStandaloneInstaller::Callback& callback) WebstoreStandaloneInstaller::Callback callback)
: WebstoreStandaloneInstaller( : WebstoreStandaloneInstaller(
extension_id, extension_id,
Profile::FromBrowserContext(web_contents->GetBrowserContext()), Profile::FromBrowserContext(web_contents->GetBrowserContext()),
callback), std::move(callback)),
content::WebContentsObserver(web_contents) { content::WebContentsObserver(web_contents) {
DCHECK( DCHECK(
ExtensionPrefs::Get(web_contents->GetBrowserContext()) ExtensionPrefs::Get(web_contents->GetBrowserContext())
......
...@@ -19,7 +19,7 @@ class WebstoreReinstaller : public WebstoreStandaloneInstaller, ...@@ -19,7 +19,7 @@ class WebstoreReinstaller : public WebstoreStandaloneInstaller,
public: public:
WebstoreReinstaller(content::WebContents* web_contents, WebstoreReinstaller(content::WebContents* web_contents,
const std::string& extension_id, const std::string& extension_id,
const WebstoreStandaloneInstaller::Callback& callback); WebstoreStandaloneInstaller::Callback callback);
// Begin the reinstall process. |callback| (from the constructor) will be // Begin the reinstall process. |callback| (from the constructor) will be
// called upon completion. // called upon completion.
......
...@@ -89,13 +89,10 @@ IN_PROC_BROWSER_TEST_F(WebstoreReinstallerBrowserTest, TestWebstoreReinstall) { ...@@ -89,13 +89,10 @@ IN_PROC_BROWSER_TEST_F(WebstoreReinstallerBrowserTest, TestWebstoreReinstall) {
// Create and run a WebstoreReinstaller. // Create and run a WebstoreReinstaller.
base::RunLoop run_loop; base::RunLoop run_loop;
scoped_refptr<WebstoreReinstaller> reinstaller( scoped_refptr<WebstoreReinstaller> reinstaller(new WebstoreReinstaller(
new WebstoreReinstaller( active_web_contents, kTestExtensionId,
active_web_contents, base::BindOnce(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
kTestExtensionId, base::Unretained(this), run_loop.QuitClosure())));
base::Bind(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
base::Unretained(this),
run_loop.QuitClosure())));
reinstaller->BeginReinstall(); reinstaller->BeginReinstall();
run_loop.Run(); run_loop.Run();
...@@ -108,13 +105,10 @@ IN_PROC_BROWSER_TEST_F(WebstoreReinstallerBrowserTest, TestWebstoreReinstall) { ...@@ -108,13 +105,10 @@ IN_PROC_BROWSER_TEST_F(WebstoreReinstallerBrowserTest, TestWebstoreReinstall) {
// Now accept the repair prompt. // Now accept the repair prompt.
AutoAcceptInstall(); AutoAcceptInstall();
base::RunLoop run_loop2; base::RunLoop run_loop2;
reinstaller = reinstaller = new WebstoreReinstaller(
new WebstoreReinstaller( active_web_contents, kTestExtensionId,
active_web_contents, base::BindOnce(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
kTestExtensionId, base::Unretained(this), run_loop2.QuitClosure()));
base::Bind(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
base::Unretained(this),
run_loop2.QuitClosure()));
reinstaller->BeginReinstall(); reinstaller->BeginReinstall();
run_loop2.Run(); run_loop2.Run();
......
...@@ -31,9 +31,9 @@ namespace extensions { ...@@ -31,9 +31,9 @@ namespace extensions {
WebstoreStandaloneInstaller::WebstoreStandaloneInstaller( WebstoreStandaloneInstaller::WebstoreStandaloneInstaller(
const std::string& webstore_item_id, const std::string& webstore_item_id,
Profile* profile, Profile* profile,
const Callback& callback) Callback callback)
: id_(webstore_item_id), : id_(webstore_item_id),
callback_(callback), callback_(std::move(callback)),
profile_(profile), profile_(profile),
install_source_(WebstoreInstaller::INSTALL_SOURCE_INLINE), install_source_(WebstoreInstaller::INSTALL_SOURCE_INLINE),
show_user_count_(true), show_user_count_(true),
...@@ -80,7 +80,8 @@ WebstoreStandaloneInstaller::~WebstoreStandaloneInstaller() { ...@@ -80,7 +80,8 @@ WebstoreStandaloneInstaller::~WebstoreStandaloneInstaller() {
void WebstoreStandaloneInstaller::RunCallback(bool success, void WebstoreStandaloneInstaller::RunCallback(bool success,
const std::string& error, const std::string& error,
webstore_install::Result result) { webstore_install::Result result) {
callback_.Run(success, error, result); DCHECK(callback_);
std::move(callback_).Run(success, error, result);
} }
void WebstoreStandaloneInstaller::AbortInstall() { void WebstoreStandaloneInstaller::AbortInstall() {
...@@ -117,7 +118,7 @@ void WebstoreStandaloneInstaller::CompleteInstall( ...@@ -117,7 +118,7 @@ void WebstoreStandaloneInstaller::CompleteInstall(
const std::string& error) { const std::string& error) {
scoped_active_install_.reset(); scoped_active_install_.reset();
if (!callback_.is_null()) if (!callback_.is_null())
callback_.Run(result == webstore_install::SUCCESS, error, result); RunCallback(result == webstore_install::SUCCESS, error, result);
Release(); // Matches the AddRef in BeginInstall. Release(); // Matches the AddRef in BeginInstall.
} }
......
...@@ -46,20 +46,21 @@ class WebstoreStandaloneInstaller ...@@ -46,20 +46,21 @@ class WebstoreStandaloneInstaller
// A callback for when the install process completes, successfully or not. If // A callback for when the install process completes, successfully or not. If
// there was a failure, |success| will be false and |error| may contain a // there was a failure, |success| will be false and |error| may contain a
// developer-readable error message about why it failed. // developer-readable error message about why it failed.
typedef base::Callback<void(bool success, using Callback = base::OnceCallback<void(bool success,
const std::string& error, const std::string& error,
webstore_install::Result result)> Callback; webstore_install::Result result)>;
WebstoreStandaloneInstaller(const std::string& webstore_item_id, WebstoreStandaloneInstaller(const std::string& webstore_item_id,
Profile* profile, Profile* profile,
const Callback& callback); Callback callback);
void BeginInstall(); void BeginInstall();
protected: protected:
~WebstoreStandaloneInstaller() override; ~WebstoreStandaloneInstaller() override;
// Runs the callback; primarily used for running a callback before it is // Runs the callback; primarily used for running a callback before it is
// cleared in AbortInstall(). // cleared in AbortInstall(). This should only be called once for the lifetime
// of the class.
void RunCallback( void RunCallback(
bool success, const std::string& error, webstore_install::Result result); bool success, const std::string& error, webstore_install::Result result);
......
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