| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 sync_api::WriteTransaction* trans, | 805 sync_api::WriteTransaction* trans, |
| 803 SyncError* error) { | 806 SyncError* error) { |
| 804 DCHECK(CalledOnValidThread()); | 807 DCHECK(CalledOnValidThread()); |
| 805 DCHECK(tab_pool_.empty()); | 808 DCHECK(tab_pool_.empty()); |
| 806 DCHECK_EQ(local_session_syncid_, sync_api::kInvalidId); | 809 DCHECK_EQ(local_session_syncid_, sync_api::kInvalidId); |
| 807 | 810 |
| 808 // Iterate through the nodes and associate any foreign sessions. | 811 // Iterate through the nodes and associate any foreign sessions. |
| 809 int64 id = root.GetFirstChildId(); | 812 int64 id = root.GetFirstChildId(); |
| 810 while (id != sync_api::kInvalidId) { | 813 while (id != sync_api::kInvalidId) { |
| 811 sync_api::WriteNode sync_node(trans); | 814 sync_api::WriteNode sync_node(trans); |
| 812 if (!sync_node.InitByIdLookup(id)) { | 815 if (sync_node.InitByIdLookup(id) != sync_api::BaseNode::INIT_OK) { |
| 813 if (error) { | 816 if (error) { |
| 814 *error = error_handler_->CreateAndUploadError( | 817 *error = error_handler_->CreateAndUploadError( |
| 815 FROM_HERE, | 818 FROM_HERE, |
| 816 "Failed to load sync node", | 819 "Failed to load sync node", |
| 817 model_type()); | 820 model_type()); |
| 818 } | 821 } |
| 819 return false; | 822 return false; |
| 820 } | 823 } |
| 821 int64 next_id = sync_node.GetSuccessorId(); | 824 int64 next_id = sync_node.GetSuccessorId(); |
| 822 | 825 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); | 1226 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); |
| 1224 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; | 1227 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; |
| 1225 } | 1228 } |
| 1226 | 1229 |
| 1227 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { | 1230 int64 SessionModelAssociator::TabNodePool::GetFreeTabNode() { |
| 1228 DCHECK_GT(machine_tag_.length(), 0U); | 1231 DCHECK_GT(machine_tag_.length(), 0U); |
| 1229 if (tab_pool_fp_ == -1) { | 1232 if (tab_pool_fp_ == -1) { |
| 1230 // Tab pool has no free nodes, allocate new one. | 1233 // Tab pool has no free nodes, allocate new one. |
| 1231 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1234 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 1232 sync_api::ReadNode root(&trans); | 1235 sync_api::ReadNode root(&trans); |
| 1233 if (!root.InitByTagLookup(kSessionsTag)) { | 1236 if (root.InitByTagLookup(kSessionsTag) != sync_api::BaseNode::INIT_OK) { |
| 1234 LOG(ERROR) << kNoSessionsFolderError; | 1237 LOG(ERROR) << kNoSessionsFolderError; |
| 1235 return sync_api::kInvalidId; | 1238 return sync_api::kInvalidId; |
| 1236 } | 1239 } |
| 1237 size_t tab_node_id = tab_syncid_pool_.size(); | 1240 size_t tab_node_id = tab_syncid_pool_.size(); |
| 1238 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); | 1241 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); |
| 1239 sync_api::WriteNode tab_node(&trans); | 1242 sync_api::WriteNode tab_node(&trans); |
| 1240 if (!tab_node.InitUniqueByCreation(SESSIONS, root, tab_node_tag)) { | 1243 if (!tab_node.InitUniqueByCreation(SESSIONS, root, tab_node_tag)) { |
| 1241 LOG(ERROR) << "Could not create new node with tag " | 1244 LOG(ERROR) << "Could not create new node with tag " |
| 1242 << tab_node_tag << "!"; | 1245 << tab_node_tag << "!"; |
| 1243 return sync_api::kInvalidId; | 1246 return sync_api::kInvalidId; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1356 return; | 1359 return; |
| 1357 } | 1360 } |
| 1358 | 1361 |
| 1359 if (!DisassociateForeignSession(tag)) { | 1362 if (!DisassociateForeignSession(tag)) { |
| 1360 // We don't have any data for this session, our work here is done! | 1363 // We don't have any data for this session, our work here is done! |
| 1361 return; | 1364 return; |
| 1362 } | 1365 } |
| 1363 | 1366 |
| 1364 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1367 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 1365 sync_api::ReadNode root(&trans); | 1368 sync_api::ReadNode root(&trans); |
| 1366 if (!root.InitByTagLookup(kSessionsTag)) { | 1369 if (root.InitByTagLookup(kSessionsTag) != sync_api::BaseNode::INIT_OK) { |
| 1367 LOG(ERROR) << kNoSessionsFolderError; | 1370 LOG(ERROR) << kNoSessionsFolderError; |
| 1368 return; | 1371 return; |
| 1369 } | 1372 } |
| 1370 int64 id = root.GetFirstChildId(); | 1373 int64 id = root.GetFirstChildId(); |
| 1371 while (id != sync_api::kInvalidId) { | 1374 while (id != sync_api::kInvalidId) { |
| 1372 sync_api::WriteNode sync_node(&trans); | 1375 sync_api::WriteNode sync_node(&trans); |
| 1373 if (!sync_node.InitByIdLookup(id)) { | 1376 if (sync_node.InitByIdLookup(id) != sync_api::BaseNode::INIT_OK) { |
| 1374 LOG(ERROR) << "Failed to fetch sync node for id " << id; | 1377 LOG(ERROR) << "Failed to fetch sync node for id " << id; |
| 1375 continue; | 1378 continue; |
| 1376 } | 1379 } |
| 1377 id = sync_node.GetSuccessorId(); | 1380 id = sync_node.GetSuccessorId(); |
| 1378 const sync_pb::SessionSpecifics& specifics = | 1381 const sync_pb::SessionSpecifics& specifics = |
| 1379 sync_node.GetSessionSpecifics(); | 1382 sync_node.GetSessionSpecifics(); |
| 1380 if (specifics.session_tag() == tag) | 1383 if (specifics.session_tag() == tag) |
| 1381 sync_node.Remove(); | 1384 sync_node.Remove(); |
| 1382 } | 1385 } |
| 1383 } | 1386 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1449 bool SessionModelAssociator::CryptoReadyIfNecessary() { | 1452 bool SessionModelAssociator::CryptoReadyIfNecessary() { |
| 1450 // We only access the cryptographer while holding a transaction. | 1453 // We only access the cryptographer while holding a transaction. |
| 1451 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1454 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 1452 const syncable::ModelTypeSet encrypted_types = | 1455 const syncable::ModelTypeSet encrypted_types = |
| 1453 sync_api::GetEncryptedTypes(&trans); | 1456 sync_api::GetEncryptedTypes(&trans); |
| 1454 return !encrypted_types.Has(SESSIONS) || | 1457 return !encrypted_types.Has(SESSIONS) || |
| 1455 sync_service_->IsCryptographerReady(&trans); | 1458 sync_service_->IsCryptographerReady(&trans); |
| 1456 } | 1459 } |
| 1457 | 1460 |
| 1458 } // namespace browser_sync | 1461 } // namespace browser_sync |
| OLD | NEW |