From 951cf458b94ef83027a0285124c55a3f4c51b2f7 Mon Sep 17 00:00:00 2001 From: Xiaohui Chen Date: Wed, 5 Sep 2018 18:21:20 +0000 Subject: [PATCH] assistant: fix crash when register fallback media handler It is possible that the callback from libassistant comes right after assistant_manager is stopped/reset. Let's guard this case. Bug: 879390 Test: locally build Change-Id: Iea2fe174a02627e6d61406a1319d172470028afd Reviewed-on: https://chromium-review.googlesource.com/1198105 Reviewed-by: Muyuan Li Commit-Queue: Xiaohui Chen Cr-Original-Commit-Position: refs/heads/master@{#587879}(cherry picked from commit c3a7b6f9d7357a192945d5ef8e99264a54d30e43) Reviewed-on: https://chromium-review.googlesource.com/1207930 Reviewed-by: Xiaohui Chen Cr-Commit-Position: refs/branch-heads/3538@{#58} Cr-Branched-From: 79f7c91a2b2a2932cd447fa6f865cb6662fa8fa6-refs/heads/master@{#587811} --- .../assistant/assistant_manager_service_impl.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/chromeos/services/assistant/assistant_manager_service_impl.cc b/chromeos/services/assistant/assistant_manager_service_impl.cc index 5e7f2058f02e..25b31c9fe940 100644 --- a/chromeos/services/assistant/assistant_manager_service_impl.cc +++ b/chromeos/services/assistant/assistant_manager_service_impl.cc @@ -4,6 +4,7 @@ #include "chromeos/services/assistant/assistant_manager_service_impl.h" +#include #include #include "ash/public/interfaces/constants.mojom.h" @@ -133,6 +134,12 @@ void AssistantManagerServiceImpl::SetAccessToken( } void AssistantManagerServiceImpl::RegisterFallbackMediaHandler() { + // This is a callback from LibAssistant, it is async from LibAssistant thread. + // It is possible that when it reaches here, the assistant_manager_ has + // been stopped. + if (!assistant_manager_internal_) + return; + // Register handler for media actions. assistant_manager_internal_->RegisterFallbackMediaHandler( [this](std::string play_media_args_proto) { @@ -735,8 +742,13 @@ void AssistantManagerServiceImpl::HandleUpdateSettingsResponse( callback.Run(result); } +// assistant_client::DeviceStateListener overrides +// Run on LibAssistant threads void AssistantManagerServiceImpl::OnStartFinished() { - RegisterFallbackMediaHandler(); + main_thread_task_runner_->PostTask( + FROM_HERE, + base::BindOnce(&AssistantManagerServiceImpl::RegisterFallbackMediaHandler, + weak_factory_.GetWeakPtr())); } void AssistantManagerServiceImpl::OnTimerSoundingStarted() {