1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67

ash / assistant / model / assistant_query_history.h [blame]

// Copyright 2018 The Chromium Authors
// 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_QUERY_HISTORY_H_
#define ASH_ASSISTANT_MODEL_ASSISTANT_QUERY_HISTORY_H_

#include <memory>
#include <optional>
#include <string>

#include "base/component_export.h"
#include "base/containers/circular_deque.h"
#include "base/memory/raw_ref.h"

namespace ash {

// Caches user query history.
class COMPONENT_EXPORT(ASSISTANT_MODEL) AssistantQueryHistory {
 public:
  class Iterator {
   public:
    explicit Iterator(const base::circular_deque<std::string>& queries);

    Iterator(const Iterator&) = delete;
    Iterator& operator=(const Iterator&) = delete;

    ~Iterator();

    // Fetches the next query. If current is already the last query, or there is
    // no query in history, returns nullopt.
    std::optional<std::string> Next();

    // Fetches the previous query. If current is already the first query, return
    // the first query. If there is no query in history, returns nullopt.
    std::optional<std::string> Prev();

    // Resets to the last query. It also makes current iterator valid again if
    // new queries are added to the underlying AssistantQueryHistory.
    void ResetToLast();

   private:
    const raw_ref<const base::circular_deque<std::string>> queries_;
    size_t cur_pos_;
  };

  AssistantQueryHistory(int capacity = 100);

  AssistantQueryHistory(const AssistantQueryHistory&) = delete;
  AssistantQueryHistory& operator=(const AssistantQueryHistory&) = delete;

  ~AssistantQueryHistory();

  // Gets the iterator of query history.
  std::unique_ptr<Iterator> GetIterator() const;

  // Adds a query to history. If it is empty, ignore it.
  void Add(const std::string& query);

 private:
  const int capacity_;
  base::circular_deque<std::string> queries_;
};

}  // namespace ash

#endif  // ASH_ASSISTANT_MODEL_ASSISTANT_QUERY_HISTORY_H_