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

Update Assistant response processing logic for v2.

In response processing v1, UI elements are immediately added to the
response and are processed in unison.

In response processing v2, UI elements are pended while they each are
processed individually. Only then are they added to the response as
being ready for rendering, being careful to maintain the correct
order of insertion.

Bug: b:2063264
Change-Id: Ia487b0f7b99850314a6c83493e9f72af1b8b7b83
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2065410Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#744115}
parent 0a5d39c1
...@@ -46,6 +46,7 @@ component("model") { ...@@ -46,6 +46,7 @@ component("model") {
deps = [ deps = [
"//ash/assistant/ui:constants", "//ash/assistant/ui:constants",
"//ash/public/cpp", "//ash/public/cpp",
"//chromeos/services/assistant/public:feature_flags",
"//chromeos/services/assistant/public/mojom", "//chromeos/services/assistant/public/mojom",
"//services/content/public/cpp", "//services/content/public/cpp",
"//ui/gfx/geometry", "//ui/gfx/geometry",
......
...@@ -10,10 +10,25 @@ ...@@ -10,10 +10,25 @@
#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"
#include "chromeos/services/assistant/public/features.h"
#include "chromeos/services/assistant/public/mojom/assistant.mojom.h" #include "chromeos/services/assistant/public/mojom/assistant.mojom.h"
namespace ash { namespace ash {
// AssistantResponse::PendingUiElement -----------------------------------------
struct AssistantResponse::PendingUiElement {
public:
PendingUiElement() = default;
~PendingUiElement() = default;
PendingUiElement(const PendingUiElement&) = delete;
PendingUiElement& operator=(const PendingUiElement&) = delete;
std::unique_ptr<AssistantUiElement> ui_element;
bool is_processing = false;
};
// AssistantResponse::Processor ------------------------------------------------ // AssistantResponse::Processor ------------------------------------------------
class AssistantResponse::Processor { class AssistantResponse::Processor {
...@@ -105,8 +120,44 @@ void AssistantResponse::RemoveObserver(AssistantResponseObserver* observer) { ...@@ -105,8 +120,44 @@ void AssistantResponse::RemoveObserver(AssistantResponseObserver* 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)); // In processing v1, UI elements are immediately added to the response.
NotifyUiElementAdded(ui_elements_.back().get()); if (!chromeos::assistant::features::IsResponseProcessingV2Enabled()) {
ui_elements_.push_back(std::move(ui_element));
NotifyUiElementAdded(ui_elements_.back().get());
return;
}
// In processing v2, UI elements are first cached in a pending state...
auto pending_ui_element = std::make_unique<PendingUiElement>();
pending_ui_element->ui_element = std::move(ui_element);
pending_ui_element->is_processing = true;
pending_ui_elements_.push_back(std::move(pending_ui_element));
// ...while we perform any pre-processing necessary prior to rendering.
pending_ui_elements_.back()->ui_element->Process(base::BindOnce(
[](const base::WeakPtr<AssistantResponse>& self,
PendingUiElement* pending_ui_element) {
if (!self)
return;
// Indicate that |pending_ui_element| has finished processing.
pending_ui_element->is_processing = false;
// Add any UI elements that are ready for rendering to the response.
// Note that this may or may not include the |pending_ui_element| which
// just finished processing as we are required to add renderable UI
// elements to the response in the same order that they were initially
// pended to avoid inadvertently shuffling the response.
while (!self->pending_ui_elements_.empty() &&
!self->pending_ui_elements_.front()->is_processing) {
self->ui_elements_.push_back(
std::move(self->pending_ui_elements_.front()->ui_element));
self->pending_ui_elements_.pop_front();
self->NotifyUiElementAdded(self->ui_elements_.back().get());
}
},
weak_factory_.GetWeakPtr(),
base::Unretained(pending_ui_elements_.back().get())));
} }
const std::vector<std::unique_ptr<AssistantUiElement>>& const std::vector<std::unique_ptr<AssistantUiElement>>&
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_ #ifndef ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_
#define ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_ #define ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_
#include <deque>
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
...@@ -83,11 +84,13 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse ...@@ -83,11 +84,13 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
void NotifyUiElementAdded(const AssistantUiElement* ui_element); void NotifyUiElementAdded(const AssistantUiElement* ui_element);
void NotifySuggestionsAdded(const std::vector<AssistantSuggestion*>&); void NotifySuggestionsAdded(const std::vector<AssistantSuggestion*>&);
struct PendingUiElement;
class Processor; class Processor;
friend class base::RefCounted<AssistantResponse>; friend class base::RefCounted<AssistantResponse>;
~AssistantResponse(); ~AssistantResponse();
std::deque<std::unique_ptr<PendingUiElement>> pending_ui_elements_;
std::vector<AssistantSuggestionPtr> suggestions_; std::vector<AssistantSuggestionPtr> suggestions_;
ProcessingState processing_state_ = ProcessingState::kUnprocessed; ProcessingState processing_state_ = ProcessingState::kUnprocessed;
bool has_tts_ = false; bool has_tts_ = false;
...@@ -102,6 +105,8 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse ...@@ -102,6 +105,8 @@ class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
base::ObserverList<AssistantResponseObserver> observers_; base::ObserverList<AssistantResponseObserver> observers_;
base::WeakPtrFactory<AssistantResponse> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(AssistantResponse); DISALLOW_COPY_AND_ASSIGN(AssistantResponse);
}; };
......
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