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

Adds AssistantResponseObserver.

AssistantResponseObserver will be used in response processing V2. This
CL just introduces the observer, follow up CLs will use it and refine
when its methods are invoked. Currently OnUiElementAdded is fired when
the UI element is added to the response, but in a follow up CL it will
first be pended and pre-processed before being added.

Bug: b:129411551
Change-Id: I905ea9a2322d241fdc609b95b98ebd7713f79bc4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2063034
Commit-Queue: David Black <dmblack@google.com>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742455}
parent 001711e3
...@@ -25,6 +25,7 @@ component("model") { ...@@ -25,6 +25,7 @@ component("model") {
"assistant_query_history.h", "assistant_query_history.h",
"assistant_response.cc", "assistant_response.cc",
"assistant_response.h", "assistant_response.h",
"assistant_response_observer.h",
"assistant_screen_context_model.cc", "assistant_screen_context_model.cc",
"assistant_screen_context_model.h", "assistant_screen_context_model.h",
"assistant_screen_context_model_observer.h", "assistant_screen_context_model_observer.h",
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "ash/assistant/model/assistant_response_observer.h"
#include "ash/assistant/model/ui/assistant_ui_element.h" #include "ash/assistant/model/ui/assistant_ui_element.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
...@@ -94,9 +95,18 @@ AssistantResponse::~AssistantResponse() { ...@@ -94,9 +95,18 @@ AssistantResponse::~AssistantResponse() {
processor_.reset(); processor_.reset();
} }
void AssistantResponse::AddObserver(AssistantResponseObserver* observer) {
observers_.AddObserver(observer);
}
void AssistantResponse::RemoveObserver(AssistantResponseObserver* observer) {
observers_.RemoveObserver(observer);
}
void AssistantResponse::AddUiElement( void AssistantResponse::AddUiElement(
std::unique_ptr<AssistantUiElement> ui_element) { std::unique_ptr<AssistantUiElement> ui_element) {
ui_elements_.push_back(std::move(ui_element)); ui_elements_.push_back(std::move(ui_element));
NotifyUiElementAdded(ui_elements_.back().get());
} }
const std::vector<std::unique_ptr<AssistantUiElement>>& const std::vector<std::unique_ptr<AssistantUiElement>>&
...@@ -106,8 +116,14 @@ AssistantResponse::GetUiElements() const { ...@@ -106,8 +116,14 @@ AssistantResponse::GetUiElements() const {
void AssistantResponse::AddSuggestions( void AssistantResponse::AddSuggestions(
std::vector<AssistantSuggestionPtr> suggestions) { std::vector<AssistantSuggestionPtr> suggestions) {
for (AssistantSuggestionPtr& suggestion : suggestions) std::vector<AssistantSuggestion*> ptrs;
for (AssistantSuggestionPtr& suggestion : suggestions) {
suggestions_.push_back(std::move(suggestion)); suggestions_.push_back(std::move(suggestion));
ptrs.push_back(suggestions_.back().get());
}
NotifySuggestionsAdded(ptrs);
} }
const chromeos::assistant::mojom::AssistantSuggestion* const chromeos::assistant::mojom::AssistantSuggestion*
...@@ -137,4 +153,16 @@ void AssistantResponse::Process(ProcessingCallback callback) { ...@@ -137,4 +153,16 @@ void AssistantResponse::Process(ProcessingCallback callback) {
processor_->Process(); processor_->Process();
} }
void AssistantResponse::NotifyUiElementAdded(
const AssistantUiElement* ui_element) {
for (auto& observer : observers_)
observer.OnUiElementAdded(ui_element);
}
void AssistantResponse::NotifySuggestionsAdded(
const std::vector<AssistantSuggestion*>& suggestions) {
for (auto& observer : observers_)
observer.OnSuggestionsAdded(suggestions);
}
} // namespace ash } // namespace ash
...@@ -13,10 +13,12 @@ ...@@ -13,10 +13,12 @@
#include "base/component_export.h" #include "base/component_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "chromeos/services/assistant/public/mojom/assistant.mojom-forward.h" #include "chromeos/services/assistant/public/mojom/assistant.mojom-forward.h"
namespace ash { namespace ash {
class AssistantResponseObserver;
class AssistantUiElement; class AssistantUiElement;
// Models a renderable Assistant response. // Models a renderable Assistant response.
...@@ -39,6 +41,10 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse ...@@ -39,6 +41,10 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
AssistantResponse(); AssistantResponse();
// Adds/removes the specified |observer|.
void AddObserver(AssistantResponseObserver* observer);
void RemoveObserver(AssistantResponseObserver* observer);
// Adds the specified |ui_element| that should be rendered for the // Adds the specified |ui_element| that should be rendered for the
// interaction. // interaction.
void AddUiElement(std::unique_ptr<AssistantUiElement> ui_element); void AddUiElement(std::unique_ptr<AssistantUiElement> ui_element);
...@@ -73,6 +79,9 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse ...@@ -73,6 +79,9 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
void Process(ProcessingCallback callback); void Process(ProcessingCallback callback);
private: private:
void NotifyUiElementAdded(const AssistantUiElement* ui_element);
void NotifySuggestionsAdded(const std::vector<AssistantSuggestion*>&);
class Processor; class Processor;
friend class base::RefCounted<AssistantResponse>; friend class base::RefCounted<AssistantResponse>;
...@@ -90,6 +99,8 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse ...@@ -90,6 +99,8 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
std::vector<std::unique_ptr<AssistantUiElement>> ui_elements_; std::vector<std::unique_ptr<AssistantUiElement>> ui_elements_;
std::unique_ptr<Processor> processor_; std::unique_ptr<Processor> processor_;
base::ObserverList<AssistantResponseObserver> observers_;
DISALLOW_COPY_AND_ASSIGN(AssistantResponse); DISALLOW_COPY_AND_ASSIGN(AssistantResponse);
}; };
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_OBSERVER_H_
#define ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_OBSERVER_H_
#include <vector>
#include "base/component_export.h"
#include "base/observer_list_types.h"
#include "chromeos/services/assistant/public/mojom/assistant.mojom-forward.h"
namespace ash {
class AssistantUiElement;
// A checked observer which receives Assistant response events.
class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponseObserver
: public base::CheckedObserver {
public:
using AssistantSuggestion = chromeos::assistant::mojom::AssistantSuggestion;
// Invoked when the specified |ui_element| is added to the response.
virtual void OnUiElementAdded(const AssistantUiElement* ui_element) {}
// Invoked when the specified |suggestions| are added to the response.
virtual void OnSuggestionsAdded(const std::vector<AssistantSuggestion*>&) {}
protected:
AssistantResponseObserver() = default;
~AssistantResponseObserver() override = default;
};
} // namespace ash
#endif // ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_OBSERVER_H_
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