| 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" |
| 11 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 11 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/stringprintf.h" |
| 13 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 14 #include "components/sync/base/hash_util.h" | 17 #include "components/sync/base/hash_util.h" |
| 15 #include "components/sync/device_info/local_device_info_provider.h" | 18 #include "components/sync/device_info/local_device_info_provider.h" |
| 16 #include "components/sync/model/sync_error.h" | 19 #include "components/sync/model/sync_error.h" |
| 17 #include "components/sync/model/sync_error_factory.h" | 20 #include "components/sync/model/sync_error_factory.h" |
| 18 #include "components/sync/model/sync_merge_result.h" | 21 #include "components/sync/model/sync_merge_result.h" |
| 19 #include "components/sync/model/time.h" | 22 #include "components/sync/model/time.h" |
| 20 #include "components/sync_sessions/sync_sessions_client.h" | 23 #include "components/sync_sessions/sync_sessions_client.h" |
| 21 #include "components/sync_sessions/synced_tab_delegate.h" | 24 #include "components/sync_sessions/synced_tab_delegate.h" |
| 22 #include "components/sync_sessions/synced_window_delegate.h" | 25 #include "components/sync_sessions/synced_window_delegate.h" |
| 23 #include "components/sync_sessions/synced_window_delegates_getter.h" | 26 #include "components/sync_sessions/synced_window_delegates_getter.h" |
| 27 #include "components/sync_sessions/tab_node_pool.h" |
| 24 #include "components/variations/variations_associated_data.h" | 28 #include "components/variations/variations_associated_data.h" |
| 25 | 29 |
| 26 using sessions::SerializedNavigationEntry; | 30 using sessions::SerializedNavigationEntry; |
| 27 using syncer::DeviceInfo; | 31 using syncer::DeviceInfo; |
| 28 using syncer::LocalDeviceInfoProvider; | 32 using syncer::LocalDeviceInfoProvider; |
| 29 using syncer::SyncChange; | 33 using syncer::SyncChange; |
| 30 using syncer::SyncData; | 34 using syncer::SyncData; |
| 31 | 35 |
| 32 namespace sync_sessions { | 36 namespace sync_sessions { |
| 33 | 37 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 55 return t1->timestamp > t2->timestamp; | 59 return t1->timestamp > t2->timestamp; |
| 56 } | 60 } |
| 57 | 61 |
| 58 // Comparator function for use with std::sort that will sort sessions by | 62 // Comparator function for use with std::sort that will sort sessions by |
| 59 // descending modified_time (i.e., most recent first). | 63 // descending modified_time (i.e., most recent first). |
| 60 bool SessionsRecencyComparator(const SyncedSession* s1, | 64 bool SessionsRecencyComparator(const SyncedSession* s1, |
| 61 const SyncedSession* s2) { | 65 const SyncedSession* s2) { |
| 62 return s1->modified_time > s2->modified_time; | 66 return s1->modified_time > s2->modified_time; |
| 63 } | 67 } |
| 64 | 68 |
| 69 std::string TabNodeIdToTag(const std::string& machine_tag, int tab_node_id) { |
| 70 return base::StringPrintf("%s %d", machine_tag.c_str(), tab_node_id); |
| 71 } |
| 72 |
| 65 std::string TagFromSpecifics(const sync_pb::SessionSpecifics& specifics) { | 73 std::string TagFromSpecifics(const sync_pb::SessionSpecifics& specifics) { |
| 66 if (specifics.has_header()) { | 74 if (specifics.has_header()) { |
| 67 return specifics.session_tag(); | 75 return specifics.session_tag(); |
| 68 } else if (specifics.has_tab()) { | 76 } else if (specifics.has_tab()) { |
| 69 return TabNodePool::TabIdToTag(specifics.session_tag(), | 77 return TabNodeIdToTag(specifics.session_tag(), specifics.tab_node_id()); |
| 70 specifics.tab_node_id()); | |
| 71 } else { | 78 } else { |
| 72 return std::string(); | 79 return std::string(); |
| 73 } | 80 } |
| 74 } | 81 } |
| 75 | 82 |
| 83 sync_pb::SessionSpecifics SessionTabToSpecifics( |
| 84 const sessions::SessionTab& session_tab, |
| 85 const std::string& local_tag, |
| 86 int tab_node_id) { |
| 87 sync_pb::SessionSpecifics specifics; |
| 88 specifics.mutable_tab()->CopyFrom(session_tab.ToSyncData()); |
| 89 specifics.set_session_tag(local_tag); |
| 90 specifics.set_tab_node_id(tab_node_id); |
| 91 return specifics; |
| 92 } |
| 93 |
| 76 } // namespace | 94 } // namespace |
| 77 | 95 |
| 78 // |local_device| is owned by ProfileSyncService, its lifetime exceeds | 96 // |local_device| is owned by ProfileSyncService, its lifetime exceeds |
| 79 // lifetime of SessionSyncManager. | 97 // lifetime of SessionSyncManager. |
| 80 SessionsSyncManager::SessionsSyncManager( | 98 SessionsSyncManager::SessionsSyncManager( |
| 81 sync_sessions::SyncSessionsClient* sessions_client, | 99 sync_sessions::SyncSessionsClient* sessions_client, |
| 82 syncer::SyncPrefs* sync_prefs, | 100 syncer::SyncPrefs* sync_prefs, |
| 83 LocalDeviceInfoProvider* local_device, | 101 LocalDeviceInfoProvider* local_device, |
| 84 std::unique_ptr<LocalSessionEventRouter> router, | 102 std::unique_ptr<LocalSessionEventRouter> router, |
| 85 const base::Closure& sessions_updated_callback, | 103 const base::Closure& sessions_updated_callback, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 110 return machine_tag; | 128 return machine_tag; |
| 111 } | 129 } |
| 112 | 130 |
| 113 syncer::SyncMergeResult SessionsSyncManager::MergeDataAndStartSyncing( | 131 syncer::SyncMergeResult SessionsSyncManager::MergeDataAndStartSyncing( |
| 114 syncer::ModelType type, | 132 syncer::ModelType type, |
| 115 const syncer::SyncDataList& initial_sync_data, | 133 const syncer::SyncDataList& initial_sync_data, |
| 116 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, | 134 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 117 std::unique_ptr<syncer::SyncErrorFactory> error_handler) { | 135 std::unique_ptr<syncer::SyncErrorFactory> error_handler) { |
| 118 syncer::SyncMergeResult merge_result(type); | 136 syncer::SyncMergeResult merge_result(type); |
| 119 DCHECK(session_tracker_.Empty()); | 137 DCHECK(session_tracker_.Empty()); |
| 120 DCHECK_EQ(0U, local_tab_pool_.Capacity()); | |
| 121 | 138 |
| 122 error_handler_ = std::move(error_handler); | 139 error_handler_ = std::move(error_handler); |
| 123 sync_processor_ = std::move(sync_processor); | 140 sync_processor_ = std::move(sync_processor); |
| 124 | 141 |
| 125 // SessionDataTypeController ensures that the local device info | 142 // SessionDataTypeController ensures that the local device info |
| 126 // is available before activating this datatype. | 143 // is available before activating this datatype. |
| 127 DCHECK(local_device_); | 144 DCHECK(local_device_); |
| 128 const DeviceInfo* local_device_info = local_device_->GetLocalDeviceInfo(); | 145 const DeviceInfo* local_device_info = local_device_->GetLocalDeviceInfo(); |
| 129 if (!local_device_info) { | 146 if (!local_device_info) { |
| 130 merge_result.set_error(error_handler_->CreateAndUploadError( | 147 merge_result.set_error(error_handler_->CreateAndUploadError( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 146 | 163 |
| 147 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; | 164 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; |
| 148 | 165 |
| 149 // Make sure we have a machine tag. We do this now (versus earlier) as it's | 166 // Make sure we have a machine tag. We do this now (versus earlier) as it's |
| 150 // a conveniently safe time to assert sync is ready and the cache_guid is | 167 // a conveniently safe time to assert sync is ready and the cache_guid is |
| 151 // initialized. | 168 // initialized. |
| 152 if (current_machine_tag_.empty()) { | 169 if (current_machine_tag_.empty()) { |
| 153 InitializeCurrentMachineTag(local_device_->GetLocalSyncCacheGUID()); | 170 InitializeCurrentMachineTag(local_device_->GetLocalSyncCacheGUID()); |
| 154 } | 171 } |
| 155 | 172 |
| 156 session_tracker_.SetLocalSessionTag(current_machine_tag_); | 173 session_tracker_.SetLocalSessionTag(current_machine_tag()); |
| 157 | 174 |
| 158 syncer::SyncChangeList new_changes; | 175 syncer::SyncChangeList new_changes; |
| 159 | 176 |
| 160 // First, we iterate over sync data to update our session_tracker_. | 177 // First, we iterate over sync data to update our session_tracker_. |
| 161 syncer::SyncDataList restored_tabs; | 178 if (!InitFromSyncModel(initial_sync_data, &new_changes)) { |
| 162 if (!InitFromSyncModel(initial_sync_data, &restored_tabs, &new_changes)) { | |
| 163 // The sync db didn't have a header node for us. Create one. | 179 // The sync db didn't have a header node for us. Create one. |
| 164 sync_pb::EntitySpecifics specifics; | 180 sync_pb::EntitySpecifics specifics; |
| 165 sync_pb::SessionSpecifics* base_specifics = specifics.mutable_session(); | 181 sync_pb::SessionSpecifics* base_specifics = specifics.mutable_session(); |
| 166 base_specifics->set_session_tag(current_machine_tag()); | 182 base_specifics->set_session_tag(current_machine_tag()); |
| 167 sync_pb::SessionHeader* header_s = base_specifics->mutable_header(); | 183 sync_pb::SessionHeader* header_s = base_specifics->mutable_header(); |
| 168 header_s->set_client_name(current_session_name_); | 184 header_s->set_client_name(current_session_name_); |
| 169 header_s->set_device_type(current_device_type_); | 185 header_s->set_device_type(current_device_type_); |
| 170 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 186 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 171 current_machine_tag(), current_session_name_, specifics); | 187 current_machine_tag(), current_session_name_, specifics); |
| 172 new_changes.push_back( | 188 new_changes.push_back( |
| 173 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_ADD, data)); | 189 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_ADD, data)); |
| 174 } | 190 } |
| 175 | 191 |
| 176 #if defined(OS_ANDROID) | 192 #if defined(OS_ANDROID) |
| 177 std::string sync_machine_tag( | 193 std::string sync_machine_tag( |
| 178 BuildMachineTag(local_device_->GetLocalSyncCacheGUID())); | 194 BuildMachineTag(local_device_->GetLocalSyncCacheGUID())); |
| 179 if (current_machine_tag_.compare(sync_machine_tag) != 0) | 195 if (current_machine_tag().compare(sync_machine_tag) != 0) |
| 180 DeleteForeignSessionInternal(sync_machine_tag, &new_changes); | 196 DeleteForeignSessionInternal(sync_machine_tag, &new_changes); |
| 181 #endif | 197 #endif |
| 182 | 198 |
| 183 // Check if anything has changed on the local client side. | 199 // Check if anything has changed on the local client side. |
| 184 AssociateWindows(RELOAD_TABS, restored_tabs, &new_changes); | 200 AssociateWindows(RELOAD_TABS, &new_changes); |
| 185 local_tab_pool_out_of_sync_ = false; | 201 local_tab_pool_out_of_sync_ = false; |
| 186 | 202 |
| 187 merge_result.set_error( | 203 merge_result.set_error( |
| 188 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); | 204 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); |
| 189 | 205 |
| 190 local_event_router_->StartRoutingTo(this); | 206 local_event_router_->StartRoutingTo(this); |
| 191 return merge_result; | 207 return merge_result; |
| 192 } | 208 } |
| 193 | 209 |
| 194 void SessionsSyncManager::AssociateWindows( | 210 void SessionsSyncManager::AssociateWindows( |
| 195 ReloadTabsOption option, | 211 ReloadTabsOption option, |
| 196 const syncer::SyncDataList& restored_tabs, | |
| 197 syncer::SyncChangeList* change_output) { | 212 syncer::SyncChangeList* change_output) { |
| 198 const std::string local_tag = current_machine_tag(); | 213 const std::string local_tag = current_machine_tag(); |
| 199 sync_pb::SessionSpecifics specifics; | 214 sync_pb::SessionSpecifics specifics; |
| 200 specifics.set_session_tag(local_tag); | 215 specifics.set_session_tag(local_tag); |
| 201 sync_pb::SessionHeader* header_s = specifics.mutable_header(); | 216 sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
| 202 SyncedSession* current_session = session_tracker_.GetSession(local_tag); | 217 SyncedSession* current_session = session_tracker_.GetSession(local_tag); |
| 203 current_session->modified_time = base::Time::Now(); | 218 current_session->modified_time = base::Time::Now(); |
| 204 header_s->set_client_name(current_session_name_); | 219 header_s->set_client_name(current_session_name_); |
| 205 header_s->set_device_type(current_device_type_); | 220 header_s->set_device_type(current_device_type_); |
| 206 | 221 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 270 |
| 256 bool found_tabs = false; | 271 bool found_tabs = false; |
| 257 for (int j = 0; j < (*i)->GetTabCount(); ++j) { | 272 for (int j = 0; j < (*i)->GetTabCount(); ++j) { |
| 258 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); | 273 SessionID::id_type tab_id = (*i)->GetTabIdAt(j); |
| 259 SyncedTabDelegate* synced_tab = (*i)->GetTabAt(j); | 274 SyncedTabDelegate* synced_tab = (*i)->GetTabAt(j); |
| 260 | 275 |
| 261 // GetTabAt can return a null tab; in that case just skip it. | 276 // GetTabAt can return a null tab; in that case just skip it. |
| 262 if (!synced_tab) | 277 if (!synced_tab) |
| 263 continue; | 278 continue; |
| 264 | 279 |
| 280 // Placeholder tabs are those without WebContents, either because they |
| 281 // were never loaded into memory or they were evicted from memory |
| 282 // (typically only on Android devices). They only have a tab id, window |
| 283 // id, and a saved synced id (corresponding to the tab node id). Note |
| 284 // that only placeholders have this sync id, as it's necessary to |
| 285 // properly reassociate the tab with the entity that was backing it. |
| 265 if (synced_tab->IsPlaceholderTab()) { | 286 if (synced_tab->IsPlaceholderTab()) { |
| 266 // For tabs without WebContents update the |tab_id| and |window_id|, | 287 // For tabs without WebContents update the |tab_id| and |window_id|, |
| 267 // as it could have changed after a session restore. | 288 // as it could have changed after a session restore. |
| 268 // Note: We cannot check if a tab is valid if it has no WebContents. | 289 // Note: We cannot check if a tab is valid if it has no WebContents. |
| 269 // We assume any such tab is valid and leave the contents of | 290 // We assume any such tab is valid and leave the contents of |
| 270 // corresponding sync node unchanged. | 291 // corresponding sync node unchanged. |
| 271 if (synced_tab->GetSyncId() > TabNodePool::kInvalidTabNodeID && | 292 if (synced_tab->GetSyncId() > TabNodePool::kInvalidTabNodeID && |
| 272 tab_id > TabNodePool::kInvalidTabID) { | 293 tab_id > TabNodePool::kInvalidTabID) { |
| 273 AssociateRestoredPlaceholderTab(*synced_tab, tab_id, window_id, | 294 AssociateRestoredPlaceholderTab(*synced_tab, tab_id, window_id, |
| 274 restored_tabs, change_output); | 295 change_output); |
| 275 found_tabs = true; | 296 found_tabs = true; |
| 276 window_s.add_tab(tab_id); | 297 window_s.add_tab(tab_id); |
| 277 } | 298 } |
| 278 continue; | 299 continue; |
| 279 } | 300 } |
| 280 | 301 |
| 281 if (RELOAD_TABS == option) | 302 if (RELOAD_TABS == option) |
| 282 AssociateTab(synced_tab, change_output); | 303 AssociateTab(synced_tab, change_output); |
| 283 | 304 |
| 284 // If the tab is valid, it would have been added to the tracker either | 305 // If the tab is valid, it would have been added to the tracker either |
| (...skipping 12 matching lines...) Expand all Loading... |
| 297 *header_window = window_s; | 318 *header_window = window_s; |
| 298 | 319 |
| 299 // Update this window's representation in the synced session tracker. | 320 // Update this window's representation in the synced session tracker. |
| 300 session_tracker_.PutWindowInSession(local_tag, window_id); | 321 session_tracker_.PutWindowInSession(local_tag, window_id); |
| 301 BuildSyncedSessionFromSpecifics( | 322 BuildSyncedSessionFromSpecifics( |
| 302 local_tag, window_s, current_session->modified_time, | 323 local_tag, window_s, current_session->modified_time, |
| 303 current_session->windows[window_id].get()); | 324 current_session->windows[window_id].get()); |
| 304 } | 325 } |
| 305 } | 326 } |
| 306 } | 327 } |
| 307 local_tab_pool_.DeleteUnassociatedTabNodes(change_output); | 328 std::set<int> deleted_tab_node_ids; |
| 308 session_tracker_.CleanupSession(local_tag); | 329 session_tracker_.CleanupLocalTabs(&deleted_tab_node_ids); |
| 330 for (int tab_node_id : deleted_tab_node_ids) { |
| 331 std::string tab_node_tag = |
| 332 TabNodeIdToTag(current_machine_tag(), tab_node_id); |
| 333 change_output->push_back(syncer::SyncChange( |
| 334 FROM_HERE, syncer::SyncChange::ACTION_DELETE, |
| 335 syncer::SyncData::CreateLocalDelete(tab_node_tag, syncer::SESSIONS))); |
| 336 } |
| 309 | 337 |
| 310 // Always update the header. Sync takes care of dropping this update | 338 // Always update the header. Sync takes care of dropping this update |
| 311 // if the entity specifics are identical (i.e windows, client name did | 339 // if the entity specifics are identical (i.e windows, client name did |
| 312 // not change). | 340 // not change). |
| 313 sync_pb::EntitySpecifics entity; | 341 sync_pb::EntitySpecifics entity; |
| 314 entity.mutable_session()->CopyFrom(specifics); | 342 entity.mutable_session()->CopyFrom(specifics); |
| 315 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 343 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 316 current_machine_tag(), current_session_name_, entity); | 344 current_machine_tag(), current_session_name_, entity); |
| 317 change_output->push_back( | 345 change_output->push_back( |
| 318 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | 346 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
| 319 } | 347 } |
| 320 | 348 |
| 321 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab, | 349 void SessionsSyncManager::AssociateTab(SyncedTabDelegate* const tab_delegate, |
| 322 syncer::SyncChangeList* change_output) { | 350 syncer::SyncChangeList* change_output) { |
| 323 DCHECK(!tab->IsPlaceholderTab()); | 351 DCHECK(!tab_delegate->IsPlaceholderTab()); |
| 324 SessionID::id_type tab_id = tab->GetSessionId(); | |
| 325 | 352 |
| 326 if (tab->IsBeingDestroyed()) { | 353 if (tab_delegate->IsBeingDestroyed()) { |
| 327 // This tab is closing. | 354 // Do nothing. By not proactively adding the tab to the session, it will be |
| 328 TabLinksMap::iterator tab_iter = local_tab_map_.find(tab_id); | 355 // removed if necessary during subsequent cleanup. |
| 329 if (tab_iter == local_tab_map_.end()) { | |
| 330 // We aren't tracking this tab (for example, sync setting page). | |
| 331 return; | |
| 332 } | |
| 333 local_tab_pool_.FreeTabNode(tab_iter->second->tab_node_id(), change_output); | |
| 334 local_tab_map_.erase(tab_iter); | |
| 335 return; | 356 return; |
| 336 } | 357 } |
| 337 | 358 |
| 338 if (!tab->ShouldSync(sessions_client_)) | 359 if (!tab_delegate->ShouldSync(sessions_client_)) |
| 339 return; | 360 return; |
| 340 | 361 |
| 341 TabLinksMap::iterator local_tab_map_iter = local_tab_map_.find(tab_id); | 362 SessionID::id_type tab_id = tab_delegate->GetSessionId(); |
| 342 TabLink* tab_link = nullptr; | 363 DVLOG(1) << "Syncing tab " << tab_id << " from window " |
| 364 << tab_delegate->GetWindowId(); |
| 343 | 365 |
| 344 if (local_tab_map_iter == local_tab_map_.end()) { | 366 int tab_node_id = TabNodePool::kInvalidTabNodeID; |
| 345 int tab_node_id = tab->GetSyncId(); | 367 bool existing_tab_node = |
| 346 // If there is an old sync node for the tab, reuse it. If this is a new | 368 session_tracker_.GetTabNodeForLocalTab(tab_id, &tab_node_id); |
| 347 // tab, get a sync node for it. | 369 DCHECK_NE(TabNodePool::kInvalidTabNodeID, tab_node_id); |
| 348 if (!local_tab_pool_.IsUnassociatedTabNode(tab_node_id)) { | 370 sessions::SessionTab* session_tab = |
| 349 tab_node_id = local_tab_pool_.GetFreeTabNode(change_output); | 371 session_tracker_.GetTab(current_machine_tag(), tab_id); |
| 350 tab->SetSyncId(tab_node_id); | |
| 351 } | |
| 352 local_tab_pool_.AssociateTabNode(tab_node_id, tab_id); | |
| 353 tab_link = new TabLink(tab_node_id, tab); | |
| 354 local_tab_map_[tab_id] = make_linked_ptr<TabLink>(tab_link); | |
| 355 } else { | |
| 356 // 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 | |
| 358 // the tab link is up to date. | |
| 359 tab_link = local_tab_map_iter->second.get(); | |
| 360 local_tab_map_iter->second->set_tab(tab); | |
| 361 } | |
| 362 DCHECK(tab_link); | |
| 363 DCHECK_NE(tab_link->tab_node_id(), TabNodePool::kInvalidTabNodeID); | |
| 364 DVLOG(1) << "Reloading tab " << tab_id << " from window " | |
| 365 << tab->GetWindowId(); | |
| 366 | 372 |
| 367 // Write to sync model. | 373 // Get the previously synced url. |
| 368 sync_pb::EntitySpecifics specifics; | 374 int old_index = session_tab->normalized_navigation_index(); |
| 369 LocalTabDelegateToSpecifics(*tab, specifics.mutable_session()); | 375 GURL old_url; |
| 370 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 376 if (session_tab->navigations.size() > static_cast<size_t>(old_index)) |
| 371 TabNodePool::TabIdToTag(current_machine_tag_, tab_link->tab_node_id()), | 377 old_url = session_tab->navigations[old_index].virtual_url(); |
| 372 current_session_name_, specifics); | |
| 373 change_output->push_back( | |
| 374 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | |
| 375 | 378 |
| 376 int current_index = tab->GetCurrentEntryIndex(); | 379 // Update the tracker's session representation. |
| 377 const GURL new_url = tab->GetVirtualURLAtIndex(current_index); | 380 SetSessionTabFromDelegate(*tab_delegate, base::Time::Now(), session_tab); |
| 378 if (new_url != tab_link->url()) { | 381 SetVariationIds(session_tab); |
| 379 tab_link->set_url(new_url); | |
| 380 favicon_cache_.OnFaviconVisited(new_url, | |
| 381 tab->GetFaviconURLAtIndex(current_index)); | |
| 382 page_revisit_broadcaster_.OnPageVisit( | |
| 383 new_url, tab->GetTransitionAtIndex(current_index)); | |
| 384 } | |
| 385 | |
| 386 session_tracker_.GetSession(current_machine_tag())->modified_time = | 382 session_tracker_.GetSession(current_machine_tag())->modified_time = |
| 387 base::Time::Now(); | 383 base::Time::Now(); |
| 384 |
| 385 // Write to the sync model itself. |
| 386 sync_pb::EntitySpecifics specifics; |
| 387 specifics.mutable_session()->CopyFrom( |
| 388 SessionTabToSpecifics(*session_tab, current_machine_tag(), tab_node_id)); |
| 389 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 390 TabNodeIdToTag(current_machine_tag(), tab_node_id), current_session_name_, |
| 391 specifics); |
| 392 change_output->push_back(syncer::SyncChange( |
| 393 FROM_HERE, existing_tab_node ? syncer::SyncChange::ACTION_UPDATE |
| 394 : syncer::SyncChange::ACTION_ADD, |
| 395 data)); |
| 396 |
| 397 int current_index = tab_delegate->GetCurrentEntryIndex(); |
| 398 const GURL new_url = tab_delegate->GetVirtualURLAtIndex(current_index); |
| 399 if (new_url != old_url) { |
| 400 favicon_cache_.OnFaviconVisited( |
| 401 new_url, tab_delegate->GetFaviconURLAtIndex(current_index)); |
| 402 page_revisit_broadcaster_.OnPageVisit( |
| 403 new_url, tab_delegate->GetTransitionAtIndex(current_index)); |
| 404 } |
| 388 } | 405 } |
| 389 | 406 |
| 390 bool SessionsSyncManager::RebuildAssociations() { | 407 bool SessionsSyncManager::RebuildAssociations() { |
| 391 syncer::SyncDataList data(sync_processor_->GetAllSyncData(syncer::SESSIONS)); | 408 syncer::SyncDataList data(sync_processor_->GetAllSyncData(syncer::SESSIONS)); |
| 392 std::unique_ptr<syncer::SyncErrorFactory> error_handler( | 409 std::unique_ptr<syncer::SyncErrorFactory> error_handler( |
| 393 std::move(error_handler_)); | 410 std::move(error_handler_)); |
| 394 std::unique_ptr<syncer::SyncChangeProcessor> processor( | 411 std::unique_ptr<syncer::SyncChangeProcessor> processor( |
| 395 std::move(sync_processor_)); | 412 std::move(sync_processor_)); |
| 396 | 413 |
| 397 StopSyncing(syncer::SESSIONS); | 414 StopSyncing(syncer::SESSIONS); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 DCHECK(!rebuild_association_succeeded || !local_tab_pool_out_of_sync_); | 454 DCHECK(!rebuild_association_succeeded || !local_tab_pool_out_of_sync_); |
| 438 return; | 455 return; |
| 439 } | 456 } |
| 440 | 457 |
| 441 syncer::SyncChangeList changes; | 458 syncer::SyncChangeList changes; |
| 442 AssociateTab(modified_tab, &changes); | 459 AssociateTab(modified_tab, &changes); |
| 443 // Note, we always associate windows because it's possible a tab became | 460 // Note, we always associate windows because it's possible a tab became |
| 444 // "interesting" by going to a valid URL, in which case it needs to be added | 461 // "interesting" by going to a valid URL, in which case it needs to be added |
| 445 // to the window's tab information. Similarly, if a tab became | 462 // to the window's tab information. Similarly, if a tab became |
| 446 // "uninteresting", we remove it from the window's tab information. | 463 // "uninteresting", we remove it from the window's tab information. |
| 447 AssociateWindows(DONT_RELOAD_TABS, syncer::SyncDataList(), &changes); | 464 AssociateWindows(DONT_RELOAD_TABS, &changes); |
| 448 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 465 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 449 } | 466 } |
| 450 | 467 |
| 451 void SessionsSyncManager::OnFaviconsChanged(const std::set<GURL>& page_urls, | 468 void SessionsSyncManager::OnFaviconsChanged(const std::set<GURL>& page_urls, |
| 452 const GURL& /* icon_url */) { | 469 const GURL& /* icon_url */) { |
| 453 // TODO(zea): consider a separate container for tabs with outstanding favicon | 470 for (const GURL& page_url : page_urls) |
| 454 // loads so we don't have to iterate through all tabs comparing urls. | 471 favicon_cache_.OnPageFaviconUpdated(page_url); |
| 455 for (const GURL& page_url : page_urls) { | |
| 456 for (TabLinksMap::iterator tab_iter = local_tab_map_.begin(); | |
| 457 tab_iter != local_tab_map_.end(); ++tab_iter) { | |
| 458 if (tab_iter->second->url() == page_url) | |
| 459 favicon_cache_.OnPageFaviconUpdated(page_url); | |
| 460 } | |
| 461 } | |
| 462 } | 472 } |
| 463 | 473 |
| 464 void SessionsSyncManager::StopSyncing(syncer::ModelType type) { | 474 void SessionsSyncManager::StopSyncing(syncer::ModelType type) { |
| 465 local_event_router_->Stop(); | 475 local_event_router_->Stop(); |
| 466 if (sync_processor_.get() && lost_navigations_recorder_.get()) { | 476 if (sync_processor_.get() && lost_navigations_recorder_.get()) { |
| 467 sync_processor_->RemoveLocalChangeObserver( | 477 sync_processor_->RemoveLocalChangeObserver( |
| 468 lost_navigations_recorder_.get()); | 478 lost_navigations_recorder_.get()); |
| 469 lost_navigations_recorder_.reset(); | 479 lost_navigations_recorder_.reset(); |
| 470 } | 480 } |
| 471 sync_processor_.reset(nullptr); | 481 sync_processor_.reset(nullptr); |
| 472 error_handler_.reset(); | 482 error_handler_.reset(); |
| 473 session_tracker_.Clear(); | 483 session_tracker_.Clear(); |
| 474 local_tab_map_.clear(); | |
| 475 local_tab_pool_.Clear(); | |
| 476 current_machine_tag_.clear(); | 484 current_machine_tag_.clear(); |
| 477 current_session_name_.clear(); | 485 current_session_name_.clear(); |
| 478 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; | 486 local_session_header_node_id_ = TabNodePool::kInvalidTabNodeID; |
| 479 } | 487 } |
| 480 | 488 |
| 481 syncer::SyncDataList SessionsSyncManager::GetAllSyncData( | 489 syncer::SyncDataList SessionsSyncManager::GetAllSyncData( |
| 482 syncer::ModelType type) const { | 490 syncer::ModelType type) const { |
| 483 syncer::SyncDataList list; | 491 syncer::SyncDataList list; |
| 484 const SyncedSession* session = nullptr; | 492 const SyncedSession* session = nullptr; |
| 485 if (!session_tracker_.LookupLocalSession(&session)) | 493 if (!session_tracker_.LookupLocalSession(&session)) |
| 486 return syncer::SyncDataList(); | 494 return syncer::SyncDataList(); |
| 487 | 495 |
| 488 // First construct the header node. | 496 // First construct the header node. |
| 489 sync_pb::EntitySpecifics header_entity; | 497 sync_pb::EntitySpecifics header_entity; |
| 490 header_entity.mutable_session()->set_session_tag(current_machine_tag()); | 498 header_entity.mutable_session()->set_session_tag(current_machine_tag()); |
| 491 sync_pb::SessionHeader* header_specifics = | 499 sync_pb::SessionHeader* header_specifics = |
| 492 header_entity.mutable_session()->mutable_header(); | 500 header_entity.mutable_session()->mutable_header(); |
| 493 header_specifics->MergeFrom(session->ToSessionHeader()); | 501 header_specifics->MergeFrom(session->ToSessionHeader()); |
| 494 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 502 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 495 current_machine_tag(), current_session_name_, header_entity); | 503 current_machine_tag(), current_session_name_, header_entity); |
| 496 list.push_back(data); | 504 list.push_back(data); |
| 497 | 505 |
| 498 for (auto win_iter = session->windows.begin(); | 506 for (auto& win_iter : session->windows) { |
| 499 win_iter != session->windows.end(); ++win_iter) { | 507 for (auto& tab : win_iter.second->tabs) { |
| 500 for (auto tabs_iter = win_iter->second->tabs.begin(); | 508 // TODO(zea): replace with with the correct tab node id once there's a |
| 501 tabs_iter != win_iter->second->tabs.end(); ++tabs_iter) { | 509 // sync specific wrapper for SessionTab. This method is only used in |
| 510 // tests though, so it's fine for now. crbug.com/662597 |
| 511 int tab_node_id = 0; |
| 502 sync_pb::EntitySpecifics entity; | 512 sync_pb::EntitySpecifics entity; |
| 503 sync_pb::SessionSpecifics* specifics = entity.mutable_session(); | 513 entity.mutable_session()->CopyFrom( |
| 504 specifics->mutable_tab()->MergeFrom((*tabs_iter)->ToSyncData()); | 514 SessionTabToSpecifics(*tab, current_machine_tag(), tab_node_id)); |
| 505 specifics->set_session_tag(current_machine_tag_); | |
| 506 | |
| 507 TabLinksMap::const_iterator tab_map_iter = | |
| 508 local_tab_map_.find((*tabs_iter)->tab_id.id()); | |
| 509 DCHECK(tab_map_iter != local_tab_map_.end()); | |
| 510 specifics->set_tab_node_id(tab_map_iter->second->tab_node_id()); | |
| 511 syncer::SyncData data = syncer::SyncData::CreateLocalData( | 515 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 512 TabNodePool::TabIdToTag(current_machine_tag_, | 516 TabNodeIdToTag(current_machine_tag(), tab_node_id), |
| 513 specifics->tab_node_id()), | |
| 514 current_session_name_, entity); | 517 current_session_name_, entity); |
| 515 list.push_back(data); | 518 list.push_back(data); |
| 516 } | 519 } |
| 517 } | 520 } |
| 518 return list; | 521 return list; |
| 519 } | 522 } |
| 520 | 523 |
| 521 bool SessionsSyncManager::GetLocalSession(const SyncedSession** local_session) { | 524 bool SessionsSyncManager::GetLocalSession(const SyncedSession** local_session) { |
| 522 if (current_machine_tag_.empty()) | 525 if (current_machine_tag().empty()) |
| 523 return false; | 526 return false; |
| 524 *local_session = session_tracker_.GetSession(current_machine_tag()); | 527 *local_session = session_tracker_.GetSession(current_machine_tag()); |
| 525 return true; | 528 return true; |
| 526 } | 529 } |
| 527 | 530 |
| 528 syncer::SyncError SessionsSyncManager::ProcessSyncChanges( | 531 syncer::SyncError SessionsSyncManager::ProcessSyncChanges( |
| 529 const tracked_objects::Location& from_here, | 532 const tracked_objects::Location& from_here, |
| 530 const syncer::SyncChangeList& change_list) { | 533 const syncer::SyncChangeList& change_list) { |
| 531 if (!sync_processor_.get()) { | 534 if (!sync_processor_.get()) { |
| 532 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 535 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 break; | 575 break; |
| 573 case syncer::SyncChange::ACTION_ADD: | 576 case syncer::SyncChange::ACTION_ADD: |
| 574 case syncer::SyncChange::ACTION_UPDATE: | 577 case syncer::SyncChange::ACTION_UPDATE: |
| 575 if (current_machine_tag() == session.session_tag()) { | 578 if (current_machine_tag() == session.session_tag()) { |
| 576 // We should only ever receive a change to our own machine's session | 579 // We should only ever receive a change to our own machine's session |
| 577 // info if encryption was turned on. In that case, the data is still | 580 // info if encryption was turned on. In that case, the data is still |
| 578 // the same, so we can ignore. | 581 // the same, so we can ignore. |
| 579 LOG(WARNING) << "Dropping modification to local session."; | 582 LOG(WARNING) << "Dropping modification to local session."; |
| 580 return syncer::SyncError(); | 583 return syncer::SyncError(); |
| 581 } | 584 } |
| 582 UpdateTrackerWithForeignSession( | 585 UpdateTrackerWithSpecifics( |
| 583 session, syncer::SyncDataRemote(it->sync_data()).GetModifiedTime()); | 586 session, syncer::SyncDataRemote(it->sync_data()).GetModifiedTime()); |
| 584 break; | 587 break; |
| 585 default: | 588 default: |
| 586 NOTREACHED() << "Processing sync changes failed, unknown change type."; | 589 NOTREACHED() << "Processing sync changes failed, unknown change type."; |
| 587 } | 590 } |
| 588 } | 591 } |
| 589 | 592 |
| 590 if (!sessions_updated_callback_.is_null()) | 593 if (!sessions_updated_callback_.is_null()) |
| 591 sessions_updated_callback_.Run(); | 594 sessions_updated_callback_.Run(); |
| 592 return syncer::SyncError(); | 595 return syncer::SyncError(); |
| 593 } | 596 } |
| 594 | 597 |
| 595 syncer::SyncChange SessionsSyncManager::TombstoneTab( | 598 syncer::SyncChange SessionsSyncManager::TombstoneTab( |
| 596 const sync_pb::SessionSpecifics& tab) { | 599 const sync_pb::SessionSpecifics& tab) { |
| 597 if (!tab.has_tab_node_id()) { | 600 if (!tab.has_tab_node_id()) { |
| 598 LOG(WARNING) << "Old sessions node without tab node id; can't tombstone."; | 601 LOG(WARNING) << "Old sessions node without tab node id; can't tombstone."; |
| 599 return syncer::SyncChange(); | 602 return syncer::SyncChange(); |
| 600 } else { | 603 } else { |
| 601 return syncer::SyncChange( | 604 return syncer::SyncChange( |
| 602 FROM_HERE, SyncChange::ACTION_DELETE, | 605 FROM_HERE, SyncChange::ACTION_DELETE, |
| 603 SyncData::CreateLocalDelete( | 606 SyncData::CreateLocalDelete( |
| 604 TabNodePool::TabIdToTag(current_machine_tag(), tab.tab_node_id()), | 607 TabNodeIdToTag(current_machine_tag(), tab.tab_node_id()), |
| 605 syncer::SESSIONS)); | 608 syncer::SESSIONS)); |
| 606 } | 609 } |
| 607 } | 610 } |
| 608 | 611 |
| 609 bool SessionsSyncManager::GetAllForeignSessions( | 612 bool SessionsSyncManager::GetAllForeignSessions( |
| 610 std::vector<const SyncedSession*>* sessions) { | 613 std::vector<const SyncedSession*>* sessions) { |
| 611 if (!session_tracker_.LookupAllForeignSessions( | 614 if (!session_tracker_.LookupAllForeignSessions( |
| 612 sessions, SyncedSessionTracker::PRESENTABLE)) | 615 sessions, SyncedSessionTracker::PRESENTABLE)) |
| 613 return false; | 616 return false; |
| 614 std::sort(sessions->begin(), sessions->end(), SessionsRecencyComparator); | 617 std::sort(sessions->begin(), sessions->end(), SessionsRecencyComparator); |
| 615 return true; | 618 return true; |
| 616 } | 619 } |
| 617 | 620 |
| 618 bool SessionsSyncManager::InitFromSyncModel( | 621 bool SessionsSyncManager::InitFromSyncModel( |
| 619 const syncer::SyncDataList& sync_data, | 622 const syncer::SyncDataList& sync_data, |
| 620 syncer::SyncDataList* restored_tabs, | |
| 621 syncer::SyncChangeList* new_changes) { | 623 syncer::SyncChangeList* new_changes) { |
| 622 bool found_current_header = false; | 624 bool found_current_header = false; |
| 623 int bad_foreign_hash_count = 0; | 625 int bad_foreign_hash_count = 0; |
| 624 for (syncer::SyncDataList::const_iterator it = sync_data.begin(); | 626 for (syncer::SyncDataList::const_iterator it = sync_data.begin(); |
| 625 it != sync_data.end(); ++it) { | 627 it != sync_data.end(); ++it) { |
| 626 const syncer::SyncData& data = *it; | 628 const syncer::SyncData& data = *it; |
| 627 DCHECK(data.GetSpecifics().has_session()); | 629 DCHECK(data.GetSpecifics().has_session()); |
| 628 syncer::SyncDataRemote remote(data); | 630 syncer::SyncDataRemote remote(data); |
| 629 const sync_pb::SessionSpecifics& specifics = data.GetSpecifics().session(); | 631 const sync_pb::SessionSpecifics& specifics = data.GetSpecifics().session(); |
| 630 if (specifics.session_tag().empty() || | 632 if (specifics.session_tag().empty() || |
| 631 (specifics.has_tab() && | 633 (specifics.has_tab() && |
| 632 (!specifics.has_tab_node_id() || !specifics.tab().has_tab_id()))) { | 634 (!specifics.has_tab_node_id() || !specifics.tab().has_tab_id()))) { |
| 633 syncer::SyncChange tombstone(TombstoneTab(specifics)); | 635 syncer::SyncChange tombstone(TombstoneTab(specifics)); |
| 634 if (tombstone.IsValid()) | 636 if (tombstone.IsValid()) |
| 635 new_changes->push_back(tombstone); | 637 new_changes->push_back(tombstone); |
| 636 } else if (specifics.session_tag() != current_machine_tag()) { | 638 } else if (specifics.session_tag() != current_machine_tag()) { |
| 637 if (TagHashFromSpecifics(specifics) == remote.GetClientTagHash()) { | 639 if (TagHashFromSpecifics(specifics) == remote.GetClientTagHash()) { |
| 638 UpdateTrackerWithForeignSession(specifics, remote.GetModifiedTime()); | 640 UpdateTrackerWithSpecifics(specifics, remote.GetModifiedTime()); |
| 639 } else { | 641 } else { |
| 640 // In the past, like years ago, we believe that some session data was | 642 // In the past, like years ago, we believe that some session data was |
| 641 // created with bad tag hashes. This causes any change this client makes | 643 // created with bad tag hashes. This causes any change this client makes |
| 642 // to that foreign data (like deletion through garbage collection) to | 644 // to that foreign data (like deletion through garbage collection) to |
| 643 // trigger a data type error because the tag looking mechanism fails. So | 645 // trigger a data type error because the tag looking mechanism fails. So |
| 644 // look for these and delete via remote SyncData, which uses a server id | 646 // look for these and delete via remote SyncData, which uses a server id |
| 645 // lookup mechanism instead, see crbug.com/604657. | 647 // lookup mechanism instead, see crbug.com/604657. |
| 646 bad_foreign_hash_count++; | 648 bad_foreign_hash_count++; |
| 647 new_changes->push_back( | 649 new_changes->push_back( |
| 648 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_DELETE, remote)); | 650 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_DELETE, remote)); |
| 649 } | 651 } |
| 650 } else { | 652 } else { |
| 651 // This is previously stored local session information. | 653 // This is previously stored local session information. |
| 652 if (specifics.has_header() && !found_current_header) { | 654 if (specifics.has_header() && !found_current_header) { |
| 653 // This is our previous header node, reuse it. | 655 // This is our previous header node, reuse it. |
| 654 found_current_header = true; | 656 found_current_header = true; |
| 655 if (specifics.header().has_client_name()) | 657 if (specifics.header().has_client_name()) |
| 656 current_session_name_ = specifics.header().client_name(); | 658 current_session_name_ = specifics.header().client_name(); |
| 659 |
| 660 // TODO(zea): crbug.com/639009 update the tracker with the specifics |
| 661 // from the header node as well. This will be necessary to preserve |
| 662 // the set of open tabs when a custom tab is opened. |
| 657 } else { | 663 } else { |
| 658 if (specifics.has_header() || !specifics.has_tab()) { | 664 if (specifics.has_header() || !specifics.has_tab()) { |
| 659 LOG(WARNING) << "Found more than one session header node with local " | 665 LOG(WARNING) << "Found more than one session header node with local " |
| 660 << "tag."; | 666 << "tag."; |
| 661 syncer::SyncChange tombstone(TombstoneTab(specifics)); | 667 syncer::SyncChange tombstone(TombstoneTab(specifics)); |
| 662 if (tombstone.IsValid()) | 668 if (tombstone.IsValid()) |
| 663 new_changes->push_back(tombstone); | 669 new_changes->push_back(tombstone); |
| 664 } else { | 670 } else { |
| 665 // This is a valid old tab node, add it to the pool so it can be | 671 // This is a valid old tab node, add it to the tracker and associate |
| 666 // reused for reassociation. | 672 // it. |
| 667 local_tab_pool_.AddTabNode(specifics.tab_node_id()); | 673 DVLOG(1) << "Associating local tab " << specifics.tab().tab_id() |
| 668 restored_tabs->push_back(*it); | 674 << " with node " << specifics.tab_node_id(); |
| 675 session_tracker_.ReassociateLocalTab(specifics.tab_node_id(), |
| 676 specifics.tab().tab_id()); |
| 677 UpdateTrackerWithSpecifics(specifics, remote.GetModifiedTime()); |
| 669 } | 678 } |
| 670 } | 679 } |
| 671 } | 680 } |
| 672 } | 681 } |
| 673 | 682 |
| 674 // Cleanup all foreign sessions, since orphaned tabs may have been added after | 683 // Cleanup all foreign sessions, since orphaned tabs may have been added after |
| 675 // the header. | 684 // the header. |
| 676 std::vector<const SyncedSession*> sessions; | 685 std::vector<const SyncedSession*> sessions; |
| 677 session_tracker_.LookupAllForeignSessions(&sessions, | 686 session_tracker_.LookupAllForeignSessions(&sessions, |
| 678 SyncedSessionTracker::RAW); | 687 SyncedSessionTracker::RAW); |
| 679 for (const auto* session : sessions) { | 688 for (const auto* session : sessions) { |
| 680 session_tracker_.CleanupSession(session->session_tag); | 689 session_tracker_.CleanupForeignSession(session->session_tag); |
| 681 } | 690 } |
| 682 | 691 |
| 683 UMA_HISTOGRAM_COUNTS_100("Sync.SessionsBadForeignHashOnMergeCount", | 692 UMA_HISTOGRAM_COUNTS_100("Sync.SessionsBadForeignHashOnMergeCount", |
| 684 bad_foreign_hash_count); | 693 bad_foreign_hash_count); |
| 685 | 694 |
| 686 return found_current_header; | 695 return found_current_header; |
| 687 } | 696 } |
| 688 | 697 |
| 689 void SessionsSyncManager::UpdateTrackerWithForeignSession( | 698 void SessionsSyncManager::UpdateTrackerWithSpecifics( |
| 690 const sync_pb::SessionSpecifics& specifics, | 699 const sync_pb::SessionSpecifics& specifics, |
| 691 const base::Time& modification_time) { | 700 const base::Time& modification_time) { |
| 692 std::string foreign_session_tag = specifics.session_tag(); | 701 std::string session_tag = specifics.session_tag(); |
| 693 DCHECK_NE(foreign_session_tag, current_machine_tag()); | 702 SyncedSession* session = session_tracker_.GetSession(session_tag); |
| 694 | |
| 695 SyncedSession* foreign_session = | |
| 696 session_tracker_.GetSession(foreign_session_tag); | |
| 697 if (specifics.has_header()) { | 703 if (specifics.has_header()) { |
| 698 // Read in the header data for this foreign session. Header data is | 704 // Read in the header data for this session. Header data is |
| 699 // essentially a collection of windows, each of which has an ordered id list | 705 // essentially a collection of windows, each of which has an ordered id list |
| 700 // for their tabs. | 706 // for their tabs. |
| 701 | 707 |
| 702 if (!IsValidSessionHeader(specifics.header())) { | 708 if (!IsValidSessionHeader(specifics.header())) { |
| 703 LOG(WARNING) << "Ignoring foreign session node with invalid header " | 709 LOG(WARNING) << "Ignoring session node with invalid header " |
| 704 << "and tag " << foreign_session_tag << "."; | 710 << "and tag " << session_tag << "."; |
| 705 return; | 711 return; |
| 706 } | 712 } |
| 707 | 713 |
| 708 // Load (or create) the SyncedSession object for this client. | 714 // Load (or create) the SyncedSession object for this client. |
| 709 const sync_pb::SessionHeader& header = specifics.header(); | 715 const sync_pb::SessionHeader& header = specifics.header(); |
| 710 PopulateSessionHeaderFromSpecifics(header, modification_time, | 716 PopulateSessionHeaderFromSpecifics(header, modification_time, session); |
| 711 foreign_session); | |
| 712 | 717 |
| 713 // Reset the tab/window tracking for this session (must do this before | 718 // Reset the tab/window tracking for this session (must do this before |
| 714 // we start calling PutWindowInSession and PutTabInWindow so that all | 719 // we start calling PutWindowInSession and PutTabInWindow so that all |
| 715 // unused tabs/windows get cleared by the CleanupSession(...) call). | 720 // unused tabs/windows get cleared by the CleanupSession(...) call). |
| 716 session_tracker_.ResetSessionTracking(foreign_session_tag); | 721 session_tracker_.ResetSessionTracking(session_tag); |
| 717 | 722 |
| 718 // Process all the windows and their tab information. | 723 // Process all the windows and their tab information. |
| 719 int num_windows = header.window_size(); | 724 int num_windows = header.window_size(); |
| 720 DVLOG(1) << "Associating " << foreign_session_tag << " with " << num_windows | 725 DVLOG(1) << "Populating " << session_tag << " with " << num_windows |
| 721 << " windows."; | 726 << " windows."; |
| 722 | 727 |
| 723 for (int i = 0; i < num_windows; ++i) { | 728 for (int i = 0; i < num_windows; ++i) { |
| 724 const sync_pb::SessionWindow& window_s = header.window(i); | 729 const sync_pb::SessionWindow& window_s = header.window(i); |
| 725 SessionID::id_type window_id = window_s.window_id(); | 730 SessionID::id_type window_id = window_s.window_id(); |
| 726 session_tracker_.PutWindowInSession(foreign_session_tag, window_id); | 731 session_tracker_.PutWindowInSession(session_tag, window_id); |
| 727 BuildSyncedSessionFromSpecifics( | 732 BuildSyncedSessionFromSpecifics(session_tag, window_s, modification_time, |
| 728 foreign_session_tag, window_s, modification_time, | 733 session->windows[window_id].get()); |
| 729 foreign_session->windows[window_id].get()); | |
| 730 } | 734 } |
| 731 // Delete any closed windows and unused tabs as necessary. | 735 // Delete any closed windows and unused tabs as necessary. |
| 732 session_tracker_.CleanupSession(foreign_session_tag); | 736 session_tracker_.CleanupForeignSession(session_tag); |
| 733 } else if (specifics.has_tab()) { | 737 } else if (specifics.has_tab()) { |
| 734 const sync_pb::SessionTab& tab_s = specifics.tab(); | 738 const sync_pb::SessionTab& tab_s = specifics.tab(); |
| 735 SessionID::id_type tab_id = tab_s.tab_id(); | 739 SessionID::id_type tab_id = tab_s.tab_id(); |
| 740 DVLOG(1) << "Populating " << session_tag << "'s tab id " << tab_id |
| 741 << " from node " << specifics.tab_node_id(); |
| 736 | 742 |
| 737 const sessions::SessionTab* existing_tab; | 743 // Ensure the tracker is aware of the tab node id. Deleting foreign sessions |
| 738 if (session_tracker_.LookupSessionTab(foreign_session_tag, tab_id, | 744 // requires deleting all relevant tab nodes, and it's easier to track the |
| 739 &existing_tab) && | 745 // tab node ids themselves separately from the tab ids. |
| 740 existing_tab->timestamp > modification_time) { | 746 // |
| 741 // Force the tracker to remember this tab node id, even if it isn't | 747 // Note that TabIDs are not stable across restarts of a client. Consider |
| 742 // currently being used. | 748 // this example with two tabs: |
| 743 session_tracker_.GetTab(foreign_session_tag, tab_id, | 749 // |
| 744 specifics.tab_node_id()); | 750 // http://a.com TabID1 --> NodeIDA |
| 745 DVLOG(1) << "Ignoring " << foreign_session_tag << "'s session tab " | 751 // http://b.com TabID2 --> NodeIDB |
| 746 << tab_id << " with earlier modification time"; | 752 // |
| 753 // After restart, tab ids are reallocated. e.g, one possibility: |
| 754 // http://a.com TabID2 --> NodeIDA |
| 755 // http://b.com TabID1 --> NodeIDB |
| 756 // |
| 757 // If that happened on a remote client, here we will see an update to |
| 758 // TabID1 with tab_node_id changing from NodeIDA to NodeIDB, and TabID2 |
| 759 // with tab_node_id changing from NodeIDB to NodeIDA. |
| 760 // |
| 761 // We can also wind up here if we created this tab as an out-of-order |
| 762 // update to the header node for this session before actually associating |
| 763 // the tab itself, so the tab node id wasn't available at the time and |
| 764 // is currently kInvalidTabNodeID. |
| 765 // |
| 766 // In both cases, we can safely throw it into the set of node ids. |
| 767 session_tracker_.OnTabNodeSeen(session_tag, specifics.tab_node_id()); |
| 768 sessions::SessionTab* tab = session_tracker_.GetTab(session_tag, tab_id); |
| 769 if (tab->timestamp > modification_time) { |
| 770 DVLOG(1) << "Ignoring " << session_tag << "'s session tab " << tab_id |
| 771 << " with earlier modification time"; |
| 747 return; | 772 return; |
| 748 } | 773 } |
| 749 | 774 |
| 750 sessions::SessionTab* tab = session_tracker_.GetTab( | |
| 751 foreign_session_tag, tab_id, specifics.tab_node_id()); | |
| 752 | |
| 753 // Update SessionTab based on protobuf. | 775 // Update SessionTab based on protobuf. |
| 754 tab->SetFromSyncData(tab_s, modification_time); | 776 tab->SetFromSyncData(tab_s, modification_time); |
| 755 | 777 |
| 756 // If a favicon or favicon urls are present, load the URLs and visit | 778 // If a favicon or favicon urls are present, load the URLs and visit |
| 757 // times into the in-memory favicon cache. | 779 // times into the in-memory favicon cache. |
| 758 RefreshFaviconVisitTimesFromForeignTab(tab_s, modification_time); | 780 RefreshFaviconVisitTimesFromForeignTab(tab_s, modification_time); |
| 759 | 781 |
| 760 // Update the last modified time. | 782 // Update the last modified time. |
| 761 if (foreign_session->modified_time < modification_time) | 783 if (session->modified_time < modification_time) |
| 762 foreign_session->modified_time = modification_time; | 784 session->modified_time = modification_time; |
| 763 } else { | 785 } else { |
| 764 LOG(WARNING) << "Ignoring foreign session node with missing header/tab " | 786 LOG(WARNING) << "Ignoring session node with missing header/tab " |
| 765 << "fields and tag " << foreign_session_tag << "."; | 787 << "fields and tag " << session_tag << "."; |
| 766 } | 788 } |
| 767 } | 789 } |
| 768 | 790 |
| 769 void SessionsSyncManager::InitializeCurrentMachineTag( | 791 void SessionsSyncManager::InitializeCurrentMachineTag( |
| 770 const std::string& cache_guid) { | 792 const std::string& cache_guid) { |
| 771 DCHECK(current_machine_tag_.empty()); | 793 DCHECK(current_machine_tag_.empty()); |
| 772 std::string persisted_guid; | 794 std::string persisted_guid; |
| 773 persisted_guid = sync_prefs_->GetSyncSessionsGUID(); | 795 persisted_guid = sync_prefs_->GetSyncSessionsGUID(); |
| 774 if (!persisted_guid.empty()) { | 796 if (!persisted_guid.empty()) { |
| 775 current_machine_tag_ = persisted_guid; | 797 current_machine_tag_ = persisted_guid; |
| 776 DVLOG(1) << "Restoring persisted session sync guid: " << persisted_guid; | 798 DVLOG(1) << "Restoring persisted session sync guid: " << persisted_guid; |
| 777 } else { | 799 } else { |
| 778 DCHECK(!cache_guid.empty()); | 800 DCHECK(!cache_guid.empty()); |
| 779 current_machine_tag_ = BuildMachineTag(cache_guid); | 801 current_machine_tag_ = BuildMachineTag(cache_guid); |
| 780 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; | 802 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; |
| 781 sync_prefs_->SetSyncSessionsGUID(current_machine_tag_); | 803 sync_prefs_->SetSyncSessionsGUID(current_machine_tag_); |
| 782 } | 804 } |
| 783 | |
| 784 local_tab_pool_.SetMachineTag(current_machine_tag_); | |
| 785 } | 805 } |
| 786 | 806 |
| 787 // static | 807 // static |
| 788 void SessionsSyncManager::PopulateSessionHeaderFromSpecifics( | 808 void SessionsSyncManager::PopulateSessionHeaderFromSpecifics( |
| 789 const sync_pb::SessionHeader& header_specifics, | 809 const sync_pb::SessionHeader& header_specifics, |
| 790 base::Time mtime, | 810 base::Time mtime, |
| 791 SyncedSession* session_header) { | 811 SyncedSession* session_header) { |
| 792 if (header_specifics.has_client_name()) | 812 if (header_specifics.has_client_name()) |
| 793 session_header->session_name = header_specifics.client_name(); | 813 session_header->session_name = header_specifics.client_name(); |
| 794 if (header_specifics.has_device_type()) { | 814 if (header_specifics.has_device_type()) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 void SessionsSyncManager::DeleteForeignSessionInternal( | 904 void SessionsSyncManager::DeleteForeignSessionInternal( |
| 885 const std::string& tag, | 905 const std::string& tag, |
| 886 syncer::SyncChangeList* change_output) { | 906 syncer::SyncChangeList* change_output) { |
| 887 if (tag == current_machine_tag()) { | 907 if (tag == current_machine_tag()) { |
| 888 LOG(ERROR) << "Attempting to delete local session. This is not currently " | 908 LOG(ERROR) << "Attempting to delete local session. This is not currently " |
| 889 << "supported."; | 909 << "supported."; |
| 890 return; | 910 return; |
| 891 } | 911 } |
| 892 | 912 |
| 893 std::set<int> tab_node_ids_to_delete; | 913 std::set<int> tab_node_ids_to_delete; |
| 894 session_tracker_.LookupTabNodeIds(tag, &tab_node_ids_to_delete); | 914 session_tracker_.LookupForeignTabNodeIds(tag, &tab_node_ids_to_delete); |
| 895 if (DisassociateForeignSession(tag)) { | 915 if (DisassociateForeignSession(tag)) { |
| 896 // Only tell sync to delete the header if there was one. | 916 // Only tell sync to delete the header if there was one. |
| 897 change_output->push_back( | 917 change_output->push_back( |
| 898 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_DELETE, | 918 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_DELETE, |
| 899 SyncData::CreateLocalDelete(tag, syncer::SESSIONS))); | 919 SyncData::CreateLocalDelete(tag, syncer::SESSIONS))); |
| 900 } | 920 } |
| 901 for (std::set<int>::const_iterator it = tab_node_ids_to_delete.begin(); | 921 for (std::set<int>::const_iterator it = tab_node_ids_to_delete.begin(); |
| 902 it != tab_node_ids_to_delete.end(); ++it) { | 922 it != tab_node_ids_to_delete.end(); ++it) { |
| 903 change_output->push_back(syncer::SyncChange( | 923 change_output->push_back( |
| 904 FROM_HERE, SyncChange::ACTION_DELETE, | 924 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_DELETE, |
| 905 SyncData::CreateLocalDelete(TabNodePool::TabIdToTag(tag, *it), | 925 SyncData::CreateLocalDelete(TabNodeIdToTag(tag, *it), |
| 906 syncer::SESSIONS))); | 926 syncer::SESSIONS))); |
| 907 } | 927 } |
| 908 if (!sessions_updated_callback_.is_null()) | 928 if (!sessions_updated_callback_.is_null()) |
| 909 sessions_updated_callback_.Run(); | 929 sessions_updated_callback_.Run(); |
| 910 } | 930 } |
| 911 | 931 |
| 912 bool SessionsSyncManager::DisassociateForeignSession( | 932 bool SessionsSyncManager::DisassociateForeignSession( |
| 913 const std::string& foreign_session_tag) { | 933 const std::string& foreign_session_tag) { |
| 914 DCHECK_NE(foreign_session_tag, current_machine_tag()); | 934 DCHECK_NE(foreign_session_tag, current_machine_tag()); |
| 915 DVLOG(1) << "Disassociating session " << foreign_session_tag; | 935 DVLOG(1) << "Disassociating session " << foreign_session_tag; |
| 916 return session_tracker_.DeleteSession(foreign_session_tag); | 936 return session_tracker_.DeleteForeignSession(foreign_session_tag); |
| 917 } | 937 } |
| 918 | 938 |
| 919 bool SessionsSyncManager::GetForeignSession( | 939 bool SessionsSyncManager::GetForeignSession( |
| 920 const std::string& tag, | 940 const std::string& tag, |
| 921 std::vector<const sessions::SessionWindow*>* windows) { | 941 std::vector<const sessions::SessionWindow*>* windows) { |
| 922 return session_tracker_.LookupSessionWindows(tag, windows); | 942 return session_tracker_.LookupSessionWindows(tag, windows); |
| 923 } | 943 } |
| 924 | 944 |
| 925 bool SessionsSyncManager::GetForeignSessionTabs( | 945 bool SessionsSyncManager::GetForeignSessionTabs( |
| 926 const std::string& tag, | 946 const std::string& tag, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 952 bool SessionsSyncManager::GetForeignTab(const std::string& tag, | 972 bool SessionsSyncManager::GetForeignTab(const std::string& tag, |
| 953 const SessionID::id_type tab_id, | 973 const SessionID::id_type tab_id, |
| 954 const sessions::SessionTab** tab) { | 974 const sessions::SessionTab** tab) { |
| 955 const sessions::SessionTab* synced_tab = nullptr; | 975 const sessions::SessionTab* synced_tab = nullptr; |
| 956 bool success = session_tracker_.LookupSessionTab(tag, tab_id, &synced_tab); | 976 bool success = session_tracker_.LookupSessionTab(tag, tab_id, &synced_tab); |
| 957 if (success) | 977 if (success) |
| 958 *tab = synced_tab; | 978 *tab = synced_tab; |
| 959 return success; | 979 return success; |
| 960 } | 980 } |
| 961 | 981 |
| 962 void SessionsSyncManager::LocalTabDelegateToSpecifics( | |
| 963 const SyncedTabDelegate& tab_delegate, | |
| 964 sync_pb::SessionSpecifics* specifics) { | |
| 965 sessions::SessionTab* session_tab = nullptr; | |
| 966 session_tab = session_tracker_.GetTab(current_machine_tag(), | |
| 967 tab_delegate.GetSessionId(), | |
| 968 tab_delegate.GetSyncId()); | |
| 969 SetSessionTabFromDelegate(tab_delegate, base::Time::Now(), session_tab); | |
| 970 SetVariationIds(session_tab); | |
| 971 sync_pb::SessionTab tab_s = session_tab->ToSyncData(); | |
| 972 specifics->set_session_tag(current_machine_tag_); | |
| 973 specifics->set_tab_node_id(tab_delegate.GetSyncId()); | |
| 974 specifics->mutable_tab()->CopyFrom(tab_s); | |
| 975 } | |
| 976 | |
| 977 void SessionsSyncManager::AssociateRestoredPlaceholderTab( | 982 void SessionsSyncManager::AssociateRestoredPlaceholderTab( |
| 978 const SyncedTabDelegate& tab_delegate, | 983 const SyncedTabDelegate& tab_delegate, |
| 979 SessionID::id_type new_tab_id, | 984 SessionID::id_type new_tab_id, |
| 980 SessionID::id_type new_window_id, | 985 SessionID::id_type new_window_id, |
| 981 const syncer::SyncDataList& restored_tabs, | |
| 982 syncer::SyncChangeList* change_output) { | 986 syncer::SyncChangeList* change_output) { |
| 983 DCHECK_NE(tab_delegate.GetSyncId(), TabNodePool::kInvalidTabNodeID); | 987 DCHECK_NE(tab_delegate.GetSyncId(), TabNodePool::kInvalidTabNodeID); |
| 984 // Rewrite the tab using |restored_tabs| to retrieve the specifics. | |
| 985 if (restored_tabs.empty()) { | |
| 986 DLOG(WARNING) << "Can't Update tab ID."; | |
| 987 return; | |
| 988 } | |
| 989 | 988 |
| 990 for (syncer::SyncDataList::const_iterator it = restored_tabs.begin(); | 989 // Update tracker with the new association (and inform it of the tab node |
| 991 it != restored_tabs.end(); ++it) { | 990 // in the process). |
| 992 if (it->GetSpecifics().session().tab_node_id() != | 991 session_tracker_.ReassociateLocalTab(tab_delegate.GetSyncId(), new_tab_id); |
| 993 tab_delegate.GetSyncId()) { | |
| 994 continue; | |
| 995 } | |
| 996 | 992 |
| 997 sync_pb::EntitySpecifics entity; | 993 // Update the window id on the SessionTab itself. |
| 998 sync_pb::SessionSpecifics* specifics = entity.mutable_session(); | 994 sessions::SessionTab* local_tab = |
| 999 specifics->CopyFrom(it->GetSpecifics().session()); | 995 session_tracker_.GetTab(current_machine_tag(), new_tab_id); |
| 1000 DCHECK(specifics->has_tab()); | 996 local_tab->window_id.set_id(new_window_id); |
| 1001 | 997 |
| 1002 // Update tab node pool with the new association. | 998 // Rewrite the specifics based on the reassociated SessionTab to preserve |
| 1003 local_tab_pool_.ReassociateTabNode(tab_delegate.GetSyncId(), new_tab_id); | 999 // the new tab and window ids. |
| 1004 TabLink* tab_link = new TabLink(tab_delegate.GetSyncId(), &tab_delegate); | 1000 sync_pb::EntitySpecifics entity; |
| 1005 local_tab_map_[new_tab_id] = make_linked_ptr<TabLink>(tab_link); | 1001 entity.mutable_session()->CopyFrom(SessionTabToSpecifics( |
| 1006 | 1002 *local_tab, current_machine_tag(), tab_delegate.GetSyncId())); |
| 1007 if (specifics->tab().tab_id() == new_tab_id && | 1003 syncer::SyncData data = syncer::SyncData::CreateLocalData( |
| 1008 specifics->tab().window_id() == new_window_id) | 1004 TabNodeIdToTag(current_machine_tag(), tab_delegate.GetSyncId()), |
| 1009 return; | 1005 current_session_name_, entity); |
| 1010 | 1006 change_output->push_back( |
| 1011 // Either the tab_id or window_id changed (e.g due to session restore), so | 1007 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); |
| 1012 // update the sync node. | |
| 1013 specifics->mutable_tab()->set_tab_id(new_tab_id); | |
| 1014 specifics->mutable_tab()->set_window_id(new_window_id); | |
| 1015 syncer::SyncData data = syncer::SyncData::CreateLocalData( | |
| 1016 TabNodePool::TabIdToTag(current_machine_tag_, specifics->tab_node_id()), | |
| 1017 current_session_name_, entity); | |
| 1018 change_output->push_back( | |
| 1019 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data)); | |
| 1020 return; | |
| 1021 } | |
| 1022 } | 1008 } |
| 1023 | 1009 |
| 1024 // static | 1010 // static |
| 1025 void SessionsSyncManager::SetSessionTabFromDelegate( | 1011 void SessionsSyncManager::SetSessionTabFromDelegate( |
| 1026 const SyncedTabDelegate& tab_delegate, | 1012 const SyncedTabDelegate& tab_delegate, |
| 1027 base::Time mtime, | 1013 base::Time mtime, |
| 1028 sessions::SessionTab* session_tab) { | 1014 sessions::SessionTab* session_tab) { |
| 1029 DCHECK(session_tab); | 1015 DCHECK(session_tab); |
| 1030 session_tab->window_id.set_id(tab_delegate.GetWindowId()); | 1016 session_tab->window_id.set_id(tab_delegate.GetWindowId()); |
| 1031 session_tab->tab_id.set_id(tab_delegate.GetSessionId()); | 1017 session_tab->tab_id.set_id(tab_delegate.GetSessionId()); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 } | 1116 } |
| 1131 | 1117 |
| 1132 // static | 1118 // static |
| 1133 std::string SessionsSyncManager::TagHashFromSpecifics( | 1119 std::string SessionsSyncManager::TagHashFromSpecifics( |
| 1134 const sync_pb::SessionSpecifics& specifics) { | 1120 const sync_pb::SessionSpecifics& specifics) { |
| 1135 return syncer::GenerateSyncableHash(syncer::SESSIONS, | 1121 return syncer::GenerateSyncableHash(syncer::SESSIONS, |
| 1136 TagFromSpecifics(specifics)); | 1122 TagFromSpecifics(specifics)); |
| 1137 } | 1123 } |
| 1138 | 1124 |
| 1139 }; // namespace sync_sessions | 1125 }; // namespace sync_sessions |
| OLD | NEW |