Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 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 | |
Shakti Sahu | ce57c7d9 | 2020-04-29 01:47:17 | [diff] [blame] | 5 | #ifndef COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_ |
| 6 | #define COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_ |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/callback.h" |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 13 | #include "base/time/clock.h" |
Shakti Sahu | ce57c7d9 | 2020-04-29 01:47:17 | [diff] [blame] | 14 | #include "components/query_tiles/internal/store.h" |
| 15 | #include "components/query_tiles/internal/tile_group.h" |
| 16 | #include "components/query_tiles/internal/tile_types.h" |
| 17 | #include "components/query_tiles/tile.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 18 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 19 | |
Xing Liu | 6d87321 | 2020-05-13 22:33:49 | [diff] [blame] | 20 | namespace query_tiles { |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 21 | |
| 22 | // Manages the in-memory tile group and coordinates with storage layer. |
| 23 | class TileManager { |
| 24 | public: |
| 25 | using TileStore = Store<TileGroup>; |
| 26 | using TileGroupStatusCallback = base::OnceCallback<void(TileGroupStatus)>; |
Hesen Zhang | 4f42c28 | 2020-04-28 21:00:24 | [diff] [blame] | 27 | using GetTilesCallback = base::OnceCallback<void(std::vector<Tile>)>; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 28 | using TileCallback = base::OnceCallback<void(absl::optional<Tile>)>; |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 29 | |
| 30 | // Creates the instance. |
| 31 | static std::unique_ptr<TileManager> Create( |
| 32 | std::unique_ptr<TileStore> tile_store, |
Min Qin | 7e9bd17 | 2020-05-12 23:20:13 | [diff] [blame] | 33 | base::Clock* clock, |
Min Qin | 80dc12c | 2020-05-14 15:13:06 | [diff] [blame] | 34 | const std::string& accept_languages); |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 35 | |
| 36 | // Initializes the query tile store, loading them into memory after |
| 37 | // validating. |
| 38 | virtual void Init(TileGroupStatusCallback callback) = 0; |
| 39 | |
Min Qin | 7e9bd17 | 2020-05-12 23:20:13 | [diff] [blame] | 40 | // Returns tiles to the caller in the given |locale|. |
Hesen Zhang | 4f42c28 | 2020-04-28 21:00:24 | [diff] [blame] | 41 | virtual void GetTiles(GetTilesCallback callback) = 0; |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 42 | |
Shakti Sahu | fe5a7bca | 2020-04-30 00:00:14 | [diff] [blame] | 43 | // Returns the tile associated with |tile_id| to the caller. |
| 44 | virtual void GetTile(const std::string& tile_id, TileCallback callback) = 0; |
| 45 | |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 46 | // Save the query tiles into database. |
Hesen Zhang | c34f49b | 2020-05-08 05:21:25 | [diff] [blame] | 47 | virtual void SaveTiles(std::unique_ptr<TileGroup> tile_group, |
Hesen Zhang | 70912aa | 2020-04-22 02:16:08 | [diff] [blame] | 48 | TileGroupStatusCallback callback) = 0; |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 49 | |
Hesen Zhang | c6b1a26 | 2020-07-07 18:38:09 | [diff] [blame] | 50 | // Delete everything in db. Used for debugging in WebUI only. |
Hesen Zhang | d411404 | 2020-06-25 22:42:50 | [diff] [blame] | 51 | virtual TileGroupStatus PurgeDb() = 0; |
| 52 | |
Hesen Zhang | c6b1a26 | 2020-07-07 18:38:09 | [diff] [blame] | 53 | // Dump the group. Used for debugging in WebUI only. |
| 54 | virtual TileGroup* GetTileGroup() = 0; |
| 55 | |
Min Qin | 0711977 | 2020-09-22 02:37:59 | [diff] [blame] | 56 | // Called when a tile is clicked. |
| 57 | virtual void OnTileClicked(const std::string& tile_id) = 0; |
| 58 | |
Min Qin | 8e822ff2 | 2020-09-26 07:40:54 | [diff] [blame] | 59 | // Called when the final query is formed. |parent_tile_id| is the parent |
| 60 | // Id of the last tile, if it exists. |
| 61 | virtual void OnQuerySelected( |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 62 | const absl::optional<std::string>& parent_tile_id, |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 63 | const std::u16string& query_text) = 0; |
Min Qin | 8e822ff2 | 2020-09-26 07:40:54 | [diff] [blame] | 64 | |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 65 | TileManager(); |
| 66 | virtual ~TileManager() = default; |
| 67 | |
| 68 | TileManager(const TileManager& other) = delete; |
| 69 | TileManager& operator=(const TileManager& other) = delete; |
Hesen Zhang | 9f839b3 | 2020-06-30 23:04:49 | [diff] [blame] | 70 | |
Hesen Zhang | c6b1a26 | 2020-07-07 18:38:09 | [diff] [blame] | 71 | // ------------------------Testing------------------------ |
Hesen Zhang | 9f839b3 | 2020-06-30 23:04:49 | [diff] [blame] | 72 | virtual void SetAcceptLanguagesForTesting( |
| 73 | const std::string& accept_languages) = 0; |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 74 | }; |
| 75 | |
Xing Liu | 6d87321 | 2020-05-13 22:33:49 | [diff] [blame] | 76 | } // namespace query_tiles |
Hesen Zhang | 07168912 | 2020-04-21 22:02:24 | [diff] [blame] | 77 | |
Shakti Sahu | ce57c7d9 | 2020-04-29 01:47:17 | [diff] [blame] | 78 | #endif // COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_ |