assistant_query_history.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_QUERY_HISTORY_H_
  5. #define ASH_ASSISTANT_MODEL_ASSISTANT_QUERY_HISTORY_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "base/containers/circular_deque.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace ash {
  12. // Caches user query history.
  13. class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantQueryHistory {
  14. public:
  15. class Iterator {
  16. public:
  17. explicit Iterator(const base::circular_deque<std::string>& queries);
  18. Iterator(const Iterator&) = delete;
  19. Iterator& operator=(const Iterator&) = delete;
  20. ~Iterator();
  21. // Fetches the next query. If current is already the last query, or there is
  22. // no query in history, returns nullopt.
  23. absl::optional<std::string> Next();
  24. // Fetches the previous query. If current is already the first query, return
  25. // the first query. If there is no query in history, returns nullopt.
  26. absl::optional<std::string> Prev();
  27. // Resets to the last query. It also makes current iterator valid again if
  28. // new queries are added to the underlying AssistantQueryHistory.
  29. void ResetToLast();
  30. private:
  31. const base::circular_deque<std::string>& queries_;
  32. size_t cur_pos_;
  33. };
  34. AssistantQueryHistory(int capacity = 100);
  35. AssistantQueryHistory(const AssistantQueryHistory&) = delete;
  36. AssistantQueryHistory& operator=(const AssistantQueryHistory&) = delete;
  37. ~AssistantQueryHistory();
  38. // Gets the iterator of query history.
  39. std::unique_ptr<Iterator> GetIterator() const;
  40. // Adds a query to history. If it is empty, ignore it.
  41. void Add(const std::string& query);
  42. private:
  43. const int capacity_;
  44. base::circular_deque<std::string> queries_;
  45. };
  46. } // namespace ash
  47. #endif // ASH_ASSISTANT_MODEL_ASSISTANT_QUERY_HISTORY_H_