tile.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/tile.h"
  5. #include <algorithm>
  6. #include <deque>
  7. #include <map>
  8. #include <sstream>
  9. #include <utility>
  10. namespace query_tiles {
  11. namespace {
  12. void DeepCopyTiles(const Tile& input, Tile* out) {
  13. DCHECK(out);
  14. out->id = input.id;
  15. out->display_text = input.display_text;
  16. out->query_text = input.query_text;
  17. out->accessibility_text = input.accessibility_text;
  18. out->image_metadatas = input.image_metadatas;
  19. out->search_params = input.search_params;
  20. out->sub_tiles.clear();
  21. for (const auto& child : input.sub_tiles) {
  22. auto entry = std::make_unique<Tile>();
  23. DeepCopyTiles(*child.get(), entry.get());
  24. out->sub_tiles.emplace_back(std::move(entry));
  25. }
  26. }
  27. void SerializeEntry(const Tile* entry, std::stringstream& out) {
  28. if (!entry)
  29. return;
  30. out << "entry id: " << entry->id << " query text: " << entry->query_text
  31. << " display text: " << entry->display_text
  32. << " accessibility_text: " << entry->accessibility_text << " \n";
  33. for (const auto& image : entry->image_metadatas)
  34. out << "image url: " << image.url.possibly_invalid_spec() << " \n";
  35. }
  36. std::string DebugStringInternal(const Tile* root) {
  37. if (!root)
  38. return std::string();
  39. std::stringstream out;
  40. out << "Entries detail: \n";
  41. std::map<std::string, std::vector<std::string>> cache;
  42. std::deque<const Tile*> queue;
  43. queue.emplace_back(root);
  44. while (!queue.empty()) {
  45. size_t size = queue.size();
  46. for (size_t i = 0; i < size; i++) {
  47. auto* parent = queue.front();
  48. SerializeEntry(parent, out);
  49. queue.pop_front();
  50. for (size_t j = 0; j < parent->sub_tiles.size(); j++) {
  51. cache[parent->id].emplace_back(parent->sub_tiles[j]->id);
  52. queue.emplace_back(parent->sub_tiles[j].get());
  53. }
  54. }
  55. }
  56. out << "Tree table: \n";
  57. for (auto& pair : cache) {
  58. std::string line;
  59. line += pair.first + " : [";
  60. std::sort(pair.second.begin(), pair.second.end());
  61. for (const auto& child : pair.second)
  62. line += " " + child;
  63. line += " ]\n";
  64. out << line;
  65. }
  66. return out.str();
  67. }
  68. } // namespace
  69. ImageMetadata::ImageMetadata() = default;
  70. ImageMetadata::ImageMetadata(const GURL& url) : url(url) {}
  71. ImageMetadata::~ImageMetadata() = default;
  72. ImageMetadata::ImageMetadata(const ImageMetadata& other) = default;
  73. bool ImageMetadata::operator==(const ImageMetadata& other) const {
  74. return url == other.url;
  75. }
  76. TileStats::TileStats() = default;
  77. TileStats::TileStats(base::Time last_clicked_time, double score)
  78. : last_clicked_time(last_clicked_time), score(score) {}
  79. TileStats::~TileStats() = default;
  80. TileStats::TileStats(const TileStats& other) = default;
  81. bool TileStats::operator==(const TileStats& other) const {
  82. return last_clicked_time == other.last_clicked_time && score == other.score;
  83. }
  84. bool Tile::operator==(const Tile& other) const {
  85. return id == other.id && display_text == other.display_text &&
  86. query_text == other.query_text &&
  87. accessibility_text == other.accessibility_text &&
  88. image_metadatas.size() == other.image_metadatas.size() &&
  89. sub_tiles.size() == other.sub_tiles.size() &&
  90. search_params == other.search_params;
  91. }
  92. bool Tile::operator!=(const Tile& other) const {
  93. return !(*this == other);
  94. }
  95. Tile::Tile(const Tile& other) {
  96. DeepCopyTiles(other, this);
  97. }
  98. Tile::Tile() = default;
  99. Tile::Tile(Tile&& other) noexcept = default;
  100. Tile::~Tile() = default;
  101. Tile& Tile::operator=(const Tile& other) {
  102. DeepCopyTiles(other, this);
  103. return *this;
  104. }
  105. Tile& Tile::operator=(Tile&& other) noexcept = default;
  106. std::string Tile::DebugString() {
  107. return DebugStringInternal(this);
  108. }
  109. } // namespace query_tiles