Commit d7379a72 authored by David Black's avatar David Black Committed by Commit Bot

Caches Assistant proactive suggestions in ash.

Also wires up AssistantUiController to observe changes to the cache in
ash in order to show/hide the associated view as necessary.

Follow up CLs will wire up the associated view to be in sync with the
live data in the cache (currently it stubs in placeholders) as well as
to handle the entry point to show content in Assistant UI.

Bug: b:139199754
Change-Id: I4bee76004fee1782abc70eab0e9ddb9f8695c648
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1762341
Commit-Queue: David Black <dmblack@google.com>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689625}
parent b281b2e3
......@@ -11,6 +11,8 @@
#include "ash/assistant/assistant_ui_controller.h"
#include "ash/assistant/util/assistant_util.h"
#include "ash/assistant/util/deep_link_util.h"
#include "ash/public/cpp/assistant/proactive_suggestions.h"
#include "ash/public/cpp/assistant/proactive_suggestions_client.h"
#include "ash/public/cpp/voice_interaction_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
......@@ -71,6 +73,10 @@ AssistantSuggestionsController::AssistantSuggestionsController(
}
AssistantSuggestionsController::~AssistantSuggestionsController() {
auto* client = ProactiveSuggestionsClient::Get();
if (client)
client->set_delegate(nullptr);
assistant_controller_->RemoveObserver(this);
VoiceInteractionController::Get()->RemoveLocalObserver(this);
}
......@@ -93,6 +99,14 @@ void AssistantSuggestionsController::OnAssistantControllerDestroying() {
assistant_controller_->ui_controller()->RemoveModelObserver(this);
}
void AssistantSuggestionsController::OnAssistantReady() {
// The proactive suggestions client initializes late so we need to wait for
// the ready signal before binding as its delegate.
auto* client = ProactiveSuggestionsClient::Get();
if (client)
client->set_delegate(this);
}
void AssistantSuggestionsController::OnUiVisibilityChanged(
AssistantVisibility new_visibility,
AssistantVisibility old_visibility,
......@@ -109,6 +123,17 @@ void AssistantSuggestionsController::OnVoiceInteractionContextEnabled(
UpdateConversationStarters();
}
void AssistantSuggestionsController::OnProactiveSuggestionsClientDestroying() {
auto* client = ProactiveSuggestionsClient::Get();
if (client)
client->set_delegate(nullptr);
}
void AssistantSuggestionsController::OnProactiveSuggestionsChanged(
std::unique_ptr<ProactiveSuggestions> proactive_suggestions) {
model_.SetProactiveSuggestions(std::move(proactive_suggestions));
}
// TODO(dmblack): The conversation starter cache should receive its contents
// from the server. Hard-coding for the time being.
void AssistantSuggestionsController::UpdateConversationStarters() {
......
......@@ -5,10 +5,13 @@
#ifndef ASH_ASSISTANT_ASSISTANT_SUGGESTIONS_CONTROLLER_H_
#define ASH_ASSISTANT_ASSISTANT_SUGGESTIONS_CONTROLLER_H_
#include <memory>
#include "ash/assistant/assistant_controller_observer.h"
#include "ash/assistant/model/assistant_suggestions_model.h"
#include "ash/assistant/model/assistant_ui_model_observer.h"
#include "ash/public/cpp/assistant/default_voice_interaction_observer.h"
#include "ash/public/cpp/assistant/proactive_suggestions_client.h"
#include "ash/public/mojom/voice_interaction_controller.mojom.h"
#include "base/macros.h"
......@@ -17,9 +20,11 @@ namespace ash {
class AssistantController;
class AssistantSuggestionsModelObserver;
class AssistantSuggestionsController : public AssistantControllerObserver,
public AssistantUiModelObserver,
public DefaultVoiceInteractionObserver {
class AssistantSuggestionsController
: public AssistantControllerObserver,
public AssistantUiModelObserver,
public DefaultVoiceInteractionObserver,
public ProactiveSuggestionsClient::Delegate {
public:
explicit AssistantSuggestionsController(
AssistantController* assistant_controller);
......@@ -35,6 +40,7 @@ class AssistantSuggestionsController : public AssistantControllerObserver,
// AssistantControllerObserver:
void OnAssistantControllerConstructed() override;
void OnAssistantControllerDestroying() override;
void OnAssistantReady() override;
// AssistantUiModelObserver:
void OnUiVisibilityChanged(
......@@ -43,6 +49,11 @@ class AssistantSuggestionsController : public AssistantControllerObserver,
base::Optional<AssistantEntryPoint> entry_point,
base::Optional<AssistantExitPoint> exit_point) override;
// ProactiveSuggestionsClient::Delegate:
void OnProactiveSuggestionsClientDestroying() override;
void OnProactiveSuggestionsChanged(
std::unique_ptr<ProactiveSuggestions> proactive_suggestions) override;
private:
// DefaultVoiceInteractionObserver:
void OnVoiceInteractionContextEnabled(bool enabled) override;
......
......@@ -145,6 +145,25 @@ void AssistantUiController::OnMicStateChanged(MicState mic_state) {
UpdateUiMode();
}
void AssistantUiController::OnProactiveSuggestionsChanged(
const ProactiveSuggestions* proactive_suggestions) {
// When proactive suggestions are present, we show the associated view if it
// isn't showing already. If it's already showing, no action need be taken.
if (proactive_suggestions) {
if (!proactive_suggestions_view_) {
CreateProactiveSuggestionsView();
proactive_suggestions_view_->GetWidget()->ShowInactive();
}
return;
}
// When proactive suggestions are absent, we need to ensure that the
// associated view is absent if it isn't already.
if (proactive_suggestions_view_) {
ResetProactiveSuggestionsView();
DCHECK(!proactive_suggestions_view_);
}
}
void AssistantUiController::OnScreenContextRequestStateChanged(
ScreenContextRequestState request_state) {
if (model_.visibility() != AssistantVisibility::kVisible)
......@@ -231,11 +250,13 @@ void AssistantUiController::OnHighlighterEnabledChanged(
void AssistantUiController::OnAssistantControllerConstructed() {
assistant_controller_->interaction_controller()->AddModelObserver(this);
assistant_controller_->screen_context_controller()->AddModelObserver(this);
assistant_controller_->suggestions_controller()->AddModelObserver(this);
assistant_controller_->view_delegate()->AddObserver(this);
}
void AssistantUiController::OnAssistantControllerDestroying() {
assistant_controller_->view_delegate()->RemoveObserver(this);
assistant_controller_->suggestions_controller()->RemoveModelObserver(this);
assistant_controller_->screen_context_controller()->RemoveModelObserver(this);
assistant_controller_->interaction_controller()->RemoveModelObserver(this);
......
......@@ -13,6 +13,7 @@
#include "ash/assistant/assistant_controller_observer.h"
#include "ash/assistant/model/assistant_interaction_model_observer.h"
#include "ash/assistant/model/assistant_screen_context_model_observer.h"
#include "ash/assistant/model/assistant_suggestions_model_observer.h"
#include "ash/assistant/model/assistant_ui_model.h"
#include "ash/assistant/model/assistant_ui_model_observer.h"
#include "ash/assistant/ui/assistant_view_delegate.h"
......@@ -51,6 +52,7 @@ class ASH_EXPORT AssistantUiController
public AssistantControllerObserver,
public AssistantInteractionModelObserver,
public AssistantScreenContextModelObserver,
public AssistantSuggestionsModelObserver,
public AssistantUiModelObserver,
public AssistantViewDelegateObserver,
public CaptionBarDelegate,
......@@ -82,6 +84,10 @@ class ASH_EXPORT AssistantUiController
void OnInteractionStateChanged(InteractionState interaction_state) override;
void OnMicStateChanged(MicState mic_state) override;
// AssistantSuggestionsModelObserver:
void OnProactiveSuggestionsChanged(
const ProactiveSuggestions* proactive_suggestions) override;
// AssistantScreenContextModelObserver:
void OnScreenContextRequestStateChanged(
ScreenContextRequestState request_state) override;
......
......@@ -3,6 +3,7 @@ include_rules = [
"+ash/assistant/ui/assistant_ui_constants.h",
"+ash/assistant/model",
"+ash/public/cpp/app_list",
"+ash/public/cpp/assistant",
"+chromeos/services/assistant/public" ,
"+services/content/public",
"+ui/gfx/geometry",
......
......@@ -5,6 +5,7 @@
#include "ash/assistant/model/assistant_suggestions_model.h"
#include "ash/assistant/model/assistant_suggestions_model_observer.h"
#include "ash/public/cpp/assistant/proactive_suggestions.h"
namespace ash {
......@@ -51,6 +52,22 @@ AssistantSuggestionsModel::GetConversationStarters() const {
return conversation_starters;
}
void AssistantSuggestionsModel::SetProactiveSuggestions(
std::unique_ptr<ProactiveSuggestions> proactive_suggestions) {
if (ProactiveSuggestions::AreEqual(proactive_suggestions.get(),
proactive_suggestions_.get())) {
return;
}
proactive_suggestions_ = std::move(proactive_suggestions);
NotifyProactiveSuggestionsChanged();
}
const ProactiveSuggestions* AssistantSuggestionsModel::GetProactiveSuggestions()
const {
return proactive_suggestions_.get();
}
void AssistantSuggestionsModel::NotifyConversationStartersChanged() {
const std::map<int, const AssistantSuggestion*> conversation_starters =
GetConversationStarters();
......@@ -59,4 +76,9 @@ void AssistantSuggestionsModel::NotifyConversationStartersChanged() {
observer.OnConversationStartersChanged(conversation_starters);
}
void AssistantSuggestionsModel::NotifyProactiveSuggestionsChanged() {
for (AssistantSuggestionsModelObserver& observer : observers_)
observer.OnProactiveSuggestionsChanged(proactive_suggestions_.get());
}
} // namespace ash
......@@ -6,6 +6,7 @@
#define ASH_ASSISTANT_MODEL_ASSISTANT_SUGGESTIONS_MODEL_H_
#include <map>
#include <memory>
#include <vector>
#include "base/component_export.h"
......@@ -16,6 +17,7 @@
namespace ash {
class AssistantSuggestionsModelObserver;
class ProactiveSuggestions;
class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantSuggestionsModel {
public:
......@@ -40,10 +42,19 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantSuggestionsModel {
// Returns all cached conversation starters, mapped to a unique id.
std::map<int, const AssistantSuggestion*> GetConversationStarters() const;
// Sets the cache of proactive suggestions.
void SetProactiveSuggestions(
std::unique_ptr<ProactiveSuggestions> proactive_suggestions);
// Returns the cache of proactive suggestions.
const ProactiveSuggestions* GetProactiveSuggestions() const;
private:
void NotifyConversationStartersChanged();
void NotifyProactiveSuggestionsChanged();
std::vector<AssistantSuggestionPtr> conversation_starters_;
std::unique_ptr<ProactiveSuggestions> proactive_suggestions_;
base::ObserverList<AssistantSuggestionsModelObserver> observers_;
......
......@@ -14,6 +14,8 @@
namespace ash {
class ProactiveSuggestions;
// A checked observer which receives notification of changes to the Assistant
// suggestions model.
class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantSuggestionsModelObserver
......@@ -25,6 +27,10 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantSuggestionsModelObserver
virtual void OnConversationStartersChanged(
const std::map<int, const AssistantSuggestion*>& conversation_starters) {}
// Invoked when the cache of proactive suggestions has changed.
virtual void OnProactiveSuggestionsChanged(
const ProactiveSuggestions* proactive_suggestions) {}
protected:
~AssistantSuggestionsModelObserver() override = default;
};
......
......@@ -19,7 +19,7 @@ class ProactiveSuggestions;
// proactive content suggestions.
class ASH_PUBLIC_EXPORT ProactiveSuggestionsClient {
public:
// A delegate for the ProactiveSuggestionsClient.
// A delegate interface for the ProactiveSuggestionsClient.
class Delegate {
public:
// Invoked when the proactive suggestions client is being destroyed so as to
......
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