Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(747)

Side by Side Diff: ios/chrome/browser/ui/ntp/recent_tabs/synced_sessions.mm

Issue 2589803002: Upstream Chrome on iOS source code [6/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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
5 #include "ios/chrome/browser/ui/ntp/recent_tabs/synced_sessions.h"
6
7 #include <functional>
8 #include <memory>
9
10 #include "base/logging.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/sync/driver/sync_service.h"
16 #include "components/sync_sessions/open_tabs_ui_delegate.h"
17 #include "ios/chrome/browser/sync/sync_setup_service.h"
18
19 namespace {
20
21 // Comparator function for sorting the sessions_ vector so that the most
22 // recently used sessions are at the beginning.
23 bool SortSessionsByTime(
24 std::unique_ptr<const synced_sessions::DistantSession>& a,
25 std::unique_ptr<const synced_sessions::DistantSession>& b) {
26 return a->modified_time > b->modified_time;
27 }
28
29 // Helper to extract the relevant content from a SessionTab and add it to a
30 // DistantSession.
31 void AddTabToDistantSession(const sessions::SessionTab& session_tab,
32 const std::string& session_tag,
33 synced_sessions::DistantSession* distant_session) {
34 if (session_tab.navigations.size() > 0) {
35 distant_session->tabs.push_back(
36 base::MakeUnique<synced_sessions::DistantTab>());
37 synced_sessions::DistantTab& distant_tab = *distant_session->tabs.back();
38 distant_tab.session_tag = session_tag;
39 distant_tab.tab_id = session_tab.tab_id.id();
40 int index = session_tab.current_navigation_index;
41 if (index < 0)
42 index = 0;
43 if (index > (int)session_tab.navigations.size() - 1)
44 index = session_tab.navigations.size() - 1;
45 const sessions::SerializedNavigationEntry* navigation =
46 &session_tab.navigations[index];
47 distant_tab.title = navigation->title();
48 distant_tab.virtual_url = navigation->virtual_url();
49 if (distant_tab.title.empty()) {
50 std::string url = navigation->virtual_url().spec();
51 distant_tab.title = base::UTF8ToUTF16(url);
52 }
53 }
54 }
55
56 } // namespace
57
58 namespace synced_sessions {
59
60 DistantTab::DistantTab() {}
61
62 size_t DistantTab::hashOfUserVisibleProperties() {
63 std::stringstream ss;
64 ss << title << std::endl << virtual_url.spec();
65 return std::hash<std::string>()(ss.str());
66 }
67
68 DistantSession::DistantSession() {}
69
70 DistantSession::DistantSession(syncer::SyncService* sync_service,
71 const std::string& tag) {
72 if (sync_service->CanSyncStart() && sync_service->GetOpenTabsUIDelegate()) {
73 sync_sessions::OpenTabsUIDelegate* open_tabs =
74 sync_service->GetOpenTabsUIDelegate();
75
76 std::vector<const sync_sessions::SyncedSession*> sessions;
77 open_tabs->GetAllForeignSessions(&sessions);
78 for (const auto& session : sessions) {
79 if (tag == session->session_tag) {
80 this->InitWithSyncedSession(session, open_tabs);
81 }
82 }
83 }
84 }
85
86 DistantSession::~DistantSession() {}
87
88 void DistantSession::InitWithSyncedSession(
89 const sync_sessions::SyncedSession* synced_session,
90 sync_sessions::OpenTabsUIDelegate* open_tabs) {
91 tag = synced_session->session_tag;
92 name = synced_session->session_name;
93 modified_time = synced_session->modified_time;
94 device_type = synced_session->device_type;
95
96 const std::string group_name =
97 base::FieldTrialList::FindFullName("TabSyncByRecency");
98 if (group_name == "Enabled") {
99 // Order tabs by recency.
100 std::vector<const sessions::SessionTab*> session_tabs;
101 open_tabs->GetForeignSessionTabs(synced_session->session_tag,
102 &session_tabs);
103 for (const auto& session_tab : session_tabs) {
104 AddTabToDistantSession(*session_tab, synced_session->session_tag, this);
105 }
106 } else {
107 // Order tabs by their visual position within window.
108 for (const auto& kv : synced_session->windows) {
109 for (const auto& session_tab : kv.second->tabs) {
110 AddTabToDistantSession(*session_tab, synced_session->session_tag, this);
111 }
112 }
113 }
114 }
115
116 SyncedSessions::SyncedSessions() {}
117
118 SyncedSessions::SyncedSessions(syncer::SyncService* sync_service) {
119 DCHECK(sync_service);
120 // Reload Sync open tab sesions.
121 if (sync_service->CanSyncStart() && sync_service->GetOpenTabsUIDelegate()) {
122 sync_sessions::OpenTabsUIDelegate* open_tabs =
123 sync_service->GetOpenTabsUIDelegate();
124
125 // Iterating through all remote sessions, then all remote windows, then all
126 // remote tabs to retrieve the tabs to display to the user. This will
127 // flatten all of those data into a sequential vector of tabs.
128
129 std::vector<const sync_sessions::SyncedSession*> sessions;
130 open_tabs->GetAllForeignSessions(&sessions);
131 for (const auto& session : sessions) {
132 std::unique_ptr<DistantSession> distant_session(new DistantSession());
133 distant_session->InitWithSyncedSession(session, open_tabs);
134 // Don't display sessions with no tabs.
135 if (distant_session->tabs.size() > 0)
136 sessions_.push_back(std::move(distant_session));
137 }
138 }
139 std::sort(sessions_.begin(), sessions_.end(), SortSessionsByTime);
140 }
141
142 SyncedSessions::~SyncedSessions() {}
143
144 // Returns the session at index |index|.
145 DistantSession const* SyncedSessions::GetSession(size_t index) const {
146 DCHECK_LE(index, GetSessionCount());
147 return sessions_[index].get();
148 }
149
150 const DistantSession* SyncedSessions::GetSessionWithTag(
151 const std::string& tag) const {
152 for (auto const& distant_session : sessions_) {
153 if (distant_session->tag == tag) {
154 return distant_session.get();
155 }
156 }
157 return nullptr;
158 }
159
160 // Returns the number of distant sessions.
161 size_t SyncedSessions::GetSessionCount() const {
162 return sessions_.size();
163 }
164
165 // Deletes the session at index |index|.
166 void SyncedSessions::EraseSession(size_t index) {
167 DCHECK_LE(index, GetSessionCount());
168 sessions_.erase(sessions_.begin() + index);
169 }
170
171 void SyncedSessions::AddDistantSessionForTest(
172 std::unique_ptr<const DistantSession> distant_session) {
173 sessions_.push_back(std::move(distant_session));
174 }
175
176 } // synced_sessions namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698