Commit 1a758930 authored by sergeyu's avatar sergeyu Committed by Commit bot

Shutdown the It2Me host cleanly.

Previously It2Me host was shutting down before letting all shutdown
tasks finish, which would often cause crashes.
Now NativeMessagingPipe is responsible for host and channel deletion
and the task runner for the main thread doesn't stop until the host
is destroyed.

BUG=453016

Review URL: https://codereview.chromium.org/887853002

Cr-Commit-Position: refs/heads/master@{#313967}
parent 4c55f398
......@@ -109,10 +109,6 @@ int StartIt2MeNativeMessagingHost() {
base::MessageLoopForUI message_loop;
base::RunLoop run_loop;
scoped_refptr<AutoThreadTaskRunner> task_runner =
new remoting::AutoThreadTaskRunner(message_loop.message_loop_proxy(),
run_loop.QuitClosure());
scoped_ptr<It2MeHostFactory> factory(new It2MeHostFactory());
scoped_ptr<NativeMessagingPipe> native_messaging_pipe(
......@@ -122,13 +118,15 @@ int StartIt2MeNativeMessagingHost() {
scoped_ptr<extensions::NativeMessagingChannel> channel(
new PipeMessagingChannel(read_file.Pass(), write_file.Pass()));
scoped_ptr<extensions::NativeMessageHost> host(new It2MeNativeMessagingHost(
ChromotingHostContext::Create(task_runner), factory.Pass()));
scoped_ptr<ChromotingHostContext> context =
ChromotingHostContext::Create(new remoting::AutoThreadTaskRunner(
message_loop.message_loop_proxy(), run_loop.QuitClosure()));
scoped_ptr<extensions::NativeMessageHost> host(
new It2MeNativeMessagingHost(context.Pass(), factory.Pass()));
host->Start(native_messaging_pipe.get());
native_messaging_pipe->Start(
host.Pass(), channel.Pass(), run_loop.QuitClosure());
native_messaging_pipe->Start(host.Pass(), channel.Pass());
// Run the loop until channel is alive.
run_loop.Run();
......
......@@ -190,7 +190,6 @@ class It2MeNativeMessagingHostTest : public testing::Test {
private:
void StartHost();
void StopHost();
void ExitTest();
// Each test creates two unidirectional pipes: "input" and "output".
......@@ -239,6 +238,10 @@ void It2MeNativeMessagingHostTest::SetUp() {
}
void It2MeNativeMessagingHostTest::TearDown() {
// Release reference to AutoThreadTaskRunner, so the host thread can be shut
// down.
host_task_runner_ = nullptr;
// Closing the write-end of the input will send an EOF to the native
// messaging reader. This will trigger a host shutdown.
input_write_file_.Close();
......@@ -443,28 +446,13 @@ void It2MeNativeMessagingHostTest::StartHost() {
make_scoped_ptr(new MockIt2MeHostFactory())));
it2me_host->Start(pipe_.get());
pipe_->Start(it2me_host.Pass(),
channel.Pass(),
base::Bind(&It2MeNativeMessagingHostTest::StopHost,
base::Unretained(this)));
pipe_->Start(it2me_host.Pass(), channel.Pass());
// Notify the test that the host has finished starting up.
test_message_loop_->message_loop_proxy()->PostTask(
FROM_HERE, test_run_loop_->QuitClosure());
}
void It2MeNativeMessagingHostTest::StopHost() {
DCHECK(host_task_runner_->RunsTasksOnCurrentThread());
pipe_.reset();
// Wait till all shutdown tasks have completed.
base::RunLoop().RunUntilIdle();
// Trigger a test shutdown via ExitTest().
host_task_runner_ = nullptr;
}
void It2MeNativeMessagingHostTest::ExitTest() {
if (!test_message_loop_->message_loop_proxy()->RunsTasksOnCurrentThread()) {
test_message_loop_->message_loop_proxy()->PostTask(
......
......@@ -19,11 +19,9 @@ NativeMessagingPipe::~NativeMessagingPipe() {
void NativeMessagingPipe::Start(
scoped_ptr<extensions::NativeMessageHost> host,
scoped_ptr<extensions::NativeMessagingChannel> channel,
const base::Closure& quit_closure) {
scoped_ptr<extensions::NativeMessagingChannel> channel) {
host_ = host.Pass();
channel_ = channel.Pass();
quit_closure_ = quit_closure;
channel_->Start(this);
}
......@@ -34,8 +32,8 @@ void NativeMessagingPipe::OnMessage(scoped_ptr<base::Value> message) {
}
void NativeMessagingPipe::OnDisconnect() {
if (!quit_closure_.is_null())
base::ResetAndReturn(&quit_closure_).Run();
host_.reset();
channel_.reset();
}
void NativeMessagingPipe::PostMessageFromNativeHost(
......@@ -45,8 +43,8 @@ void NativeMessagingPipe::PostMessageFromNativeHost(
}
void NativeMessagingPipe::CloseChannel(const std::string& error_message) {
if (!quit_closure_.is_null())
base::ResetAndReturn(&quit_closure_).Run();
host_.reset();
channel_.reset();
}
} // namespace remoting
......@@ -26,8 +26,7 @@ class NativeMessagingPipe
// Starts processing messages from the pipe.
void Start(scoped_ptr<extensions::NativeMessageHost> host,
scoped_ptr<extensions::NativeMessagingChannel> channel,
const base::Closure& quit_closure);
scoped_ptr<extensions::NativeMessagingChannel> channel);
// extensions::NativeMessageHost::Client implementation.
void PostMessageFromNativeHost(const std::string& message) override;
......@@ -38,7 +37,6 @@ class NativeMessagingPipe
void OnDisconnect() override;
private:
base::Closure quit_closure_;
scoped_ptr<extensions::NativeMessagingChannel> channel_;
scoped_ptr<extensions::NativeMessageHost> host_;
......
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