assistant_interaction_model.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_INTERACTION_MODEL_H_
  5. #define ASH_ASSISTANT_MODEL_ASSISTANT_INTERACTION_MODEL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/assistant/model/assistant_query_history.h"
  10. #include "base/component_export.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/observer_list.h"
  13. namespace ash {
  14. class AssistantInteractionModelObserver;
  15. class AssistantQuery;
  16. class AssistantResponse;
  17. // Enumeration of interaction input modalities.
  18. enum class InputModality {
  19. kKeyboard,
  20. kVoice,
  21. };
  22. // TODO(dmblack): This is an oversimplification. We will eventually want to
  23. // distinctly represent listening/thinking/etc. states explicitly so they can
  24. // be adequately represented in the UI.
  25. // Enumeration of interaction states.
  26. enum class InteractionState {
  27. kActive,
  28. kInactive,
  29. };
  30. // Enumeration of interaction mic states.
  31. enum class MicState {
  32. kClosed,
  33. kOpen,
  34. };
  35. // Models the Assistant interaction. This includes query state, state of speech
  36. // recognition, as well as a renderable AssistantResponse.
  37. class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantInteractionModel {
  38. public:
  39. AssistantInteractionModel();
  40. AssistantInteractionModel(const AssistantInteractionModel&) = delete;
  41. AssistantInteractionModel& operator=(const AssistantInteractionModel&) =
  42. delete;
  43. ~AssistantInteractionModel();
  44. // Adds/removes the specified interaction model |observer|.
  45. void AddObserver(AssistantInteractionModelObserver* observer) const;
  46. void RemoveObserver(AssistantInteractionModelObserver* observer) const;
  47. // Resets the interaction to its initial state.
  48. void ClearInteraction();
  49. // Resets the interaction to its initial state. There are instances in which
  50. // we wish to clear the interaction but retain the committed query. Similarly,
  51. // there are instances in which we wish to retain the pending response that is
  52. // currently cached. For such instances, use |retain_committed_query| and
  53. // |retain_pending_response| respectively.
  54. void ClearInteraction(bool retain_committed_query,
  55. bool retain_pending_response);
  56. // Sets the interaction state.
  57. void SetInteractionState(InteractionState interaction_state);
  58. // Returns the interaction state.
  59. InteractionState interaction_state() const { return interaction_state_; }
  60. // Updates the input modality for the interaction.
  61. void SetInputModality(InputModality input_modality);
  62. // Returns the input modality for the interaction.
  63. InputModality input_modality() const { return input_modality_; }
  64. // Updates the mic state for the interaction.
  65. void SetMicState(MicState mic_state);
  66. // Returns the mic state for the interaction.
  67. MicState mic_state() const { return mic_state_; }
  68. // Returns the committed query for the interaction.
  69. const AssistantQuery& committed_query() const { return *committed_query_; }
  70. // Clears the committed query for the interaction.
  71. void ClearCommittedQuery();
  72. // Updates the pending query for the interaction.
  73. void SetPendingQuery(std::unique_ptr<AssistantQuery> pending_query);
  74. // Returns the pending query for the interaction.
  75. const AssistantQuery& pending_query() const { return *pending_query_; }
  76. // Commits the pending query for the interaction.
  77. void CommitPendingQuery();
  78. // Clears the pending query for the interaction.
  79. void ClearPendingQuery();
  80. // Sets the pending response for the interaction.
  81. void SetPendingResponse(scoped_refptr<AssistantResponse> response);
  82. // Returns the pending response for the interaction.
  83. AssistantResponse* pending_response() { return pending_response_.get(); }
  84. // Commits the pending response for the interaction. Note that this will cause
  85. // the previously committed response, if one exists, to be animated off stage
  86. // after which the newly committed response will begin rendering.
  87. void CommitPendingResponse();
  88. // Clears the pending response for the interaction.
  89. void ClearPendingResponse();
  90. // Returns the committed response for the interaction.
  91. AssistantResponse* response() { return response_.get(); }
  92. const AssistantResponse* response() const { return response_.get(); }
  93. // Clears the committed response for the interaction.
  94. void ClearResponse();
  95. // Updates the speech level in dB.
  96. void SetSpeechLevel(float speech_level_db);
  97. // Returns the reference to query history.
  98. AssistantQueryHistory& query_history() { return query_history_; }
  99. // Returns the const reference to query history.
  100. const AssistantQueryHistory& query_history() const { return query_history_; }
  101. private:
  102. void NotifyInteractionStateChanged();
  103. void NotifyInputModalityChanged();
  104. void NotifyMicStateChanged();
  105. void NotifyCommittedQueryChanged();
  106. void NotifyCommittedQueryCleared();
  107. void NotifyPendingQueryChanged();
  108. void NotifyPendingQueryCleared(bool due_to_commit);
  109. void NotifyResponseChanged();
  110. void NotifyResponseCleared();
  111. void NotifySpeechLevelChanged(float speech_level_db);
  112. InteractionState interaction_state_ = InteractionState::kInactive;
  113. InputModality input_modality_ = InputModality::kKeyboard;
  114. MicState mic_state_ = MicState::kClosed;
  115. AssistantQueryHistory query_history_;
  116. std::unique_ptr<AssistantQuery> committed_query_;
  117. std::unique_ptr<AssistantQuery> pending_query_;
  118. scoped_refptr<AssistantResponse> pending_response_;
  119. scoped_refptr<AssistantResponse> response_;
  120. mutable base::ObserverList<AssistantInteractionModelObserver> observers_;
  121. };
  122. } // namespace ash
  123. #endif // ASH_ASSISTANT_MODEL_ASSISTANT_INTERACTION_MODEL_H_