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

Side by Side Diff: components/sync_sessions/synced_session_tracker.cc

Issue 2575773003: Revert "Reland of [Sync] Put session tracker in charge of maintaining local state." (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
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client) 47 SyncedSessionTracker::SyncedSessionTracker(SyncSessionsClient* sessions_client)
48 : sessions_client_(sessions_client) {} 48 : sessions_client_(sessions_client) {}
49 49
50 SyncedSessionTracker::~SyncedSessionTracker() { 50 SyncedSessionTracker::~SyncedSessionTracker() {
51 Clear(); 51 Clear();
52 } 52 }
53 53
54 void SyncedSessionTracker::SetLocalSessionTag( 54 void SyncedSessionTracker::SetLocalSessionTag(
55 const std::string& local_session_tag) { 55 const std::string& local_session_tag) {
56 DCHECK(local_session_tag_.empty());
57 DCHECK(!local_session_tag.empty());
58 local_session_tag_ = local_session_tag; 56 local_session_tag_ = local_session_tag;
59 } 57 }
60 58
61 bool SyncedSessionTracker::LookupAllForeignSessions( 59 bool SyncedSessionTracker::LookupAllForeignSessions(
62 std::vector<const SyncedSession*>* sessions, 60 std::vector<const SyncedSession*>* sessions,
63 SessionLookup lookup) const { 61 SessionLookup lookup) const {
64 DCHECK(sessions); 62 DCHECK(sessions);
65 sessions->clear(); 63 sessions->clear();
66 for (const auto& session_pair : synced_session_map_) { 64 for (const auto& session_pair : synced_session_map_) {
67 SyncedSession* foreign_session = session_pair.second.get(); 65 SyncedSession* foreign_session = session_pair.second.get();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 auto tab_iter = tab_map_iter->second.find(tab_id); 100 auto tab_iter = tab_map_iter->second.find(tab_id);
103 if (tab_iter == tab_map_iter->second.end()) { 101 if (tab_iter == tab_map_iter->second.end()) {
104 // We have no record of this tab. 102 // We have no record of this tab.
105 *tab = nullptr; 103 *tab = nullptr;
106 return false; 104 return false;
107 } 105 }
108 *tab = tab_iter->second; 106 *tab = tab_iter->second;
109 return true; 107 return true;
110 } 108 }
111 109
112 void SyncedSessionTracker::LookupForeignTabNodeIds( 110 void SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag,
113 const std::string& session_tag, 111 std::set<int>* tab_node_ids) {
114 std::set<int>* tab_node_ids) const {
115 tab_node_ids->clear(); 112 tab_node_ids->clear();
116 auto session_iter = synced_session_map_.find(session_tag); 113 auto session_iter = synced_session_map_.find(session_tag);
117 if (session_iter != synced_session_map_.end()) { 114 if (session_iter != synced_session_map_.end()) {
118 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(), 115 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(),
119 session_iter->second->tab_node_ids.end()); 116 session_iter->second->tab_node_ids.end());
120 } 117 }
121 // In case an invalid node id was included, remove it. 118 // In case an invalid node id was included, remove it.
122 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID); 119 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID);
123 } 120 }
124 121
(...skipping 15 matching lines...) Expand all
140 std::unique_ptr<SyncedSession> synced_session = 137 std::unique_ptr<SyncedSession> synced_session =
141 base::MakeUnique<SyncedSession>(); 138 base::MakeUnique<SyncedSession>();
142 DVLOG(1) << "Creating new session with tag " << session_tag << " at " 139 DVLOG(1) << "Creating new session with tag " << session_tag << " at "
143 << synced_session.get(); 140 << synced_session.get();
144 synced_session->session_tag = session_tag; 141 synced_session->session_tag = session_tag;
145 synced_session_map_[session_tag] = std::move(synced_session); 142 synced_session_map_[session_tag] = std::move(synced_session);
146 143
147 return synced_session_map_[session_tag].get(); 144 return synced_session_map_[session_tag].get();
148 } 145 }
149 146
150 bool SyncedSessionTracker::DeleteForeignSession( 147 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) {
151 const std::string& session_tag) {
152 DCHECK_NE(local_session_tag_, session_tag);
153 unmapped_windows_.erase(session_tag); 148 unmapped_windows_.erase(session_tag);
154 unmapped_tabs_.erase(session_tag); 149 unmapped_tabs_.erase(session_tag);
155 150
156 bool header_existed = false; 151 bool header_existed = false;
157 auto iter = synced_session_map_.find(session_tag); 152 auto iter = synced_session_map_.find(session_tag);
158 if (iter != synced_session_map_.end()) { 153 if (iter != synced_session_map_.end()) {
159 // An implicitly created session that has children tabs but no header node 154 // An implicitly created session that has children tabs but no header node
160 // will have never had the device_type changed from unset. 155 // will have never had the device_type changed from unset.
161 header_existed = iter->second->device_type != SyncedSession::TYPE_UNSET; 156 header_existed = iter->second->device_type != SyncedSession::TYPE_UNSET;
162 // SyncedSession's destructor will trigger deletion of windows which will in 157 // SyncedSession's destructor will trigger deletion of windows which will in
(...skipping 23 matching lines...) Expand all
186 181
187 // Then unmap the window itself. 182 // Then unmap the window itself.
188 unmapped_windows_[session_tag][window_pair.first] = 183 unmapped_windows_[session_tag][window_pair.first] =
189 std::move(window_pair.second); 184 std::move(window_pair.second);
190 } 185 }
191 session->windows.clear(); 186 session->windows.clear();
192 } 187 }
193 188
194 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag, 189 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag,
195 int tab_node_id) { 190 int tab_node_id) {
196 DCHECK_NE(local_session_tag_, session_tag);
197 auto session_iter = synced_session_map_.find(session_tag); 191 auto session_iter = synced_session_map_.find(session_tag);
198 if (session_iter != synced_session_map_.end()) { 192 if (session_iter != synced_session_map_.end()) {
199 session_iter->second->tab_node_ids.erase(tab_node_id); 193 session_iter->second->tab_node_ids.erase(tab_node_id);
200 } 194 }
201 } 195 }
202 196
203 void SyncedSessionTracker::CleanupSessionImpl(const std::string& session_tag) { 197 void SyncedSessionTracker::CleanupSession(const std::string& session_tag) {
204 for (const auto& window_pair : unmapped_windows_[session_tag]) 198 for (const auto& window_pair : unmapped_windows_[session_tag])
205 synced_window_map_[session_tag].erase(window_pair.first); 199 synced_window_map_[session_tag].erase(window_pair.first);
206 unmapped_windows_[session_tag].clear(); 200 unmapped_windows_[session_tag].clear();
207 201
208 for (const auto& tab_pair : unmapped_tabs_[session_tag]) 202 for (const auto& tab_pair : unmapped_tabs_[session_tag])
209 synced_tab_map_[session_tag].erase(tab_pair.first); 203 synced_tab_map_[session_tag].erase(tab_pair.first);
210 unmapped_tabs_[session_tag].clear(); 204 unmapped_tabs_[session_tag].clear();
211 } 205 }
212 206
213 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, 207 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag,
(...skipping 21 matching lines...) Expand all
235 DCHECK(GetSession(session_tag)->windows.end() == 229 DCHECK(GetSession(session_tag)->windows.end() ==
236 GetSession(session_tag)->windows.find(window_id)); 230 GetSession(session_tag)->windows.find(window_id));
237 GetSession(session_tag)->windows[window_id] = std::move(window); 231 GetSession(session_tag)->windows[window_id] = std::move(window);
238 } 232 }
239 233
240 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag, 234 void SyncedSessionTracker::PutTabInWindow(const std::string& session_tag,
241 SessionID::id_type window_id, 235 SessionID::id_type window_id,
242 SessionID::id_type tab_id, 236 SessionID::id_type tab_id,
243 size_t tab_index) { 237 size_t tab_index) {
244 // We're called here for two reasons. 1) We've received an update to the 238 // We're called here for two reasons. 1) We've received an update to the
245 // SessionWindow information of a SessionHeader node for a session, 239 // SessionWindow information of a SessionHeader node for a foreign session,
246 // and 2) The SessionHeader node for our local session changed. In both cases 240 // and 2) The SessionHeader node for our local session changed. In both cases
247 // we need to update our tracking state to reflect the change. 241 // we need to update our tracking state to reflect the change.
248 // 242 //
249 // Because the SessionHeader nodes are separate from the individual tab nodes 243 // Because the SessionHeader nodes are separate from the individual tab nodes
250 // and we don't store tab_node_ids in the header / SessionWindow specifics, 244 // and we don't store tab_node_ids in the header / SessionWindow specifics,
251 // the tab_node_ids are not always available when processing headers. 245 // the tab_node_ids are not always available when processing headers.
252 // We know that we will eventually process (via GetTab) every single tab node 246 // We know that we will eventually process (via GetTab) every single tab node
253 // in the system, so we permit ourselves to use kInvalidTabNodeID here and 247 // in the system, so we permit ourselves to use kInvalidTabNodeID here and
254 // rely on the later update to build the mapping (or a restart). 248 // rely on the later update to build the mapping (or a restart).
255 GetTab(session_tag, tab_id); 249 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID);
256 250
257 // The tab should be unmapped. 251 // The tab should be unmapped.
258 std::unique_ptr<sessions::SessionTab> tab; 252 std::unique_ptr<sessions::SessionTab> tab;
259 auto it = unmapped_tabs_[session_tag].find(tab_id); 253 auto it = unmapped_tabs_[session_tag].find(tab_id);
260 if (it != unmapped_tabs_[session_tag].end()) { 254 if (it != unmapped_tabs_[session_tag].end()) {
261 tab = std::move(it->second); 255 tab = std::move(it->second);
262 unmapped_tabs_[session_tag].erase(it); 256 unmapped_tabs_[session_tag].erase(it);
263 } 257 }
264 if (!tab) { 258 DCHECK(tab);
265 LOG(ERROR) << "crbug.com/665196 Attempting to map tab " << tab_id
266 << " multiple times!";
267 return;
268 }
269 259
270 tab->window_id.set_id(window_id); 260 tab->window_id.set_id(window_id);
271 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; 261 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id;
272 DCHECK(GetSession(session_tag)->windows.find(window_id) != 262 DCHECK(GetSession(session_tag)->windows.find(window_id) !=
273 GetSession(session_tag)->windows.end()); 263 GetSession(session_tag)->windows.end());
274 auto& window_tabs = GetSession(session_tag)->windows[window_id]->tabs; 264 auto& window_tabs = GetSession(session_tag)->windows[window_id]->tabs;
275 if (window_tabs.size() <= tab_index) { 265 if (window_tabs.size() <= tab_index) {
276 window_tabs.resize(tab_index + 1); 266 window_tabs.resize(tab_index + 1);
277 } 267 }
278 DCHECK(!window_tabs[tab_index]); 268 DCHECK(!window_tabs[tab_index]);
279 window_tabs[tab_index] = std::move(tab); 269 window_tabs[tab_index] = std::move(tab);
280 } 270 }
281 271
282 void SyncedSessionTracker::OnTabNodeSeen(const std::string& session_tag,
283 int tab_node_id) {
284 GetSession(session_tag)->tab_node_ids.insert(tab_node_id);
285 }
286
287 sessions::SessionTab* SyncedSessionTracker::GetTab( 272 sessions::SessionTab* SyncedSessionTracker::GetTab(
288 const std::string& session_tag, 273 const std::string& session_tag,
289 SessionID::id_type tab_id) { 274 SessionID::id_type tab_id,
275 int tab_node_id) {
276 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id);
277 return GetTabImpl(session_tag, tab_id, tab_node_id);
278 }
279
280 sessions::SessionTab* SyncedSessionTracker::GetTabImpl(
281 const std::string& session_tag,
282 SessionID::id_type tab_id,
283 int tab_node_id) {
290 sessions::SessionTab* tab_ptr = nullptr; 284 sessions::SessionTab* tab_ptr = nullptr;
291 auto iter = synced_tab_map_[session_tag].find(tab_id); 285 auto iter = synced_tab_map_[session_tag].find(tab_id);
292 if (iter != synced_tab_map_[session_tag].end()) { 286 if (iter != synced_tab_map_[session_tag].end()) {
293 tab_ptr = iter->second; 287 tab_ptr = iter->second;
288 if (tab_node_id != TabNodePool::kInvalidTabNodeID &&
289 tab_id != TabNodePool::kInvalidTabID) {
290 // TabIDs are not stable across restarts of a client. Consider this
291 // example with two tabs:
292 //
293 // http://a.com TabID1 --> NodeIDA
294 // http://b.com TabID2 --> NodeIDB
295 //
296 // After restart, tab ids are reallocated. e.g, one possibility:
297 // http://a.com TabID2 --> NodeIDA
298 // http://b.com TabID1 --> NodeIDB
299 //
300 // If that happend on a remote client, here we will see an update to
301 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2
302 // with tab_node_id changing from NodeIDB to NodeIDA.
303 //
304 // We can also wind up here if we created this tab as an out-of-order
305 // update to the header node for this session before actually associating
306 // the tab itself, so the tab node id wasn't available at the time and
307 // is currently kInvalidTabNodeID.
308 //
309 // In both cases, we can safely throw it into the set of node ids.
310 GetSession(session_tag)->tab_node_ids.insert(tab_node_id);
311 }
294 312
295 if (VLOG_IS_ON(1)) { 313 if (VLOG_IS_ON(1)) {
296 std::string title; 314 std::string title;
297 if (tab_ptr->navigations.size() > 0) { 315 if (tab_ptr->navigations.size() > 0) {
298 title = 316 title =
299 " (" + base::UTF16ToUTF8(tab_ptr->navigations.back().title()) + ")"; 317 " (" + base::UTF16ToUTF8(tab_ptr->navigations.back().title()) + ")";
300 } 318 }
301 DVLOG(1) << "Getting " 319 DVLOG(1) << "Getting "
302 << (session_tag == local_session_tag_ ? "local session" 320 << (session_tag == local_session_tag_ ? "local session"
303 : session_tag) 321 : session_tag)
304 << "'s seen tab " << tab_id << " at " << tab_ptr << " " << title; 322 << "'s seen tab " << tab_id << " at " << tab_ptr << " " << title;
305 } 323 }
306 } else { 324 } else {
307 std::unique_ptr<sessions::SessionTab> tab = 325 std::unique_ptr<sessions::SessionTab> tab =
308 base::MakeUnique<sessions::SessionTab>(); 326 base::MakeUnique<sessions::SessionTab>();
309 tab_ptr = tab.get(); 327 tab_ptr = tab.get();
310 tab->tab_id.set_id(tab_id); 328 tab->tab_id.set_id(tab_id);
311 synced_tab_map_[session_tag][tab_id] = tab_ptr; 329 synced_tab_map_[session_tag][tab_id] = tab_ptr;
312 unmapped_tabs_[session_tag][tab_id] = std::move(tab); 330 unmapped_tabs_[session_tag][tab_id] = std::move(tab);
331 GetSession(session_tag)->tab_node_ids.insert(tab_node_id);
313 DVLOG(1) << "Getting " 332 DVLOG(1) << "Getting "
314 << (session_tag == local_session_tag_ ? "local session" 333 << (session_tag == local_session_tag_ ? "local session"
315 : session_tag) 334 : session_tag)
316 << "'s new tab " << tab_id << " at " << tab_ptr; 335 << "'s new tab " << tab_id << " at " << tab_ptr;
317 } 336 }
318 DCHECK(tab_ptr); 337 DCHECK(tab_ptr);
319 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); 338 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id);
320 return tab_ptr; 339 return tab_ptr;
321 } 340 }
322 341
323 void SyncedSessionTracker::CleanupForeignSession(
324 const std::string& session_tag) {
325 DCHECK_NE(local_session_tag_, session_tag);
326 CleanupSessionImpl(session_tag);
327 }
328
329 void SyncedSessionTracker::CleanupLocalTabs(std::set<int>* deleted_node_ids) {
330 DCHECK(!local_session_tag_.empty());
331 for (const auto& tab_pair : unmapped_tabs_[local_session_tag_])
332 local_tab_pool_.FreeTab(tab_pair.first);
333 CleanupSessionImpl(local_session_tag_);
334 local_tab_pool_.CleanupTabNodes(deleted_node_ids);
335 for (int tab_node_id : *deleted_node_ids) {
336 GetSession(local_session_tag_)->tab_node_ids.erase(tab_node_id);
337 }
338 }
339
340 bool SyncedSessionTracker::GetTabNodeForLocalTab(int tab_id, int* tab_node_id) {
341 DCHECK(!local_session_tag_.empty());
342 // Ensure a placeholder SessionTab is in place, if not already.
343 // Although we don't need a SessionTab to fulfill this request, this forces
344 // the
345 // creation of one if it doesn't already exist. This helps to make sure we're
346 // tracking this |tab_id| if |local_tab_pool_| is, and everyone's data
347 // structures
348 // are kept in sync and as consistent as possible.
349 GetTab(local_session_tag_, tab_id); // Ignore result.
350
351 bool reused_existing_tab =
352 local_tab_pool_.GetTabNodeForTab(tab_id, tab_node_id);
353 DCHECK_NE(TabNodePool::kInvalidTabNodeID, *tab_node_id);
354 GetSession(local_session_tag_)->tab_node_ids.insert(*tab_node_id);
355 return reused_existing_tab;
356 }
357
358 void SyncedSessionTracker::ReassociateLocalTab(int tab_node_id,
359 SessionID::id_type new_tab_id) {
360 DCHECK(!local_session_tag_.empty());
361 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id);
362 DCHECK_NE(TabNodePool::kInvalidTabID, new_tab_id);
363
364 SessionID::id_type old_tab_id =
365 local_tab_pool_.GetTabIdFromTabNodeId(tab_node_id);
366 local_tab_pool_.ReassociateTabNode(tab_node_id, new_tab_id);
367
368 sessions::SessionTab* tab_ptr = nullptr;
369
370 auto old_tab_iter = synced_tab_map_[local_session_tag_].find(old_tab_id);
371 if (old_tab_iter != synced_tab_map_[local_session_tag_].end()) {
372 tab_ptr = old_tab_iter->second;
373 // Remove the tab from the synced tab map under the old id.
374 synced_tab_map_[local_session_tag_].erase(old_tab_iter);
375 } else {
376 // It's possible a placeholder is already in place for the new tab. If so,
377 // reuse it, otherwise create a new one (which will default to unmapped).
378 tab_ptr = GetTab(local_session_tag_, new_tab_id);
379 }
380
381 // If the old tab is unmapped, update the tab id under which it is indexed.
382 auto unmapped_tabs_iter = unmapped_tabs_[local_session_tag_].find(old_tab_id);
383 if (old_tab_id != TabNodePool::kInvalidTabID &&
384 unmapped_tabs_iter != unmapped_tabs_[local_session_tag_].end()) {
385 std::unique_ptr<sessions::SessionTab> tab =
386 std::move(unmapped_tabs_iter->second);
387 DCHECK_EQ(tab_ptr, tab.get());
388 unmapped_tabs_[local_session_tag_].erase(unmapped_tabs_iter);
389 unmapped_tabs_[local_session_tag_][new_tab_id] = std::move(tab);
390 }
391
392 // Update the tab id.
393 if (old_tab_id != TabNodePool::kInvalidTabID) {
394 DVLOG(1) << "Remapped tab " << old_tab_id << " with node " << tab_node_id
395 << " to tab " << new_tab_id;
396 } else {
397 DVLOG(1) << "Mapped new tab node " << tab_node_id << " to tab "
398 << new_tab_id;
399 }
400 tab_ptr->tab_id.set_id(new_tab_id);
401
402 // Add the tab back into the tab map with the new id.
403 synced_tab_map_[local_session_tag_][new_tab_id] = tab_ptr;
404 GetSession(local_session_tag_)->tab_node_ids.insert(tab_node_id);
405 }
406
407 void SyncedSessionTracker::Clear() { 342 void SyncedSessionTracker::Clear() {
408 // Cleanup unmapped tabs and windows. 343 // Cleanup unmapped tabs and windows.
409 unmapped_windows_.clear(); 344 unmapped_windows_.clear();
410 unmapped_tabs_.clear(); 345 unmapped_tabs_.clear();
411 346
412 // Delete SyncedSession objects (which also deletes all their windows/tabs). 347 // Delete SyncedSession objects (which also deletes all their windows/tabs).
413 synced_session_map_.clear(); 348 synced_session_map_.clear();
414 349
415 // Get rid of our convenience maps (does not delete the actual Window/Tabs 350 // Get rid of our convenience maps (does not delete the actual Window/Tabs
416 // themselves; they should have all been deleted above). 351 // themselves; they should have all been deleted above).
417 synced_window_map_.clear(); 352 synced_window_map_.clear();
418 synced_tab_map_.clear(); 353 synced_tab_map_.clear();
419 354
420 local_tab_pool_.Clear();
421 local_session_tag_.clear(); 355 local_session_tag_.clear();
422 } 356 }
423 357
424 } // namespace sync_sessions 358 } // namespace sync_sessions
OLDNEW
« no previous file with comments | « components/sync_sessions/synced_session_tracker.h ('k') | components/sync_sessions/synced_session_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698