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

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: Reverting now inaccurate comments from previous versions. 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
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
33 SyncedSessionTracker::SyncedSessionTracker( 47 SyncedSessionTracker::SyncedSessionTracker(
34 sync_sessions::SyncSessionsClient* sessions_client) 48 sync_sessions::SyncSessionsClient* sessions_client)
35 : sessions_client_(sessions_client) {} 49 : sessions_client_(sessions_client) {}
36 50
37 SyncedSessionTracker::~SyncedSessionTracker() { 51 SyncedSessionTracker::~SyncedSessionTracker() {
38 Clear(); 52 Clear();
39 } 53 }
40 54
41 void SyncedSessionTracker::SetLocalSessionTag( 55 void SyncedSessionTracker::SetLocalSessionTag(
42 const std::string& local_session_tag) { 56 const std::string& local_session_tag) {
43 local_session_tag_ = local_session_tag; 57 local_session_tag_ = local_session_tag;
44 } 58 }
45 59
46 bool SyncedSessionTracker::LookupAllForeignSessions( 60 bool SyncedSessionTracker::LookupAllForeignSessions(
47 std::vector<const sync_driver::SyncedSession*>* sessions) const { 61 std::vector<const sync_driver::SyncedSession*>* sessions,
62 bool presentable) const {
48 DCHECK(sessions); 63 DCHECK(sessions);
49 sessions->clear(); 64 sessions->clear();
50 // Fill vector of sessions from our synced session map.
51 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin(); 65 for (SyncedSessionMap::const_iterator i = synced_session_map_.begin();
52 i != synced_session_map_.end(); ++i) { 66 i != synced_session_map_.end(); ++i) {
53 // Only include foreign sessions with open tabs.
54 sync_driver::SyncedSession* foreign_session = i->second; 67 sync_driver::SyncedSession* foreign_session = i->second;
55 if (i->first != local_session_tag_ && !foreign_session->windows.empty()) { 68 if (i->first != local_session_tag_ &&
56 bool found_tabs = false; 69 (!presentable || IsPresentable(sessions_client_, foreign_session))) {
57 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator iter = 70 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 } 71 }
68 } 72 }
69
70 return !sessions->empty(); 73 return !sessions->empty();
71 } 74 }
72 75
73 bool SyncedSessionTracker::LookupSessionWindows( 76 bool SyncedSessionTracker::LookupSessionWindows(
74 const std::string& session_tag, 77 const std::string& session_tag,
75 std::vector<const sessions::SessionWindow*>* windows) const { 78 std::vector<const sessions::SessionWindow*>* windows) const {
76 DCHECK(windows); 79 DCHECK(windows);
77 windows->clear(); 80 windows->clear();
78 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag); 81 SyncedSessionMap::const_iterator iter = synced_session_map_.find(session_tag);
79 if (iter == synced_session_map_.end()) 82 if (iter == synced_session_map_.end())
80 return false; 83 return false;
81 windows->clear(); 84 windows->clear();
82 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator window_iter = 85 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator window_iter =
83 iter->second->windows.begin(); 86 iter->second->windows.begin();
84 window_iter != iter->second->windows.end(); ++window_iter) { 87 window_iter != iter->second->windows.end(); ++window_iter) {
85 windows->push_back(window_iter->second); 88 windows->push_back(window_iter->second);
86 } 89 }
87 return true; 90 return true;
88 } 91 }
89 92
90 bool SyncedSessionTracker::LookupSessionTab( 93 bool SyncedSessionTracker::LookupSessionTab(
91 const std::string& tag, 94 const std::string& tag,
92 SessionID::id_type tab_id, 95 SessionID::id_type tab_id,
93 const sessions::SessionTab** tab) const { 96 const sessions::SessionTab** tab) const {
94 DCHECK(tab); 97 DCHECK(tab);
95 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(tag); 98 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(tag);
96 if (tab_map_iter == synced_tab_map_.end()) { 99 if (tab_map_iter == synced_tab_map_.end()) {
97 // We have no record of this session. 100 // We have no record of this session.
98 *tab = NULL; 101 *tab = nullptr;
99 return false; 102 return false;
100 } 103 }
101 IDToSessionTabMap::const_iterator tab_iter = 104 IDToSessionTabMap::const_iterator tab_iter =
102 tab_map_iter->second.find(tab_id); 105 tab_map_iter->second.find(tab_id);
103 if (tab_iter == tab_map_iter->second.end()) { 106 if (tab_iter == tab_map_iter->second.end()) {
104 // We have no record of this tab. 107 // We have no record of this tab.
105 *tab = NULL; 108 *tab = nullptr;
106 return false; 109 return false;
107 } 110 }
108 *tab = tab_iter->second.tab_ptr; 111 *tab = tab_iter->second.tab_ptr;
109 return true; 112 return true;
110 } 113 }
111 114
112 bool SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag, 115 void SyncedSessionTracker::LookupTabNodeIds(const std::string& session_tag,
113 std::set<int>* tab_node_ids) { 116 std::set<int>* tab_node_ids) {
114 tab_node_ids->clear(); 117 tab_node_ids->clear();
115 SyncedTabMap::const_iterator tab_map_iter = synced_tab_map_.find(session_tag); 118 SyncedSessionMap::const_iterator session_iter =
116 if (tab_map_iter == synced_tab_map_.end()) 119 synced_session_map_.find(session_tag);
117 return false; 120 if (session_iter != synced_session_map_.end()) {
118 121 tab_node_ids->insert(session_iter->second->tab_node_ids.begin(),
119 IDToSessionTabMap::const_iterator tab_iter = tab_map_iter->second.begin(); 122 session_iter->second->tab_node_ids.end());
120 while (tab_iter != tab_map_iter->second.end()) {
121 if (tab_iter->second.tab_node_id != TabNodePool::kInvalidTabNodeID)
122 tab_node_ids->insert(tab_iter->second.tab_node_id);
123 ++tab_iter;
124 } 123 }
125 return true; 124 // Incase an invalid node id was included, remove it.
125 tab_node_ids->erase(TabNodePool::kInvalidTabNodeID);
Nicolas Zea 2016/04/12 20:38:04 When would this happen?
skym 2016/04/12 22:12:14 So I'm not sure if this is needed, partly because
126 } 126 }
127 127
128 bool SyncedSessionTracker::LookupLocalSession( 128 bool SyncedSessionTracker::LookupLocalSession(
129 const sync_driver::SyncedSession** output) const { 129 const sync_driver::SyncedSession** output) const {
130 SyncedSessionMap::const_iterator it = 130 SyncedSessionMap::const_iterator it =
131 synced_session_map_.find(local_session_tag_); 131 synced_session_map_.find(local_session_tag_);
132 if (it != synced_session_map_.end()) { 132 if (it != synced_session_map_.end()) {
133 *output = it->second; 133 *output = it->second;
134 return true; 134 return true;
135 } 135 }
(...skipping 10 matching lines...) Expand all
146 DVLOG(1) << "Creating new session with tag " << session_tag << " at " 146 DVLOG(1) << "Creating new session with tag " << session_tag << " at "
147 << synced_session; 147 << synced_session;
148 synced_session->session_tag = session_tag; 148 synced_session->session_tag = session_tag;
149 synced_session_map_[session_tag] = synced_session; 149 synced_session_map_[session_tag] = synced_session;
150 } 150 }
151 DCHECK(synced_session); 151 DCHECK(synced_session);
152 return synced_session; 152 return synced_session;
153 } 153 }
154 154
155 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) { 155 bool SyncedSessionTracker::DeleteSession(const std::string& session_tag) {
156 bool found_session = false; 156 // Cleanup first, we need to check every wrapper and remove model objects
157 // if the parent session isn't going to delete in it's destructor.
Nicolas Zea 2016/04/12 20:38:04 "isn't going to delete" delete what?
skym 2016/04/12 22:12:14 Orphaned SessionTab and SessionWindow objects. Upd
158 CleanupSession(session_tag);
159
160 bool header_existed = false;
157 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag); 161 SyncedSessionMap::iterator iter = synced_session_map_.find(session_tag);
158 if (iter != synced_session_map_.end()) { 162 if (iter != synced_session_map_.end()) {
159 sync_driver::SyncedSession* session = iter->second; 163 sync_driver::SyncedSession* session = iter->second;
164 // An implicitly created session that has children tabs but no header node
165 // will have never had the device_type changed from unset.
166 header_existed =
167 session->device_type != sync_driver::SyncedSession::TYPE_UNSET;
160 synced_session_map_.erase(iter); 168 synced_session_map_.erase(iter);
161 // SyncedSession's destructor will trigger deletion of windows which will in 169 // SyncedSession's destructor will trigger deletion of windows which will in
162 // turn trigger the deletion of tabs. This doens't affect wrappers. 170 // turn trigger the deletion of tabs. This doesn't affect wrappers.
163 delete session; 171 delete session;
164 found_session = true;
165 } 172 }
173
166 // These two erase(...) calls only affect the wrappers. 174 // These two erase(...) calls only affect the wrappers.
167 synced_window_map_.erase(session_tag); 175 synced_window_map_.erase(session_tag);
168 // It's possible there was no header node but there were tab nodes. 176 synced_tab_map_.erase(session_tag);
169 if (synced_tab_map_.erase(session_tag) > 0) { 177
170 found_session = true; 178 return header_existed;
171 }
172 return found_session;
173 } 179 }
174 180
175 void SyncedSessionTracker::ResetSessionTracking( 181 void SyncedSessionTracker::ResetSessionTracking(
176 const std::string& session_tag) { 182 const std::string& session_tag) {
177 // Reset window tracking. 183 // Reset window tracking.
178 GetSession(session_tag)->windows.clear(); 184 GetSession(session_tag)->windows.clear();
179 SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag); 185 SyncedWindowMap::iterator window_iter = synced_window_map_.find(session_tag);
180 if (window_iter != synced_window_map_.end()) { 186 if (window_iter != synced_window_map_.end()) {
181 for (IDToSessionWindowMap::iterator window_map_iter = 187 for (IDToSessionWindowMap::iterator window_map_iter =
182 window_iter->second.begin(); 188 window_iter->second.begin();
183 window_map_iter != window_iter->second.end(); ++window_map_iter) { 189 window_map_iter != window_iter->second.end(); ++window_map_iter) {
184 window_map_iter->second.owned = false; 190 window_map_iter->second.owned = false;
185 // We clear out the tabs to prevent double referencing of the same tab. 191 // 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. 192 // All tabs that are in use will be added back as needed.
187 window_map_iter->second.window_ptr->tabs.clear(); 193 window_map_iter->second.window_ptr->tabs.clear();
188 } 194 }
189 } 195 }
190 196
191 // Reset tab tracking. 197 // Reset tab tracking.
192 SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag); 198 SyncedTabMap::iterator tab_iter = synced_tab_map_.find(session_tag);
193 if (tab_iter != synced_tab_map_.end()) { 199 if (tab_iter != synced_tab_map_.end()) {
194 for (IDToSessionTabMap::iterator tab_map_iter = tab_iter->second.begin(); 200 for (IDToSessionTabMap::iterator tab_map_iter = tab_iter->second.begin();
195 tab_map_iter != tab_iter->second.end(); ++tab_map_iter) { 201 tab_map_iter != tab_iter->second.end(); ++tab_map_iter) {
196 tab_map_iter->second.owned = false; 202 tab_map_iter->second.owned = false;
197 } 203 }
198 } 204 }
199 } 205 }
200 206
207 void SyncedSessionTracker::DeleteForeignTab(const std::string& session_tag,
208 int tab_node_id) {
209 SyncedSessionMap::const_iterator session_iter =
210 synced_session_map_.find(session_tag);
211 if (session_iter != synced_session_map_.end()) {
212 session_iter->second->tab_node_ids.erase(tab_node_id);
213 }
214 }
215
201 bool SyncedSessionTracker::DeleteOldSessionWindowIfNecessary( 216 bool SyncedSessionTracker::DeleteOldSessionWindowIfNecessary(
202 SessionWindowWrapper window_wrapper) { 217 const SessionWindowWrapper& window_wrapper) {
203 if (!window_wrapper.owned) { 218 if (!window_wrapper.owned) {
204 DVLOG(1) << "Deleting closed window " 219 DVLOG(1) << "Deleting closed window "
205 << window_wrapper.window_ptr->window_id.id(); 220 << window_wrapper.window_ptr->window_id.id();
206 // Clear the tabs first, since we don't want the destructor to destroy 221 // Clear the tabs first, since we don't want the destructor to destroy
207 // them. Their deletion will be handled by DeleteOldSessionTabIfNecessary. 222 // them. Their deletion will be handled by DeleteOldSessionTabIfNecessary.
208 window_wrapper.window_ptr->tabs.clear(); 223 window_wrapper.window_ptr->tabs.clear();
209 delete window_wrapper.window_ptr; 224 delete window_wrapper.window_ptr;
210 return true; 225 return true;
211 } 226 }
212 return false; 227 return false;
213 } 228 }
214 229
215 bool SyncedSessionTracker::DeleteOldSessionTabIfNecessary( 230 bool SyncedSessionTracker::DeleteOldSessionTabIfNecessary(
216 SessionTabWrapper tab_wrapper) { 231 const SessionTabWrapper& tab_wrapper) {
217 if (!tab_wrapper.owned) { 232 if (!tab_wrapper.owned) {
218 if (VLOG_IS_ON(1)) { 233 if (VLOG_IS_ON(1)) {
219 sessions::SessionTab* tab_ptr = tab_wrapper.tab_ptr; 234 sessions::SessionTab* tab_ptr = tab_wrapper.tab_ptr;
220 std::string title; 235 std::string title;
221 if (tab_ptr->navigations.size() > 0) { 236 if (tab_ptr->navigations.size() > 0) {
222 title = 237 title =
223 " (" + 238 " (" +
224 base::UTF16ToUTF8( 239 base::UTF16ToUTF8(
225 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) + 240 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) +
226 ")"; 241 ")";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 tab_iter->second.erase(iter++); 273 tab_iter->second.erase(iter++);
259 } else { 274 } else {
260 ++iter; 275 ++iter;
261 } 276 }
262 } 277 }
263 } 278 }
264 } 279 }
265 280
266 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag, 281 void SyncedSessionTracker::PutWindowInSession(const std::string& session_tag,
267 SessionID::id_type window_id) { 282 SessionID::id_type window_id) {
268 sessions::SessionWindow* window_ptr = NULL; 283 sessions::SessionWindow* window_ptr = nullptr;
269 IDToSessionWindowMap::iterator iter = 284 IDToSessionWindowMap::iterator iter =
270 synced_window_map_[session_tag].find(window_id); 285 synced_window_map_[session_tag].find(window_id);
271 if (iter != synced_window_map_[session_tag].end()) { 286 if (iter != synced_window_map_[session_tag].end()) {
272 iter->second.owned = true; 287 iter->second.owned = true;
273 window_ptr = iter->second.window_ptr; 288 window_ptr = iter->second.window_ptr;
274 DVLOG(1) << "Putting seen window " << window_id << " at " << window_ptr 289 DVLOG(1) << "Putting seen window " << window_id << " at " << window_ptr
275 << "in " << (session_tag == local_session_tag_ ? "local session" 290 << "in " << (session_tag == local_session_tag_ ? "local session"
276 : session_tag); 291 : session_tag);
277 } else { 292 } else {
278 // Create the window. 293 // Create the window.
(...skipping 20 matching lines...) Expand all
299 // SessionWindow information of a SessionHeader node for a foreign session, 314 // SessionWindow information of a SessionHeader node for a foreign session,
300 // and 2) The SessionHeader node for our local session changed. In both cases 315 // 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. 316 // we need to update our tracking state to reflect the change.
302 // 317 //
303 // Because the SessionHeader nodes are separate from the individual tab nodes 318 // 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, 319 // 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. 320 // 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 321 // 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 322 // 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). 323 // 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 = 324 sessions::SessionTab* tab_ptr =
314 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID); 325 GetTabImpl(session_tag, tab_id, TabNodePool::kInvalidTabNodeID);
315 326
316 // It's up to the caller to ensure this never happens. Tabs should not 327 // 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. 328 // belong to more than one window or appear twice within the same window.
318 // 329 //
319 // If this condition were violated, we would double-free during shutdown. 330 // 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 331 // That could cause all sorts of hard to diagnose crashes, possibly in code
321 // far away from here. We crash early to avoid this. 332 // far away from here. We crash early to avoid this.
322 // 333 //
323 // See http://crbug.com/360822. 334 // See http://crbug.com/360822.
324 CHECK(!synced_tab_map_[session_tag][tab_id].owned); 335 CHECK(!synced_tab_map_[session_tag][tab_id].owned);
325 336
326 // Only tabs that were just created in GetTabImpl(...) are still present in 337 // Only tabs that were just created in GetTabImpl(...) are still present in
327 // unmapped_tabs_. Most of the time when PutTabInWindow(...) is invoked, the 338 // 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 339 // 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 340 // 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. 341 // std::set::erase(...) will simply have no effect in these cases.
331 unmapped_tabs_.erase(tab_ptr); 342 unmapped_tabs_.erase(tab_ptr);
332 synced_tab_map_[session_tag][tab_id].owned = true; 343 synced_tab_map_[session_tag][tab_id].owned = true;
333 344
334 tab_ptr->window_id.set_id(window_id); 345 tab_ptr->window_id.set_id(window_id);
335 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id; 346 DVLOG(1) << " - tab " << tab_id << " added to window " << window_id;
336 DCHECK(GetSession(session_tag)->windows.find(window_id) != 347 DCHECK(GetSession(session_tag)->windows.find(window_id) !=
337 GetSession(session_tag)->windows.end()); 348 GetSession(session_tag)->windows.end());
338 std::vector<sessions::SessionTab*>& window_tabs = 349 std::vector<sessions::SessionTab*>& window_tabs =
339 GetSession(session_tag)->windows[window_id]->tabs; 350 GetSession(session_tag)->windows[window_id]->tabs;
340 if (window_tabs.size() <= tab_index) { 351 if (window_tabs.size() <= tab_index) {
341 window_tabs.resize(tab_index + 1, NULL); 352 window_tabs.resize(tab_index + 1, nullptr);
342 } 353 }
343 DCHECK(!window_tabs[tab_index]); 354 DCHECK(!window_tabs[tab_index]);
344 window_tabs[tab_index] = tab_ptr; 355 window_tabs[tab_index] = tab_ptr;
345 } 356 }
346 357
347 sessions::SessionTab* SyncedSessionTracker::GetTab( 358 sessions::SessionTab* SyncedSessionTracker::GetTab(
348 const std::string& session_tag, 359 const std::string& session_tag,
349 SessionID::id_type tab_id, 360 SessionID::id_type tab_id,
350 int tab_node_id) { 361 int tab_node_id) {
351 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); 362 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id);
352 return GetTabImpl(session_tag, tab_id, tab_node_id); 363 return GetTabImpl(session_tag, tab_id, tab_node_id);
353 } 364 }
354 365
355 sessions::SessionTab* SyncedSessionTracker::GetTabImpl( 366 sessions::SessionTab* SyncedSessionTracker::GetTabImpl(
356 const std::string& session_tag, 367 const std::string& session_tag,
357 SessionID::id_type tab_id, 368 SessionID::id_type tab_id,
358 int tab_node_id) { 369 int tab_node_id) {
359 sessions::SessionTab* tab_ptr = NULL; 370 sessions::SessionTab* tab_ptr = nullptr;
360 IDToSessionTabMap::iterator iter = synced_tab_map_[session_tag].find(tab_id); 371 IDToSessionTabMap::iterator iter = synced_tab_map_[session_tag].find(tab_id);
361 if (iter != synced_tab_map_[session_tag].end()) { 372 if (iter != synced_tab_map_[session_tag].end()) {
362 tab_ptr = iter->second.tab_ptr; 373 tab_ptr = iter->second.tab_ptr;
363 if (tab_node_id != TabNodePool::kInvalidTabNodeID && 374 if (tab_node_id != TabNodePool::kInvalidTabNodeID &&
364 tab_id != TabNodePool::kInvalidTabID) { 375 tab_id != TabNodePool::kInvalidTabID) {
365 // TabIDs are not stable across restarts of a client. Consider this 376 // TabIDs are not stable across restarts of a client. Consider this
366 // example with two tabs: 377 // example with two tabs:
367 // 378 //
368 // http://a.com TabID1 --> NodeIDA 379 // http://a.com TabID1 --> NodeIDA
369 // http://b.com TabID2 --> NodeIDB 380 // http://b.com TabID2 --> NodeIDB
370 // 381 //
371 // After restart, tab ids are reallocated. e.g, one possibility: 382 // After restart, tab ids are reallocated. e.g, one possibility:
372 // http://a.com TabID2 --> NodeIDA 383 // http://a.com TabID2 --> NodeIDA
373 // http://b.com TabID1 --> NodeIDB 384 // http://b.com TabID1 --> NodeIDB
374 // 385 //
375 // If that happend on a remote client, here we will see an update to 386 // 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 387 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2
377 // with tab_node_id changing from NodeIDB to NodeIDA. 388 // with tab_node_id changing from NodeIDB to NodeIDA.
378 // 389 //
379 // We can also wind up here if we created this tab as an out-of-order 390 // 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 391 // 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 392 // the tab itself, so the tab node id wasn't available at the time and
382 // is currenlty kInvalidTabNodeID. 393 // is currenlty kInvalidTabNodeID.
383 // 394 //
384 // In both cases, we update the tab_node_id. 395 // In both cases, we can safely throw it into the set of node ids.
385 iter->second.tab_node_id = tab_node_id; 396 GetSession(session_tag)->tab_node_ids.insert(tab_node_id);
386 } 397 }
387 398
388 if (VLOG_IS_ON(1)) { 399 if (VLOG_IS_ON(1)) {
389 std::string title; 400 std::string title;
390 if (tab_ptr->navigations.size() > 0) { 401 if (tab_ptr->navigations.size() > 0) {
391 title = 402 title =
392 " (" + 403 " (" +
393 base::UTF16ToUTF8( 404 base::UTF16ToUTF8(
394 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) + 405 tab_ptr->navigations[tab_ptr->navigations.size() - 1].title()) +
395 ")"; 406 ")";
396 } 407 }
397 DVLOG(1) << "Getting " 408 DVLOG(1) << "Getting "
398 << (session_tag == local_session_tag_ ? "local session" 409 << (session_tag == local_session_tag_ ? "local session"
399 : session_tag) 410 : session_tag)
400 << "'s seen tab " << tab_id << " at " << tab_ptr << title; 411 << "'s seen tab " << tab_id << " at " << tab_ptr << title;
401 } 412 }
402 } else { 413 } else {
403 tab_ptr = new sessions::SessionTab(); 414 tab_ptr = new sessions::SessionTab();
404 tab_ptr->tab_id.set_id(tab_id); 415 tab_ptr->tab_id.set_id(tab_id);
405 synced_tab_map_[session_tag][tab_id] = 416 synced_tab_map_[session_tag][tab_id] =
406 SessionTabWrapper(tab_ptr, NOT_OWNED, tab_node_id); 417 SessionTabWrapper(tab_ptr, NOT_OWNED);
407 unmapped_tabs_.insert(tab_ptr); 418 unmapped_tabs_.insert(tab_ptr);
419 GetSession(session_tag)->tab_node_ids.insert(tab_node_id);
408 DVLOG(1) << "Getting " 420 DVLOG(1) << "Getting "
409 << (session_tag == local_session_tag_ ? "local session" 421 << (session_tag == local_session_tag_ ? "local session"
410 : session_tag) 422 : session_tag)
411 << "'s new tab " << tab_id << " at " << tab_ptr; 423 << "'s new tab " << tab_id << " at " << tab_ptr;
412 } 424 }
413 DCHECK(tab_ptr); 425 DCHECK(tab_ptr);
414 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id); 426 DCHECK_EQ(tab_ptr->tab_id.id(), tab_id);
415 return tab_ptr; 427 return tab_ptr;
416 } 428 }
417 429
418 void SyncedSessionTracker::Clear() { 430 void SyncedSessionTracker::Clear() {
431 // Cleanup unowned things first, before we let SyncedSession's destructor take
432 // care of everything else.
433 for (const auto& kv : synced_session_map_) {
434 CleanupSession(kv.first);
435 }
436
419 // Delete SyncedSession objects (which also deletes all their windows/tabs). 437 // Delete SyncedSession objects (which also deletes all their windows/tabs).
420 STLDeleteValues(&synced_session_map_); 438 STLDeleteValues(&synced_session_map_);
421 439
422 // Go through and delete any tabs we had allocated but had not yet placed into 440 // Go through and delete any tabs we had allocated but had not yet placed into
423 // a SyncedSession object. 441 // a SyncedSession object.
424 STLDeleteElements(&unmapped_tabs_); 442 STLDeleteElements(&unmapped_tabs_);
425 443
426 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs 444 // Get rid of our Window/Tab maps (does not delete the actual Window/Tabs
427 // themselves; they should have all been deleted above). 445 // themselves; they should have all been deleted above).
428 synced_window_map_.clear(); 446 synced_window_map_.clear();
429 synced_tab_map_.clear(); 447 synced_tab_map_.clear();
430 local_session_tag_.clear(); 448 local_session_tag_.clear();
431 } 449 }
432 450
433 } // namespace browser_sync 451 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698