Commit f94d9f16 authored by Muyuan Li's avatar Muyuan Li Committed by Commit Bot

assistant: fix DCHECK due to thread synchronization primitive

Bug: b/109875600
Test: Debug build and run
Change-Id: I751611b7617e1683bd17307ad971f4b9ae1bb4a0
Reviewed-on: https://chromium-review.googlesource.com/1116289
Commit-Queue: Muyuan Li <muyuanli@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570869}
parent 3a23edd1
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "ash/public/interfaces/constants.mojom.h" #include "ash/public/interfaces/constants.mojom.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/i18n/rtl.h" #include "base/i18n/rtl.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -45,7 +44,9 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl( ...@@ -45,7 +44,9 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl(
display_connection_(std::make_unique<CrosDisplayConnection>(this)), display_connection_(std::make_unique<CrosDisplayConnection>(this)),
main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
voice_interaction_observer_binding_(this), voice_interaction_observer_binding_(this),
background_thread_("background thread"),
weak_factory_(this) { weak_factory_(this) {
background_thread_.Start();
connector->BindInterface(ash::mojom::kServiceName, connector->BindInterface(ash::mojom::kServiceName,
&voice_interaction_controller_); &voice_interaction_controller_);
...@@ -57,19 +58,19 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl( ...@@ -57,19 +58,19 @@ AssistantManagerServiceImpl::AssistantManagerServiceImpl(
AssistantManagerServiceImpl::~AssistantManagerServiceImpl() {} AssistantManagerServiceImpl::~AssistantManagerServiceImpl() {}
void AssistantManagerServiceImpl::Start(const std::string& access_token, void AssistantManagerServiceImpl::Start(const std::string& access_token,
base::OnceClosure callback) { base::OnceClosure post_init_callback) {
// LibAssistant creation will make file IO. Post the creation to
// background thread to avoid DCHECK.
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&assistant_client::AssistantManager::Create,
&platform_api_, CreateLibAssistantConfig()),
base::BindOnce(&AssistantManagerServiceImpl::StartAssistantInternal,
base::Unretained(this), std::move(callback), access_token,
chromeos::version_loader::GetARCVersion()));
// Set the flag to avoid starting the service multiple times. // Set the flag to avoid starting the service multiple times.
state_ = State::STARTED; state_ = State::STARTED;
// LibAssistant creation will make file IO and sync wait. Post the creation to
// background thread to avoid DCHECK.
background_thread_.task_runner()->PostTaskAndReply(
FROM_HERE,
base::BindOnce(&AssistantManagerServiceImpl::StartAssistantInternal,
base::Unretained(this), access_token,
chromeos::version_loader::GetARCVersion()),
base::BindOnce(&AssistantManagerServiceImpl::PostInitAssistant,
base::Unretained(this), std::move(post_init_callback)));
} }
AssistantManagerService::State AssistantManagerServiceImpl::GetState() const { AssistantManagerService::State AssistantManagerServiceImpl::GetState() const {
...@@ -99,6 +100,8 @@ AssistantManagerServiceImpl::GetAssistantSettingsManager() { ...@@ -99,6 +100,8 @@ AssistantManagerServiceImpl::GetAssistantSettingsManager() {
void AssistantManagerServiceImpl::SendGetSettingsUiRequest( void AssistantManagerServiceImpl::SendGetSettingsUiRequest(
const std::string& selector, const std::string& selector,
GetSettingsUiResponseCallback callback) { GetSettingsUiResponseCallback callback) {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
std::string serialized_proto = SerializeGetSettingsUiRequest(selector); std::string serialized_proto = SerializeGetSettingsUiRequest(selector);
assistant_manager_internal_->SendGetSettingsUiRequest( assistant_manager_internal_->SendGetSettingsUiRequest(
serialized_proto, std::string(), [ serialized_proto, std::string(), [
...@@ -123,6 +126,8 @@ void AssistantManagerServiceImpl::SendGetSettingsUiRequest( ...@@ -123,6 +126,8 @@ void AssistantManagerServiceImpl::SendGetSettingsUiRequest(
void AssistantManagerServiceImpl::SendUpdateSettingsUiRequest( void AssistantManagerServiceImpl::SendUpdateSettingsUiRequest(
const std::string& update, const std::string& update,
UpdateSettingsUiResponseCallback callback) { UpdateSettingsUiResponseCallback callback) {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
std::string serialized_proto = SerializeUpdateSettingsUiRequest(update); std::string serialized_proto = SerializeUpdateSettingsUiRequest(update);
assistant_manager_internal_->SendUpdateSettingsUiRequest( assistant_manager_internal_->SendUpdateSettingsUiRequest(
serialized_proto, std::string(), [ serialized_proto, std::string(), [
...@@ -258,11 +263,12 @@ void AssistantManagerServiceImpl::OnVoiceInteractionSetupCompleted( ...@@ -258,11 +263,12 @@ void AssistantManagerServiceImpl::OnVoiceInteractionSetupCompleted(
} }
void AssistantManagerServiceImpl::StartAssistantInternal( void AssistantManagerServiceImpl::StartAssistantInternal(
base::OnceCallback<void()> callback,
const std::string& access_token, const std::string& access_token,
const std::string& arc_version, const std::string& arc_version) {
assistant_client::AssistantManager* assistant_manager) { DCHECK(background_thread_.task_runner()->BelongsToCurrentThread());
assistant_manager_.reset(assistant_manager);
assistant_manager_.reset(assistant_client::AssistantManager::Create(
&platform_api_, CreateLibAssistantConfig()));
assistant_manager_internal_ = assistant_manager_internal_ =
UnwrapAssistantManagerInternal(assistant_manager_.get()); UnwrapAssistantManagerInternal(assistant_manager_.get());
...@@ -278,26 +284,23 @@ void AssistantManagerServiceImpl::StartAssistantInternal( ...@@ -278,26 +284,23 @@ void AssistantManagerServiceImpl::StartAssistantInternal(
assistant_manager_internal_->SetAssistantManagerDelegate(this); assistant_manager_internal_->SetAssistantManagerDelegate(this);
assistant_manager_->AddConversationStateListener(this); assistant_manager_->AddConversationStateListener(this);
if (!assistant_settings_manager_) {
assistant_settings_manager_ =
std::make_unique<AssistantSettingsManagerImpl>(this);
}
SetAccessToken(access_token); SetAccessToken(access_token);
assistant_client::Callback0 assistant_callback([ assistant_manager_->Start();
callback = std::move(callback), }
update_settings_callback =
base::BindOnce(&AssistantManagerServiceImpl::UpdateDeviceSettings,
weak_factory_.GetWeakPtr())
]() mutable {
std::move(callback).Run();
std::move(update_settings_callback).Run();
});
assistant_manager_->Start(std::move(assistant_callback)); void AssistantManagerServiceImpl::PostInitAssistant(
base::OnceClosure post_init_callback) {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
state_ = State::RUNNING; state_ = State::RUNNING;
if (!assistant_settings_manager_) {
assistant_settings_manager_ =
std::make_unique<AssistantSettingsManagerImpl>(this);
}
std::move(post_init_callback).Run();
UpdateDeviceSettings();
} }
std::string AssistantManagerServiceImpl::BuildUserAgent( std::string AssistantManagerServiceImpl::BuildUserAgent(
...@@ -324,6 +327,8 @@ std::string AssistantManagerServiceImpl::BuildUserAgent( ...@@ -324,6 +327,8 @@ std::string AssistantManagerServiceImpl::BuildUserAgent(
} }
void AssistantManagerServiceImpl::UpdateDeviceSettings() { void AssistantManagerServiceImpl::UpdateDeviceSettings() {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
const std::string device_id = assistant_manager_->GetDeviceId(); const std::string device_id = assistant_manager_->GetDeviceId();
if (device_id.empty()) if (device_id.empty())
return; return;
...@@ -342,16 +347,14 @@ void AssistantManagerServiceImpl::UpdateDeviceSettings() { ...@@ -342,16 +347,14 @@ void AssistantManagerServiceImpl::UpdateDeviceSettings() {
SendUpdateSettingsUiRequest(update.SerializeAsString(), base::DoNothing()); SendUpdateSettingsUiRequest(update.SerializeAsString(), base::DoNothing());
// Update device locale if voice interaction setup is completed. // Update device locale if voice interaction setup is completed.
main_thread_task_runner_->PostTask( AssistantManagerServiceImpl::IsVoiceInteractionSetupCompleted(
FROM_HERE, base::BindOnce(&AssistantManagerServiceImpl::UpdateDeviceLocale,
base::BindOnce( weak_factory_.GetWeakPtr()));
&AssistantManagerServiceImpl::IsVoiceInteractionSetupCompleted,
weak_factory_.GetWeakPtr(),
base::BindOnce(&AssistantManagerServiceImpl::UpdateDeviceLocale,
weak_factory_.GetWeakPtr())));
} }
void AssistantManagerServiceImpl::UpdateDeviceLocale(bool is_setup_completed) { void AssistantManagerServiceImpl::UpdateDeviceLocale(bool is_setup_completed) {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
if (!is_setup_completed) if (!is_setup_completed)
return; return;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
// TODO(xiaohuic): replace with "base/macros.h" once we remove // TODO(xiaohuic): replace with "base/macros.h" once we remove
// libassistant/contrib dependency. // libassistant/contrib dependency.
#include "ash/public/interfaces/voice_interaction_controller.mojom.h" #include "ash/public/interfaces/voice_interaction_controller.mojom.h"
#include "base/threading/thread.h"
#include "chromeos/assistant/internal/action/cros_action_module.h" #include "chromeos/assistant/internal/action/cros_action_module.h"
#include "chromeos/assistant/internal/cros_display_connection.h" #include "chromeos/assistant/internal/cros_display_connection.h"
#include "chromeos/services/assistant/assistant_manager_service.h" #include "chromeos/services/assistant/assistant_manager_service.h"
...@@ -110,16 +111,16 @@ class AssistantManagerServiceImpl ...@@ -110,16 +111,16 @@ class AssistantManagerServiceImpl
ash::mojom::AssistantAllowedState state) override {} ash::mojom::AssistantAllowedState state) override {}
private: private:
void StartAssistantInternal( void StartAssistantInternal(const std::string& access_token,
base::OnceClosure callback, const std::string& arc_version);
const std::string& access_token, void PostInitAssistant(base::OnceClosure post_init_callback);
const std::string& arc_version,
assistant_client::AssistantManager* assistant_manager);
std::string BuildUserAgent(const std::string& arc_version) const; std::string BuildUserAgent(const std::string& arc_version) const;
// Update device id, type, and call |UpdateDeviceLocale| when assistant // Update device id, type, and call |UpdateDeviceLocale| when assistant
// service starts. // service starts.
void UpdateDeviceSettings(); void UpdateDeviceSettings();
// Update device locale if |is_setup_completed| is true; // Update device locale if |is_setup_completed| is true;
void UpdateDeviceLocale(bool is_setup_completed); void UpdateDeviceLocale(bool is_setup_completed);
...@@ -160,6 +161,9 @@ class AssistantManagerServiceImpl ...@@ -160,6 +161,9 @@ class AssistantManagerServiceImpl
ash::mojom::VoiceInteractionControllerPtr voice_interaction_controller_; ash::mojom::VoiceInteractionControllerPtr voice_interaction_controller_;
mojo::Binding<ash::mojom::VoiceInteractionObserver> mojo::Binding<ash::mojom::VoiceInteractionObserver>
voice_interaction_observer_binding_; voice_interaction_observer_binding_;
base::Thread background_thread_;
base::WeakPtrFactory<AssistantManagerServiceImpl> weak_factory_; base::WeakPtrFactory<AssistantManagerServiceImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(AssistantManagerServiceImpl); DISALLOW_COPY_AND_ASSIGN(AssistantManagerServiceImpl);
......
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