blob: a37a97cd77d7ff2233b92c39d9a420db2454ebeb [file] [log] [blame]
Hesen Zhang071689122020-04-21 22:02:241// 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 Sahuce57c7d92020-04-29 01:47:175#ifndef COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_
6#define COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_
Hesen Zhang071689122020-04-21 22:02:247
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "base/callback.h"
Hesen Zhang071689122020-04-21 22:02:2413#include "base/time/clock.h"
Shakti Sahuce57c7d92020-04-29 01:47:1714#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 Bikineev1156b5f2021-05-15 22:35:3618#include "third_party/abseil-cpp/absl/types/optional.h"
Hesen Zhang071689122020-04-21 22:02:2419
Xing Liu6d873212020-05-13 22:33:4920namespace query_tiles {
Hesen Zhang071689122020-04-21 22:02:2421
22// Manages the in-memory tile group and coordinates with storage layer.
23class TileManager {
24 public:
25 using TileStore = Store<TileGroup>;
26 using TileGroupStatusCallback = base::OnceCallback<void(TileGroupStatus)>;
Hesen Zhang4f42c282020-04-28 21:00:2427 using GetTilesCallback = base::OnceCallback<void(std::vector<Tile>)>;
Anton Bikineev1156b5f2021-05-15 22:35:3628 using TileCallback = base::OnceCallback<void(absl::optional<Tile>)>;
Hesen Zhang071689122020-04-21 22:02:2429
30 // Creates the instance.
31 static std::unique_ptr<TileManager> Create(
32 std::unique_ptr<TileStore> tile_store,
Min Qin7e9bd172020-05-12 23:20:1333 base::Clock* clock,
Min Qin80dc12c2020-05-14 15:13:0634 const std::string& accept_languages);
Hesen Zhang071689122020-04-21 22:02:2435
36 // Initializes the query tile store, loading them into memory after
37 // validating.
38 virtual void Init(TileGroupStatusCallback callback) = 0;
39
Min Qin7e9bd172020-05-12 23:20:1340 // Returns tiles to the caller in the given |locale|.
Hesen Zhang4f42c282020-04-28 21:00:2441 virtual void GetTiles(GetTilesCallback callback) = 0;
Hesen Zhang071689122020-04-21 22:02:2442
Shakti Sahufe5a7bca2020-04-30 00:00:1443 // Returns the tile associated with |tile_id| to the caller.
44 virtual void GetTile(const std::string& tile_id, TileCallback callback) = 0;
45
Hesen Zhang071689122020-04-21 22:02:2446 // Save the query tiles into database.
Hesen Zhangc34f49b2020-05-08 05:21:2547 virtual void SaveTiles(std::unique_ptr<TileGroup> tile_group,
Hesen Zhang70912aa2020-04-22 02:16:0848 TileGroupStatusCallback callback) = 0;
Hesen Zhang071689122020-04-21 22:02:2449
Hesen Zhangc6b1a262020-07-07 18:38:0950 // Delete everything in db. Used for debugging in WebUI only.
Hesen Zhangd4114042020-06-25 22:42:5051 virtual TileGroupStatus PurgeDb() = 0;
52
Hesen Zhangc6b1a262020-07-07 18:38:0953 // Dump the group. Used for debugging in WebUI only.
54 virtual TileGroup* GetTileGroup() = 0;
55
Min Qin07119772020-09-22 02:37:5956 // Called when a tile is clicked.
57 virtual void OnTileClicked(const std::string& tile_id) = 0;
58
Min Qin8e822ff22020-09-26 07:40:5459 // 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 Bikineev1156b5f2021-05-15 22:35:3662 const absl::optional<std::string>& parent_tile_id,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0163 const std::u16string& query_text) = 0;
Min Qin8e822ff22020-09-26 07:40:5464
Hesen Zhang071689122020-04-21 22:02:2465 TileManager();
66 virtual ~TileManager() = default;
67
68 TileManager(const TileManager& other) = delete;
69 TileManager& operator=(const TileManager& other) = delete;
Hesen Zhang9f839b32020-06-30 23:04:4970
Hesen Zhangc6b1a262020-07-07 18:38:0971 // ------------------------Testing------------------------
Hesen Zhang9f839b32020-06-30 23:04:4972 virtual void SetAcceptLanguagesForTesting(
73 const std::string& accept_languages) = 0;
Hesen Zhang071689122020-04-21 22:02:2474};
75
Xing Liu6d873212020-05-13 22:33:4976} // namespace query_tiles
Hesen Zhang071689122020-04-21 22:02:2477
Shakti Sahuce57c7d92020-04-29 01:47:1778#endif // COMPONENTS_QUERY_TILES_INTERNAL_TILE_MANAGER_H_