| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/bookmark_change_processor.h" | 5 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <stack> | 8 #include <stack> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 113 |
| 114 void BookmarkChangeProcessor::RemoveSyncNodeHierarchy( | 114 void BookmarkChangeProcessor::RemoveSyncNodeHierarchy( |
| 115 const BookmarkNode* topmost) { | 115 const BookmarkNode* topmost) { |
| 116 int64 new_version = | 116 int64 new_version = |
| 117 syncer::syncable::kInvalidTransactionVersion; | 117 syncer::syncable::kInvalidTransactionVersion; |
| 118 { | 118 { |
| 119 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); | 119 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); |
| 120 syncer::WriteNode topmost_sync_node(&trans); | 120 syncer::WriteNode topmost_sync_node(&trans); |
| 121 if (!model_associator_->InitSyncNodeFromChromeId(topmost->id(), | 121 if (!model_associator_->InitSyncNodeFromChromeId(topmost->id(), |
| 122 &topmost_sync_node)) { | 122 &topmost_sync_node)) { |
| 123 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 123 syncer::SyncError error(FROM_HERE, |
| 124 std::string()); | 124 syncer::SyncError::DATATYPE_ERROR, |
| 125 "Failed to init sync node from chrome node", |
| 126 syncer::BOOKMARKS); |
| 127 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 125 return; | 128 return; |
| 126 } | 129 } |
| 127 // Check that |topmost| has been unlinked. | 130 // Check that |topmost| has been unlinked. |
| 128 DCHECK(topmost->is_root()); | 131 DCHECK(topmost->is_root()); |
| 129 RemoveAllChildNodes(&trans, topmost->id()); | 132 RemoveAllChildNodes(&trans, topmost->id()); |
| 130 // Remove the node itself. | 133 // Remove the node itself. |
| 131 RemoveOneSyncNode(&topmost_sync_node); | 134 RemoveOneSyncNode(&topmost_sync_node); |
| 132 } | 135 } |
| 133 | 136 |
| 134 // Don't need to update versions of deleted nodes. | 137 // Don't need to update versions of deleted nodes. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 156 // Don't need to update versions of deleted nodes. | 159 // Don't need to update versions of deleted nodes. |
| 157 UpdateTransactionVersion(new_version, bookmark_model_, | 160 UpdateTransactionVersion(new_version, bookmark_model_, |
| 158 std::vector<const BookmarkNode*>()); | 161 std::vector<const BookmarkNode*>()); |
| 159 } | 162 } |
| 160 | 163 |
| 161 void BookmarkChangeProcessor::RemoveAllChildNodes( | 164 void BookmarkChangeProcessor::RemoveAllChildNodes( |
| 162 syncer::WriteTransaction* trans, const int64& topmost_node_id) { | 165 syncer::WriteTransaction* trans, const int64& topmost_node_id) { |
| 163 syncer::WriteNode topmost_node(trans); | 166 syncer::WriteNode topmost_node(trans); |
| 164 if (!model_associator_->InitSyncNodeFromChromeId(topmost_node_id, | 167 if (!model_associator_->InitSyncNodeFromChromeId(topmost_node_id, |
| 165 &topmost_node)) { | 168 &topmost_node)) { |
| 166 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 169 syncer::SyncError error(FROM_HERE, |
| 167 std::string()); | 170 syncer::SyncError::DATATYPE_ERROR, |
| 171 "Failed to init sync node from chrome node", |
| 172 syncer::BOOKMARKS); |
| 173 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 168 return; | 174 return; |
| 169 } | 175 } |
| 170 const int64 topmost_sync_id = topmost_node.GetId(); | 176 const int64 topmost_sync_id = topmost_node.GetId(); |
| 171 | 177 |
| 172 // Do a DFS and delete all the child sync nodes, use sync id instead of | 178 // Do a DFS and delete all the child sync nodes, use sync id instead of |
| 173 // bookmark node ids since the bookmark nodes may already be deleted. | 179 // bookmark node ids since the bookmark nodes may already be deleted. |
| 174 // The equivalent recursive version of this iterative DFS: | 180 // The equivalent recursive version of this iterative DFS: |
| 175 // remove_all_children(node_id, topmost_node_id): | 181 // remove_all_children(node_id, topmost_node_id): |
| 176 // node.initByIdLookup(node_id) | 182 // node.initByIdLookup(node_id) |
| 177 // while(node.GetFirstChildId() != syncer::kInvalidId) | 183 // while(node.GetFirstChildId() != syncer::kInvalidId) |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 BookmarkModelAssociator* associator, | 271 BookmarkModelAssociator* associator, |
| 266 sync_driver::DataTypeErrorHandler* error_handler) { | 272 sync_driver::DataTypeErrorHandler* error_handler) { |
| 267 const BookmarkNode* child = parent->GetChild(index); | 273 const BookmarkNode* child = parent->GetChild(index); |
| 268 DCHECK(child); | 274 DCHECK(child); |
| 269 | 275 |
| 270 // Create a WriteNode container to hold the new node. | 276 // Create a WriteNode container to hold the new node. |
| 271 syncer::WriteNode sync_child(trans); | 277 syncer::WriteNode sync_child(trans); |
| 272 | 278 |
| 273 // Actually create the node with the appropriate initial position. | 279 // Actually create the node with the appropriate initial position. |
| 274 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator)) { | 280 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator)) { |
| 275 error_handler->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 281 syncer::SyncError error(FROM_HERE, |
| 276 "Sync node creation failed; recovery unlikely"); | 282 syncer::SyncError::DATATYPE_ERROR, |
| 283 "Failed ot creat sync node.", |
| 284 syncer::BOOKMARKS); |
| 285 error_handler->OnSingleDataTypeUnrecoverableError(error); |
| 277 return syncer::kInvalidId; | 286 return syncer::kInvalidId; |
| 278 } | 287 } |
| 279 | 288 |
| 280 UpdateSyncNodeProperties(child, model, &sync_child); | 289 UpdateSyncNodeProperties(child, model, &sync_child); |
| 281 | 290 |
| 282 // Associate the ID from the sync domain with the bookmark node, so that we | 291 // Associate the ID from the sync domain with the bookmark node, so that we |
| 283 // can refer back to this item later. | 292 // can refer back to this item later. |
| 284 associator->Associate(child, sync_child.GetId()); | 293 associator->Associate(child, sync_child.GetId()); |
| 285 | 294 |
| 286 return sync_child.GetId(); | 295 return sync_child.GetId(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 314 // Static. | 323 // Static. |
| 315 int64 BookmarkChangeProcessor::UpdateSyncNode( | 324 int64 BookmarkChangeProcessor::UpdateSyncNode( |
| 316 const BookmarkNode* node, | 325 const BookmarkNode* node, |
| 317 BookmarkModel* model, | 326 BookmarkModel* model, |
| 318 syncer::WriteTransaction* trans, | 327 syncer::WriteTransaction* trans, |
| 319 BookmarkModelAssociator* associator, | 328 BookmarkModelAssociator* associator, |
| 320 sync_driver::DataTypeErrorHandler* error_handler) { | 329 sync_driver::DataTypeErrorHandler* error_handler) { |
| 321 // Lookup the sync node that's associated with |node|. | 330 // Lookup the sync node that's associated with |node|. |
| 322 syncer::WriteNode sync_node(trans); | 331 syncer::WriteNode sync_node(trans); |
| 323 if (!associator->InitSyncNodeFromChromeId(node->id(), &sync_node)) { | 332 if (!associator->InitSyncNodeFromChromeId(node->id(), &sync_node)) { |
| 324 error_handler->OnSingleDatatypeUnrecoverableError( | 333 syncer::SyncError error(FROM_HERE, |
| 325 FROM_HERE, "Could not load bookmark node on update."); | 334 syncer::SyncError::DATATYPE_ERROR, |
| 335 "Failed to init sync node from chrome node", |
| 336 syncer::BOOKMARKS); |
| 337 error_handler->OnSingleDataTypeUnrecoverableError(error); |
| 326 return syncer::kInvalidId; | 338 return syncer::kInvalidId; |
| 327 } | 339 } |
| 328 UpdateSyncNodeProperties(node, model, &sync_node); | 340 UpdateSyncNodeProperties(node, model, &sync_node); |
| 329 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); | 341 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); |
| 330 DCHECK_EQ(associator->GetChromeNodeFromSyncId(sync_node.GetParentId()), | 342 DCHECK_EQ(associator->GetChromeNodeFromSyncId(sync_node.GetParentId()), |
| 331 node->parent()); | 343 node->parent()); |
| 332 DCHECK_EQ(node->parent()->GetIndexOf(node), sync_node.GetPositionIndex()); | 344 DCHECK_EQ(node->parent()->GetIndexOf(node), sync_node.GetPositionIndex()); |
| 333 return sync_node.GetId(); | 345 return sync_node.GetId(); |
| 334 } | 346 } |
| 335 | 347 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 349 } | 361 } |
| 350 | 362 |
| 351 int64 new_version = syncer::syncable::kInvalidTransactionVersion; | 363 int64 new_version = syncer::syncable::kInvalidTransactionVersion; |
| 352 { | 364 { |
| 353 // Acquire a scoped write lock via a transaction. | 365 // Acquire a scoped write lock via a transaction. |
| 354 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); | 366 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); |
| 355 | 367 |
| 356 // Lookup the sync node that's associated with |child|. | 368 // Lookup the sync node that's associated with |child|. |
| 357 syncer::WriteNode sync_node(&trans); | 369 syncer::WriteNode sync_node(&trans); |
| 358 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { | 370 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { |
| 359 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 371 syncer::SyncError error(FROM_HERE, |
| 360 std::string()); | 372 syncer::SyncError::DATATYPE_ERROR, |
| 373 "Failed to init sync node from chrome node", |
| 374 syncer::BOOKMARKS); |
| 375 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 361 return; | 376 return; |
| 362 } | 377 } |
| 363 | 378 |
| 364 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, | 379 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, |
| 365 model_associator_)) { | 380 model_associator_)) { |
| 366 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 381 syncer::SyncError error(FROM_HERE, |
| 367 std::string()); | 382 syncer::SyncError::DATATYPE_ERROR, |
| 383 "Failed to place sync node", |
| 384 syncer::BOOKMARKS); |
| 385 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 368 return; | 386 return; |
| 369 } | 387 } |
| 370 } | 388 } |
| 371 | 389 |
| 372 UpdateTransactionVersion(new_version, model, | 390 UpdateTransactionVersion(new_version, model, |
| 373 std::vector<const BookmarkNode*>(1, child)); | 391 std::vector<const BookmarkNode*>(1, child)); |
| 374 } | 392 } |
| 375 | 393 |
| 376 void BookmarkChangeProcessor::BookmarkNodeFaviconChanged( | 394 void BookmarkChangeProcessor::BookmarkNodeFaviconChanged( |
| 377 BookmarkModel* model, | 395 BookmarkModel* model, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 389 | 407 |
| 390 // The given node's children got reordered. We need to reorder all the | 408 // The given node's children got reordered. We need to reorder all the |
| 391 // children of the corresponding sync node. | 409 // children of the corresponding sync node. |
| 392 for (int i = 0; i < node->child_count(); ++i) { | 410 for (int i = 0; i < node->child_count(); ++i) { |
| 393 const BookmarkNode* child = node->GetChild(i); | 411 const BookmarkNode* child = node->GetChild(i); |
| 394 children.push_back(child); | 412 children.push_back(child); |
| 395 | 413 |
| 396 syncer::WriteNode sync_child(&trans); | 414 syncer::WriteNode sync_child(&trans); |
| 397 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), | 415 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), |
| 398 &sync_child)) { | 416 &sync_child)) { |
| 399 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 417 syncer::SyncError error(FROM_HERE, |
| 400 std::string()); | 418 syncer::SyncError::DATATYPE_ERROR, |
| 419 "Failed to init sync node from chrome node", |
| 420 syncer::BOOKMARKS); |
| 421 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 401 return; | 422 return; |
| 402 } | 423 } |
| 403 DCHECK_EQ(sync_child.GetParentId(), | 424 DCHECK_EQ(sync_child.GetParentId(), |
| 404 model_associator_->GetSyncIdFromChromeId(node->id())); | 425 model_associator_->GetSyncIdFromChromeId(node->id())); |
| 405 | 426 |
| 406 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, | 427 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, |
| 407 model_associator_)) { | 428 model_associator_)) { |
| 408 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 429 syncer::SyncError error(FROM_HERE, |
| 409 std::string()); | 430 syncer::SyncError::DATATYPE_ERROR, |
| 431 "Failed to place sync node", |
| 432 syncer::BOOKMARKS); |
| 433 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 410 return; | 434 return; |
| 411 } | 435 } |
| 412 } | 436 } |
| 413 } | 437 } |
| 414 | 438 |
| 415 // TODO(haitaol): Filter out children that didn't actually change. | 439 // TODO(haitaol): Filter out children that didn't actually change. |
| 416 UpdateTransactionVersion(new_version, model, children); | 440 UpdateTransactionVersion(new_version, model, children); |
| 417 } | 441 } |
| 418 | 442 |
| 419 // static | 443 // static |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 continue; | 545 continue; |
| 522 | 546 |
| 523 // Children of a deleted node should not be deleted; they may be | 547 // Children of a deleted node should not be deleted; they may be |
| 524 // reparented by a later change record. Move them to a temporary place. | 548 // reparented by a later change record. Move them to a temporary place. |
| 525 if (!dst->empty()) { | 549 if (!dst->empty()) { |
| 526 if (!foster_parent) { | 550 if (!foster_parent) { |
| 527 foster_parent = model->AddFolder(model->other_node(), | 551 foster_parent = model->AddFolder(model->other_node(), |
| 528 model->other_node()->child_count(), | 552 model->other_node()->child_count(), |
| 529 base::string16()); | 553 base::string16()); |
| 530 if (!foster_parent) { | 554 if (!foster_parent) { |
| 531 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 555 syncer::SyncError error(FROM_HERE, |
| 532 "Failed to create foster parent."); | 556 syncer::SyncError::DATATYPE_ERROR, |
| 557 "Failed to create foster parent", |
| 558 syncer::BOOKMARKS); |
| 559 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 533 return; | 560 return; |
| 534 } | 561 } |
| 535 } | 562 } |
| 536 for (int i = dst->child_count() - 1; i >= 0; --i) { | 563 for (int i = dst->child_count() - 1; i >= 0; --i) { |
| 537 model->Move(dst->GetChild(i), foster_parent, | 564 model->Move(dst->GetChild(i), foster_parent, |
| 538 foster_parent->child_count()); | 565 foster_parent->child_count()); |
| 539 } | 566 } |
| 540 } | 567 } |
| 541 DCHECK_EQ(dst->child_count(), 0) << "Node being deleted has children"; | 568 DCHECK_EQ(dst->child_count(), 0) << "Node being deleted has children"; |
| 542 | 569 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 // This is a newly created Synced Bookmarks node. Associate it. | 606 // This is a newly created Synced Bookmarks node. Associate it. |
| 580 model_associator_->Associate(model->mobile_node(), it->id); | 607 model_associator_->Associate(model->mobile_node(), it->id); |
| 581 continue; | 608 continue; |
| 582 } | 609 } |
| 583 | 610 |
| 584 DCHECK_NE(it->action, ChangeRecord::ACTION_DELETE) | 611 DCHECK_NE(it->action, ChangeRecord::ACTION_DELETE) |
| 585 << "We should have passed all deletes by this point."; | 612 << "We should have passed all deletes by this point."; |
| 586 | 613 |
| 587 syncer::ReadNode src(trans); | 614 syncer::ReadNode src(trans); |
| 588 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { | 615 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { |
| 589 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 616 syncer::SyncError error(FROM_HERE, |
| 590 "ApplyModelChanges was passed a bad ID"); | 617 syncer::SyncError::DATATYPE_ERROR, |
| 618 "Failed to load sync node", |
| 619 syncer::BOOKMARKS); |
| 620 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 591 return; | 621 return; |
| 592 } | 622 } |
| 593 | 623 |
| 594 const BookmarkNode* parent = | 624 const BookmarkNode* parent = |
| 595 model_associator_->GetChromeNodeFromSyncId(src.GetParentId()); | 625 model_associator_->GetChromeNodeFromSyncId(src.GetParentId()); |
| 596 if (!parent) { | 626 if (!parent) { |
| 597 LOG(ERROR) << "Could not find parent of node being added/updated." | 627 LOG(ERROR) << "Could not find parent of node being added/updated." |
| 598 << " Node title: " << src.GetTitle() | 628 << " Node title: " << src.GetTitle() |
| 599 << ", parent id = " << src.GetParentId(); | 629 << ", parent id = " << src.GetParentId(); |
| 600 continue; | 630 continue; |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 sync_pb::BookmarkSpecifics updated_specifics( | 868 sync_pb::BookmarkSpecifics updated_specifics( |
| 839 sync_node->GetBookmarkSpecifics()); | 869 sync_node->GetBookmarkSpecifics()); |
| 840 updated_specifics.set_favicon(favicon_bytes->front(), | 870 updated_specifics.set_favicon(favicon_bytes->front(), |
| 841 favicon_bytes->size()); | 871 favicon_bytes->size()); |
| 842 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); | 872 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); |
| 843 sync_node->SetBookmarkSpecifics(updated_specifics); | 873 sync_node->SetBookmarkSpecifics(updated_specifics); |
| 844 } | 874 } |
| 845 } | 875 } |
| 846 | 876 |
| 847 } // namespace browser_sync | 877 } // namespace browser_sync |
| OLD | NEW |