OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 const std::string& id, | 48 const std::string& id, |
49 sync_api::BaseNode* sync_node) { | 49 sync_api::BaseNode* sync_node) { |
50 NOTREACHED(); | 50 NOTREACHED(); |
51 return false; | 51 return false; |
52 } | 52 } |
53 | 53 |
54 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 54 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
55 DCHECK(CalledOnValidThread()); | 55 DCHECK(CalledOnValidThread()); |
56 CHECK(has_nodes); | 56 CHECK(has_nodes); |
57 *has_nodes = false; | 57 *has_nodes = false; |
58 sync_api::ReadTransaction trans( | 58 sync_api::ReadTransaction trans(sync_service_->GetUserShare()); |
59 sync_service_->backend()->GetUserShareHandle()); | |
60 sync_api::ReadNode root(&trans); | 59 sync_api::ReadNode root(&trans); |
61 if (!root.InitByTagLookup(kSessionsTag)) { | 60 if (!root.InitByTagLookup(kSessionsTag)) { |
62 LOG(ERROR) << kNoSessionsFolderError; | 61 LOG(ERROR) << kNoSessionsFolderError; |
63 return false; | 62 return false; |
64 } | 63 } |
65 // The sync model has user created nodes iff the sessions folder has | 64 // The sync model has user created nodes iff the sessions folder has |
66 // any children. | 65 // any children. |
67 *has_nodes = root.GetFirstChildId() != sync_api::kInvalidId; | 66 *has_nodes = root.GetFirstChildId() != sync_api::kInvalidId; |
68 return true; | 67 return true; |
69 } | 68 } |
70 | 69 |
71 int64 SessionModelAssociator::GetSyncIdFromChromeId(const size_t& id) { | 70 int64 SessionModelAssociator::GetSyncIdFromChromeId(const size_t& id) { |
72 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
73 return GetSyncIdFromSessionTag(TabIdToTag(GetCurrentMachineTag(), id)); | 72 return GetSyncIdFromSessionTag(TabIdToTag(GetCurrentMachineTag(), id)); |
74 } | 73 } |
75 | 74 |
76 int64 SessionModelAssociator::GetSyncIdFromSessionTag(const std::string& tag) { | 75 int64 SessionModelAssociator::GetSyncIdFromSessionTag(const std::string& tag) { |
77 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
78 sync_api::ReadTransaction trans( | 77 sync_api::ReadTransaction trans(sync_service_->GetUserShare()); |
79 sync_service_->backend()->GetUserShareHandle()); | |
80 sync_api::ReadNode node(&trans); | 78 sync_api::ReadNode node(&trans); |
81 if (!node.InitByClientTagLookup(syncable::SESSIONS, tag)) | 79 if (!node.InitByClientTagLookup(syncable::SESSIONS, tag)) |
82 return sync_api::kInvalidId; | 80 return sync_api::kInvalidId; |
83 return node.GetId(); | 81 return node.GetId(); |
84 } | 82 } |
85 void SessionModelAssociator::ReassociateWindows(bool reload_tabs) { | 83 void SessionModelAssociator::ReassociateWindows(bool reload_tabs) { |
86 DCHECK(CalledOnValidThread()); | 84 DCHECK(CalledOnValidThread()); |
87 sync_pb::SessionSpecifics specifics; | 85 sync_pb::SessionSpecifics specifics; |
88 specifics.set_session_tag(GetCurrentMachineTag()); | 86 specifics.set_session_tag(GetCurrentMachineTag()); |
89 sync_pb::SessionHeader* header_s = specifics.mutable_header(); | 87 sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } | 125 } |
128 } | 126 } |
129 // Only add a window if it contains valid tabs. | 127 // Only add a window if it contains valid tabs. |
130 if (found_tabs) { | 128 if (found_tabs) { |
131 sync_pb::SessionWindow* header_window = header_s->add_window(); | 129 sync_pb::SessionWindow* header_window = header_s->add_window(); |
132 *header_window = window_s; | 130 *header_window = window_s; |
133 } | 131 } |
134 } | 132 } |
135 } | 133 } |
136 | 134 |
137 sync_api::WriteTransaction trans( | 135 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
138 sync_service_->backend()->GetUserShareHandle()); | |
139 sync_api::WriteNode header_node(&trans); | 136 sync_api::WriteNode header_node(&trans); |
140 if (!header_node.InitByIdLookup(local_session_syncid_)) { | 137 if (!header_node.InitByIdLookup(local_session_syncid_)) { |
141 LOG(ERROR) << "Failed to load local session header node."; | 138 LOG(ERROR) << "Failed to load local session header node."; |
142 return; | 139 return; |
143 } | 140 } |
144 header_node.SetSessionSpecifics(specifics); | 141 header_node.SetSessionSpecifics(specifics); |
145 } | 142 } |
146 | 143 |
147 // Static. | 144 // Static. |
148 bool SessionModelAssociator::ShouldSyncWindowType(const Browser::Type& type) { | 145 bool SessionModelAssociator::ShouldSyncWindowType(const Browser::Type& type) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 Associate(&tab, sync_id); | 200 Associate(&tab, sync_id); |
204 } | 201 } |
205 | 202 |
206 void SessionModelAssociator::Associate(const TabContents* tab, int64 sync_id) { | 203 void SessionModelAssociator::Associate(const TabContents* tab, int64 sync_id) { |
207 DCHECK(CalledOnValidThread()); | 204 DCHECK(CalledOnValidThread()); |
208 SessionID::id_type session_id = tab->controller().session_id().id(); | 205 SessionID::id_type session_id = tab->controller().session_id().id(); |
209 | 206 |
210 TabLinks t(sync_id, tab); | 207 TabLinks t(sync_id, tab); |
211 tab_map_[session_id] = t; | 208 tab_map_[session_id] = t; |
212 | 209 |
213 sync_api::WriteTransaction trans( | 210 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
214 sync_service_->backend()->GetUserShareHandle()); | |
215 WriteTabContentsToSyncModel(*tab, sync_id, &trans); | 211 WriteTabContentsToSyncModel(*tab, sync_id, &trans); |
216 } | 212 } |
217 | 213 |
218 bool SessionModelAssociator::WriteTabContentsToSyncModel( | 214 bool SessionModelAssociator::WriteTabContentsToSyncModel( |
219 const TabContents& tab, | 215 const TabContents& tab, |
220 int64 sync_id, | 216 int64 sync_id, |
221 sync_api::WriteTransaction* trans) { | 217 sync_api::WriteTransaction* trans) { |
222 DCHECK(CalledOnValidThread()); | 218 DCHECK(CalledOnValidThread()); |
223 sync_api::WriteNode tab_node(trans); | 219 sync_api::WriteNode tab_node(trans); |
224 if (!tab_node.InitByIdLookup(sync_id)) { | 220 if (!tab_node.InitByIdLookup(sync_id)) { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 352 |
357 // Ensure that we disassociated properly, otherwise memory might leak. | 353 // Ensure that we disassociated properly, otherwise memory might leak. |
358 DCHECK(foreign_session_tracker_.empty()); | 354 DCHECK(foreign_session_tracker_.empty()); |
359 DCHECK_EQ(0U, tab_pool_.capacity()); | 355 DCHECK_EQ(0U, tab_pool_.capacity()); |
360 | 356 |
361 local_session_syncid_ = sync_api::kInvalidId; | 357 local_session_syncid_ = sync_api::kInvalidId; |
362 | 358 |
363 // Read any available foreign sessions and load any session data we may have. | 359 // Read any available foreign sessions and load any session data we may have. |
364 // If we don't have any local session data in the db, create a header node. | 360 // If we don't have any local session data in the db, create a header node. |
365 { | 361 { |
366 sync_api::WriteTransaction trans( | 362 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
367 sync_service_->backend()->GetUserShareHandle()); | |
368 | 363 |
369 sync_api::ReadNode root(&trans); | 364 sync_api::ReadNode root(&trans); |
370 if (!root.InitByTagLookup(kSessionsTag)) { | 365 if (!root.InitByTagLookup(kSessionsTag)) { |
371 LOG(ERROR) << kNoSessionsFolderError; | 366 LOG(ERROR) << kNoSessionsFolderError; |
372 return false; | 367 return false; |
373 } | 368 } |
374 | 369 |
375 // Make sure we have a machine tag. | 370 // Make sure we have a machine tag. |
376 if (current_machine_tag_.empty()) | 371 if (current_machine_tag_.empty()) |
377 InitializeCurrentMachineTag(&trans); | 372 InitializeCurrentMachineTag(&trans); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 | 694 |
700 void SessionModelAssociator::TabNodePool::AddTabNode(int64 sync_id) { | 695 void SessionModelAssociator::TabNodePool::AddTabNode(int64 sync_id) { |
701 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); | 696 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); |
702 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 697 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; |
703 } | 698 } |
704 | 699 |
705 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { | 700 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { |
706 DCHECK_GT(machine_tag_.length(), 0U); | 701 DCHECK_GT(machine_tag_.length(), 0U); |
707 if (tab_pool_fp_ == -1) { | 702 if (tab_pool_fp_ == -1) { |
708 // Tab pool has no free nodes, allocate new one. | 703 // Tab pool has no free nodes, allocate new one. |
709 sync_api::WriteTransaction trans( | 704 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
710 sync_service_->backend()->GetUserShareHandle()); | |
711 sync_api::ReadNode root(&trans); | 705 sync_api::ReadNode root(&trans); |
712 if (!root.InitByTagLookup(kSessionsTag)) { | 706 if (!root.InitByTagLookup(kSessionsTag)) { |
713 LOG(ERROR) << kNoSessionsFolderError; | 707 LOG(ERROR) << kNoSessionsFolderError; |
714 return 0; | 708 return 0; |
715 } | 709 } |
716 size_t tab_node_id = tab_syncid_pool_.size(); | 710 size_t tab_node_id = tab_syncid_pool_.size(); |
717 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); | 711 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); |
718 sync_api::WriteNode tab_node(&trans); | 712 sync_api::WriteNode tab_node(&trans); |
719 if (!tab_node.InitUniqueByCreation(syncable::SESSIONS, root, | 713 if (!tab_node.InitUniqueByCreation(syncable::SESSIONS, root, |
720 tab_node_tag)) { | 714 tab_node_tag)) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 int handle, | 825 int handle, |
832 std::vector<SessionWindow*>* windows) { | 826 std::vector<SessionWindow*>* windows) { |
833 DCHECK(CalledOnValidThread()); | 827 DCHECK(CalledOnValidThread()); |
834 DCHECK(local_session_syncid_); | 828 DCHECK(local_session_syncid_); |
835 | 829 |
836 sync_pb::SessionSpecifics specifics; | 830 sync_pb::SessionSpecifics specifics; |
837 specifics.set_session_tag(GetCurrentMachineTag()); | 831 specifics.set_session_tag(GetCurrentMachineTag()); |
838 sync_pb::SessionHeader* header_s = specifics.mutable_header(); | 832 sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
839 PopulateSessionSpecificsHeader(*windows, header_s); | 833 PopulateSessionSpecificsHeader(*windows, header_s); |
840 | 834 |
841 sync_api::WriteTransaction trans( | 835 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
842 sync_service_->backend()->GetUserShareHandle()); | |
843 sync_api::ReadNode root(&trans); | 836 sync_api::ReadNode root(&trans); |
844 if (!root.InitByTagLookup(kSessionsTag)) { | 837 if (!root.InitByTagLookup(kSessionsTag)) { |
845 LOG(ERROR) << kNoSessionsFolderError; | 838 LOG(ERROR) << kNoSessionsFolderError; |
846 return; | 839 return; |
847 } | 840 } |
848 | 841 |
849 sync_api::WriteNode header_node(&trans); | 842 sync_api::WriteNode header_node(&trans); |
850 if (!header_node.InitByIdLookup(local_session_syncid_)) { | 843 if (!header_node.InitByIdLookup(local_session_syncid_)) { |
851 LOG(ERROR) << "Failed to load local session header node."; | 844 LOG(ERROR) << "Failed to load local session header node."; |
852 return; | 845 return; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 DCHECK(CalledOnValidThread()); | 899 DCHECK(CalledOnValidThread()); |
907 DCHECK(tab_map_.empty()); | 900 DCHECK(tab_map_.empty()); |
908 for (size_t i = 0; i < window.tabs.size(); ++i) { | 901 for (size_t i = 0; i < window.tabs.size(); ++i) { |
909 SessionTab* tab = window.tabs[i]; | 902 SessionTab* tab = window.tabs[i]; |
910 int64 id = tab_pool_.GetFreeTabNode(); | 903 int64 id = tab_pool_.GetFreeTabNode(); |
911 if (id == -1) { | 904 if (id == -1) { |
912 LOG(ERROR) << "Failed to find/generate free sync node for tab."; | 905 LOG(ERROR) << "Failed to find/generate free sync node for tab."; |
913 return false; | 906 return false; |
914 } | 907 } |
915 | 908 |
916 sync_api::WriteTransaction trans( | 909 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
917 sync_service_->backend()->GetUserShareHandle()); | |
918 if (!WriteSessionTabToSyncModel(*tab, id, &trans)) { | 910 if (!WriteSessionTabToSyncModel(*tab, id, &trans)) { |
919 return false; | 911 return false; |
920 } | 912 } |
921 | 913 |
922 TabLinks t(id, tab); | 914 TabLinks t(id, tab); |
923 tab_map_[tab->tab_id.id()] = t; | 915 tab_map_[tab->tab_id.id()] = t; |
924 } | 916 } |
925 return true; | 917 return true; |
926 } | 918 } |
927 | 919 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 for (std::vector<TabNavigation>::const_iterator i = | 951 for (std::vector<TabNavigation>::const_iterator i = |
960 tab.navigations.begin(); i != tab.navigations.end(); ++i) { | 952 tab.navigations.begin(); i != tab.navigations.end(); ++i) { |
961 const TabNavigation navigation = *i; | 953 const TabNavigation navigation = *i; |
962 sync_pb::TabNavigation* tab_navigation = | 954 sync_pb::TabNavigation* tab_navigation = |
963 session_tab->add_navigation(); | 955 session_tab->add_navigation(); |
964 PopulateSessionSpecificsNavigation(&navigation, tab_navigation); | 956 PopulateSessionSpecificsNavigation(&navigation, tab_navigation); |
965 } | 957 } |
966 } | 958 } |
967 | 959 |
968 } // namespace browser_sync | 960 } // namespace browser_sync |
OLD | NEW |