Commit b9ef2f5a authored by Wez's avatar Wez Committed by Commit Bot

Migrate ServiceProcess and MockLaunchd to use RunLoop::QuitClosure().

Use of RunLoop::QuitCurrent*Deprecated() is deprecated, and risky,
potentially causing unexpected early-exits if run under a different
RunLoop to the one intended.

Bug: 844016, 859151
Change-Id: Ib38310c0feeec09dbaac6cb74ffd061476e30cf3
Reviewed-on: https://chromium-review.googlesource.com/1123763
Commit-Queue: Wez <wez@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#573449}
parent 4af4ad39
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
...@@ -122,15 +123,15 @@ class TestServiceProcess : public ServiceProcess { ...@@ -122,15 +123,15 @@ class TestServiceProcess : public ServiceProcess {
TestServiceProcess() { } TestServiceProcess() { }
~TestServiceProcess() override {} ~TestServiceProcess() override {}
bool Initialize(base::MessageLoopForUI* message_loop, bool Initialize(base::OnceClosure quit_closure,
ServiceProcessState* state); std::unique_ptr<ServiceProcessState> state);
}; };
bool TestServiceProcess::Initialize(base::MessageLoopForUI* message_loop, bool TestServiceProcess::Initialize(
ServiceProcessState* state) { base::OnceClosure quit_closure,
main_message_loop_ = message_loop; std::unique_ptr<ServiceProcessState> state) {
quit_closure_ = std::move(quit_closure);
service_process_state_.reset(state); service_process_state_ = std::move(state);
base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
io_thread_.reset(new base::Thread("TestServiceProcess_IO")); io_thread_.reset(new base::Thread("TestServiceProcess_IO"));
...@@ -190,6 +191,7 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { ...@@ -190,6 +191,7 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) {
CHECK(!user_data_dir.empty()); CHECK(!user_data_dir.empty());
CHECK(test_launcher_utils::OverrideUserDataDir(user_data_dir)); CHECK(test_launcher_utils::OverrideUserDataDir(user_data_dir));
base::RunLoop run_loop;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
if (!command_line->HasSwitch(kTestExecutablePath)) if (!command_line->HasSwitch(kTestExecutablePath))
return kMissingSwitch; return kMissingSwitch;
...@@ -197,11 +199,10 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { ...@@ -197,11 +199,10 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) {
command_line->GetSwitchValuePath(kTestExecutablePath); command_line->GetSwitchValuePath(kTestExecutablePath);
EXPECT_FALSE(executable_path.empty()); EXPECT_FALSE(executable_path.empty());
MockLaunchd mock_launchd(executable_path, main_message_loop.task_runner(), MockLaunchd mock_launchd(executable_path, main_message_loop.task_runner(),
true, true); run_loop.QuitClosure(), true, true);
Launchd::ScopedInstance use_mock(&mock_launchd); Launchd::ScopedInstance use_mock(&mock_launchd);
#endif #endif
ServiceProcessState* state(new ServiceProcessState); ServiceProcessState* state(new ServiceProcessState);
bool service_process_state_initialized = state->Initialize(); bool service_process_state_initialized = state->Initialize();
EXPECT_TRUE(service_process_state_initialized); EXPECT_TRUE(service_process_state_initialized);
...@@ -213,7 +214,8 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { ...@@ -213,7 +214,8 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) {
// Takes ownership of the pointer, but we can use it since we have the same // Takes ownership of the pointer, but we can use it since we have the same
// lifetime. // lifetime.
EXPECT_TRUE(service_process.Initialize(&main_message_loop, state)); EXPECT_TRUE(service_process.Initialize(run_loop.QuitClosure(),
base::WrapUnique(state)));
// Needed for IPC. // Needed for IPC.
mojo::core::Init(); mojo::core::Init();
...@@ -255,7 +257,7 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { ...@@ -255,7 +257,7 @@ int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) {
service_process.io_task_runner(), service_process.io_task_runner(),
base::ThreadTaskRunnerHandle::Get()); base::ThreadTaskRunnerHandle::Get());
base::RunLoop().Run(); run_loop.Run();
if (!Mock::VerifyAndClearExpectations(&server)) if (!Mock::VerifyAndClearExpectations(&server))
return kExpectationsNotMet; return kExpectationsNotMet;
if (!g_good_shutdown) if (!g_good_shutdown)
...@@ -388,8 +390,9 @@ void CloudPrintProxyPolicyStartupTest::SetUp() { ...@@ -388,8 +390,9 @@ void CloudPrintProxyPolicyStartupTest::SetUp() {
EXPECT_TRUE(MockLaunchd::MakeABundle(temp_dir_.GetPath(), EXPECT_TRUE(MockLaunchd::MakeABundle(temp_dir_.GetPath(),
"CloudPrintProxyTest", &bundle_path_, "CloudPrintProxyTest", &bundle_path_,
&executable_path_)); &executable_path_));
mock_launchd_.reset(new MockLaunchd( mock_launchd_.reset(new MockLaunchd(executable_path_,
executable_path_, base::ThreadTaskRunnerHandle::Get(), true, false)); base::ThreadTaskRunnerHandle::Get(),
base::DoNothing(), true, false));
scoped_launchd_instance_.reset( scoped_launchd_instance_.reset(
new Launchd::ScopedInstance(mock_launchd_.get())); new Launchd::ScopedInstance(mock_launchd_.get()));
#endif #endif
......
...@@ -94,11 +94,13 @@ bool MockLaunchd::MakeABundle(const base::FilePath& dst, ...@@ -94,11 +94,13 @@ bool MockLaunchd::MakeABundle(const base::FilePath& dst,
MockLaunchd::MockLaunchd( MockLaunchd::MockLaunchd(
const base::FilePath& file, const base::FilePath& file,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
base::OnceClosure quit_closure,
bool create_socket, bool create_socket,
bool as_service) bool as_service)
: file_(file), : file_(file),
pipe_name_(GetServiceProcessServerName()), pipe_name_(GetServiceProcessServerName()),
main_task_runner_(std::move(main_task_runner)), main_task_runner_(std::move(main_task_runner)),
quit_closure_(std::move(quit_closure)),
create_socket_(create_socket), create_socket_(create_socket),
as_service_(as_service), as_service_(as_service),
restart_called_(false), restart_called_(false),
...@@ -227,8 +229,7 @@ CFDictionaryRef MockLaunchd::CopyDictionaryByCheckingIn(CFErrorRef* error) { ...@@ -227,8 +229,7 @@ CFDictionaryRef MockLaunchd::CopyDictionaryByCheckingIn(CFErrorRef* error) {
bool MockLaunchd::RemoveJob(CFStringRef label, CFErrorRef* error) { bool MockLaunchd::RemoveJob(CFStringRef label, CFErrorRef* error) {
remove_called_ = true; remove_called_ = true;
main_task_runner_->PostTask( std::move(quit_closure_).Run();
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
return true; return true;
} }
...@@ -237,8 +238,7 @@ bool MockLaunchd::RestartJob(Domain domain, ...@@ -237,8 +238,7 @@ bool MockLaunchd::RestartJob(Domain domain,
CFStringRef name, CFStringRef name,
CFStringRef session_type) { CFStringRef session_type) {
restart_called_ = true; restart_called_ = true;
main_task_runner_->PostTask( std::move(quit_closure_).Run();
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
return true; return true;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
...@@ -31,6 +32,7 @@ class MockLaunchd : public Launchd { ...@@ -31,6 +32,7 @@ class MockLaunchd : public Launchd {
MockLaunchd(const base::FilePath& file, MockLaunchd(const base::FilePath& file,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
base::OnceClosure quit_closure,
bool create_socket, bool create_socket,
bool as_service); bool as_service);
~MockLaunchd() override; ~MockLaunchd() override;
...@@ -63,6 +65,7 @@ class MockLaunchd : public Launchd { ...@@ -63,6 +65,7 @@ class MockLaunchd : public Launchd {
base::FilePath file_; base::FilePath file_;
std::string pipe_name_; std::string pipe_name_;
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
base::OnceClosure quit_closure_;
std::unique_ptr<MultiProcessLock> running_lock_; std::unique_ptr<MultiProcessLock> running_lock_;
bool create_socket_; bool create_socket_;
bool as_service_; bool as_service_;
......
...@@ -48,8 +48,8 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test { ...@@ -48,8 +48,8 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(), "Test", ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(), "Test",
&bundle_path_, &executable_path_)); &bundle_path_, &executable_path_));
mock_launchd_.reset( mock_launchd_.reset(new MockLaunchd(executable_path_, loop_.task_runner(),
new MockLaunchd(executable_path_, loop_.task_runner(), false, false)); run_loop_.QuitClosure(), false, false));
scoped_launchd_instance_.reset( scoped_launchd_instance_.reset(
new Launchd::ScopedInstance(mock_launchd_.get())); new Launchd::ScopedInstance(mock_launchd_.get()));
ASSERT_TRUE(service_process_state_.Initialize()); ASSERT_TRUE(service_process_state_.Initialize());
......
...@@ -32,11 +32,11 @@ int CloudPrintServiceProcessMain( ...@@ -32,11 +32,11 @@ int CloudPrintServiceProcessMain(
if (!state->Initialize()) if (!state->Initialize())
return 0; return 0;
base::RunLoop run_loop;
ServiceProcess service_process; ServiceProcess service_process;
if (service_process.Initialize(&main_message_loop, if (service_process.Initialize(run_loop.QuitClosure(),
parameters.command_line, parameters.command_line, std::move(state))) {
state.release())) { run_loop.Run();
base::RunLoop().Run();
} else { } else {
LOG(ERROR) << "Service process failed to initialize"; LOG(ERROR) << "Service process failed to initialize";
} }
......
...@@ -131,16 +131,15 @@ void PrepareRestartOnCrashEnviroment( ...@@ -131,16 +131,15 @@ void PrepareRestartOnCrashEnviroment(
ServiceProcess::ServiceProcess() ServiceProcess::ServiceProcess()
: shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, : shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED), base::WaitableEvent::InitialState::NOT_SIGNALED),
main_message_loop_(NULL),
enabled_services_(0), enabled_services_(0),
update_available_(false) { update_available_(false) {
DCHECK(!g_service_process); DCHECK(!g_service_process);
g_service_process = this; g_service_process = this;
} }
bool ServiceProcess::Initialize(base::MessageLoopForUI* message_loop, bool ServiceProcess::Initialize(base::OnceClosure quit_closure,
const base::CommandLine& command_line, const base::CommandLine& command_line,
ServiceProcessState* state) { std::unique_ptr<ServiceProcessState> state) {
#if defined(USE_GLIB) #if defined(USE_GLIB)
// g_type_init has been deprecated since version 2.35. // g_type_init has been deprecated since version 2.35.
#if !GLIB_CHECK_VERSION(2, 35, 0) #if !GLIB_CHECK_VERSION(2, 35, 0)
...@@ -148,8 +147,8 @@ bool ServiceProcess::Initialize(base::MessageLoopForUI* message_loop, ...@@ -148,8 +147,8 @@ bool ServiceProcess::Initialize(base::MessageLoopForUI* message_loop,
g_type_init(); g_type_init();
#endif #endif
#endif // defined(USE_GLIB) #endif // defined(USE_GLIB)
main_message_loop_ = message_loop; quit_closure_ = std::move(quit_closure);
service_process_state_.reset(state); service_process_state_ = std::move(state);
// Initialize TaskScheduler. // Initialize TaskScheduler.
constexpr int kMaxBackgroundThreads = 1; constexpr int kMaxBackgroundThreads = 1;
...@@ -300,8 +299,7 @@ void ServiceProcess::Shutdown() { ...@@ -300,8 +299,7 @@ void ServiceProcess::Shutdown() {
} }
void ServiceProcess::Terminate() { void ServiceProcess::Terminate() {
main_message_loop_->task_runner()->PostTask( std::move(quit_closure_).Run();
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
} }
void ServiceProcess::OnShutdown() { void ServiceProcess::OnShutdown() {
......
...@@ -50,11 +50,11 @@ class ServiceProcess : public ServiceIPCServer::Client, ...@@ -50,11 +50,11 @@ class ServiceProcess : public ServiceIPCServer::Client,
ServiceProcess(); ServiceProcess();
~ServiceProcess() override; ~ServiceProcess() override;
// Initialize the ServiceProcess with the message loop that it should run on. // Initialize the ServiceProcess. |quit_closure| will be run when the service
// ServiceProcess takes ownership of |state|. // process is ready to exit.
bool Initialize(base::MessageLoopForUI* message_loop, bool Initialize(base::OnceClosure quit_closure,
const base::CommandLine& command_line, const base::CommandLine& command_line,
ServiceProcessState* state); std::unique_ptr<ServiceProcessState> state);
bool Teardown(); bool Teardown();
...@@ -123,8 +123,8 @@ class ServiceProcess : public ServiceIPCServer::Client, ...@@ -123,8 +123,8 @@ class ServiceProcess : public ServiceIPCServer::Client,
// An event that will be signalled when we shutdown. // An event that will be signalled when we shutdown.
base::WaitableEvent shutdown_event_; base::WaitableEvent shutdown_event_;
// Pointer to the main message loop that host this object. // Closure to run to cause the main message loop to exit.
base::MessageLoop* main_message_loop_; base::OnceClosure quit_closure_;
// Count of currently enabled services in this process. // Count of currently enabled services in this process.
int enabled_services_; int enabled_services_;
......
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