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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 NOTREACHED(); | 137 NOTREACHED(); |
138 return false; | 138 return false; |
139 } | 139 } |
140 | 140 |
141 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 141 bool SessionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
142 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
143 CHECK(has_nodes); | 143 CHECK(has_nodes); |
144 *has_nodes = false; | 144 *has_nodes = false; |
145 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 145 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
146 sync_api::ReadNode root(&trans); | 146 sync_api::ReadNode root(&trans); |
147 if (!root.InitByTagLookup(kSessionsTag)) { | 147 if (root.InitByTagLookup(kSessionsTag) != sync_api::BaseNode::INIT_OK) { |
148 LOG(ERROR) << kNoSessionsFolderError; | 148 LOG(ERROR) << kNoSessionsFolderError; |
149 return false; | 149 return false; |
150 } | 150 } |
151 // The sync model has user created nodes iff the sessions folder has | 151 // The sync model has user created nodes iff the sessions folder has |
152 // any children. | 152 // any children. |
153 *has_nodes = root.HasChildren(); | 153 *has_nodes = root.HasChildren(); |
154 return true; | 154 return true; |
155 } | 155 } |
156 | 156 |
157 int64 SessionModelAssociator::GetSyncIdFromChromeId(const size_t& id) { | 157 int64 SessionModelAssociator::GetSyncIdFromChromeId(const size_t& id) { |
158 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
159 return GetSyncIdFromSessionTag(TabIdToTag(GetCurrentMachineTag(), id)); | 159 return GetSyncIdFromSessionTag(TabIdToTag(GetCurrentMachineTag(), id)); |
160 } | 160 } |
161 | 161 |
162 int64 SessionModelAssociator::GetSyncIdFromSessionTag(const std::string& tag) { | 162 int64 SessionModelAssociator::GetSyncIdFromSessionTag(const std::string& tag) { |
163 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
164 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 164 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
165 sync_api::ReadNode node(&trans); | 165 sync_api::ReadNode node(&trans); |
166 if (!node.InitByClientTagLookup(SESSIONS, tag)) | 166 if (node.InitByClientTagLookup(SESSIONS, tag) != sync_api::BaseNode::INIT_OK) |
167 return sync_api::kInvalidId; | 167 return sync_api::kInvalidId; |
168 return node.GetId(); | 168 return node.GetId(); |
169 } | 169 } |
170 | 170 |
171 const SyncedTabDelegate* | 171 const SyncedTabDelegate* |
172 SessionModelAssociator::GetChromeNodeFromSyncId(int64 sync_id) { | 172 SessionModelAssociator::GetChromeNodeFromSyncId(int64 sync_id) { |
173 NOTREACHED(); | 173 NOTREACHED(); |
174 return NULL; | 174 return NULL; |
175 } | 175 } |
176 | 176 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 current_session->windows[window_id], | 265 current_session->windows[window_id], |
266 &synced_session_tracker_); | 266 &synced_session_tracker_); |
267 } | 267 } |
268 } | 268 } |
269 } | 269 } |
270 // Free memory for closed windows and tabs. | 270 // Free memory for closed windows and tabs. |
271 synced_session_tracker_.CleanupSession(local_tag); | 271 synced_session_tracker_.CleanupSession(local_tag); |
272 | 272 |
273 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 273 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
274 sync_api::WriteNode header_node(&trans); | 274 sync_api::WriteNode header_node(&trans); |
275 if (!header_node.InitByIdLookup(local_session_syncid_)) { | 275 if (header_node.InitByIdLookup(local_session_syncid_) != |
| 276 sync_api::BaseNode::INIT_OK) { |
276 if (error) { | 277 if (error) { |
277 *error = error_handler_->CreateAndUploadError( | 278 *error = error_handler_->CreateAndUploadError( |
278 FROM_HERE, | 279 FROM_HERE, |
279 "Failed to load local session header node.", | 280 "Failed to load local session header node.", |
280 model_type()); | 281 model_type()); |
281 } | 282 } |
282 return false; | 283 return false; |
283 } | 284 } |
284 header_node.SetSessionSpecifics(specifics); | 285 header_node.SetSessionSpecifics(specifics); |
285 if (waiting_for_change_) QuitLoopForSubtleTesting(); | 286 if (waiting_for_change_) QuitLoopForSubtleTesting(); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 421 |
421 // If the url changed, kick off the favicon load for the new url. | 422 // If the url changed, kick off the favicon load for the new url. |
422 bool url_changed = false; | 423 bool url_changed = false; |
423 if (tab_link->url().is_valid() && tab_link->url() != old_tab_url) { | 424 if (tab_link->url().is_valid() && tab_link->url() != old_tab_url) { |
424 url_changed = true; | 425 url_changed = true; |
425 LoadFaviconForTab(tab_link); | 426 LoadFaviconForTab(tab_link); |
426 } | 427 } |
427 | 428 |
428 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 429 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
429 sync_api::WriteNode tab_node(&trans); | 430 sync_api::WriteNode tab_node(&trans); |
430 if (!tab_node.InitByIdLookup(sync_id)) { | 431 if (tab_node.InitByIdLookup(sync_id) != sync_api::BaseNode::INIT_OK) { |
431 if (error) { | 432 if (error) { |
432 *error = error_handler_->CreateAndUploadError( | 433 *error = error_handler_->CreateAndUploadError( |
433 FROM_HERE, | 434 FROM_HERE, |
434 "Failed to look up local tab node", | 435 "Failed to look up local tab node", |
435 model_type()); | 436 model_type()); |
436 } | 437 } |
437 return false; | 438 return false; |
438 } | 439 } |
439 | 440 |
440 // Preserve the existing favicon only if the url didn't change. | 441 // Preserve the existing favicon only if the url didn't change. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 // up to date. | 495 // up to date. |
495 | 496 |
496 if (favicon.is_valid()) { | 497 if (favicon.is_valid()) { |
497 DCHECK_EQ(handle, tab_link->favicon_load_handle()); | 498 DCHECK_EQ(handle, tab_link->favicon_load_handle()); |
498 tab_link->set_favicon_load_handle(0); | 499 tab_link->set_favicon_load_handle(0); |
499 DCHECK_EQ(favicon.icon_type, history::FAVICON); | 500 DCHECK_EQ(favicon.icon_type, history::FAVICON); |
500 DCHECK_NE(tab_link->sync_id(), sync_api::kInvalidId); | 501 DCHECK_NE(tab_link->sync_id(), sync_api::kInvalidId); |
501 // Load the sync tab node and update the favicon data. | 502 // Load the sync tab node and update the favicon data. |
502 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 503 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
503 sync_api::WriteNode tab_node(&trans); | 504 sync_api::WriteNode tab_node(&trans); |
504 if (!tab_node.InitByIdLookup(tab_link->sync_id())) { | 505 if (tab_node.InitByIdLookup(tab_link->sync_id()) != |
| 506 sync_api::BaseNode::INIT_OK) { |
505 LOG(WARNING) << "Failed to load sync tab node for tab id " << tab_id | 507 LOG(WARNING) << "Failed to load sync tab node for tab id " << tab_id |
506 << " and url " << tab_link->url().spec(); | 508 << " and url " << tab_link->url().spec(); |
507 return; | 509 return; |
508 } | 510 } |
509 sync_pb::SessionSpecifics session_specifics = | 511 sync_pb::SessionSpecifics session_specifics = |
510 tab_node.GetSessionSpecifics(); | 512 tab_node.GetSessionSpecifics(); |
511 DCHECK(session_specifics.has_tab()); | 513 DCHECK(session_specifics.has_tab()); |
512 sync_pb::SessionTab* tab = session_specifics.mutable_tab(); | 514 sync_pb::SessionTab* tab = session_specifics.mutable_tab(); |
513 if (favicon.image_data->size() > 0) { | 515 if (favicon.image_data->size() > 0) { |
514 DVLOG(1) << "Storing session favicon for " | 516 DVLOG(1) << "Storing session favicon for " |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 DCHECK_EQ(0U, tab_pool_.capacity()); | 654 DCHECK_EQ(0U, tab_pool_.capacity()); |
653 | 655 |
654 local_session_syncid_ = sync_api::kInvalidId; | 656 local_session_syncid_ = sync_api::kInvalidId; |
655 | 657 |
656 // Read any available foreign sessions and load any session data we may have. | 658 // Read any available foreign sessions and load any session data we may have. |
657 // If we don't have any local session data in the db, create a header node. | 659 // If we don't have any local session data in the db, create a header node. |
658 { | 660 { |
659 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 661 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
660 | 662 |
661 sync_api::ReadNode root(&trans); | 663 sync_api::ReadNode root(&trans); |
662 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(model_type()))) { | 664 if (root.InitByTagLookup(syncable::ModelTypeToRootTag(model_type())) != |
| 665 sync_api::BaseNode::INIT_OK) { |
663 return error_handler_->CreateAndUploadError( | 666 return error_handler_->CreateAndUploadError( |
664 FROM_HERE, | 667 FROM_HERE, |
665 kNoSessionsFolderError, | 668 kNoSessionsFolderError, |
666 model_type()); | 669 model_type()); |
667 } | 670 } |
668 | 671 |
669 // Make sure we have a machine tag. | 672 // Make sure we have a machine tag. |
670 if (current_machine_tag_.empty()) { | 673 if (current_machine_tag_.empty()) { |
671 InitializeCurrentMachineTag(&trans); | 674 InitializeCurrentMachineTag(&trans); |
672 // The session name is retrieved asynchronously so it might not come back | 675 // The session name is retrieved asynchronously so it might not come back |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 sync_api::WriteTransaction* trans, | 777 sync_api::WriteTransaction* trans, |
775 SyncError* error) { | 778 SyncError* error) { |
776 DCHECK(CalledOnValidThread()); | 779 DCHECK(CalledOnValidThread()); |
777 DCHECK(tab_pool_.empty()); | 780 DCHECK(tab_pool_.empty()); |
778 DCHECK_EQ(local_session_syncid_, sync_api::kInvalidId); | 781 DCHECK_EQ(local_session_syncid_, sync_api::kInvalidId); |
779 | 782 |
780 // Iterate through the nodes and associate any foreign sessions. | 783 // Iterate through the nodes and associate any foreign sessions. |
781 int64 id = root.GetFirstChildId(); | 784 int64 id = root.GetFirstChildId(); |
782 while (id != sync_api::kInvalidId) { | 785 while (id != sync_api::kInvalidId) { |
783 sync_api::WriteNode sync_node(trans); | 786 sync_api::WriteNode sync_node(trans); |
784 if (!sync_node.InitByIdLookup(id)) { | 787 if (sync_node.InitByIdLookup(id) != sync_api::BaseNode::INIT_OK) { |
785 if (error) { | 788 if (error) { |
786 *error = error_handler_->CreateAndUploadError( | 789 *error = error_handler_->CreateAndUploadError( |
787 FROM_HERE, | 790 FROM_HERE, |
788 "Failed to load sync node", | 791 "Failed to load sync node", |
789 model_type()); | 792 model_type()); |
790 } | 793 } |
791 return false; | 794 return false; |
792 } | 795 } |
793 int64 next_id = sync_node.GetSuccessorId(); | 796 int64 next_id = sync_node.GetSuccessorId(); |
794 | 797 |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1138 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); | 1141 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); |
1139 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 1142 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; |
1140 } | 1143 } |
1141 | 1144 |
1142 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { | 1145 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { |
1143 DCHECK_GT(machine_tag_.length(), 0U); | 1146 DCHECK_GT(machine_tag_.length(), 0U); |
1144 if (tab_pool_fp_ == -1) { | 1147 if (tab_pool_fp_ == -1) { |
1145 // Tab pool has no free nodes, allocate new one. | 1148 // Tab pool has no free nodes, allocate new one. |
1146 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1149 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
1147 sync_api::ReadNode root(&trans); | 1150 sync_api::ReadNode root(&trans); |
1148 if (!root.InitByTagLookup(kSessionsTag)) { | 1151 if (root.InitByTagLookup(kSessionsTag) != sync_api::BaseNode::INIT_OK) { |
1149 LOG(ERROR) << kNoSessionsFolderError; | 1152 LOG(ERROR) << kNoSessionsFolderError; |
1150 return sync_api::kInvalidId; | 1153 return sync_api::kInvalidId; |
1151 } | 1154 } |
1152 size_t tab_node_id = tab_syncid_pool_.size(); | 1155 size_t tab_node_id = tab_syncid_pool_.size(); |
1153 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); | 1156 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); |
1154 sync_api::WriteNode tab_node(&trans); | 1157 sync_api::WriteNode tab_node(&trans); |
1155 if (!tab_node.InitUniqueByCreation(SESSIONS, root, tab_node_tag)) { | 1158 if (!tab_node.InitUniqueByCreation(SESSIONS, root, tab_node_tag)) { |
1156 LOG(ERROR) << "Could not create new node with tag " | 1159 LOG(ERROR) << "Could not create new node with tag " |
1157 << tab_node_tag << "!"; | 1160 << tab_node_tag << "!"; |
1158 return sync_api::kInvalidId; | 1161 return sync_api::kInvalidId; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 return; | 1274 return; |
1272 } | 1275 } |
1273 | 1276 |
1274 if (!DisassociateForeignSession(tag)) { | 1277 if (!DisassociateForeignSession(tag)) { |
1275 // We don't have any data for this session, our work here is done! | 1278 // We don't have any data for this session, our work here is done! |
1276 return; | 1279 return; |
1277 } | 1280 } |
1278 | 1281 |
1279 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1282 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
1280 sync_api::ReadNode root(&trans); | 1283 sync_api::ReadNode root(&trans); |
1281 if (!root.InitByTagLookup(kSessionsTag)) { | 1284 if (root.InitByTagLookup(kSessionsTag) != sync_api::BaseNode::INIT_OK) { |
1282 LOG(ERROR) << kNoSessionsFolderError; | 1285 LOG(ERROR) << kNoSessionsFolderError; |
1283 return; | 1286 return; |
1284 } | 1287 } |
1285 int64 id = root.GetFirstChildId(); | 1288 int64 id = root.GetFirstChildId(); |
1286 while (id != sync_api::kInvalidId) { | 1289 while (id != sync_api::kInvalidId) { |
1287 sync_api::WriteNode sync_node(&trans); | 1290 sync_api::WriteNode sync_node(&trans); |
1288 if (!sync_node.InitByIdLookup(id)) { | 1291 if (sync_node.InitByIdLookup(id) != sync_api::BaseNode::INIT_OK) { |
1289 LOG(ERROR) << "Failed to fetch sync node for id " << id; | 1292 LOG(ERROR) << "Failed to fetch sync node for id " << id; |
1290 continue; | 1293 continue; |
1291 } | 1294 } |
1292 id = sync_node.GetSuccessorId(); | 1295 id = sync_node.GetSuccessorId(); |
1293 const sync_pb::SessionSpecifics& specifics = | 1296 const sync_pb::SessionSpecifics& specifics = |
1294 sync_node.GetSessionSpecifics(); | 1297 sync_node.GetSessionSpecifics(); |
1295 if (specifics.session_tag() == tag) | 1298 if (specifics.session_tag() == tag) |
1296 sync_node.Remove(); | 1299 sync_node.Remove(); |
1297 } | 1300 } |
1298 } | 1301 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1364 bool SessionModelAssociator::CryptoReadyIfNecessary() { | 1367 bool SessionModelAssociator::CryptoReadyIfNecessary() { |
1365 // We only access the cryptographer while holding a transaction. | 1368 // We only access the cryptographer while holding a transaction. |
1366 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1369 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
1367 const syncable::ModelTypeSet encrypted_types = | 1370 const syncable::ModelTypeSet encrypted_types = |
1368 sync_api::GetEncryptedTypes(&trans); | 1371 sync_api::GetEncryptedTypes(&trans); |
1369 return !encrypted_types.Has(SESSIONS) || | 1372 return !encrypted_types.Has(SESSIONS) || |
1370 sync_service_->IsCryptographerReady(&trans); | 1373 sync_service_->IsCryptographerReady(&trans); |
1371 } | 1374 } |
1372 | 1375 |
1373 } // namespace browser_sync | 1376 } // namespace browser_sync |
OLD | NEW |