Commit c014ab34 authored by Xiaohui Chen's avatar Xiaohui Chen Committed by Commit Bot

assistant: avoid notifying duplicate state changes

Also moved some mojom service registration to later stage during
initialization.  This prevented crash during autotest because the tests
are much faster than human when enabling the service which caused the UI
to call into Assistant service before the libassistant components are
ready.

Bug: b:79879622
Test: run a localy autotest that invokes this new api
Change-Id: I643d0388115fa2690fa059b73b83748f8d06c335
Reviewed-on: https://chromium-review.googlesource.com/c/1330751Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607293}
parent 1f787ebf
...@@ -24,6 +24,9 @@ void VoiceInteractionController::BindRequest( ...@@ -24,6 +24,9 @@ void VoiceInteractionController::BindRequest(
void VoiceInteractionController::NotifyStatusChanged( void VoiceInteractionController::NotifyStatusChanged(
mojom::VoiceInteractionState state) { mojom::VoiceInteractionState state) {
if (voice_interaction_state_ == state)
return;
voice_interaction_state_ = state; voice_interaction_state_ = state;
observers_.ForAllPtrs([state](auto* observer) { observers_.ForAllPtrs([state](auto* observer) {
observer->OnVoiceInteractionStatusChanged(state); observer->OnVoiceInteractionStatusChanged(state);
...@@ -33,6 +36,9 @@ void VoiceInteractionController::NotifyStatusChanged( ...@@ -33,6 +36,9 @@ void VoiceInteractionController::NotifyStatusChanged(
} }
void VoiceInteractionController::NotifySettingsEnabled(bool enabled) { void VoiceInteractionController::NotifySettingsEnabled(bool enabled) {
if (settings_enabled_.has_value() && settings_enabled_.value() == enabled)
return;
settings_enabled_ = enabled; settings_enabled_ = enabled;
observers_.ForAllPtrs([enabled](auto* observer) { observers_.ForAllPtrs([enabled](auto* observer) {
observer->OnVoiceInteractionSettingsEnabled(enabled); observer->OnVoiceInteractionSettingsEnabled(enabled);
...@@ -42,6 +48,9 @@ void VoiceInteractionController::NotifySettingsEnabled(bool enabled) { ...@@ -42,6 +48,9 @@ void VoiceInteractionController::NotifySettingsEnabled(bool enabled) {
} }
void VoiceInteractionController::NotifyContextEnabled(bool enabled) { void VoiceInteractionController::NotifyContextEnabled(bool enabled) {
if (context_enabled_.has_value() && context_enabled_.value() == enabled)
return;
context_enabled_ = enabled; context_enabled_ = enabled;
observers_.ForAllPtrs([enabled](auto* observer) { observers_.ForAllPtrs([enabled](auto* observer) {
observer->OnVoiceInteractionContextEnabled(enabled); observer->OnVoiceInteractionContextEnabled(enabled);
...@@ -51,6 +60,9 @@ void VoiceInteractionController::NotifyContextEnabled(bool enabled) { ...@@ -51,6 +60,9 @@ void VoiceInteractionController::NotifyContextEnabled(bool enabled) {
} }
void VoiceInteractionController::NotifyHotwordEnabled(bool enabled) { void VoiceInteractionController::NotifyHotwordEnabled(bool enabled) {
if (hotword_enabled_.has_value() && hotword_enabled_.value() == enabled)
return;
hotword_enabled_ = enabled; hotword_enabled_ = enabled;
observers_.ForAllPtrs([enabled](auto* observer) { observers_.ForAllPtrs([enabled](auto* observer) {
observer->OnVoiceInteractionHotwordEnabled(enabled); observer->OnVoiceInteractionHotwordEnabled(enabled);
...@@ -60,6 +72,9 @@ void VoiceInteractionController::NotifyHotwordEnabled(bool enabled) { ...@@ -60,6 +72,9 @@ void VoiceInteractionController::NotifyHotwordEnabled(bool enabled) {
} }
void VoiceInteractionController::NotifySetupCompleted(bool completed) { void VoiceInteractionController::NotifySetupCompleted(bool completed) {
if (setup_completed_.has_value() && setup_completed_.value() == completed)
return;
setup_completed_ = completed; setup_completed_ = completed;
observers_.ForAllPtrs([completed](auto* observer) { observers_.ForAllPtrs([completed](auto* observer) {
observer->OnVoiceInteractionSetupCompleted(completed); observer->OnVoiceInteractionSetupCompleted(completed);
...@@ -70,6 +85,9 @@ void VoiceInteractionController::NotifySetupCompleted(bool completed) { ...@@ -70,6 +85,9 @@ void VoiceInteractionController::NotifySetupCompleted(bool completed) {
void VoiceInteractionController::NotifyFeatureAllowed( void VoiceInteractionController::NotifyFeatureAllowed(
mojom::AssistantAllowedState state) { mojom::AssistantAllowedState state) {
if (allowed_state_ == state)
return;
allowed_state_ = state; allowed_state_ = state;
observers_.ForAllPtrs([state](auto* observer) { observers_.ForAllPtrs([state](auto* observer) {
observer->OnAssistantFeatureAllowedChanged(state); observer->OnAssistantFeatureAllowedChanged(state);
...@@ -84,6 +102,9 @@ void VoiceInteractionController::NotifyNotificationEnabled(bool enabled) { ...@@ -84,6 +102,9 @@ void VoiceInteractionController::NotifyNotificationEnabled(bool enabled) {
void VoiceInteractionController::NotifyLocaleChanged( void VoiceInteractionController::NotifyLocaleChanged(
const std::string& locale) { const std::string& locale) {
if (locale_ == locale)
return;
locale_ = locale; locale_ = locale;
observers_.ForAllPtrs( observers_.ForAllPtrs(
[locale](auto* observer) { observer->OnLocaleChanged(locale); }); [locale](auto* observer) { observer->OnLocaleChanged(locale); });
......
...@@ -282,17 +282,8 @@ void Service::CreateAssistantManagerService() { ...@@ -282,17 +282,8 @@ void Service::CreateAssistantManagerService() {
// Bind to Assistant controller in ash. // Bind to Assistant controller in ash.
context()->connector()->BindInterface(ash::mojom::kServiceName, context()->connector()->BindInterface(ash::mojom::kServiceName,
&assistant_controller_); &assistant_controller_);
mojom::AssistantPtr ptr;
BindAssistantConnection(mojo::MakeRequest(&ptr));
assistant_controller_->SetAssistant(std::move(ptr));
registry_.AddInterface<mojom::Assistant>(base::BindRepeating(
&Service::BindAssistantConnection, base::Unretained(this)));
assistant_settings_manager_ = assistant_settings_manager_ =
assistant_manager_service_.get()->GetAssistantSettingsManager(); assistant_manager_service_.get()->GetAssistantSettingsManager();
registry_.AddInterface<mojom::AssistantSettingsManager>(base::BindRepeating(
&Service::BindAssistantSettingsManager, base::Unretained(this)));
#else #else
assistant_manager_service_ = assistant_manager_service_ =
std::make_unique<FakeAssistantManagerServiceImpl>(); std::make_unique<FakeAssistantManagerServiceImpl>();
...@@ -303,8 +294,20 @@ void Service::FinalizeAssistantManagerService() { ...@@ -303,8 +294,20 @@ void Service::FinalizeAssistantManagerService() {
DCHECK(assistant_manager_service_->GetState() == DCHECK(assistant_manager_service_->GetState() ==
AssistantManagerService::State::RUNNING); AssistantManagerService::State::RUNNING);
if (!session_observer_binding_) // Using session_observer_binding_ as a flag to control onetime initialization
if (!session_observer_binding_) {
mojom::AssistantPtr ptr;
BindAssistantConnection(mojo::MakeRequest(&ptr));
assistant_controller_->SetAssistant(std::move(ptr));
registry_.AddInterface<mojom::Assistant>(base::BindRepeating(
&Service::BindAssistantConnection, base::Unretained(this)));
registry_.AddInterface<mojom::AssistantSettingsManager>(base::BindRepeating(
&Service::BindAssistantSettingsManager, base::Unretained(this)));
AddAshSessionObserver(); AddAshSessionObserver();
}
client_->OnAssistantStatusChanged(true /* running */); client_->OnAssistantStatusChanged(true /* running */);
UpdateListeningState(); UpdateListeningState();
......
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