OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sessions_sync_manager.h" | 5 #include "components/sync_sessions/sessions_sync_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 kMaxSyncFavicons), | 129 kMaxSyncFavicons), |
130 local_tab_pool_out_of_sync_(true), | 130 local_tab_pool_out_of_sync_(true), |
131 sync_prefs_(sync_prefs), | 131 sync_prefs_(sync_prefs), |
132 local_device_(local_device), | 132 local_device_(local_device), |
133 current_device_type_(sync_pb::SyncEnums_DeviceType_TYPE_OTHER), | 133 current_device_type_(sync_pb::SyncEnums_DeviceType_TYPE_OTHER), |
134 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), | 134 local_session_header_node_id_(TabNodePool::kInvalidTabNodeID), |
135 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), | 135 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), |
136 local_event_router_(std::move(router)), | 136 local_event_router_(std::move(router)), |
137 page_revisit_broadcaster_(this, sessions_client), | 137 page_revisit_broadcaster_(this, sessions_client), |
138 sessions_updated_callback_(sessions_updated_callback), | 138 sessions_updated_callback_(sessions_updated_callback), |
139 datatype_refresh_callback_(datatype_refresh_callback) {} | 139 datatype_refresh_callback_(datatype_refresh_callback), |
| 140 task_tracker_(base::MakeUnique<TaskTracker>()) {} |
140 | 141 |
141 SessionsSyncManager::~SessionsSyncManager() {} | 142 SessionsSyncManager::~SessionsSyncManager() {} |
142 | 143 |
143 // Returns the GUID-based string that should be used for | 144 // Returns the GUID-based string that should be used for |
144 // |SessionsSyncManager::current_machine_tag_|. | 145 // |SessionsSyncManager::current_machine_tag_|. |
145 static std::string BuildMachineTag(const std::string& cache_guid) { | 146 static std::string BuildMachineTag(const std::string& cache_guid) { |
146 std::string machine_tag = "session_sync"; | 147 std::string machine_tag = "session_sync"; |
147 machine_tag.append(cache_guid); | 148 machine_tag.append(cache_guid); |
148 return machine_tag; | 149 return machine_tag; |
149 } | 150 } |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 current_machine_tag(), current_session_name_, entity); | 357 current_machine_tag(), current_session_name_, entity); |
357 change_output->push_back( | 358 change_output->push_back( |
358 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | 359 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
359 } | 360 } |
360 | 361 |
361 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab_delegate, | 362 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab_delegate, |
362 syncer::SyncChangeList* change_output) { | 363 syncer::SyncChangeList* change_output) { |
363 DCHECK(!tab_delegate->IsPlaceholderTab()); | 364 DCHECK(!tab_delegate->IsPlaceholderTab()); |
364 | 365 |
365 if (tab_delegate->IsBeingDestroyed()) { | 366 if (tab_delegate->IsBeingDestroyed()) { |
366 // Do nothing. By not proactively adding the tab to the session, it will be | 367 task_tracker_->CleanTabTasks(tab_delegate->GetSessionId()); |
367 // removed if necessary during subsequent cleanup. | 368 // Do nothing else. By not proactively adding the tab to the session, it |
| 369 // will be removed if necessary during subsequent cleanup. |
368 return; | 370 return; |
369 } | 371 } |
370 | 372 |
371 if (!tab_delegate->ShouldSync(sessions_client_)) | 373 if (!tab_delegate->ShouldSync(sessions_client_)) |
372 return; | 374 return; |
373 | 375 |
374 SessionID::id_type tab_id = tab_delegate->GetSessionId(); | 376 SessionID::id_type tab_id = tab_delegate->GetSessionId(); |
375 DVLOG(1) << "Syncing tab " << tab_id << " from window " | 377 DVLOG(1) << "Syncing tab " << tab_id << " from window " |
376 << tab_delegate->GetWindowId(); | 378 << tab_delegate->GetWindowId(); |
377 | 379 |
(...skipping 14 matching lines...) Expand all Loading... |
392 // Update the tracker's session representation. | 394 // Update the tracker's session representation. |
393 SetSessionTabFromDelegate(*tab_delegate, base::Time::Now(), session_tab); | 395 SetSessionTabFromDelegate(*tab_delegate, base::Time::Now(), session_tab); |
394 SetVariationIds(session_tab); | 396 SetVariationIds(session_tab); |
395 session_tracker_.GetSession(current_machine_tag())->modified_time = | 397 session_tracker_.GetSession(current_machine_tag())->modified_time = |
396 base::Time::Now(); | 398 base::Time::Now(); |
397 | 399 |
398 // Write to the sync model itself. | 400 // Write to the sync model itself. |
399 sync_pb::EntitySpecifics specifics; | 401 sync_pb::EntitySpecifics specifics; |
400 specifics.mutable_session()->CopyFrom( | 402 specifics.mutable_session()->CopyFrom( |
401 SessionTabToSpecifics(*session_tab, current_machine_tag(), tab_node_id)); | 403 SessionTabToSpecifics(*session_tab, current_machine_tag(), tab_node_id)); |
| 404 // Intercept the sync model here to update task tracker and fill navigations |
| 405 // with their ancestor navigations. |
| 406 TrackTasks(tab_delegate, specifics.mutable_session()); |
402 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 407 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
403 TabNodeIdToTag(current_machine_tag(), tab_node_id), current_session_name_, | 408 TabNodeIdToTag(current_machine_tag(), tab_node_id), current_session_name_, |
404 specifics); | 409 specifics); |
405 change_output->push_back( | 410 change_output->push_back( |
406 syncer::SyncChange(FROM_HERE, | 411 syncer::SyncChange(FROM_HERE, |
407 existing_tab_node ? syncer::SyncChange::ACTION_UPDATE | 412 existing_tab_node ? syncer::SyncChange::ACTION_UPDATE |
408 : syncer::SyncChange::ACTION_ADD, | 413 : syncer::SyncChange::ACTION_ADD, |
409 data)); | 414 data)); |
410 | 415 |
411 int current_index = tab_delegate->GetCurrentEntryIndex(); | 416 int current_index = tab_delegate->GetCurrentEntryIndex(); |
412 const GURL new_url = tab_delegate->GetVirtualURLAtIndex(current_index); | 417 const GURL new_url = tab_delegate->GetVirtualURLAtIndex(current_index); |
413 if (new_url != old_url) { | 418 if (new_url != old_url) { |
414 favicon_cache_.OnFaviconVisited( | 419 favicon_cache_.OnFaviconVisited( |
415 new_url, tab_delegate->GetFaviconURLAtIndex(current_index)); | 420 new_url, tab_delegate->GetFaviconURLAtIndex(current_index)); |
416 page_revisit_broadcaster_.OnPageVisit( | 421 page_revisit_broadcaster_.OnPageVisit( |
417 new_url, tab_delegate->GetTransitionAtIndex(current_index)); | 422 new_url, tab_delegate->GetTransitionAtIndex(current_index)); |
418 } | 423 } |
419 } | 424 } |
420 | 425 |
| 426 void SessionsSyncManager::TrackTasks( |
| 427 SyncedTabDelegate* const tab_delegate, |
| 428 sync_pb::SessionSpecifics* session_specifics) { |
| 429 sync_pb::SessionTab* tab_specifics = session_specifics->mutable_tab(); |
| 430 // Index in the whole navigations of the tab. |
| 431 int current_navigation_index = tab_delegate->GetCurrentEntryIndex(); |
| 432 // Index in the tab_specifics, where the navigations is a -6/+6 window |
| 433 int current_index_in_tab_specifics = |
| 434 tab_specifics->current_navigation_index(); |
| 435 int64_t current_navigation_global_id = |
| 436 tab_specifics->navigation(current_index_in_tab_specifics).global_id(); |
| 437 |
| 438 TabTasks* tab_tasks = |
| 439 task_tracker_->GetTabTasks(tab_delegate->GetSessionId()); |
| 440 tab_tasks->UpdateWithNavigation( |
| 441 current_navigation_index, |
| 442 tab_delegate->GetTransitionAtIndex(current_navigation_index), |
| 443 current_navigation_global_id); |
| 444 |
| 445 for (int i = 0; i < tab_specifics->navigation_size(); i++) { |
| 446 // Excluding blocked navigations, which are appended at tail. |
| 447 if (tab_specifics->navigation(i).blocked_state() == |
| 448 sync_pb::TabNavigation::STATE_BLOCKED) { |
| 449 break; |
| 450 } |
| 451 |
| 452 int navigation_index = |
| 453 current_navigation_index - current_index_in_tab_specifics + i; |
| 454 // Skipping navigations not been tracked by task_tracker. |
| 455 if (navigation_index < 0 || |
| 456 navigation_index >= tab_tasks->GetNavigationsCount()) { |
| 457 continue; |
| 458 } |
| 459 std::vector<int64_t> task_ids = |
| 460 tab_tasks->GetTaskIdsForNavigation(navigation_index); |
| 461 if (task_ids.empty()) |
| 462 continue; |
| 463 |
| 464 tab_specifics->mutable_navigation(i)->set_task_id(task_ids.back()); |
| 465 // Pop the task id of navigation self. |
| 466 task_ids.pop_back(); |
| 467 for (auto ancestor_task_id : task_ids) { |
| 468 tab_specifics->mutable_navigation(i)->add_ancestor_task_id( |
| 469 ancestor_task_id); |
| 470 } |
| 471 } |
| 472 } |
| 473 |
421 bool SessionsSyncManager::RebuildAssociations() { | 474 bool SessionsSyncManager::RebuildAssociations() { |
422 syncer::SyncDataList data(sync_processor_->GetAllSyncData(syncer::SESSIONS)); | 475 syncer::SyncDataList data(sync_processor_->GetAllSyncData(syncer::SESSIONS)); |
423 std::unique_ptr<syncer::SyncErrorFactory> error_handler( | 476 std::unique_ptr<syncer::SyncErrorFactory> error_handler( |
424 std::move(error_handler_)); | 477 std::move(error_handler_)); |
425 std::unique_ptr<syncer::SyncChangeProcessor> processor( | 478 std::unique_ptr<syncer::SyncChangeProcessor> processor( |
426 std::move(sync_processor_)); | 479 std::move(sync_processor_)); |
427 | 480 |
428 StopSyncing(syncer::SESSIONS); | 481 StopSyncing(syncer::SESSIONS); |
429 syncer::SyncMergeResult merge_result = MergeDataAndStartSyncing( | 482 syncer::SyncMergeResult merge_result = MergeDataAndStartSyncing( |
430 syncer::SESSIONS, data, std::move(processor), std::move(error_handler)); | 483 syncer::SESSIONS, data, std::move(processor), std::move(error_handler)); |
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 } | 1192 } |
1140 | 1193 |
1141 // static | 1194 // static |
1142 std::string SessionsSyncManager::TagHashFromSpecifics( | 1195 std::string SessionsSyncManager::TagHashFromSpecifics( |
1143 const sync_pb::SessionSpecifics& specifics) { | 1196 const sync_pb::SessionSpecifics& specifics) { |
1144 return syncer::GenerateSyncableHash(syncer::SESSIONS, | 1197 return syncer::GenerateSyncableHash(syncer::SESSIONS, |
1145 TagFromSpecifics(specifics)); | 1198 TagFromSpecifics(specifics)); |
1146 } | 1199 } |
1147 | 1200 |
1148 }; // namespace sync_sessions | 1201 }; // namespace sync_sessions |
OLD | NEW |