tile_manager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2020 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 COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_
  5. #define COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "components/query_tiles/internal/store.h"
  11. #include "components/query_tiles/internal/tile_group.h"
  12. #include "components/query_tiles/internal/tile_types.h"
  13. #include "components/query_tiles/tile.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace query_tiles {
  16. // Manages the in-memory tile group and coordinates with storage layer.
  17. class TileManager {
  18. public:
  19. using TileStore = Store<TileGroup>;
  20. using TileGroupStatusCallback = base::OnceCallback<void(TileGroupStatus)>;
  21. using GetTilesCallback = base::OnceCallback<void(std::vector<Tile>)>;
  22. using TileCallback = base::OnceCallback<void(absl::optional<Tile>)>;
  23. // Creates the instance.
  24. static std::unique_ptr<TileManager> Create(
  25. std::unique_ptr<TileStore> tile_store,
  26. const std::string& accept_languages);
  27. // Initializes the query tile store, loading them into memory after
  28. // validating.
  29. virtual void Init(TileGroupStatusCallback callback) = 0;
  30. // Returns tiles to the caller in the given |locale|.
  31. virtual void GetTiles(bool shuffle_tiles, GetTilesCallback callback) = 0;
  32. // Returns the tile associated with |tile_id| to the caller.
  33. virtual void GetTile(const std::string& tile_id,
  34. bool shuffle_tiles,
  35. TileCallback callback) = 0;
  36. // Save the query tiles into database.
  37. virtual void SaveTiles(std::unique_ptr<TileGroup> tile_group,
  38. TileGroupStatusCallback callback) = 0;
  39. // Delete everything in db. Used for debugging in WebUI only.
  40. virtual TileGroupStatus PurgeDb() = 0;
  41. // Dump the group. Used for debugging in WebUI only.
  42. virtual TileGroup* GetTileGroup() = 0;
  43. // Called when a tile is clicked.
  44. virtual void OnTileClicked(const std::string& tile_id) = 0;
  45. // Called when the final query is formed. |parent_tile_id| is the parent
  46. // Id of the last tile, if it exists.
  47. virtual void OnQuerySelected(
  48. const absl::optional<std::string>& parent_tile_id,
  49. const std::u16string& query_text) = 0;
  50. TileManager();
  51. virtual ~TileManager() = default;
  52. TileManager(const TileManager& other) = delete;
  53. TileManager& operator=(const TileManager& other) = delete;
  54. // ------------------------Testing------------------------
  55. virtual void SetAcceptLanguagesForTesting(
  56. const std::string& accept_languages) = 0;
  57. };
  58. } // namespace query_tiles
  59. #endif // COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_