| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 178 } |
| 179 | 179 |
| 180 bool SessionModelAssociator::InitSyncNodeFromChromeId( | 180 bool SessionModelAssociator::InitSyncNodeFromChromeId( |
| 181 const size_t& id, | 181 const size_t& id, |
| 182 csync::BaseNode* sync_node) { | 182 csync::BaseNode* sync_node) { |
| 183 NOTREACHED(); | 183 NOTREACHED(); |
| 184 return false; | 184 return false; |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool SessionModelAssociator::AssociateWindows(bool reload_tabs, | 187 bool SessionModelAssociator::AssociateWindows(bool reload_tabs, |
| 188 SyncError* error) { | 188 csync::SyncError* error) { |
| 189 DCHECK(CalledOnValidThread()); | 189 DCHECK(CalledOnValidThread()); |
| 190 std::string local_tag = GetCurrentMachineTag(); | 190 std::string local_tag = GetCurrentMachineTag(); |
| 191 sync_pb::SessionSpecifics specifics; | 191 sync_pb::SessionSpecifics specifics; |
| 192 specifics.set_session_tag(local_tag); | 192 specifics.set_session_tag(local_tag); |
| 193 sync_pb::SessionHeader* header_s = specifics.mutable_header(); | 193 sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
| 194 SyncedSession* current_session = | 194 SyncedSession* current_session = |
| 195 synced_session_tracker_.GetSession(local_tag); | 195 synced_session_tracker_.GetSession(local_tag); |
| 196 current_session->modified_time = base::Time::Now(); | 196 current_session->modified_time = base::Time::Now(); |
| 197 header_s->set_client_name(current_session_name_); | 197 header_s->set_client_name(current_session_name_); |
| 198 header_s->set_device_type(GetLocalDeviceType()); | 198 header_s->set_device_type(GetLocalDeviceType()); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // Static. | 293 // Static. |
| 294 bool SessionModelAssociator::ShouldSyncWindow( | 294 bool SessionModelAssociator::ShouldSyncWindow( |
| 295 const SyncedWindowDelegate* window) { | 295 const SyncedWindowDelegate* window) { |
| 296 if (window->IsApp()) | 296 if (window->IsApp()) |
| 297 return false; | 297 return false; |
| 298 return window->IsTypeTabbed() || window->IsTypePopup(); | 298 return window->IsTypeTabbed() || window->IsTypePopup(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 bool SessionModelAssociator::AssociateTabs( | 301 bool SessionModelAssociator::AssociateTabs( |
| 302 const std::vector<SyncedTabDelegate*>& tabs, | 302 const std::vector<SyncedTabDelegate*>& tabs, |
| 303 SyncError* error) { | 303 csync::SyncError* error) { |
| 304 DCHECK(CalledOnValidThread()); | 304 DCHECK(CalledOnValidThread()); |
| 305 for (std::vector<SyncedTabDelegate*>::const_iterator i = tabs.begin(); | 305 for (std::vector<SyncedTabDelegate*>::const_iterator i = tabs.begin(); |
| 306 i != tabs.end(); | 306 i != tabs.end(); |
| 307 ++i) { | 307 ++i) { |
| 308 if (!AssociateTab(**i, error)) | 308 if (!AssociateTab(**i, error)) |
| 309 return false; | 309 return false; |
| 310 } | 310 } |
| 311 if (waiting_for_change_) QuitLoopForSubtleTesting(); | 311 if (waiting_for_change_) QuitLoopForSubtleTesting(); |
| 312 return true; | 312 return true; |
| 313 } | 313 } |
| 314 | 314 |
| 315 bool SessionModelAssociator::AssociateTab(const SyncedTabDelegate& tab, | 315 bool SessionModelAssociator::AssociateTab(const SyncedTabDelegate& tab, |
| 316 SyncError* error) { | 316 csync::SyncError* error) { |
| 317 DCHECK(CalledOnValidThread()); | 317 DCHECK(CalledOnValidThread()); |
| 318 int64 sync_id; | 318 int64 sync_id; |
| 319 SessionID::id_type tab_id = tab.GetSessionId(); | 319 SessionID::id_type tab_id = tab.GetSessionId(); |
| 320 if (tab.IsBeingDestroyed()) { | 320 if (tab.IsBeingDestroyed()) { |
| 321 // This tab is closing. | 321 // This tab is closing. |
| 322 TabLinksMap::iterator tab_iter = tab_map_.find(tab_id); | 322 TabLinksMap::iterator tab_iter = tab_map_.find(tab_id); |
| 323 if (tab_iter == tab_map_.end()) { | 323 if (tab_iter == tab_map_.end()) { |
| 324 // We aren't tracking this tab (for example, sync setting page). | 324 // We aren't tracking this tab (for example, sync setting page). |
| 325 return true; | 325 return true; |
| 326 } | 326 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 357 tab_map_iter->second->set_tab(&tab); | 357 tab_map_iter->second->set_tab(&tab); |
| 358 } | 358 } |
| 359 DCHECK(tab_link); | 359 DCHECK(tab_link); |
| 360 DCHECK_NE(tab_link->sync_id(), csync::kInvalidId); | 360 DCHECK_NE(tab_link->sync_id(), csync::kInvalidId); |
| 361 | 361 |
| 362 DVLOG(1) << "Reloading tab " << tab_id << " from window " | 362 DVLOG(1) << "Reloading tab " << tab_id << " from window " |
| 363 << tab.GetWindowId(); | 363 << tab.GetWindowId(); |
| 364 return WriteTabContentsToSyncModel(tab_link, error); | 364 return WriteTabContentsToSyncModel(tab_link, error); |
| 365 } | 365 } |
| 366 | 366 |
| 367 bool SessionModelAssociator::WriteTabContentsToSyncModel(TabLink* tab_link, | 367 bool SessionModelAssociator::WriteTabContentsToSyncModel( |
| 368 SyncError* error) { | 368 TabLink* tab_link, |
| 369 csync::SyncError* error) { |
| 369 DCHECK(CalledOnValidThread()); | 370 DCHECK(CalledOnValidThread()); |
| 370 const SyncedTabDelegate& tab = *(tab_link->tab()); | 371 const SyncedTabDelegate& tab = *(tab_link->tab()); |
| 371 const SyncedWindowDelegate& window = | 372 const SyncedWindowDelegate& window = |
| 372 *SyncedWindowDelegate::FindSyncedWindowDelegateWithId( | 373 *SyncedWindowDelegate::FindSyncedWindowDelegateWithId( |
| 373 tab.GetWindowId()); | 374 tab.GetWindowId()); |
| 374 int64 sync_id = tab_link->sync_id(); | 375 int64 sync_id = tab_link->sync_id(); |
| 375 GURL old_tab_url = tab_link->url(); | 376 GURL old_tab_url = tab_link->url(); |
| 376 | 377 |
| 377 // Load the last stored version of this tab so we can compare changes. If this | 378 // Load the last stored version of this tab so we can compare changes. If this |
| 378 // is a new tab, session_tab will be a blank/newly created SessionTab object. | 379 // is a new tab, session_tab will be a blank/newly created SessionTab object. |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 void SessionModelAssociator::Associate(const SyncedTabDelegate* tab, | 722 void SessionModelAssociator::Associate(const SyncedTabDelegate* tab, |
| 722 int64 sync_id) { | 723 int64 sync_id) { |
| 723 NOTIMPLEMENTED(); | 724 NOTIMPLEMENTED(); |
| 724 } | 725 } |
| 725 | 726 |
| 726 void SessionModelAssociator::Disassociate(int64 sync_id) { | 727 void SessionModelAssociator::Disassociate(int64 sync_id) { |
| 727 DCHECK(CalledOnValidThread()); | 728 DCHECK(CalledOnValidThread()); |
| 728 NOTIMPLEMENTED(); | 729 NOTIMPLEMENTED(); |
| 729 } | 730 } |
| 730 | 731 |
| 731 SyncError SessionModelAssociator::AssociateModels() { | 732 csync::SyncError SessionModelAssociator::AssociateModels() { |
| 732 DCHECK(CalledOnValidThread()); | 733 DCHECK(CalledOnValidThread()); |
| 733 SyncError error; | 734 csync::SyncError error; |
| 734 | 735 |
| 735 // Ensure that we disassociated properly, otherwise memory might leak. | 736 // Ensure that we disassociated properly, otherwise memory might leak. |
| 736 DCHECK(synced_session_tracker_.Empty()); | 737 DCHECK(synced_session_tracker_.Empty()); |
| 737 DCHECK_EQ(0U, tab_pool_.capacity()); | 738 DCHECK_EQ(0U, tab_pool_.capacity()); |
| 738 | 739 |
| 739 local_session_syncid_ = csync::kInvalidId; | 740 local_session_syncid_ = csync::kInvalidId; |
| 740 | 741 |
| 741 // Read any available foreign sessions and load any session data we may have. | 742 // Read any available foreign sessions and load any session data we may have. |
| 742 // If we don't have any local session data in the db, create a header node. | 743 // If we don't have any local session data in the db, create a header node. |
| 743 { | 744 { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 if (!UpdateSyncModelDataFromClient(&error)) { | 800 if (!UpdateSyncModelDataFromClient(&error)) { |
| 800 DCHECK(error.IsSet()); | 801 DCHECK(error.IsSet()); |
| 801 return error; | 802 return error; |
| 802 } | 803 } |
| 803 | 804 |
| 804 DVLOG(1) << "Session models associated."; | 805 DVLOG(1) << "Session models associated."; |
| 805 DCHECK(!error.IsSet()); | 806 DCHECK(!error.IsSet()); |
| 806 return error; | 807 return error; |
| 807 } | 808 } |
| 808 | 809 |
| 809 SyncError SessionModelAssociator::DisassociateModels() { | 810 csync::SyncError SessionModelAssociator::DisassociateModels() { |
| 810 DCHECK(CalledOnValidThread()); | 811 DCHECK(CalledOnValidThread()); |
| 811 DVLOG(1) << "Disassociating local session " << GetCurrentMachineTag(); | 812 DVLOG(1) << "Disassociating local session " << GetCurrentMachineTag(); |
| 812 synced_session_tracker_.Clear(); | 813 synced_session_tracker_.Clear(); |
| 813 tab_map_.clear(); | 814 tab_map_.clear(); |
| 814 tab_pool_.clear(); | 815 tab_pool_.clear(); |
| 815 local_session_syncid_ = csync::kInvalidId; | 816 local_session_syncid_ = csync::kInvalidId; |
| 816 current_machine_tag_ = ""; | 817 current_machine_tag_ = ""; |
| 817 current_session_name_ = ""; | 818 current_session_name_ = ""; |
| 818 load_consumer_.CancelAllRequests(); | 819 load_consumer_.CancelAllRequests(); |
| 819 synced_favicons_.clear(); | 820 synced_favicons_.clear(); |
| 820 synced_favicon_pages_.clear(); | 821 synced_favicon_pages_.clear(); |
| 821 | 822 |
| 822 // There is no local model stored with which to disassociate, just notify | 823 // There is no local model stored with which to disassociate, just notify |
| 823 // foreign session handlers. | 824 // foreign session handlers. |
| 824 content::NotificationService::current()->Notify( | 825 content::NotificationService::current()->Notify( |
| 825 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 826 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
| 826 content::Source<Profile>(sync_service_->profile()), | 827 content::Source<Profile>(sync_service_->profile()), |
| 827 content::NotificationService::NoDetails()); | 828 content::NotificationService::NoDetails()); |
| 828 return SyncError(); | 829 return csync::SyncError(); |
| 829 } | 830 } |
| 830 | 831 |
| 831 void SessionModelAssociator::InitializeCurrentMachineTag( | 832 void SessionModelAssociator::InitializeCurrentMachineTag( |
| 832 csync::WriteTransaction* trans) { | 833 csync::WriteTransaction* trans) { |
| 833 DCHECK(CalledOnValidThread()); | 834 DCHECK(CalledOnValidThread()); |
| 834 DCHECK(current_machine_tag_.empty()); | 835 DCHECK(current_machine_tag_.empty()); |
| 835 std::string persisted_guid; | 836 std::string persisted_guid; |
| 836 if (pref_service_) | 837 if (pref_service_) |
| 837 persisted_guid = pref_service_->GetString(kSyncSessionsGUID); | 838 persisted_guid = pref_service_->GetString(kSyncSessionsGUID); |
| 838 if (!persisted_guid.empty()) { | 839 if (!persisted_guid.empty()) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 csync::GetSessionName( | 894 csync::GetSessionName( |
| 894 BrowserThread::GetBlockingPool(), | 895 BrowserThread::GetBlockingPool(), |
| 895 base::Bind(&SessionModelAssociator::OnSessionNameInitialized, | 896 base::Bind(&SessionModelAssociator::OnSessionNameInitialized, |
| 896 AsWeakPtr())); | 897 AsWeakPtr())); |
| 897 } | 898 } |
| 898 } | 899 } |
| 899 | 900 |
| 900 bool SessionModelAssociator::UpdateAssociationsFromSyncModel( | 901 bool SessionModelAssociator::UpdateAssociationsFromSyncModel( |
| 901 const csync::ReadNode& root, | 902 const csync::ReadNode& root, |
| 902 csync::WriteTransaction* trans, | 903 csync::WriteTransaction* trans, |
| 903 SyncError* error) { | 904 csync::SyncError* error) { |
| 904 DCHECK(CalledOnValidThread()); | 905 DCHECK(CalledOnValidThread()); |
| 905 DCHECK(tab_pool_.empty()); | 906 DCHECK(tab_pool_.empty()); |
| 906 DCHECK_EQ(local_session_syncid_, csync::kInvalidId); | 907 DCHECK_EQ(local_session_syncid_, csync::kInvalidId); |
| 907 | 908 |
| 908 // Iterate through the nodes and associate any foreign sessions. | 909 // Iterate through the nodes and associate any foreign sessions. |
| 909 int64 id = root.GetFirstChildId(); | 910 int64 id = root.GetFirstChildId(); |
| 910 while (id != csync::kInvalidId) { | 911 while (id != csync::kInvalidId) { |
| 911 csync::WriteNode sync_node(trans); | 912 csync::WriteNode sync_node(trans); |
| 912 if (sync_node.InitByIdLookup(id) != csync::BaseNode::INIT_OK) { | 913 if (sync_node.InitByIdLookup(id) != csync::BaseNode::INIT_OK) { |
| 913 if (error) { | 914 if (error) { |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 if (favicon_iter == synced_favicons_.end()) { | 1319 if (favicon_iter == synced_favicons_.end()) { |
| 1319 synced_favicons_[favicon_source.spec()] = | 1320 synced_favicons_[favicon_source.spec()] = |
| 1320 make_linked_ptr<SyncedFaviconInfo>(new SyncedFaviconInfo(favicon)); | 1321 make_linked_ptr<SyncedFaviconInfo>(new SyncedFaviconInfo(favicon)); |
| 1321 } else { | 1322 } else { |
| 1322 favicon_iter->second->data = favicon; | 1323 favicon_iter->second->data = favicon; |
| 1323 ++favicon_iter->second->usage_count; | 1324 ++favicon_iter->second->usage_count; |
| 1324 } | 1325 } |
| 1325 synced_favicon_pages_[navigation_url.spec()] = favicon_source.spec(); | 1326 synced_favicon_pages_[navigation_url.spec()] = favicon_source.spec(); |
| 1326 } | 1327 } |
| 1327 | 1328 |
| 1328 bool SessionModelAssociator::UpdateSyncModelDataFromClient(SyncError* error) { | 1329 bool SessionModelAssociator::UpdateSyncModelDataFromClient( |
| 1330 csync::SyncError* error) { |
| 1329 DCHECK(CalledOnValidThread()); | 1331 DCHECK(CalledOnValidThread()); |
| 1330 | 1332 |
| 1331 // Associate all open windows and their tabs. | 1333 // Associate all open windows and their tabs. |
| 1332 return AssociateWindows(true, error); | 1334 return AssociateWindows(true, error); |
| 1333 } | 1335 } |
| 1334 | 1336 |
| 1335 SessionModelAssociator::TabNodePool::TabNodePool( | 1337 SessionModelAssociator::TabNodePool::TabNodePool( |
| 1336 ProfileSyncService* sync_service) | 1338 ProfileSyncService* sync_service) |
| 1337 : tab_pool_fp_(-1), | 1339 : tab_pool_fp_(-1), |
| 1338 sync_service_(sync_service) { | 1340 sync_service_(sync_service) { |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1578 bool SessionModelAssociator::CryptoReadyIfNecessary() { | 1580 bool SessionModelAssociator::CryptoReadyIfNecessary() { |
| 1579 // We only access the cryptographer while holding a transaction. | 1581 // We only access the cryptographer while holding a transaction. |
| 1580 csync::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1582 csync::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 1581 const syncable::ModelTypeSet encrypted_types = | 1583 const syncable::ModelTypeSet encrypted_types = |
| 1582 csync::GetEncryptedTypes(&trans); | 1584 csync::GetEncryptedTypes(&trans); |
| 1583 return !encrypted_types.Has(SESSIONS) || | 1585 return !encrypted_types.Has(SESSIONS) || |
| 1584 sync_service_->IsCryptographerReady(&trans); | 1586 sync_service_->IsCryptographerReady(&trans); |
| 1585 } | 1587 } |
| 1586 | 1588 |
| 1587 } // namespace browser_sync | 1589 } // namespace browser_sync |
| OLD | NEW |