browser_tabs_model.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "ash/components/phonehub/browser_tabs_model.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. namespace ash {
  7. namespace phonehub {
  8. const size_t BrowserTabsModel::kMaxMostRecentTabs = 2;
  9. BrowserTabsModel::BrowserTabMetadata::BrowserTabMetadata(
  10. GURL url,
  11. const std::u16string& title,
  12. base::Time last_accessed_timestamp,
  13. const gfx::Image& favicon)
  14. : url(url),
  15. title(title),
  16. last_accessed_timestamp(last_accessed_timestamp),
  17. favicon(favicon) {}
  18. BrowserTabsModel::BrowserTabMetadata::BrowserTabMetadata(
  19. const BrowserTabMetadata& other) = default;
  20. BrowserTabsModel::BrowserTabMetadata&
  21. BrowserTabsModel::BrowserTabMetadata::operator=(
  22. const BrowserTabMetadata& other) = default;
  23. bool BrowserTabsModel::BrowserTabMetadata::operator==(
  24. const BrowserTabMetadata& other) const {
  25. // The favicon is not compared because equality of gfx::Image is defined
  26. // by the same storage space rather than the image itself.
  27. return url == other.url && title == other.title &&
  28. last_accessed_timestamp == other.last_accessed_timestamp;
  29. }
  30. bool BrowserTabsModel::BrowserTabMetadata::operator!=(
  31. const BrowserTabMetadata& other) const {
  32. return !(*this == other);
  33. }
  34. bool BrowserTabsModel::BrowserTabMetadata::operator<(
  35. const BrowserTabMetadata& other) const {
  36. // More recently visited tabs should come before earlier visited tabs.
  37. return std::tie(last_accessed_timestamp) >
  38. std::tie(other.last_accessed_timestamp);
  39. }
  40. BrowserTabsModel::BrowserTabsModel(
  41. bool is_tab_sync_enabled,
  42. const std::vector<BrowserTabMetadata>& most_recent_tabs)
  43. : is_tab_sync_enabled_(is_tab_sync_enabled),
  44. most_recent_tabs_(most_recent_tabs) {
  45. if (!is_tab_sync_enabled_ && !most_recent_tabs_.empty()) {
  46. PA_LOG(WARNING) << "Tab sync is not enabled, but tab metadata was "
  47. << "provided; clearing metadata.";
  48. most_recent_tabs_.clear();
  49. return;
  50. }
  51. std::sort(most_recent_tabs_.begin(), most_recent_tabs_.end());
  52. if (most_recent_tabs_.size() > kMaxMostRecentTabs) {
  53. PA_LOG(WARNING) << "More than the max number of browser tab metadatas were "
  54. "provided; truncating least recently visited browser "
  55. "tabs' metadatas.";
  56. most_recent_tabs_.erase(most_recent_tabs_.begin() + kMaxMostRecentTabs,
  57. most_recent_tabs_.end());
  58. return;
  59. }
  60. }
  61. BrowserTabsModel::BrowserTabsModel(const BrowserTabsModel& other) = default;
  62. BrowserTabsModel::~BrowserTabsModel() = default;
  63. bool BrowserTabsModel::operator==(const BrowserTabsModel& other) const {
  64. return is_tab_sync_enabled_ == other.is_tab_sync_enabled_ &&
  65. most_recent_tabs_ == other.most_recent_tabs_;
  66. }
  67. bool BrowserTabsModel::operator!=(const BrowserTabsModel& other) const {
  68. return !(*this == other);
  69. }
  70. std::ostream& operator<<(
  71. std::ostream& stream,
  72. BrowserTabsModel::BrowserTabMetadata browser_tab_metadata) {
  73. stream << "{URL: " << browser_tab_metadata.url.spec() << ", "
  74. << "Title: " << browser_tab_metadata.title << ", "
  75. << "Timestamp: " << browser_tab_metadata.last_accessed_timestamp
  76. << "}";
  77. return stream;
  78. }
  79. } // namespace phonehub
  80. } // namespace ash