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

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

Issue 1877083002: [Sync] Moved tab_node_id tracking to session object and improved foreign session garbage collection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months 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 "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/sync_sessions/sync_sessions_client.h" 10 #include "components/sync_sessions/sync_sessions_client.h"
(...skipping 10 matching lines...) Expand all
21 for (const sessions::SerializedNavigationEntry& navigation : 21 for (const sessions::SerializedNavigationEntry& navigation :
22 tab->navigations) { 22 tab->navigations) {
23 if (sessions_client->ShouldSyncURL(navigation.virtual_url())) { 23 if (sessions_client->ShouldSyncURL(navigation.virtual_url())) {
24 return true; 24 return true;
25 } 25 }
26 } 26 }
27 } 27 }
28 return false; 28 return false;
29 } 29 }
30 30
31 // To be presentable it means you must have windows with tabs with syncable
Nicolas Zea 2016/04/12 20:38:03 "To be presentable it means you must have windows
skym 2016/04/12 22:12:14 Done.
32 // content.
33 bool IsPresentable(sync_sessions::SyncSessionsClient* sessions_client,
34 sync_driver::SyncedSession* foreign_session) {
35 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator iter =
36 foreign_session->windows.begin();
37 iter != foreign_session->windows.end(); ++iter) {
38 if (ShouldSyncSessionWindow(sessions_client, *(iter->second))) {
39 return true;
40 }
41 }
42 return false;
43 }
44
31 } // namespace 45 } // namespace
32 46
47 SyncedSessionTracker::SessionTabWrapper::SessionTabWrapper()
48 : tab_ptr(NULL), owned(false) {}
49
50 SyncedSessionTracker::SessionTabWrapper::SessionTabWrapper(
51 sessions::SessionTab* tab_ptr,
52 OwnedState owned,
53 int tab_node_id)
54 : tab_ptr(tab_ptr), owned(owned == IS_OWNED) {
55 tab_node_ids.insert(tab_node_id);
56 }
57
58 SyncedSessionTracker::SessionTabWrapper::~SessionTabWrapper() {}
59
33 SyncedSessionTracker::SyncedSessionTracker( 60 SyncedSessionTracker::SyncedSessionTracker(
34 sync_sessions::SyncSessionsClient* sessions_client) 61 sync_sessions::SyncSessionsClient* sessions_client)
35 : sessions_client_(sessions_client) {} 62 : sessions_client_(sessions_client) {}
36 63
37 SyncedSessionTracker::~SyncedSessionTracker() { 64 SyncedSessionTracker::~SyncedSessionTracker() {
38 Clear(); 65 Clear();
39 } 66 }
40 67
41 void SyncedSessionTracker::SetLocalSessionTag( 68 void SyncedSessionTracker::SetLocalSessionTag(
42 const std::string& local_session_tag) { 69 const std::string& local_session_tag) {
43 local_session_tag_ = local_session_tag; 70 local_session_tag_ = local_session_tag;
44 } 71 }
45 72
46 bool SyncedSessionTracker::LookupAllForeignSessions( 73 bool SyncedSessionTracker::LookupAllForeignSessions(
47 std::vector<const sync_driver::SyncedSession*>* sessions) const { 74 std::vector<const sync_driver::SyncedSession*>* sessions,
75 bool presentable) const {
48 DCHECK(sessions); 76 DCHECK(sessions);
49 sessions->clear(); 77 sessions->clear();
50 // Fill vector of sessions from our synced session map.
51 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin(); 78 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin();
52 i != synced_session_map_.end(); ++i) { 79 i != synced_session_map_.end(); ++i) {
53 // Only include foreign sessions with open tabs.
54 sync_driver::SyncedSession* foreign_session = i->second; 80 sync_driver::SyncedSession* foreign_session = i->second;
55 if (i->first != local_session_tag_ && !foreign_session->windows.empty()) { 81 if (i->first != local_session_tag_ &&
56 bool found_tabs = false; 82 (!presentable || IsPresentable(sessions_client_, foreign_session))) {
57 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator iter = 83 sessions->push_back(foreign_session);
58 foreign_session->windows.begin();
59 iter != foreign_session->windows.end(); ++iter) {
60 if (ShouldSyncSessionWindow(sessions_client_, *(iter->second))) {
61 found_tabs = true;
62 break;
63 }
64 }
65 if (found_tabs)
66 sessions->push_back(foreign_session);
67 } 84 }
68 } 85 }
69
70 return !sessions->empty(); 86 return !sessions->empty();
71 } 87 }
72 88
73 bool SyncedSessionTracker::LookupSessionWindows( 89 bool SyncedSessionTracker::LookupSessionWindows(
74 const std::string& session_tag, 90 const std::string& session_tag,
75 std::vector<const sessions::SessionWindow*>* windows) const { 91 std::vector<const sessions::SessionWindow*>* windows) const {
76 DCHECK(windows); 92 DCHECK(windows);
77 windows->clear(); 93 windows->clear();
78 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag); 94 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag);
79 if (iter == synced_session_map_.end()) 95 if (iter == synced_session_map_.end())
80 return false; 96 return false;
81 windows->clear(); 97 windows->clear();
82 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator window_iter = 98 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator window_iter =
83 iter->second->windows.begin(); 99 iter->second->windows.begin();
84 window_iter != iter->second->windows.end(); ++window_iter) { 100 window_iter != iter->second->windows.end(); ++window_iter) {
85 windows->push_back(window_iter->second); 101 windows->push_back(window_iter->second);
86 } 102 }
87 return true; 103 return true;
88 } 104 }
89 105
90 bool SyncedSessionTracker::LookupSessionTab( 106 bool SyncedSessionTracker::LookupSessionTab(
91 const std::string& tag, 107 const std::string& tag,
92 SessionID::id_type tab_id, 108 SessionID::id_type tab_id,
93 const sessions::SessionTab** tab) const { 109 const sessions::SessionTab** tab) const {
94 DCHECK(tab); 110 DCHECK(tab);
95 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(tag); 111 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(tag);
96 if (tab_map_iter == synced_tab_map_.end()) { 112 if (tab_map_iter == synced_tab_map_.end()) {
97 // We have no record of this session. 113 // We have no record of this session.
98 *tab = NULL; 114 *tab = nullptr;
99 return false; 115 return false;
100 } 116 }
101 IDToSessionTabMap::const_iterator tab_iter = 117 IDToSessionTabMap::const_iterator tab_iter =
102 tab_map_iter->second.find(tab_id); 118 tab_map_iter->second.find(tab_id);
103 if (tab_iter == tab_map_iter->second.end()) { 119 if (tab_iter == tab_map_iter->second.end()) {
104 // We have no record of this tab. 120 // We have no record of this tab.
105 *tab = NULL; 121 *tab = nullptr;
106 return false; 122 return false;
107 } 123 }
108 *tab = tab_iter->second.tab_ptr; 124 *tab = tab_iter->second.tab_ptr;
109 return true; 125 return true;
110 } 126 }
111 127
112 bool SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag, 128 bool SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag,
113 std::set<int>* tab_node_ids) { 129 std::set<int>* tab_node_ids) {
114 tab_node_ids->clear(); 130 tab_node_ids->clear();
115 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(session_tag); 131 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(session_tag);
116 if (tab_map_iter == synced_tab_map_.end()) 132 if (tab_map_iter == synced_tab_map_.end()) {
Nicolas Zea 2016/04/12 20:38:03 nit: single line if body
skym 2016/04/12 22:12:14 Method has changed significantly.
117 return false; 133 return false;
134 }
118 135
119 IDToSessionTabMap::const_iterator tab_iter = tab_map_iter->second.begin(); 136 IDToSessionTabMap::const_iterator tab_iter = tab_map_iter->second.begin();
120 while (tab_iter != tab_map_iter->second.end()) { 137 while (tab_iter != tab_map_iter->second.end()) {
121 if (tab_iter->second.tab_node_id != TabNodePool::kInvalidTabNodeID) 138 tab_node_ids->insert(tab_iter->second.tab_node_ids.begin(),
122 tab_node_ids->insert(tab_iter->second.tab_node_id); 139 tab_iter->second.tab_node_ids.end());
123 ++tab_iter; 140 ++tab_iter;
124 } 141 }
142
143 // Incase an invalid node id was included, remove it.
144 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID);
145
125 return true; 146 return true;
126 } 147 }
127 148
128 bool SyncedSessionTracker::LookupLocalSession( 149 bool SyncedSessionTracker::LookupLocalSession(
129 const sync_driver::SyncedSession** output) const { 150 const sync_driver::SyncedSession** output) const {
130 SyncedSessionMap::const_iterator it = 151 SyncedSessionMap::const_iterator it =
131 synced_session_map_.find(local_session_tag_); 152 synced_session_map_.find(local_session_tag_);
132 if (it != synced_session_map_.end()) { 153 if (it != synced_session_map_.end()) {
133 *output = it->second; 154 *output = it->second;
134 return true; 155 return true;
(...skipping 11 matching lines...) Expand all
146 DVLOG(1) << "Creating new session with tag " << session_tag << " at " 167 DVLOG(1) << "Creating new session with tag " << session_tag << " at "
147 << synced_session; 168 << synced_session;
148 synced_session->session_tag = session_tag; 169 synced_session->session_tag = session_tag;
149 synced_session_map_[session_tag] = synced_session; 170 synced_session_map_[session_tag] = synced_session;
150 } 171 }
151 DCHECK(synced_session); 172 DCHECK(synced_session);
152 return synced_session; 173 return synced_session;
153 } 174 }
154 175
155 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) { 176 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) {
156 bool found_session = false; 177 // Cleanup first, we need to check every wrapper and remove model objects
178 // if the parent session isn't going to delete in it's destructor.
179 CleanupSession(session_tag);
180
181 bool header_existed = false;
157 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag); 182 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag);
158 if (iter != synced_session_map_.end()) { 183 if (iter != synced_session_map_.end()) {
159 sync_driver::SyncedSession* session = iter->second; 184 sync_driver::SyncedSession* session = iter->second;
185 // An implicitly created session that has children tabs but no header node
186 // will have never had the device_type changed from unset.
187 header_existed =
188 session->device_type != sync_driver::SyncedSession::TYPE_UNSET;
160 synced_session_map_.erase(iter); 189 synced_session_map_.erase(iter);
161 // SyncedSession's destructor will trigger deletion of windows which will in 190 // SyncedSession's destructor will trigger deletion of windows which will in
162 // turn trigger the deletion of tabs. This doens't affect wrappers. 191 // turn trigger the deletion of tabs. This doesn't affect wrappers.
163 delete session; 192 delete session;
164 found_session = true;
165 } 193 }
194
166 // These two erase(...) calls only affect the wrappers. 195 // These two erase(...) calls only affect the wrappers.
167 synced_window_map_.erase(session_tag); 196 synced_window_map_.erase(session_tag);
168 // It's possible there was no header node but there were tab nodes. 197 synced_tab_map_.erase(session_tag);
169 if (synced_tab_map_.erase(session_tag) > 0) { 198
170 found_session = true; 199 return header_existed;
171 }
172 return found_session;
173 } 200 }
174 201
175 void SyncedSessionTracker::ResetSessionTracking( 202 void SyncedSessionTracker::ResetSessionTracking(
176 const std::string& session_tag) { 203 const std::string& session_tag) {
177 // Reset window tracking. 204 // Reset window tracking.
178 GetSession(session_tag)->windows.clear(); 205 GetSession(session_tag)->windows.clear();
179 SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag); 206 SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag);
180 if (window_iter != synced_window_map_.end()) { 207 if (window_iter != synced_window_map_.end()) {
181 for (IDToSessionWindowMap::iterator window_map_iter = 208 for (IDToSessionWindowMap::iterator window_map_iter =
182 window_iter->second.begin(); 209 window_iter->second.begin();
183 window_map_iter != window_iter->second.end(); ++window_map_iter) { 210 window_map_iter != window_iter->second.end(); ++window_map_iter) {
184 window_map_iter->second.owned = false; 211 window_map_iter->second.owned = false;
185 // We clear out the tabs to prevent double referencing of the same tab. 212 // We clear out the tabs to prevent double referencing of the same tab.
186 // All tabs that are in use will be added back as needed. 213 // All tabs that are in use will be added back as needed.
187 window_map_iter->second.window_ptr->tabs.clear(); 214 window_map_iter->second.window_ptr->tabs.clear();
188 } 215 }
189 } 216 }
190 217
191 // Reset tab tracking. 218 // Reset tab tracking.
192 SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag); 219 SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag);
193 if (tab_iter != synced_tab_map_.end()) { 220 if (tab_iter != synced_tab_map_.end()) {
194 for (IDToSessionTabMap::iterator tab_map_iter = tab_iter->second.begin(); 221 for (IDToSessionTabMap::iterator tab_map_iter = tab_iter->second.begin();
195 tab_map_iter != tab_iter->second.end(); ++tab_map_iter) { 222 tab_map_iter != tab_iter->second.end(); ++tab_map_iter) {
196 tab_map_iter->second.owned = false; 223 tab_map_iter->second.owned = false;
197 } 224 }
198 } 225 }
199 } 226 }
200 227
228 void SyncedSessionTracker::TryDeleteTab(const std::string& session_tag,
229 SessionID::id_type tab_id,
230 int tab_node_id) {
231 SyncedTabMap::iterator tab_map_iter = synced_tab_map_.find(session_tag);
232 if (tab_map_iter != synced_tab_map_.end()) {
233 IDToSessionTabMap::iterator tab_iter = tab_map_iter->second.find(tab_id);
234 if (tab_iter != tab_map_iter->second.end()) {
235 // Scrub any occurrence of |tab_node_id| first to make verifying if we
236 // should actually delete easier.
237 tab_iter->second.tab_node_ids.erase(tab_node_id);
238
239 // Try to delete the tab if it no longer holds valid tab node ids and it's
240 // an orphan.
241 if (tab_iter->second.tab_node_ids.empty() &&
242 DeleteOldSessionTabIfNecessary(tab_iter->second)) {
243 tab_map_iter->second.erase(tab_iter);
244 }
245 }
246 }
247 }
248
201 bool SyncedSessionTracker::DeleteOldSessionWindowIfNecessary( 249 bool SyncedSessionTracker::DeleteOldSessionWindowIfNecessary(
202 SessionWindowWrapper window_wrapper) { 250 const SessionWindowWrapper& window_wrapper) {
203 if (!window_wrapper.owned) { 251 if (!window_wrapper.owned) {
204 DVLOG(1) << "Deleting closed window " 252 DVLOG(1) << "Deleting closed window "
205 << window_wrapper.window_ptr->window_id.id(); 253 << window_wrapper.window_ptr->window_id.id();
206 // Clear the tabs first, since we don't want the destructor to destroy 254 // Clear the tabs first, since we don't want the destructor to destroy
207 // them. Their deletion will be handled by DeleteOldSessionTabIfNecessary. 255 // them. Their deletion will be handled by DeleteOldSessionTabIfNecessary.
208 window_wrapper.window_ptr->tabs.clear(); 256 window_wrapper.window_ptr->tabs.clear();
209 delete window_wrapper.window_ptr; 257 delete window_wrapper.window_ptr;
210 return true; 258 return true;
211 } 259 }
212 return false; 260 return false;
213 } 261 }
214 262
215 bool SyncedSessionTracker::DeleteOldSessionTabIfNecessary( 263 bool SyncedSessionTracker::DeleteOldSessionTabIfNecessary(
216 SessionTabWrapper tab_wrapper) { 264 const SessionTabWrapper& tab_wrapper) {
217 if (!tab_wrapper.owned) { 265 if (!tab_wrapper.owned) {
218 if (VLOG_IS_ON(1)) { 266 if (VLOG_IS_ON(1)) {
219 sessions::SessionTab* tab_ptr = tab_wrapper.tab_ptr; 267 sessions::SessionTab* tab_ptr = tab_wrapper.tab_ptr;
220 std::string title; 268 std::string title;
221 if (tab_ptr->navigations.size() > 0) { 269 if (tab_ptr->navigations.size() > 0) {
222 title = 270 title =
223 " (" + 271 " (" +
224 base::UTF16ToUTF8( 272 base::UTF16ToUTF8(
225 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) + 273 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) +
226 ")"; 274 ")";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 tab_iter->second.erase(iter++); 306 tab_iter->second.erase(iter++);
259 } else { 307 } else {
260 ++iter; 308 ++iter;
261 } 309 }
262 } 310 }
263 } 311 }
264 } 312 }
265 313
266 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, 314 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag,
267 SessionID::id_type window_id) { 315 SessionID::id_type window_id) {
268 sessions::SessionWindow* window_ptr = NULL; 316 sessions::SessionWindow* window_ptr = nullptr;
269 IDToSessionWindowMap::iterator iter = 317 IDToSessionWindowMap::iterator iter =
270 synced_window_map_[session_tag].find(window_id); 318 synced_window_map_[session_tag].find(window_id);
271 if (iter != synced_window_map_[session_tag].end()) { 319 if (iter != synced_window_map_[session_tag].end()) {
272 iter->second.owned = true; 320 iter->second.owned = true;
273 window_ptr = iter->second.window_ptr; 321 window_ptr = iter->second.window_ptr;
274 DVLOG(1) << "Putting seen window " << window_id << " at " << window_ptr 322 DVLOG(1) << "Putting seen window " << window_id << " at " << window_ptr
275 << "in " << (session_tag == local_session_tag_ ? "local session" 323 << "in " << (session_tag == local_session_tag_ ? "local session"
276 : session_tag); 324 : session_tag);
277 } else { 325 } else {
278 // Create the window. 326 // Create the window.
(...skipping 20 matching lines...) Expand all
299 // SessionWindow information of a SessionHeader node for a foreign session, 347 // SessionWindow information of a SessionHeader node for a foreign session,
300 // and 2) The SessionHeader node for our local session changed. In both cases 348 // and 2) The SessionHeader node for our local session changed. In both cases
301 // we need to update our tracking state to reflect the change. 349 // we need to update our tracking state to reflect the change.
302 // 350 //
303 // Because the SessionHeader nodes are separate from the individual tab nodes 351 // Because the SessionHeader nodes are separate from the individual tab nodes
304 // and we don't store tab_node_ids in the header / SessionWindow specifics, 352 // and we don't store tab_node_ids in the header / SessionWindow specifics,
305 // the tab_node_ids are not always available when processing headers. 353 // the tab_node_ids are not always available when processing headers.
306 // We know that we will eventually process (via GetTab) every single tab node 354 // We know that we will eventually process (via GetTab) every single tab node
307 // in the system, so we permit ourselves to use kInvalidTabNodeID here and 355 // in the system, so we permit ourselves to use kInvalidTabNodeID here and
308 // rely on the later update to build the mapping (or a restart). 356 // rely on the later update to build the mapping (or a restart).
309 // TODO(tim): Bug 98892. Update comment when Sync API conversion finishes to
310 // mention that in the meantime, the only ill effect is that we may not be
311 // able to fully clean up a stale foreign session, but it will get garbage
312 // collected eventually.
313 sessions::SessionTab* tab_ptr = 357 sessions::SessionTab* tab_ptr =
314 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID); 358 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID);
315 359
316 // It's up to the caller to ensure this never happens. Tabs should not 360 // It's up to the caller to ensure this never happens. Tabs should not
317 // belong to more than one window or appear twice within the same window. 361 // belong to more than one window or appear twice within the same window.
318 // 362 //
319 // If this condition were violated, we would double-free during shutdown. 363 // If this condition were violated, we would double-free during shutdown.
320 // That could cause all sorts of hard to diagnose crashes, possibly in code 364 // That could cause all sorts of hard to diagnose crashes, possibly in code
321 // far away from here. We crash early to avoid this. 365 // far away from here. We crash early to avoid this.
322 // 366 //
323 // See http://crbug.com/360822. 367 // See http://crbug.com/360822.
324 CHECK(!synced_tab_map_[session_tag][tab_id].owned); 368 CHECK(!synced_tab_map_[session_tag][tab_id].owned);
325 369
326 // Only tabs that were just created in GetTabImpl(...) are still present in 370 // Only tabs that were just created in GetTabImpl(...) are still present in
327 // unmapped_tabs_. Most of the time when PutTabInWindow(...) is invoked, the 371 // unmapped_tabs_. Most of the time when PutTabInWindow(...) is invoked, the
328 // specified tab was already a child of the specified window, and hasn't been 372 // specified tab was already a child of the specified window, and hasn't been
329 // in unmapped_tabs_ for quite some time. However, this is not a problem, as 373 // in unmapped_tabs_ for quite some time. However, this is not a problem, as
330 // std::set::erase(...) will simply have no effect in these cases. 374 // std::set::erase(...) will simply have no effect in these cases.
331 unmapped_tabs_.erase(tab_ptr); 375 unmapped_tabs_.erase(tab_ptr);
332 synced_tab_map_[session_tag][tab_id].owned = true; 376 synced_tab_map_[session_tag][tab_id].owned = true;
333 377
334 tab_ptr->window_id.set_id(window_id); 378 tab_ptr->window_id.set_id(window_id);
335 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; 379 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id;
336 DCHECK(GetSession(session_tag)->windows.find(window_id) != 380 DCHECK(GetSession(session_tag)->windows.find(window_id) !=
337 GetSession(session_tag)->windows.end()); 381 GetSession(session_tag)->windows.end());
338 std::vector<sessions::SessionTab*>& window_tabs = 382 std::vector<sessions::SessionTab*>& window_tabs =
339 GetSession(session_tag)->windows[window_id]->tabs; 383 GetSession(session_tag)->windows[window_id]->tabs;
340 if (window_tabs.size() <= tab_index) { 384 if (window_tabs.size() <= tab_index) {
341 window_tabs.resize(tab_index + 1, NULL); 385 window_tabs.resize(tab_index + 1, nullptr);
342 } 386 }
343 DCHECK(!window_tabs[tab_index]); 387 DCHECK(!window_tabs[tab_index]);
344 window_tabs[tab_index] = tab_ptr; 388 window_tabs[tab_index] = tab_ptr;
345 } 389 }
346 390
347 sessions::SessionTab* SyncedSessionTracker::GetTab( 391 sessions::SessionTab* SyncedSessionTracker::GetTab(
348 const std::string& session_tag, 392 const std::string& session_tag,
349 SessionID::id_type tab_id, 393 SessionID::id_type tab_id,
350 int tab_node_id) { 394 int tab_node_id) {
351 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); 395 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id);
352 return GetTabImpl(session_tag, tab_id, tab_node_id); 396 return GetTabImpl(session_tag, tab_id, tab_node_id);
353 } 397 }
354 398
355 sessions::SessionTab* SyncedSessionTracker::GetTabImpl( 399 sessions::SessionTab* SyncedSessionTracker::GetTabImpl(
356 const std::string& session_tag, 400 const std::string& session_tag,
357 SessionID::id_type tab_id, 401 SessionID::id_type tab_id,
358 int tab_node_id) { 402 int tab_node_id) {
359 sessions::SessionTab* tab_ptr = NULL; 403 sessions::SessionTab* tab_ptr = nullptr;
360 IDToSessionTabMap::iterator iter = synced_tab_map_[session_tag].find(tab_id); 404 IDToSessionTabMap::iterator iter = synced_tab_map_[session_tag].find(tab_id);
361 if (iter != synced_tab_map_[session_tag].end()) { 405 if (iter != synced_tab_map_[session_tag].end()) {
362 tab_ptr = iter->second.tab_ptr; 406 tab_ptr = iter->second.tab_ptr;
363 if (tab_node_id != TabNodePool::kInvalidTabNodeID && 407 if (tab_node_id != TabNodePool::kInvalidTabNodeID &&
364 tab_id != TabNodePool::kInvalidTabID) { 408 tab_id != TabNodePool::kInvalidTabID) {
365 // TabIDs are not stable across restarts of a client. Consider this 409 // TabIDs are not stable across restarts of a client. Consider this
366 // example with two tabs: 410 // example with two tabs:
367 // 411 //
368 // http://a.com TabID1 --> NodeIDA 412 // http://a.com TabID1 --> NodeIDA
369 // http://b.com TabID2 --> NodeIDB 413 // http://b.com TabID2 --> NodeIDB
370 // 414 //
371 // After restart, tab ids are reallocated. e.g, one possibility: 415 // After restart, tab ids are reallocated. e.g, one possibility:
372 // http://a.com TabID2 --> NodeIDA 416 // http://a.com TabID2 --> NodeIDA
373 // http://b.com TabID1 --> NodeIDB 417 // http://b.com TabID1 --> NodeIDB
374 // 418 //
375 // If that happend on a remote client, here we will see an update to 419 // If that happend on a remote client, here we will see an update to
376 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2 420 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2
377 // with tab_node_id changing from NodeIDB to NodeIDA. 421 // with tab_node_id changing from NodeIDB to NodeIDA.
378 // 422 //
379 // We can also wind up here if we created this tab as an out-of-order 423 // We can also wind up here if we created this tab as an out-of-order
380 // update to the header node for this session before actually associating 424 // update to the header node for this session before actually associating
381 // the tab itself, so the tab node id wasn't available at the time and 425 // the tab itself, so the tab node id wasn't available at the time and
382 // is currenlty kInvalidTabNodeID. 426 // is currenlty kInvalidTabNodeID.
383 // 427 //
384 // In both cases, we update the tab_node_id. 428 // In both cases, we can safely throw it into the set of node ids.
385 iter->second.tab_node_id = tab_node_id; 429 iter->second.tab_node_ids.insert(tab_node_id);
386 } 430 }
387 431
388 if (VLOG_IS_ON(1)) { 432 if (VLOG_IS_ON(1)) {
389 std::string title; 433 std::string title;
390 if (tab_ptr->navigations.size() > 0) { 434 if (tab_ptr->navigations.size() > 0) {
391 title = 435 title =
392 " (" + 436 " (" +
393 base::UTF16ToUTF8( 437 base::UTF16ToUTF8(
394 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) + 438 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) +
395 ")"; 439 ")";
(...skipping 13 matching lines...) Expand all
409 << (session_tag == local_session_tag_ ? "local session" 453 << (session_tag == local_session_tag_ ? "local session"
410 : session_tag) 454 : session_tag)
411 << "'s new tab " << tab_id << " at " << tab_ptr; 455 << "'s new tab " << tab_id << " at " << tab_ptr;
412 } 456 }
413 DCHECK(tab_ptr); 457 DCHECK(tab_ptr);
414 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); 458 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id);
415 return tab_ptr; 459 return tab_ptr;
416 } 460 }
417 461
418 void SyncedSessionTracker::Clear() { 462 void SyncedSessionTracker::Clear() {
463 // Cleanup unowned things first, before we let SyncedSession's destructor take
464 // care of everything else.
465 for (const auto& kv : synced_session_map_) {
466 CleanupSession(kv.first);
467 }
468
419 // Delete SyncedSession objects (which also deletes all their windows/tabs). 469 // Delete SyncedSession objects (which also deletes all their windows/tabs).
420 STLDeleteValues(&synced_session_map_); 470 STLDeleteValues(&synced_session_map_);
421 471
422 // Go through and delete any tabs we had allocated but had not yet placed into 472 // Go through and delete any tabs we had allocated but had not yet placed into
423 // a SyncedSession object. 473 // a SyncedSession object.
424 STLDeleteElements(&unmapped_tabs_); 474 STLDeleteElements(&unmapped_tabs_);
425 475
426 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs 476 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs
427 // themselves; they should have all been deleted above). 477 // themselves; they should have all been deleted above).
428 synced_window_map_.clear(); 478 synced_window_map_.clear();
429 synced_tab_map_.clear(); 479 synced_tab_map_.clear();
430 local_session_tag_.clear(); 480 local_session_tag_.clear();
431 } 481 }
432 482
433 } // namespace browser_sync 483 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698