tile_group.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "components/query_tiles/internal/tile_group.h"
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include "components/query_tiles/internal/tile_iterator.h"
  9. #include "components/query_tiles/internal/tile_utils.h"
  10. namespace query_tiles {
  11. namespace {
  12. // Score to be received by a tile when it is clicked.
  13. constexpr double kTileClickScore = 1.0;
  14. void DeepCopyGroup(const TileGroup& input, TileGroup* output) {
  15. DCHECK(output);
  16. output->id = input.id;
  17. output->locale = input.locale;
  18. output->last_updated_ts = input.last_updated_ts;
  19. output->tiles.clear();
  20. for (const auto& tile : input.tiles)
  21. output->tiles.emplace_back(std::make_unique<Tile>(*tile.get()));
  22. output->tile_stats = input.tile_stats;
  23. }
  24. // Removes |id| from |id_set|. Returns true if |id| is found, or false
  25. // otherwise.
  26. bool RemoveIdFromSet(std::set<std::string>* id_set, const std::string& id) {
  27. const auto it = id_set->find(id);
  28. if (it != id_set->end()) {
  29. id_set->erase(it);
  30. return true;
  31. }
  32. return false;
  33. }
  34. } // namespace
  35. TileGroup::TileGroup() = default;
  36. TileGroup::~TileGroup() = default;
  37. bool TileGroup::operator==(const TileGroup& other) const {
  38. return id == other.id && locale == other.locale &&
  39. last_updated_ts == other.last_updated_ts &&
  40. tiles.size() == other.tiles.size();
  41. }
  42. bool TileGroup::operator!=(const TileGroup& other) const {
  43. return !(*this == other);
  44. }
  45. void TileGroup::OnTileClicked(const std::string& tile_id) {
  46. base::Time now_time = base::Time::Now();
  47. auto iter = tile_stats.find(tile_id);
  48. double score =
  49. (iter == tile_stats.end())
  50. ? kTileClickScore
  51. : kTileClickScore + CalculateTileScore(iter->second, now_time);
  52. tile_stats[tile_id] = TileStats(now_time, score);
  53. }
  54. TileGroup::TileGroup(const TileGroup& other) {
  55. DeepCopyGroup(other, this);
  56. }
  57. TileGroup::TileGroup(TileGroup&& other) = default;
  58. TileGroup& TileGroup::operator=(const TileGroup& other) {
  59. DeepCopyGroup(other, this);
  60. return *this;
  61. }
  62. TileGroup& TileGroup::operator=(TileGroup&& other) = default;
  63. std::string TileGroup::DebugString() {
  64. std::stringstream out;
  65. out << "Group detail: \n";
  66. out << "id: " << this->id << " | locale: " << this->locale
  67. << " | last_updated_ts: " << this->last_updated_ts << " \n";
  68. for (const auto& tile : this->tiles)
  69. out << tile->DebugString();
  70. return out.str();
  71. }
  72. void TileGroup::RemoveTiles(const std::vector<std::string>& tile_ids) {
  73. std::set<std::string> id_set(tile_ids.begin(), tile_ids.end());
  74. std::queue<Tile*> tile_queue;
  75. // Check if there are top level tiles to be removed.
  76. for (auto iter = tiles.begin(); iter != tiles.end();) {
  77. if (RemoveIdFromSet(&id_set, (*iter)->id)) {
  78. iter = tiles.erase(iter);
  79. if (id_set.empty())
  80. return;
  81. } else {
  82. tile_queue.push(iter->get());
  83. ++iter;
  84. }
  85. }
  86. // Recursively check if there are sub tiles to be removed.
  87. while (!tile_queue.empty()) {
  88. Tile* tile = tile_queue.front();
  89. tile_queue.pop();
  90. for (auto it = tile->sub_tiles.begin(); it != tile->sub_tiles.end();) {
  91. if (RemoveIdFromSet(&id_set, (*it)->id)) {
  92. it = tile->sub_tiles.erase(it);
  93. if (id_set.empty())
  94. return;
  95. } else {
  96. tile_queue.push(it->get());
  97. ++it;
  98. }
  99. }
  100. }
  101. }
  102. } // namespace query_tiles