assistant_response.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_
  5. #define ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_
  6. #include <deque>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/component_export.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/observer_list.h"
  13. #include "chromeos/services/libassistant/public/cpp/assistant_suggestion.h"
  14. namespace base {
  15. class UnguessableToken;
  16. } // namespace base
  17. namespace ash {
  18. class AssistantResponseObserver;
  19. class AssistantUiElement;
  20. // TODO(dmblack): Remove ProcessingState after launch of response processing v2.
  21. // Models a renderable Assistant response.
  22. // It is refcounted so that views that display the response can safely
  23. // reference the data inside this response.
  24. class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantResponse
  25. : public base::RefCounted<AssistantResponse> {
  26. public:
  27. using AssistantSuggestion = chromeos::assistant::AssistantSuggestion;
  28. using ProcessingCallback = base::OnceCallback<void(bool)>;
  29. enum class ProcessingState {
  30. kUnprocessed, // Response has not yet been processed.
  31. kProcessing, // Response is currently being processed.
  32. kProcessed, // Response has finished processing.
  33. };
  34. AssistantResponse();
  35. AssistantResponse(const AssistantResponse&) = delete;
  36. AssistantResponse& operator=(const AssistantResponse&) = delete;
  37. // Adds/removes the specified |observer|.
  38. // NOTE: only the AssistantInteractionController is able to obtain non-const
  39. // access to an AssistantResponse through its owned model, but there are const
  40. // accessors who wish to observe the response for changes in its underlying
  41. // data. To accomplish this, we make AddObserver() and RemoveObserver() const,
  42. // though these methods do modify the underlying ObserverList. This is safe to
  43. // do as AssistantResponseObserver only exposes const access to the underlying
  44. // response data and so doesn't expose AssistantResponse for modification.
  45. void AddObserver(AssistantResponseObserver* observer) const;
  46. void RemoveObserver(AssistantResponseObserver* observer) const;
  47. // Adds the specified |ui_element| that should be rendered for the
  48. // interaction.
  49. void AddUiElement(std::unique_ptr<AssistantUiElement> ui_element);
  50. // Returns all UI elements belonging to the response.
  51. const std::vector<std::unique_ptr<AssistantUiElement>>& GetUiElements() const;
  52. // Adds the specified |suggestions| that should be rendered for the
  53. // interaction.
  54. void AddSuggestions(const std::vector<AssistantSuggestion>& suggestions);
  55. // Returns the suggestion uniquely identified by |id|.
  56. const AssistantSuggestion* GetSuggestionById(
  57. const base::UnguessableToken& id) const;
  58. // Returns all suggestions belongs to the response.
  59. const std::vector<AssistantSuggestion>& GetSuggestions() const;
  60. // Gets/sets the processing state for the response.
  61. ProcessingState processing_state() const { return processing_state_; }
  62. void set_processing_state(ProcessingState processing_state) {
  63. processing_state_ = processing_state;
  64. }
  65. // Gets/sets if the response has TTS. This can only be reliably checked after
  66. // the response is finalized for obvious reasons.
  67. bool has_tts() const { return has_tts_; }
  68. void set_has_tts(bool has_tts) { has_tts_ = has_tts; }
  69. // Invoke to begin processing the response. The specified |callback| will be
  70. // run to indicate whether or not the processor has completed processing of
  71. // all UI elements in the response.
  72. void Process(ProcessingCallback callback);
  73. // Return true if this response contains an identical ui element.
  74. bool ContainsUiElement(const AssistantUiElement* element) const;
  75. private:
  76. void NotifyUiElementAdded(const AssistantUiElement* ui_element);
  77. void NotifySuggestionsAdded(const std::vector<AssistantSuggestion>&);
  78. // Return true if the pending ui elements contain an identical ui element.
  79. bool ContainsPendingUiElement(const AssistantUiElement* other) const;
  80. struct PendingUiElement;
  81. class Processor;
  82. friend class base::RefCounted<AssistantResponse>;
  83. ~AssistantResponse();
  84. std::deque<std::unique_ptr<PendingUiElement>> pending_ui_elements_;
  85. std::vector<AssistantSuggestion> suggestions_;
  86. ProcessingState processing_state_ = ProcessingState::kUnprocessed;
  87. bool has_tts_ = false;
  88. // We specify the declaration order below as intended because we want
  89. // |processor_| to be destroyed before |ui_elements_| (we also forced this
  90. // order in the destructor), so that when the response processing got
  91. // interrupted, the |ProcessingCallback| can have a chance to return false
  92. // during the destruction to indicate the failure of completion.
  93. std::vector<std::unique_ptr<AssistantUiElement>> ui_elements_;
  94. std::unique_ptr<Processor> processor_;
  95. mutable base::ObserverList<AssistantResponseObserver> observers_;
  96. base::WeakPtrFactory<AssistantResponse> weak_factory_{this};
  97. };
  98. } // namespace ash
  99. #endif // ASH_ASSISTANT_MODEL_ASSISTANT_RESPONSE_H_