| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sync_sessions/synced_session_tracker.h" | 5 #include "components/sync_sessions/synced_session_tracker.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 return false; | 30 return false; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Presentable means |foreign_session| must have syncable content. | 33 // Presentable means |foreign_session| must have syncable content. |
| 34 bool IsPresentable(SyncSessionsClient* sessions_client, | 34 bool IsPresentable(SyncSessionsClient* sessions_client, |
| 35 SyncedSession* foreign_session) { | 35 SyncedSession* foreign_session) { |
| 36 for (auto iter = foreign_session->windows.begin(); | 36 for (auto iter = foreign_session->windows.begin(); |
| 37 iter != foreign_session->windows.end(); ++iter) { | 37 iter != foreign_session->windows.end(); ++iter) { |
| 38 if (ShouldSyncSessionWindow(sessions_client, *(iter->second))) { | 38 if (ShouldSyncSessionWindow(sessions_client, |
| 39 iter->second->wrapped_window)) { |
| 39 return true; | 40 return true; |
| 40 } | 41 } |
| 41 } | 42 } |
| 42 return false; | 43 return false; |
| 43 } | 44 } |
| 44 | 45 |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 47 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) | 48 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) |
| 48 : sessions_client_(sessions_client) {} | 49 : sessions_client_(sessions_client) {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 76 bool SyncedSessionTracker::LookupSessionWindows( | 77 bool SyncedSessionTracker::LookupSessionWindows( |
| 77 const std::string& session_tag, | 78 const std::string& session_tag, |
| 78 std::vector<const sessions::SessionWindow*>* windows) const { | 79 std::vector<const sessions::SessionWindow*>* windows) const { |
| 79 DCHECK(windows); | 80 DCHECK(windows); |
| 80 windows->clear(); | 81 windows->clear(); |
| 81 | 82 |
| 82 auto iter = synced_session_map_.find(session_tag); | 83 auto iter = synced_session_map_.find(session_tag); |
| 83 if (iter == synced_session_map_.end()) | 84 if (iter == synced_session_map_.end()) |
| 84 return false; | 85 return false; |
| 85 for (const auto& window_pair : iter->second->windows) | 86 for (const auto& window_pair : iter->second->windows) |
| 86 windows->push_back(window_pair.second.get()); | 87 windows->push_back(&window_pair.second->wrapped_window); |
| 87 | 88 |
| 88 return true; | 89 return true; |
| 89 } | 90 } |
| 90 | 91 |
| 91 bool SyncedSessionTracker::LookupSessionTab( | 92 bool SyncedSessionTracker::LookupSessionTab( |
| 92 const std::string& tag, | 93 const std::string& tag, |
| 93 SessionID::id_type tab_id, | 94 SessionID::id_type tab_id, |
| 94 const sessions::SessionTab** tab) const { | 95 const sessions::SessionTab** tab) const { |
| 95 if (tab_id == kInvalidTabID) | 96 if (tab_id == kInvalidTabID) |
| 96 return false; | 97 return false; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 175 |
| 175 return header_existed; | 176 return header_existed; |
| 176 } | 177 } |
| 177 | 178 |
| 178 void SyncedSessionTracker::ResetSessionTracking( | 179 void SyncedSessionTracker::ResetSessionTracking( |
| 179 const std::string& session_tag) { | 180 const std::string& session_tag) { |
| 180 SyncedSession* session = GetSession(session_tag); | 181 SyncedSession* session = GetSession(session_tag); |
| 181 | 182 |
| 182 for (auto& window_pair : session->windows) { | 183 for (auto& window_pair : session->windows) { |
| 183 // First unmap the tabs in the window. | 184 // First unmap the tabs in the window. |
| 184 for (auto& tab : window_pair.second->tabs) { | 185 for (auto& tab : window_pair.second->wrapped_window.tabs) { |
| 185 SessionID::id_type tab_id = tab->tab_id.id(); | 186 SessionID::id_type tab_id = tab->tab_id.id(); |
| 186 unmapped_tabs_[session_tag][tab_id] = std::move(tab); | 187 unmapped_tabs_[session_tag][tab_id] = std::move(tab); |
| 187 } | 188 } |
| 188 window_pair.second->tabs.clear(); | 189 window_pair.second->wrapped_window.tabs.clear(); |
| 189 | 190 |
| 190 // Then unmap the window itself. | 191 // Then unmap the window itself. |
| 191 unmapped_windows_[session_tag][window_pair.first] = | 192 unmapped_windows_[session_tag][window_pair.first] = |
| 192 std::move(window_pair.second); | 193 std::move(window_pair.second); |
| 193 } | 194 } |
| 194 session->windows.clear(); | 195 session->windows.clear(); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag, | 198 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag, |
| 198 int tab_node_id) { | 199 int tab_node_id) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 213 unmapped_tabs_[session_tag].clear(); | 214 unmapped_tabs_[session_tag].clear(); |
| 214 } | 215 } |
| 215 | 216 |
| 216 bool SyncedSessionTracker::IsTabUnmappedForTesting(SessionID::id_type tab_id) { | 217 bool SyncedSessionTracker::IsTabUnmappedForTesting(SessionID::id_type tab_id) { |
| 217 auto it = unmapped_tabs_[local_session_tag_].find(tab_id); | 218 auto it = unmapped_tabs_[local_session_tag_].find(tab_id); |
| 218 return it != unmapped_tabs_[local_session_tag_].end(); | 219 return it != unmapped_tabs_[local_session_tag_].end(); |
| 219 } | 220 } |
| 220 | 221 |
| 221 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, | 222 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, |
| 222 SessionID::id_type window_id) { | 223 SessionID::id_type window_id) { |
| 223 std::unique_ptr<sessions::SessionWindow> window; | 224 std::unique_ptr<SyncedSessionWindow> window; |
| 224 | 225 |
| 225 auto iter = unmapped_windows_[session_tag].find(window_id); | 226 auto iter = unmapped_windows_[session_tag].find(window_id); |
| 226 if (iter != unmapped_windows_[session_tag].end()) { | 227 if (iter != unmapped_windows_[session_tag].end()) { |
| 227 DCHECK_EQ(synced_window_map_[session_tag][window_id], iter->second.get()); | 228 DCHECK_EQ(synced_window_map_[session_tag][window_id], iter->second.get()); |
| 228 window = std::move(iter->second); | 229 window = std::move(iter->second); |
| 229 unmapped_windows_[session_tag].erase(iter); | 230 unmapped_windows_[session_tag].erase(iter); |
| 230 DVLOG(1) << "Putting seen window " << window_id << " at " << window.get() | 231 DVLOG(1) << "Putting seen window " << window_id << " at " << window.get() |
| 231 << "in " << (session_tag == local_session_tag_ ? "local session" | 232 << "in " << (session_tag == local_session_tag_ ? "local session" |
| 232 : session_tag); | 233 : session_tag); |
| 233 } else { | 234 } else { |
| 234 // Create the window. | 235 // Create the window. |
| 235 window = base::MakeUnique<sessions::SessionWindow>(); | 236 window = base::MakeUnique<SyncedSessionWindow>(); |
| 236 window->window_id.set_id(window_id); | 237 window->wrapped_window.window_id.set_id(window_id); |
| 237 synced_window_map_[session_tag][window_id] = window.get(); | 238 synced_window_map_[session_tag][window_id] = window.get(); |
| 238 DVLOG(1) << "Putting new window " << window_id << " at " << window.get() | 239 DVLOG(1) << "Putting new window " << window_id << " at " << window.get() |
| 239 << "in " << (session_tag == local_session_tag_ ? "local session" | 240 << "in " << (session_tag == local_session_tag_ ? "local session" |
| 240 : session_tag); | 241 : session_tag); |
| 241 } | 242 } |
| 242 DCHECK_EQ(window->window_id.id(), window_id); | 243 DCHECK_EQ(window->wrapped_window.window_id.id(), window_id); |
| 243 DCHECK(GetSession(session_tag)->windows.end() == | 244 DCHECK(GetSession(session_tag)->windows.end() == |
| 244 GetSession(session_tag)->windows.find(window_id)); | 245 GetSession(session_tag)->windows.find(window_id)); |
| 245 GetSession(session_tag)->windows[window_id] = std::move(window); | 246 GetSession(session_tag)->windows[window_id] = std::move(window); |
| 246 } | 247 } |
| 247 | 248 |
| 248 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag, | 249 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag, |
| 249 SessionID::id_type window_id, | 250 SessionID::id_type window_id, |
| 250 SessionID::id_type tab_id) { | 251 SessionID::id_type tab_id) { |
| 251 // We're called here for two reasons. 1) We've received an update to the | 252 // We're called here for two reasons. 1) We've received an update to the |
| 252 // SessionWindow information of a SessionHeader node for a session, | 253 // SessionWindow information of a SessionHeader node for a session, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 266 auto it = unmapped_tabs_[session_tag].find(tab_id); | 267 auto it = unmapped_tabs_[session_tag].find(tab_id); |
| 267 if (it != unmapped_tabs_[session_tag].end()) { | 268 if (it != unmapped_tabs_[session_tag].end()) { |
| 268 tab = std::move(it->second); | 269 tab = std::move(it->second); |
| 269 unmapped_tabs_[session_tag].erase(it); | 270 unmapped_tabs_[session_tag].erase(it); |
| 270 } else { | 271 } else { |
| 271 // The tab has already been mapped, possibly because of the tab node id | 272 // The tab has already been mapped, possibly because of the tab node id |
| 272 // being reused across tabs. Find the existing tab and move it to the right | 273 // being reused across tabs. Find the existing tab and move it to the right |
| 273 // window. | 274 // window. |
| 274 for (auto& window_iter_pair : GetSession(session_tag)->windows) { | 275 for (auto& window_iter_pair : GetSession(session_tag)->windows) { |
| 275 auto tab_iter = std::find_if( | 276 auto tab_iter = std::find_if( |
| 276 window_iter_pair.second->tabs.begin(), | 277 window_iter_pair.second->wrapped_window.tabs.begin(), |
| 277 window_iter_pair.second->tabs.end(), | 278 window_iter_pair.second->wrapped_window.tabs.end(), |
| 278 [&tab_ptr](const std::unique_ptr<sessions::SessionTab>& tab) { | 279 [&tab_ptr](const std::unique_ptr<sessions::SessionTab>& tab) { |
| 279 return tab.get() == tab_ptr; | 280 return tab.get() == tab_ptr; |
| 280 }); | 281 }); |
| 281 if (tab_iter != window_iter_pair.second->tabs.end()) { | 282 if (tab_iter != window_iter_pair.second->wrapped_window.tabs.end()) { |
| 282 tab = std::move(*tab_iter); | 283 tab = std::move(*tab_iter); |
| 283 window_iter_pair.second->tabs.erase(tab_iter); | 284 window_iter_pair.second->wrapped_window.tabs.erase(tab_iter); |
| 284 | 285 |
| 285 DVLOG(1) << "Moving tab " << tab_id << " from window " | 286 DVLOG(1) << "Moving tab " << tab_id << " from window " |
| 286 << window_iter_pair.first << " to " << window_id; | 287 << window_iter_pair.first << " to " << window_id; |
| 287 break; | 288 break; |
| 288 } | 289 } |
| 289 } | 290 } |
| 290 // TODO(zea): remove this once PutTabInWindow isn't crashing anymore. | 291 // TODO(zea): remove this once PutTabInWindow isn't crashing anymore. |
| 291 CHECK(tab) << " Unable to find tab " << tab_id | 292 CHECK(tab) << " Unable to find tab " << tab_id |
| 292 << " within unmapped tabs or previously mapped windows." | 293 << " within unmapped tabs or previously mapped windows." |
| 293 << " crbug.com/639009"; | 294 << " crbug.com/639009"; |
| 294 } | 295 } |
| 295 | 296 |
| 296 tab->window_id.set_id(window_id); | 297 tab->window_id.set_id(window_id); |
| 297 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; | 298 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; |
| 298 DCHECK(GetSession(session_tag)->windows.find(window_id) != | 299 DCHECK(GetSession(session_tag)->windows.find(window_id) != |
| 299 GetSession(session_tag)->windows.end()); | 300 GetSession(session_tag)->windows.end()); |
| 300 auto& window_tabs = GetSession(session_tag)->windows[window_id]->tabs; | 301 GetSession(session_tag) |
| 301 window_tabs.push_back(std::move(tab)); | 302 ->windows[window_id] |
| 303 ->wrapped_window.tabs.push_back(std::move(tab)); |
| 302 } | 304 } |
| 303 | 305 |
| 304 void SyncedSessionTracker::OnTabNodeSeen(const std::string& session_tag, | 306 void SyncedSessionTracker::OnTabNodeSeen(const std::string& session_tag, |
| 305 int tab_node_id) { | 307 int tab_node_id) { |
| 306 GetSession(session_tag)->tab_node_ids.insert(tab_node_id); | 308 GetSession(session_tag)->tab_node_ids.insert(tab_node_id); |
| 307 } | 309 } |
| 308 | 310 |
| 309 sessions::SessionTab* SyncedSessionTracker::GetTab( | 311 sessions::SessionTab* SyncedSessionTracker::GetTab( |
| 310 const std::string& session_tag, | 312 const std::string& session_tag, |
| 311 SessionID::id_type tab_id) { | 313 SessionID::id_type tab_id) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 // Get rid of our convenience maps (does not delete the actual Window/Tabs | 448 // Get rid of our convenience maps (does not delete the actual Window/Tabs |
| 447 // themselves; they should have all been deleted above). | 449 // themselves; they should have all been deleted above). |
| 448 synced_window_map_.clear(); | 450 synced_window_map_.clear(); |
| 449 synced_tab_map_.clear(); | 451 synced_tab_map_.clear(); |
| 450 | 452 |
| 451 local_tab_pool_.Clear(); | 453 local_tab_pool_.Clear(); |
| 452 local_session_tag_.clear(); | 454 local_session_tag_.clear(); |
| 453 } | 455 } |
| 454 | 456 |
| 455 } // namespace sync_sessions | 457 } // namespace sync_sessions |
| OLD | NEW |