open_tabs_ui_delegate_impl.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 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/sync_sessions/open_tabs_ui_delegate_impl.h"
  5. #include <memory>
  6. #include "base/ranges/algorithm.h"
  7. #include "components/sync_sessions/sync_sessions_client.h"
  8. #include "components/sync_sessions/synced_session_tracker.h"
  9. namespace sync_sessions {
  10. OpenTabsUIDelegateImpl::OpenTabsUIDelegateImpl(
  11. const SyncSessionsClient* sessions_client,
  12. const SyncedSessionTracker* session_tracker,
  13. const DeleteForeignSessionCallback& delete_foreign_session_cb)
  14. : sessions_client_(sessions_client),
  15. session_tracker_(session_tracker),
  16. delete_foreign_session_cb_(delete_foreign_session_cb) {}
  17. OpenTabsUIDelegateImpl::~OpenTabsUIDelegateImpl() = default;
  18. bool OpenTabsUIDelegateImpl::GetAllForeignSessions(
  19. std::vector<const SyncedSession*>* sessions) {
  20. *sessions = session_tracker_->LookupAllForeignSessions(
  21. SyncedSessionTracker::PRESENTABLE);
  22. base::ranges::sort(
  23. *sessions, std::greater(),
  24. [](const SyncedSession* session) { return session->modified_time; });
  25. return !sessions->empty();
  26. }
  27. bool OpenTabsUIDelegateImpl::GetForeignSession(
  28. const std::string& tag,
  29. std::vector<const sessions::SessionWindow*>* windows) {
  30. return session_tracker_->LookupSessionWindows(tag, windows);
  31. }
  32. bool OpenTabsUIDelegateImpl::GetForeignTab(const std::string& tag,
  33. const SessionID tab_id,
  34. const sessions::SessionTab** tab) {
  35. *tab = session_tracker_->LookupSessionTab(tag, tab_id);
  36. return *tab != nullptr;
  37. }
  38. bool OpenTabsUIDelegateImpl::GetForeignSessionTabs(
  39. const std::string& tag,
  40. std::vector<const sessions::SessionTab*>* tabs) {
  41. std::vector<const sessions::SessionWindow*> windows;
  42. if (!session_tracker_->LookupSessionWindows(tag, &windows)) {
  43. return false;
  44. }
  45. // Prune those tabs that are not syncable or are NewTabPage, then sort them
  46. // from most recent to least recent, independent of which window the tabs were
  47. // from.
  48. for (const sessions::SessionWindow* window : windows) {
  49. for (const std::unique_ptr<sessions::SessionTab>& tab : window->tabs) {
  50. if (tab->navigations.empty()) {
  51. continue;
  52. }
  53. const sessions::SerializedNavigationEntry& current_navigation =
  54. tab->navigations.at(tab->normalized_navigation_index());
  55. if (!sessions_client_->ShouldSyncURL(current_navigation.virtual_url())) {
  56. continue;
  57. }
  58. tabs->push_back(tab.get());
  59. }
  60. }
  61. base::ranges::stable_sort(
  62. *tabs, std::greater(),
  63. [](const sessions::SessionTab* tab) { return tab->timestamp; });
  64. return true;
  65. }
  66. void OpenTabsUIDelegateImpl::DeleteForeignSession(const std::string& tag) {
  67. delete_foreign_session_cb_.Run(tag);
  68. }
  69. bool OpenTabsUIDelegateImpl::GetLocalSession(
  70. const SyncedSession** local_session) {
  71. *local_session = session_tracker_->LookupLocalSession();
  72. return *local_session != nullptr;
  73. }
  74. } // namespace sync_sessions