OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/sync/glue/session_model_associator.h" | 5 #include "chrome/browser/sync/glue/session_model_associator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 DCHECK(CalledOnValidThread()); | 136 DCHECK(CalledOnValidThread()); |
137 } | 137 } |
138 | 138 |
139 bool SessionModelAssociator::InitSyncNodeFromChromeId( | 139 bool SessionModelAssociator::InitSyncNodeFromChromeId( |
140 const std::string& id, | 140 const std::string& id, |
141 syncer::BaseNode* sync_node) { | 141 syncer::BaseNode* sync_node) { |
142 NOTREACHED(); | 142 NOTREACHED(); |
143 return false; | 143 return false; |
144 } | 144 } |
145 | 145 |
146 void SessionModelAssociator::NotifySyncIdGenerated( | |
147 const SyncedTabDelegate& tab) { | |
148 content::NotificationService::current()->Notify( | |
149 chrome::NOTIFICATION_SESSION_SYNC_ID_GENERATED, | |
150 content::Source<SessionModelAssociator>(this), | |
151 content::Details<const SyncedTabDelegate>(&tab)); | |
152 } | |
153 | |
146 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 154 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
147 DCHECK(CalledOnValidThread()); | 155 DCHECK(CalledOnValidThread()); |
148 CHECK(has_nodes); | 156 CHECK(has_nodes); |
149 *has_nodes = false; | 157 *has_nodes = false; |
150 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 158 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
151 syncer::ReadNode root(&trans); | 159 syncer::ReadNode root(&trans); |
152 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) != | 160 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) != |
153 syncer::BaseNode::INIT_OK) { | 161 syncer::BaseNode::INIT_OK) { |
154 LOG(ERROR) << kNoSessionsFolderError; | 162 LOG(ERROR) << kNoSessionsFolderError; |
155 return false; | 163 return false; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 sync_pb::SessionSpecifics specifics; | 203 sync_pb::SessionSpecifics specifics; |
196 specifics.set_session_tag(local_tag); | 204 specifics.set_session_tag(local_tag); |
197 sync_pb::SessionHeader* header_s = specifics.mutable_header(); | 205 sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
198 SyncedSession* current_session = | 206 SyncedSession* current_session = |
199 synced_session_tracker_.GetSession(local_tag); | 207 synced_session_tracker_.GetSession(local_tag); |
200 current_session->modified_time = base::Time::Now(); | 208 current_session->modified_time = base::Time::Now(); |
201 header_s->set_client_name(current_session_name_); | 209 header_s->set_client_name(current_session_name_); |
202 header_s->set_device_type(DeviceInfo::GetLocalDeviceType()); | 210 header_s->set_device_type(DeviceInfo::GetLocalDeviceType()); |
203 | 211 |
204 synced_session_tracker_.ResetSessionTracking(local_tag); | 212 synced_session_tracker_.ResetSessionTracking(local_tag); |
213 std::vector<int64> used_sync_ids; | |
205 std::set<SyncedWindowDelegate*> windows = | 214 std::set<SyncedWindowDelegate*> windows = |
206 SyncedWindowDelegate::GetSyncedWindowDelegates(); | 215 SyncedWindowDelegate::GetSyncedWindowDelegates(); |
207 for (std::set<SyncedWindowDelegate*>::const_iterator i = | 216 for (std::set<SyncedWindowDelegate*>::const_iterator i = |
208 windows.begin(); i != windows.end(); ++i) { | 217 windows.begin(); i != windows.end(); ++i) { |
209 // Make sure the window has tabs and a viewable window. The viewable window | 218 // Make sure the window has tabs and a viewable window. The viewable window |
210 // check is necessary because, for example, when a browser is closed the | 219 // check is necessary because, for example, when a browser is closed the |
211 // destructor is not necessarily run immediately. This means its possible | 220 // destructor is not necessarily run immediately. This means its possible |
212 // for us to get a handle to a browser that is about to be removed. If | 221 // for us to get a handle to a browser that is about to be removed. If |
213 // the tab count is 0 or the window is NULL, the browser is about to be | 222 // the tab count is 0 or the window is NULL, the browser is about to be |
214 // deleted, so we ignore it. | 223 // deleted, so we ignore it. |
215 if (ShouldSyncWindow(*i) && (*i)->GetTabCount() && (*i)->HasWindow()) { | 224 if (ShouldSyncWindow(*i) && (*i)->GetTabCount() && (*i)->HasWindow()) { |
216 sync_pb::SessionWindow window_s; | 225 sync_pb::SessionWindow window_s; |
217 SessionID::id_type window_id = (*i)->GetSessionId(); | 226 SessionID::id_type window_id = (*i)->GetSessionId(); |
218 DVLOG(1) << "Associating window " << window_id << " with " | 227 DVLOG(1) << "Associating window " << window_id << " with " |
219 << (*i)->GetTabCount() << " tabs."; | 228 << (*i)->GetTabCount() << " tabs."; |
220 window_s.set_window_id(window_id); | 229 window_s.set_window_id(window_id); |
221 // Note: We don't bother to set selected tab index anymore. We still | 230 // Note: We don't bother to set selected tab index anymore. We still |
222 // consume it when receiving foreign sessions, as reading it is free, but | 231 // consume it when foreign sessions, as reading it is free, but |
223 // it triggers too many sync cycles with too little value to make setting | 232 // it triggers too many sync cycles with too little value to make setting |
224 // it worthwhile. | 233 // it worthwhile. |
225 if ((*i)->IsTypeTabbed()) { | 234 if ((*i)->IsTypeTabbed()) { |
226 window_s.set_browser_type( | 235 window_s.set_browser_type( |
227 sync_pb::SessionWindow_BrowserType_TYPE_TABBED); | 236 sync_pb::SessionWindow_BrowserType_TYPE_TABBED); |
228 } else { | 237 } else { |
229 window_s.set_browser_type( | 238 window_s.set_browser_type( |
230 sync_pb::SessionWindow_BrowserType_TYPE_POPUP); | 239 sync_pb::SessionWindow_BrowserType_TYPE_POPUP); |
231 } | 240 } |
232 | 241 |
233 // Store the order of tabs. | 242 // Store the order of tabs. |
234 bool found_tabs = false; | 243 bool found_tabs = false; |
244 std::vector<SessionID::id_type> used_tabs; | |
235 for (int j = 0; j < (*i)->GetTabCount(); ++j) { | 245 for (int j = 0; j < (*i)->GetTabCount(); ++j) { |
236 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); | 246 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); |
247 SyncedTabDelegate* tab_delegate = (*i)->GetTabAt(j); | |
237 | 248 |
238 if (reload_tabs) { | 249 if (reload_tabs) { |
239 SyncedTabDelegate* tab = (*i)->GetTabAt(j); | 250 if (tab_delegate && !tab_delegate->IsTabInMemory()) { |
251 found_tabs = true; | |
252 // Update tab id since it might have changed. | |
253 UpdateTabIdForOldTab(tab_delegate->GetSyncSessionId(), tab_id); | |
Nicolas Zea
2013/05/16 22:59:03
I don't think you should ever update the tab id. T
shashi
2013/05/17 00:29:16
When restore happens, the tab ids (different from
| |
254 window_s.add_tab(tab_id); | |
255 used_sync_ids.push_back(tab_delegate->GetSyncSessionId()); | |
256 continue; | |
257 } | |
258 | |
240 // It's possible for GetTabAt to return a null tab if it's not in | 259 // It's possible for GetTabAt to return a null tab if it's not in |
241 // memory. We can assume this means the tab already existed but hasn't | 260 // memory. We can assume this means the tab already existed but hasn't |
242 // changed, so no need to reassociate. | 261 // changed, so no need to reassociate. |
243 if (tab && !AssociateTab(*tab, error)) { | 262 if (tab_delegate && tab_delegate->IsTabInMemory() && |
263 !AssociateTab(*tab_delegate, error)) { | |
244 // Association failed. Either we need to re-associate, or this is an | 264 // Association failed. Either we need to re-associate, or this is an |
245 // unrecoverable error. | 265 // unrecoverable error. |
246 return false; | 266 return false; |
247 } | 267 } |
248 } | 268 } |
249 | 269 |
250 // If the tab is valid, it would have been added to the tracker either | 270 // If the tab is valid, it would have been added to the tracker either |
251 // by the above AssociateTab call (at association time), or by the | 271 // by the above AssociateTab call (at association time), or by the |
252 // change processor calling AssociateTab for all modified tabs. | 272 // change processor calling AssociateTab for all modified tabs. |
253 // Therefore, we can key whether this window has valid tabs based on | 273 // Therefore, we can key whether this window has valid tabs based on |
254 // the tab's presence in the tracker. | 274 // the tab's presence in the tracker. |
255 const SessionTab* tab = NULL; | 275 const SessionTab* tab = NULL; |
256 if (synced_session_tracker_.LookupSessionTab(local_tag, tab_id, &tab)) { | 276 if (synced_session_tracker_.LookupSessionTab(local_tag, tab_id, &tab)) { |
277 used_tabs.push_back(tab_id); | |
257 found_tabs = true; | 278 found_tabs = true; |
279 used_sync_ids.push_back(tab_delegate->GetSyncSessionId()); | |
258 window_s.add_tab(tab_id); | 280 window_s.add_tab(tab_id); |
259 } | 281 } |
260 } | 282 } |
261 // Only add a window if it contains valid tabs. | 283 // Only add a window if it contains valid tabs. |
262 if (found_tabs) { | 284 if (found_tabs) { |
263 sync_pb::SessionWindow* header_window = header_s->add_window(); | 285 sync_pb::SessionWindow* header_window = header_s->add_window(); |
264 *header_window = window_s; | 286 *header_window = window_s; |
265 | 287 |
266 // Update this window's representation in the synced session tracker. | 288 // Update this window's representation in the synced session tracker. |
267 synced_session_tracker_.PutWindowInSession(local_tag, window_id); | 289 synced_session_tracker_.PutWindowInSession(local_tag, window_id); |
268 PopulateSessionWindowFromSpecifics( | 290 PopulateSessionWindowFromSpecifics( |
269 local_tag, | 291 local_tag, |
270 window_s, | 292 window_s, |
271 base::Time::Now(), | 293 base::Time::Now(), |
272 current_session->windows[window_id], | 294 current_session->windows[window_id], |
273 &synced_session_tracker_); | 295 &synced_session_tracker_); |
274 } | 296 } |
275 } | 297 } |
276 } | 298 } |
277 // Free memory for closed windows and tabs. | 299 // Free memory for closed windows and tabs. |
278 synced_session_tracker_.CleanupSession(local_tag); | 300 synced_session_tracker_.CleanupSession(local_tag); |
301 tab_pool_.PurgeOldSyncNodes(used_sync_ids); | |
279 | 302 |
280 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 303 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
281 syncer::WriteNode header_node(&trans); | 304 syncer::WriteNode header_node(&trans); |
282 if (header_node.InitByIdLookup(local_session_syncid_) != | 305 if (header_node.InitByIdLookup(local_session_syncid_) != |
283 syncer::BaseNode::INIT_OK) { | 306 syncer::BaseNode::INIT_OK) { |
284 if (error) { | 307 if (error) { |
285 *error = error_handler_->CreateAndUploadError( | 308 *error = error_handler_->CreateAndUploadError( |
286 FROM_HERE, | 309 FROM_HERE, |
287 "Failed to load local session header node.", | 310 "Failed to load local session header node.", |
288 model_type()); | 311 model_type()); |
289 } | 312 } |
290 return false; | 313 return false; |
291 } | 314 } |
292 header_node.SetSessionSpecifics(specifics); | 315 header_node.SetSessionSpecifics(specifics); |
293 if (waiting_for_change_) QuitLoopForSubtleTesting(); | 316 if (waiting_for_change_) QuitLoopForSubtleTesting(); |
294 return true; | 317 return true; |
295 } | 318 } |
296 | 319 |
320 void SessionModelAssociator::UpdateTabIdForOldTab( | |
321 int64 sync_id, | |
322 SessionID::id_type new_tab_id) { | |
323 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | |
324 syncer::WriteNode tab_node(&trans); | |
325 // Rewrite tab id if required. | |
326 if (tab_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { | |
327 return; | |
328 } | |
329 sync_pb::SessionSpecifics session_specifics = tab_node.GetSessionSpecifics(); | |
330 DCHECK(session_specifics.has_tab()); | |
331 sync_pb::SessionTab* tab_s = session_specifics.mutable_tab(); | |
332 tab_s->set_tab_id(new_tab_id); | |
333 tab_node.SetSessionSpecifics(session_specifics); | |
334 } | |
335 | |
297 // Static. | 336 // Static. |
298 bool SessionModelAssociator::ShouldSyncWindow( | 337 bool SessionModelAssociator::ShouldSyncWindow( |
299 const SyncedWindowDelegate* window) { | 338 const SyncedWindowDelegate* window) { |
300 if (window->IsApp()) | 339 if (window->IsApp()) |
301 return false; | 340 return false; |
302 return window->IsTypeTabbed() || window->IsTypePopup(); | 341 return window->IsTypeTabbed() || window->IsTypePopup(); |
303 } | 342 } |
304 | 343 |
305 bool SessionModelAssociator::AssociateTabs( | 344 bool SessionModelAssociator::AssociateTabs( |
306 const std::vector<SyncedTabDelegate*>& tabs, | 345 const std::vector<SyncedTabDelegate*>& tabs, |
307 syncer::SyncError* error) { | 346 syncer::SyncError* error) { |
308 DCHECK(CalledOnValidThread()); | 347 DCHECK(CalledOnValidThread()); |
309 for (std::vector<SyncedTabDelegate*>::const_iterator i = tabs.begin(); | 348 for (std::vector<SyncedTabDelegate*>::const_iterator i = tabs.begin(); |
310 i != tabs.end(); | 349 i != tabs.end(); |
311 ++i) { | 350 ++i) { |
312 if (!AssociateTab(**i, error)) | 351 if (!AssociateTab(**i, error)) |
313 return false; | 352 return false; |
314 } | 353 } |
315 if (waiting_for_change_) QuitLoopForSubtleTesting(); | 354 if (waiting_for_change_) QuitLoopForSubtleTesting(); |
316 return true; | 355 return true; |
317 } | 356 } |
318 | 357 |
319 bool SessionModelAssociator::AssociateTab(const SyncedTabDelegate& tab, | 358 bool SessionModelAssociator::AssociateTab(SyncedTabDelegate& tab, |
320 syncer::SyncError* error) { | 359 syncer::SyncError* error) { |
321 DCHECK(CalledOnValidThread()); | 360 DCHECK(CalledOnValidThread()); |
322 int64 sync_id; | 361 int64 sync_id; |
323 SessionID::id_type tab_id = tab.GetSessionId(); | 362 SessionID::id_type tab_id = tab.GetSessionId().id(); |
324 if (tab.IsBeingDestroyed()) { | 363 if (tab.IsBeingDestroyed()) { |
325 // This tab is closing. | 364 // This tab is closing. |
326 TabLinksMap::iterator tab_iter = tab_map_.find(tab_id); | 365 TabLinksMap::iterator tab_iter = tab_map_.find(tab_id); |
327 if (tab_iter == tab_map_.end()) { | 366 if (tab_iter == tab_map_.end()) { |
328 // We aren't tracking this tab (for example, sync setting page). | 367 // We aren't tracking this tab (for example, sync setting page). |
329 return true; | 368 return true; |
330 } | 369 } |
331 tab_pool_.FreeTabNode(tab_iter->second->sync_id()); | 370 tab_pool_.FreeTabNode(tab_iter->second->sync_id()); |
332 tab_map_.erase(tab_iter); | 371 tab_map_.erase(tab_iter); |
333 return true; | 372 return true; |
334 } | 373 } |
335 | 374 |
336 if (!ShouldSyncTab(tab)) | 375 if (!ShouldSyncTab(tab)) |
337 return true; | 376 return true; |
338 | 377 |
339 TabLinksMap::iterator tab_map_iter = tab_map_.find(tab_id); | 378 TabLinksMap::iterator tab_map_iter = tab_map_.find(tab_id); |
340 TabLink* tab_link = NULL; | 379 TabLink* tab_link = NULL; |
341 if (tab_map_iter == tab_map_.end()) { | 380 if (tab_map_iter == tab_map_.end()) { |
342 // This is a new tab, get a sync node for it. | 381 // This is a new tab, get a sync node for it. Check if there is an old sync |
343 sync_id = tab_pool_.GetFreeTabNode(); | 382 // node for it. |
383 sync_id = tab_pool_.GetOldSyncNode(tab.GetSyncSessionId()); | |
384 if (sync_id < 0) { | |
385 sync_id = tab_pool_.GetFreeTabNode(); | |
386 } | |
387 if (tab.GetSyncSessionId() != sync_id) { | |
388 tab.SetSyncSessionId(sync_id); | |
389 NotifySyncIdGenerated(tab); | |
Nicolas Zea
2013/05/16 22:59:03
would it be better to have the equivalency check a
shashi
2013/05/17 00:29:16
On 2013/05/16 22:59:03, Nicolas Zea wrote:
> would
shashi
2013/05/17 00:29:16
Yes, I can replace it with an equivalency check, a
| |
390 } | |
391 | |
344 if (sync_id == syncer::kInvalidId) { | 392 if (sync_id == syncer::kInvalidId) { |
345 if (error) { | 393 if (error) { |
346 *error = error_handler_->CreateAndUploadError( | 394 *error = error_handler_->CreateAndUploadError( |
347 FROM_HERE, | 395 FROM_HERE, |
348 "Received invalid tab node from tab pool.", | 396 "Received invalid tab node from tab pool.", |
349 model_type()); | 397 model_type()); |
350 } | 398 } |
351 return false; | 399 return false; |
352 } | 400 } |
353 tab_link = new TabLink(sync_id, &tab); | 401 tab_link = new TabLink(sync_id, &tab); |
354 tab_map_[tab_id] = make_linked_ptr<TabLink>(tab_link); | 402 tab_map_[tab_id] = make_linked_ptr<TabLink>(tab_link); |
355 } else { | 403 } else { |
356 // This tab is already associated with a sync node, reuse it. | 404 // This tab is already associated with a sync node, reuse it. |
357 // Note: on some platforms the tab object may have changed, so we ensure | 405 // Note: on some platforms the tab object may have changed, so we ensure |
358 // the tab link is up to date. | 406 // the tab link is up to date. |
359 tab_link = tab_map_iter->second.get(); | 407 tab_link = tab_map_iter->second.get(); |
360 tab_map_iter->second->set_tab(&tab); | 408 tab_map_iter->second->set_tab(&tab); |
361 } | 409 } |
362 DCHECK(tab_link); | 410 DCHECK(tab_link); |
363 DCHECK_NE(tab_link->sync_id(), syncer::kInvalidId); | 411 DCHECK_NE(tab_link->sync_id(), syncer::kInvalidId); |
364 | 412 |
365 DVLOG(1) << "Reloading tab " << tab_id << " from window " | 413 DVLOG(1) << "Reloading tab " << tab_id << " from window " |
366 << tab.GetWindowId(); | 414 << tab.GetWindowId().id(); |
367 return WriteTabContentsToSyncModel(tab_link, error); | 415 return WriteTabContentsToSyncModel(tab_link, error); |
368 } | 416 } |
369 | 417 |
370 // static | 418 // static |
371 GURL SessionModelAssociator::GetCurrentVirtualURL( | 419 GURL SessionModelAssociator::GetCurrentVirtualURL( |
372 const SyncedTabDelegate& tab_delegate) { | 420 const SyncedTabDelegate& tab_delegate) { |
373 const int current_index = tab_delegate.GetCurrentEntryIndex(); | 421 const int current_index = tab_delegate.GetCurrentEntryIndex(); |
374 const int pending_index = tab_delegate.GetPendingEntryIndex(); | 422 const int pending_index = tab_delegate.GetPendingEntryIndex(); |
375 const NavigationEntry* current_entry = | 423 const NavigationEntry* current_entry = |
376 (current_index == pending_index) ? | 424 (current_index == pending_index) ? |
(...skipping 13 matching lines...) Expand all Loading... | |
390 tab_delegate.GetEntryAtIndex(current_index); | 438 tab_delegate.GetEntryAtIndex(current_index); |
391 return (current_entry->GetFavicon().valid ? | 439 return (current_entry->GetFavicon().valid ? |
392 current_entry->GetFavicon().url : | 440 current_entry->GetFavicon().url : |
393 GURL()); | 441 GURL()); |
394 } | 442 } |
395 | 443 |
396 bool SessionModelAssociator::WriteTabContentsToSyncModel( | 444 bool SessionModelAssociator::WriteTabContentsToSyncModel( |
397 TabLink* tab_link, | 445 TabLink* tab_link, |
398 syncer::SyncError* error) { | 446 syncer::SyncError* error) { |
399 DCHECK(CalledOnValidThread()); | 447 DCHECK(CalledOnValidThread()); |
448 DCHECK(tab_link->tab()); | |
400 const SyncedTabDelegate& tab_delegate = *(tab_link->tab()); | 449 const SyncedTabDelegate& tab_delegate = *(tab_link->tab()); |
401 int64 sync_id = tab_link->sync_id(); | 450 int64 sync_id = tab_link->sync_id(); |
402 GURL old_tab_url = tab_link->url(); | 451 GURL old_tab_url = tab_link->url(); |
403 | 452 |
404 // Load the last stored version of this tab so we can compare changes. If this | 453 // Load the last stored version of this tab so we can compare changes. If this |
405 // is a new tab, session_tab will be a blank/newly created SessionTab object. | 454 // is a new tab, session_tab will be a blank/newly created SessionTab object. |
406 SessionTab* session_tab = | 455 SessionTab* session_tab = synced_session_tracker_.GetTab( |
407 synced_session_tracker_.GetTab(GetCurrentMachineTag(), | 456 GetCurrentMachineTag(), tab_delegate.GetSessionId().id()); |
408 tab_delegate.GetSessionId()); | |
409 | 457 |
410 SetSessionTabFromDelegate(tab_delegate, base::Time::Now(), session_tab); | 458 SetSessionTabFromDelegate(tab_delegate, base::Time::Now(), session_tab); |
411 | 459 |
412 const GURL new_url = GetCurrentVirtualURL(tab_delegate); | 460 const GURL new_url = GetCurrentVirtualURL(tab_delegate); |
413 DVLOG(1) << "Local tab " << tab_delegate.GetSessionId() | 461 VLOG(1) << "Local tab " << tab_delegate.GetSessionId().id() << " now has URL " |
414 << " now has URL " << new_url.spec(); | 462 << new_url.spec(); |
415 | 463 |
416 // Trigger the favicon load if needed. We do this before opening the write | 464 // Trigger the favicon load if needed. We do this before opening the write |
417 // transaction to avoid jank. | 465 // transaction to avoid jank. |
418 tab_link->set_url(new_url); | 466 tab_link->set_url(new_url); |
419 if (new_url != old_tab_url) { | 467 if (new_url != old_tab_url) { |
420 favicon_cache_.OnFaviconVisited(new_url, | 468 favicon_cache_.OnFaviconVisited(new_url, |
421 GetCurrentFaviconURL(tab_delegate)); | 469 GetCurrentFaviconURL(tab_delegate)); |
422 } | 470 } |
423 | 471 |
424 // Update our last modified time. | 472 // Update our last modified time. |
425 synced_session_tracker_.GetSession(GetCurrentMachineTag())->modified_time = | 473 synced_session_tracker_.GetSession(GetCurrentMachineTag())->modified_time = |
426 base::Time::Now(); | 474 base::Time::Now(); |
427 | |
428 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 475 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
429 syncer::WriteNode tab_node(&trans); | 476 syncer::WriteNode tab_node(&trans); |
430 if (tab_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { | 477 if (tab_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { |
431 if (error) { | 478 if (error) { |
432 *error = error_handler_->CreateAndUploadError( | 479 *error = error_handler_->CreateAndUploadError( |
433 FROM_HERE, | 480 FROM_HERE, |
434 "Failed to look up local tab node", | 481 "Failed to look up local tab node", |
435 model_type()); | 482 model_type()); |
436 } | 483 } |
437 return false; | 484 return false; |
438 } | 485 } |
439 | |
440 sync_pb::SessionTab tab_s = session_tab->ToSyncData(); | 486 sync_pb::SessionTab tab_s = session_tab->ToSyncData(); |
441 sync_pb::SessionSpecifics specifics = tab_node.GetSessionSpecifics(); | 487 sync_pb::SessionSpecifics specifics = tab_node.GetSessionSpecifics(); |
442 if (new_url == old_tab_url) { | 488 if (new_url == old_tab_url) { |
443 // Load the old specifics and copy over the favicon data if needed. | 489 // Load the old specifics and copy over the favicon data if needed. |
444 // TODO(zea): remove this once favicon sync is enabled as a separate type. | 490 // TODO(zea): remove this once favicon sync is enabled as a separate type. |
445 tab_s.set_favicon(specifics.tab().favicon()); | 491 tab_s.set_favicon(specifics.tab().favicon()); |
446 tab_s.set_favicon_source(specifics.tab().favicon_source()); | 492 tab_s.set_favicon_source(specifics.tab().favicon_source()); |
447 tab_s.set_favicon_type(specifics.tab().favicon_type()); | 493 tab_s.set_favicon_type(specifics.tab().favicon_type()); |
448 } | 494 } |
449 // Retain the base SessionSpecifics data (tag, tab_node_id, etc.), and just | 495 // Retain the base SessionSpecifics data (tag, tab_node_id, etc.), and just |
450 // write the new SessionTabSpecifics. | 496 // write the new SessionTabSpecifics. |
451 specifics.mutable_tab()->CopyFrom(tab_s); | 497 specifics.mutable_tab()->CopyFrom(tab_s); |
452 | |
453 // Write into the actual sync model. | 498 // Write into the actual sync model. |
454 tab_node.SetSessionSpecifics(specifics); | 499 tab_node.SetSessionSpecifics(specifics); |
455 | 500 |
456 return true; | 501 return true; |
457 } | 502 } |
458 | 503 |
459 // static | 504 // static |
460 void SessionModelAssociator::SetSessionTabFromDelegate( | 505 void SessionModelAssociator::SetSessionTabFromDelegate( |
461 const SyncedTabDelegate& tab_delegate, | 506 const SyncedTabDelegate& tab_delegate, |
462 base::Time mtime, | 507 base::Time mtime, |
463 SessionTab* session_tab) { | 508 SessionTab* session_tab) { |
464 DCHECK(session_tab); | 509 DCHECK(session_tab); |
465 session_tab->window_id.set_id(tab_delegate.GetWindowId()); | 510 session_tab->window_id.set_id(tab_delegate.GetWindowId().id()); |
466 session_tab->tab_id.set_id(tab_delegate.GetSessionId()); | 511 session_tab->tab_id.set_id(tab_delegate.GetSessionId().id()); |
467 session_tab->tab_visual_index = 0; | 512 session_tab->tab_visual_index = 0; |
468 session_tab->current_navigation_index = tab_delegate.GetCurrentEntryIndex(); | 513 session_tab->current_navigation_index = tab_delegate.GetCurrentEntryIndex(); |
469 session_tab->pinned = tab_delegate.IsPinned(); | 514 session_tab->pinned = tab_delegate.IsPinned(); |
470 session_tab->extension_app_id = tab_delegate.GetExtensionAppId(); | 515 session_tab->extension_app_id = tab_delegate.GetExtensionAppId(); |
516 session_tab->sync_session_id = tab_delegate.GetSyncSessionId(); | |
471 session_tab->user_agent_override.clear(); | 517 session_tab->user_agent_override.clear(); |
472 session_tab->timestamp = mtime; | 518 session_tab->timestamp = mtime; |
473 const int current_index = tab_delegate.GetCurrentEntryIndex(); | 519 const int current_index = tab_delegate.GetCurrentEntryIndex(); |
474 const int pending_index = tab_delegate.GetPendingEntryIndex(); | 520 const int pending_index = tab_delegate.GetPendingEntryIndex(); |
475 const int min_index = std::max(0, | 521 const int min_index = std::max(0, |
476 current_index - kMaxSyncNavigationCount); | 522 current_index - kMaxSyncNavigationCount); |
477 const int max_index = std::min(current_index + kMaxSyncNavigationCount, | 523 const int max_index = std::min(current_index + kMaxSyncNavigationCount, |
478 tab_delegate.GetEntryCount()); | 524 tab_delegate.GetEntryCount()); |
479 session_tab->navigations.clear(); | 525 session_tab->navigations.clear(); |
480 for (int i = min_index; i < max_index; ++i) { | 526 for (int i = min_index; i < max_index; ++i) { |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
750 if (specifics.has_header()) { | 796 if (specifics.has_header()) { |
751 LOG(WARNING) << "Found more than one session header node with local " | 797 LOG(WARNING) << "Found more than one session header node with local " |
752 << " tag."; | 798 << " tag."; |
753 } else if (!specifics.has_tab()) { | 799 } else if (!specifics.has_tab()) { |
754 LOG(WARNING) << "Found local node with no header or tag field."; | 800 LOG(WARNING) << "Found local node with no header or tag field."; |
755 } | 801 } |
756 | 802 |
757 // TODO(zea): fix this once we add support for reassociating | 803 // TODO(zea): fix this once we add support for reassociating |
758 // pre-existing tabs with pre-existing tab nodes. We'll need to load | 804 // pre-existing tabs with pre-existing tab nodes. We'll need to load |
759 // the tab_node_id and ensure the tab_pool_ keeps track of them. | 805 // the tab_node_id and ensure the tab_pool_ keeps track of them. |
760 sync_node.Tombstone(); | 806 if (specifics.has_tab()) |
807 tab_pool_.AddTabNode(id, specifics.tab_node_id()); | |
Nicolas Zea
2013/05/16 22:59:03
else tombstone? (it would be the above error case
shashi
2013/05/17 00:29:16
Good point, I should handle the case for multiple
| |
761 } | 808 } |
762 } | 809 } |
763 id = next_id; | 810 id = next_id; |
764 } | 811 } |
765 | 812 |
766 // After updating from sync model all tabid's should be free. | 813 // After updating from sync model all tabid's should be free. |
767 DCHECK(tab_pool_.full()); | 814 DCHECK(tab_pool_.full()); |
768 return true; | 815 return true; |
769 } | 816 } |
770 | 817 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1091 } | 1138 } |
1092 } | 1139 } |
1093 | 1140 |
1094 bool SessionModelAssociator::IsValidTab(const SyncedTabDelegate& tab) const { | 1141 bool SessionModelAssociator::IsValidTab(const SyncedTabDelegate& tab) const { |
1095 if ((!sync_service_ || tab.profile() != sync_service_->profile()) && | 1142 if ((!sync_service_ || tab.profile() != sync_service_->profile()) && |
1096 !setup_for_test_) { | 1143 !setup_for_test_) { |
1097 return false; | 1144 return false; |
1098 } | 1145 } |
1099 const SyncedWindowDelegate* window = | 1146 const SyncedWindowDelegate* window = |
1100 SyncedWindowDelegate::FindSyncedWindowDelegateWithId( | 1147 SyncedWindowDelegate::FindSyncedWindowDelegateWithId( |
1101 tab.GetWindowId()); | 1148 tab.GetWindowId().id()); |
1102 if (!window && !setup_for_test_) | 1149 if (!window && !setup_for_test_) |
1103 return false; | 1150 return false; |
1104 return true; | 1151 return true; |
1105 } | 1152 } |
1106 | 1153 |
1107 bool SessionModelAssociator::TabHasValidEntry( | 1154 bool SessionModelAssociator::TabHasValidEntry( |
1108 const SyncedTabDelegate& tab) const { | 1155 const SyncedTabDelegate& tab) const { |
1109 int pending_index = tab.GetPendingEntryIndex(); | 1156 int pending_index = tab.GetPendingEntryIndex(); |
1110 int entry_count = tab.GetEntryCount(); | 1157 int entry_count = tab.GetEntryCount(); |
1111 bool found_valid_url = false; | 1158 bool found_valid_url = false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1161 | 1208 |
1162 bool SessionModelAssociator::CryptoReadyIfNecessary() { | 1209 bool SessionModelAssociator::CryptoReadyIfNecessary() { |
1163 // We only access the cryptographer while holding a transaction. | 1210 // We only access the cryptographer while holding a transaction. |
1164 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1211 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
1165 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); | 1212 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); |
1166 return !encrypted_types.Has(SESSIONS) || | 1213 return !encrypted_types.Has(SESSIONS) || |
1167 sync_service_->IsCryptographerReady(&trans); | 1214 sync_service_->IsCryptographerReady(&trans); |
1168 } | 1215 } |
1169 | 1216 |
1170 } // namespace browser_sync | 1217 } // namespace browser_sync |
OLD | NEW |