Commit 60c28ffd authored by Sorin Jianu's avatar Sorin Jianu Committed by Commit Bot

Make derived classes from updater::App singletons.

Some future work for the COM server requires accessing the ComServer
instance, and there is no way to get to it starting from the
objects instantiated by COM factories as a response to an RPC call.

Bug: 1064498

Change-Id: I45923bf70cd922cb30a766fabea9bee87f524ebe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2119213
Commit-Queue: Sorin Jianu <sorin@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753249}
parent 4ca9f79e
...@@ -7,11 +7,23 @@ ...@@ -7,11 +7,23 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/no_destructor.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
namespace updater { namespace updater {
// An App is a main processing mode for the updater. // Creates a ref-counted singleton instance of the type T. Use this function
// to get instances of classes derived from updater::App.
template <typename T>
scoped_refptr<T> AppInstance() {
static base::NoDestructor<scoped_refptr<T>> instance{
base::MakeRefCounted<T>()};
return *instance;
}
// An App is an abstract class used as a main processing mode for the updater.
// Instances of classes derived from App must be accessed as singletons by
// calling the function template AppInstance<T>.
class App : public base::RefCountedThreadSafe<App> { class App : public base::RefCountedThreadSafe<App> {
public: public:
// Starts the thread pool and task executor, then runs a runloop on the main // Starts the thread pool and task executor, then runs a runloop on the main
......
...@@ -22,6 +22,9 @@ namespace updater { ...@@ -22,6 +22,9 @@ namespace updater {
// AppUninstall uninstalls the updater. // AppUninstall uninstalls the updater.
class AppUninstall : public App { class AppUninstall : public App {
public:
AppUninstall() = default;
private: private:
~AppUninstall() override = default; ~AppUninstall() override = default;
void FirstTaskRun() override; void FirstTaskRun() override;
...@@ -33,8 +36,8 @@ void AppUninstall::FirstTaskRun() { ...@@ -33,8 +36,8 @@ void AppUninstall::FirstTaskRun() {
base::BindOnce(&AppUninstall::Shutdown, this)); base::BindOnce(&AppUninstall::Shutdown, this));
} }
scoped_refptr<App> MakeAppUninstall() { scoped_refptr<App> AppUninstallInstance() {
return base::MakeRefCounted<AppUninstall>(); return AppInstance<AppUninstall>();
} }
} // namespace updater } // namespace updater
...@@ -11,7 +11,7 @@ namespace updater { ...@@ -11,7 +11,7 @@ namespace updater {
class App; class App;
scoped_refptr<App> MakeAppUninstall(); scoped_refptr<App> AppUninstallInstance();
} // namespace updater } // namespace updater
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
namespace updater { namespace updater {
class AppUpdateAll : public App { class AppUpdateAll : public App {
public:
AppUpdateAll() = default;
private: private:
~AppUpdateAll() override = default; ~AppUpdateAll() override = default;
...@@ -50,8 +53,8 @@ void AppUpdateAll::FirstTaskRun() { ...@@ -50,8 +53,8 @@ void AppUpdateAll::FirstTaskRun() {
base::BindOnce(&AppUpdateAll::Shutdown, this))); base::BindOnce(&AppUpdateAll::Shutdown, this)));
} }
scoped_refptr<App> MakeAppUpdateAll() { scoped_refptr<App> AppUpdateAllInstance() {
return base::MakeRefCounted<AppUpdateAll>(); return AppInstance<AppUpdateAll>();
} }
} // namespace updater } // namespace updater
...@@ -11,7 +11,7 @@ namespace updater { ...@@ -11,7 +11,7 @@ namespace updater {
class App; class App;
scoped_refptr<App> MakeAppUpdateAll(); scoped_refptr<App> AppUpdateAllInstance();
} // namespace updater } // namespace updater
......
...@@ -11,7 +11,7 @@ namespace updater { ...@@ -11,7 +11,7 @@ namespace updater {
class App; class App;
scoped_refptr<App> MakeAppServer(); scoped_refptr<App> AppServerInstance();
} // namespace updater } // namespace updater
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <xpc/xpc.h> #include <xpc/xpc.h>
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "base/memory/ref_counted.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/task/task_traits.h" #include "base/task/task_traits.h"
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
...@@ -57,8 +56,8 @@ void AppServer::FirstTaskRun() { ...@@ -57,8 +56,8 @@ void AppServer::FirstTaskRun() {
} }
} }
scoped_refptr<App> MakeAppServer() { scoped_refptr<App> AppServerInstance() {
return base::MakeRefCounted<AppServer>(); return AppInstance<AppServer>();
} }
} // namespace updater } // namespace updater
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <memory> #include <memory>
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/system/sys_info.h" #include "base/system/sys_info.h"
#include "base/task/thread_pool/thread_pool_instance.h" #include "base/task/thread_pool/thread_pool_instance.h"
...@@ -206,8 +205,8 @@ void ComServer::FirstTaskRun() { ...@@ -206,8 +205,8 @@ void ComServer::FirstTaskRun() {
Shutdown(hr); Shutdown(hr);
} }
scoped_refptr<App> MakeAppServer() { scoped_refptr<App> AppServerInstance() {
return base::MakeRefCounted<ComServer>(); return AppInstance<ComServer>();
} }
} // namespace updater } // namespace updater
...@@ -63,7 +63,7 @@ class UpdaterImpl ...@@ -63,7 +63,7 @@ class UpdaterImpl
class App; class App;
scoped_refptr<App> MakeAppServer(); scoped_refptr<App> AppServerInstance();
} // namespace updater } // namespace updater
......
...@@ -90,7 +90,7 @@ int HandleUpdaterCommands(const base::CommandLine* command_line) { ...@@ -90,7 +90,7 @@ int HandleUpdaterCommands(const base::CommandLine* command_line) {
#endif #endif
if (command_line->HasSwitch(kServerSwitch)) { if (command_line->HasSwitch(kServerSwitch)) {
return MakeAppServer()->Run(); return AppServerInstance()->Run();
} }
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -98,14 +98,14 @@ int HandleUpdaterCommands(const base::CommandLine* command_line) { ...@@ -98,14 +98,14 @@ int HandleUpdaterCommands(const base::CommandLine* command_line) {
return ServiceMain::RunComService(command_line); return ServiceMain::RunComService(command_line);
if (command_line->HasSwitch(kInstallSwitch)) if (command_line->HasSwitch(kInstallSwitch))
return MakeAppInstall({kChromeAppId})->Run(); return AppInstallInstance()->Run();
#endif // OS_WIN #endif // OS_WIN
if (command_line->HasSwitch(kUninstallSwitch)) if (command_line->HasSwitch(kUninstallSwitch))
return MakeAppUninstall()->Run(); return AppUninstallInstance()->Run();
if (command_line->HasSwitch(kUpdateAppsSwitch)) { if (command_line->HasSwitch(kUpdateAppsSwitch)) {
return MakeAppUpdateAll()->Run(); return AppUpdateAllInstance()->Run();
} }
VLOG(1) << "Unknown command line switch."; VLOG(1) << "Unknown command line switch.";
......
...@@ -692,10 +692,11 @@ DWORD InstallAppController::GetUIThreadID() const { ...@@ -692,10 +692,11 @@ DWORD InstallAppController::GetUIThreadID() const {
} // namespace } // namespace
// Installs the updater and one application specified by |app_id|. // Sets the updater up, shows up a splash screen, then installs an application
// while displaying the UI progress window.
class AppInstall : public App { class AppInstall : public App {
public: public:
explicit AppInstall(const std::string& app_id); AppInstall() = default;
private: private:
~AppInstall() override = default; ~AppInstall() override = default;
...@@ -705,7 +706,10 @@ class AppInstall : public App { ...@@ -705,7 +706,10 @@ class AppInstall : public App {
void SetupDone(int result); void SetupDone(int result);
std::string app_id_; // TODO(sorin): remove the hardcoding of the application id.
// https://crbug.com/1014298
const std::string app_id_ = {kChromeAppId};
scoped_refptr<Configurator> config_; scoped_refptr<Configurator> config_;
scoped_refptr<InstallAppController> app_install_controller_; scoped_refptr<InstallAppController> app_install_controller_;
...@@ -714,8 +718,6 @@ class AppInstall : public App { ...@@ -714,8 +718,6 @@ class AppInstall : public App {
std::unique_ptr<ui::SplashScreen> splash_screen_; std::unique_ptr<ui::SplashScreen> splash_screen_;
}; };
AppInstall::AppInstall(const std::string& app_id) : app_id_(app_id) {}
void AppInstall::Initialize() { void AppInstall::Initialize() {
base::i18n::InitializeICU(); base::i18n::InitializeICU();
config_ = base::MakeRefCounted<Configurator>(); config_ = base::MakeRefCounted<Configurator>();
...@@ -758,12 +760,12 @@ void AppInstall::SetupDone(int result) { ...@@ -758,12 +760,12 @@ void AppInstall::SetupDone(int result) {
app_id_, base::BindOnce(&AppInstall::Shutdown, this)); app_id_, base::BindOnce(&AppInstall::Shutdown, this));
} }
scoped_refptr<App> MakeAppInstall(const std::string& app_id) { scoped_refptr<App> AppInstallInstance() {
// TODO(sorin) "--install" must be run with "--single-process" until // TODO(sorin) "--install" must be run with "--single-process" until
// crbug.com/1053729 is resolved. // crbug.com/1053729 is resolved.
DCHECK( DCHECK(
base::CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcessSwitch)); base::CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcessSwitch));
return base::MakeRefCounted<AppInstall>(app_id); return AppInstance<AppInstall>();
} }
} // namespace updater } // namespace updater
...@@ -13,9 +13,7 @@ namespace updater { ...@@ -13,9 +13,7 @@ namespace updater {
class App; class App;
// Sets the updater up, shows up a splash screen, then installs an application scoped_refptr<App> AppInstallInstance();
// while displaying the UI progress window.
scoped_refptr<App> MakeAppInstall(const std::string& app_id);
} // namespace updater } // namespace updater
......
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