| 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/bookmark_model_associator.h" | 5 #include "chrome/browser/sync/glue/bookmark_model_associator.h" |
| 6 | 6 |
| 7 #include <stack> | 7 #include <stack> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sync/glue/bookmark_change_processor.h" | 18 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "sync/api/sync_error.h" | 20 #include "sync/api/sync_error.h" |
| 21 #include "sync/internal_api/public/read_node.h" | 21 #include "sync/internal_api/public/read_node.h" |
| 22 #include "sync/internal_api/public/read_transaction.h" | 22 #include "sync/internal_api/public/read_transaction.h" |
| 23 #include "sync/internal_api/public/write_node.h" | 23 #include "sync/internal_api/public/write_node.h" |
| 24 #include "sync/internal_api/public/write_transaction.h" | 24 #include "sync/internal_api/public/write_transaction.h" |
| 25 #include "sync/syncable/write_transaction.h" |
| 25 #include "sync/util/cryptographer.h" | 26 #include "sync/util/cryptographer.h" |
| 26 #include "sync/util/data_type_histogram.h" | 27 #include "sync/util/data_type_histogram.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 | 30 |
| 30 namespace browser_sync { | 31 namespace browser_sync { |
| 31 | 32 |
| 32 // The sync protocol identifies top-level entities by means of well-known tags, | 33 // The sync protocol identifies top-level entities by means of well-known tags, |
| 33 // which should not be confused with titles. Each tag corresponds to a | 34 // which should not be confused with titles. Each tag corresponds to a |
| 34 // singleton instance of a particular top-level node in a user's share; the | 35 // singleton instance of a particular top-level node in a user's share; the |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 void BookmarkModelAssociator::PersistAssociations() { | 521 void BookmarkModelAssociator::PersistAssociations() { |
| 521 // If there are no dirty associations we have nothing to do. We handle this | 522 // If there are no dirty associations we have nothing to do. We handle this |
| 522 // explicity instead of letting the for loop do it to avoid creating a write | 523 // explicity instead of letting the for loop do it to avoid creating a write |
| 523 // transaction in this case. | 524 // transaction in this case. |
| 524 if (dirty_associations_sync_ids_.empty()) { | 525 if (dirty_associations_sync_ids_.empty()) { |
| 525 DCHECK(id_map_.empty()); | 526 DCHECK(id_map_.empty()); |
| 526 DCHECK(id_map_inverse_.empty()); | 527 DCHECK(id_map_inverse_.empty()); |
| 527 return; | 528 return; |
| 528 } | 529 } |
| 529 | 530 |
| 530 syncer::WriteTransaction trans(FROM_HERE, user_share_); | 531 int64 new_version = syncer::syncable::kInvalidTransactionVersion; |
| 531 DirtyAssociationsSyncIds::iterator iter; | 532 std::vector<const BookmarkNode*> bnodes; |
| 532 for (iter = dirty_associations_sync_ids_.begin(); | 533 { |
| 533 iter != dirty_associations_sync_ids_.end(); | 534 syncer::WriteTransaction trans(FROM_HERE, user_share_, &new_version); |
| 534 ++iter) { | 535 DirtyAssociationsSyncIds::iterator iter; |
| 535 int64 sync_id = *iter; | 536 for (iter = dirty_associations_sync_ids_.begin(); |
| 536 syncer::WriteNode sync_node(&trans); | 537 iter != dirty_associations_sync_ids_.end(); |
| 537 if (sync_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { | 538 ++iter) { |
| 538 unrecoverable_error_handler_->OnSingleDatatypeUnrecoverableError( | 539 int64 sync_id = *iter; |
| 539 FROM_HERE, | 540 syncer::WriteNode sync_node(&trans); |
| 540 "Could not lookup bookmark node for ID persistence."); | 541 if (sync_node.InitByIdLookup(sync_id) != syncer::BaseNode::INIT_OK) { |
| 541 return; | 542 unrecoverable_error_handler_->OnSingleDatatypeUnrecoverableError( |
| 543 FROM_HERE, |
| 544 "Could not lookup bookmark node for ID persistence."); |
| 545 return; |
| 546 } |
| 547 const BookmarkNode* node = GetChromeNodeFromSyncId(sync_id); |
| 548 if (node && sync_node.GetExternalId() != node->id()) { |
| 549 sync_node.SetExternalId(node->id()); |
| 550 bnodes.push_back(node); |
| 551 } |
| 542 } | 552 } |
| 543 const BookmarkNode* node = GetChromeNodeFromSyncId(sync_id); | 553 dirty_associations_sync_ids_.clear(); |
| 544 if (node) | |
| 545 sync_node.SetExternalId(node->id()); | |
| 546 else | |
| 547 NOTREACHED(); | |
| 548 } | 554 } |
| 549 dirty_associations_sync_ids_.clear(); | 555 |
| 556 BookmarkChangeProcessor::UpdateTransactionVersion(new_version, |
| 557 bookmark_model_, |
| 558 bnodes); |
| 550 } | 559 } |
| 551 | 560 |
| 552 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { | 561 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { |
| 553 // We only access the cryptographer while holding a transaction. | 562 // We only access the cryptographer while holding a transaction. |
| 554 syncer::ReadTransaction trans(FROM_HERE, user_share_); | 563 syncer::ReadTransaction trans(FROM_HERE, user_share_); |
| 555 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); | 564 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); |
| 556 return !encrypted_types.Has(syncer::BOOKMARKS) || | 565 return !encrypted_types.Has(syncer::BOOKMARKS) || |
| 557 trans.GetCryptographer()->is_ready(); | 566 trans.GetCryptographer()->is_ready(); |
| 558 } | 567 } |
| 559 | 568 |
| 560 void BookmarkModelAssociator::CheckModelSyncState() const { | 569 void BookmarkModelAssociator::CheckModelSyncState() const { |
| 561 std::string version_str; | 570 std::string version_str; |
| 562 if (bookmark_model_->root_node()->GetMetaInfo(kBookmarkTransactionVersionKey, | 571 if (bookmark_model_->root_node()->GetMetaInfo(kBookmarkTransactionVersionKey, |
| 563 &version_str)) { | 572 &version_str)) { |
| 564 syncer::ReadTransaction trans(FROM_HERE, user_share_); | 573 syncer::ReadTransaction trans(FROM_HERE, user_share_); |
| 565 int64 native_version; | 574 int64 native_version; |
| 566 if (base::StringToInt64(version_str, &native_version) && | 575 if (base::StringToInt64(version_str, &native_version) && |
| 567 native_version != trans.GetModelVersion(syncer::BOOKMARKS)) { | 576 native_version != trans.GetModelVersion(syncer::BOOKMARKS)) { |
| 568 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", | 577 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", |
| 569 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); | 578 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); |
| 570 // Clear version on bookmark model so that we only report error once. | 579 // Clear version on bookmark model so that we only report error once. |
| 571 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), | 580 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), |
| 572 kBookmarkTransactionVersionKey); | 581 kBookmarkTransactionVersionKey); |
| 573 } | 582 } |
| 574 } | 583 } |
| 575 } | 584 } |
| 576 | 585 |
| 577 } // namespace browser_sync | 586 } // namespace browser_sync |
| OLD | NEW |