Chromium Code Reviews| 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 |
|
Nicolas Zea
2012/02/27 20:03:12
remove extra newline
akalin
2012/02/27 22:04:18
Done.
| |
| 5 | |
| 5 #include "chrome/browser/sync/glue/bookmark_model_associator.h" | 6 #include "chrome/browser/sync/glue/bookmark_model_associator.h" |
| 6 | 7 |
| 7 #include <stack> | 8 #include <stack> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/sync/api/sync_error.h" | 18 #include "chrome/browser/sync/api/sync_error.h" |
| 18 #include "chrome/browser/sync/glue/bookmark_change_processor.h" | 19 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
| 19 #include "chrome/browser/sync/internal_api/read_node.h" | 20 #include "chrome/browser/sync/internal_api/read_node.h" |
| 20 #include "chrome/browser/sync/internal_api/read_transaction.h" | 21 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 21 #include "chrome/browser/sync/internal_api/write_node.h" | 22 #include "chrome/browser/sync/internal_api/write_node.h" |
| 22 #include "chrome/browser/sync/internal_api/write_transaction.h" | 23 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 23 #include "chrome/browser/sync/util/cryptographer.h" | 24 #include "chrome/browser/sync/util/cryptographer.h" |
| 24 #include "chrome/common/chrome_switches.h" | |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 | 28 |
| 29 namespace browser_sync { | 29 namespace browser_sync { |
| 30 | 30 |
| 31 // The sync protocol identifies top-level entities by means of well-known tags, | 31 // The sync protocol identifies top-level entities by means of well-known tags, |
| 32 // which should not be confused with titles. Each tag corresponds to a | 32 // which should not be confused with titles. Each tag corresponds to a |
| 33 // singleton instance of a particular top-level node in a user's share; the | 33 // singleton instance of a particular top-level node in a user's share; the |
| 34 // tags are consistent across users. The tags allow us to locate the specific | 34 // tags are consistent across users. The tags allow us to locate the specific |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 } | 180 } |
| 181 | 181 |
| 182 const BookmarkNode* BookmarkNodeIdIndex::Find(int64 id) const { | 182 const BookmarkNode* BookmarkNodeIdIndex::Find(int64 id) const { |
| 183 BookmarkIdMap::const_iterator iter = node_index_.find(id); | 183 BookmarkIdMap::const_iterator iter = node_index_.find(id); |
| 184 return iter == node_index_.end() ? NULL : iter->second; | 184 return iter == node_index_.end() ? NULL : iter->second; |
| 185 } | 185 } |
| 186 | 186 |
| 187 BookmarkModelAssociator::BookmarkModelAssociator( | 187 BookmarkModelAssociator::BookmarkModelAssociator( |
| 188 BookmarkModel* bookmark_model, | 188 BookmarkModel* bookmark_model, |
| 189 sync_api::UserShare* user_share, | 189 sync_api::UserShare* user_share, |
| 190 DataTypeErrorHandler* unrecoverable_error_handler) | 190 DataTypeErrorHandler* unrecoverable_error_handler, |
| 191 bool create_mobile_bookmarks_folder) | |
| 191 : bookmark_model_(bookmark_model), | 192 : bookmark_model_(bookmark_model), |
| 192 user_share_(user_share), | 193 user_share_(user_share), |
| 193 unrecoverable_error_handler_(unrecoverable_error_handler), | 194 unrecoverable_error_handler_(unrecoverable_error_handler), |
| 195 create_mobile_bookmarks_folder_(create_mobile_bookmarks_folder), | |
| 194 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 196 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 195 number_of_new_sync_nodes_created_at_association_(0) { | 197 number_of_new_sync_nodes_created_at_association_(0) { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 197 DCHECK(bookmark_model_); | 199 DCHECK(bookmark_model_); |
| 198 DCHECK(user_share_); | 200 DCHECK(user_share_); |
| 199 DCHECK(unrecoverable_error_handler_); | 201 DCHECK(unrecoverable_error_handler_); |
| 200 } | 202 } |
| 201 | 203 |
| 202 BookmarkModelAssociator::~BookmarkModelAssociator() { | 204 BookmarkModelAssociator::~BookmarkModelAssociator() { |
| 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 error->Reset(FROM_HERE, kServerError, model_type()); | 394 error->Reset(FROM_HERE, kServerError, model_type()); |
| 393 return false; | 395 return false; |
| 394 } | 396 } |
| 395 if (!AssociateTaggedPermanentNode(bookmark_model_->bookmark_bar_node(), | 397 if (!AssociateTaggedPermanentNode(bookmark_model_->bookmark_bar_node(), |
| 396 kBookmarkBarTag)) { | 398 kBookmarkBarTag)) { |
| 397 error->Reset(FROM_HERE, kServerError, model_type()); | 399 error->Reset(FROM_HERE, kServerError, model_type()); |
| 398 return false; | 400 return false; |
| 399 } | 401 } |
| 400 if (!AssociateTaggedPermanentNode(bookmark_model_->mobile_node(), | 402 if (!AssociateTaggedPermanentNode(bookmark_model_->mobile_node(), |
| 401 kMobileBookmarksTag) && | 403 kMobileBookmarksTag) && |
| 402 // The mobile folder only need exist if kCreateMobileBookmarksFolder is | 404 create_mobile_bookmarks_folder_) { |
| 403 // set. | |
| 404 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 405 switches::kCreateMobileBookmarksFolder)) { | |
| 406 error->Reset(FROM_HERE, kServerError, model_type()); | 405 error->Reset(FROM_HERE, kServerError, model_type()); |
| 407 return false; | 406 return false; |
| 408 } | 407 } |
| 408 | |
| 409 int64 bookmark_bar_sync_id = GetSyncIdFromChromeId( | 409 int64 bookmark_bar_sync_id = GetSyncIdFromChromeId( |
| 410 bookmark_model_->bookmark_bar_node()->id()); | 410 bookmark_model_->bookmark_bar_node()->id()); |
| 411 DCHECK_NE(bookmark_bar_sync_id, sync_api::kInvalidId); | 411 DCHECK_NE(bookmark_bar_sync_id, sync_api::kInvalidId); |
| 412 int64 other_bookmarks_sync_id = GetSyncIdFromChromeId( | 412 int64 other_bookmarks_sync_id = GetSyncIdFromChromeId( |
| 413 bookmark_model_->other_node()->id()); | 413 bookmark_model_->other_node()->id()); |
| 414 DCHECK_NE(other_bookmarks_sync_id, sync_api::kInvalidId); | 414 DCHECK_NE(other_bookmarks_sync_id, sync_api::kInvalidId); |
| 415 int64 mobile_bookmarks_sync_id = GetSyncIdFromChromeId( | 415 int64 mobile_bookmarks_sync_id = GetSyncIdFromChromeId( |
| 416 bookmark_model_->mobile_node()->id()); | 416 bookmark_model_->mobile_node()->id()); |
| 417 if (CommandLine::ForCurrentProcess()->HasSwitch( | 417 if (create_mobile_bookmarks_folder_) { |
| 418 switches::kCreateMobileBookmarksFolder)) { | |
| 419 DCHECK_NE(sync_api::kInvalidId, mobile_bookmarks_sync_id); | 418 DCHECK_NE(sync_api::kInvalidId, mobile_bookmarks_sync_id); |
| 420 } | 419 } |
| 421 | 420 |
| 422 std::stack<int64> dfs_stack; | 421 std::stack<int64> dfs_stack; |
| 423 if (mobile_bookmarks_sync_id != sync_api::kInvalidId) | 422 if (mobile_bookmarks_sync_id != sync_api::kInvalidId) |
| 424 dfs_stack.push(mobile_bookmarks_sync_id); | 423 dfs_stack.push(mobile_bookmarks_sync_id); |
| 425 dfs_stack.push(other_bookmarks_sync_id); | 424 dfs_stack.push(other_bookmarks_sync_id); |
| 426 dfs_stack.push(bookmark_bar_sync_id); | 425 dfs_stack.push(bookmark_bar_sync_id); |
| 427 | 426 |
| 428 sync_api::WriteTransaction trans(FROM_HERE, user_share_); | 427 sync_api::WriteTransaction trans(FROM_HERE, user_share_); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 // We should always be able to find the permanent nodes. | 563 // We should always be able to find the permanent nodes. |
| 565 return false; | 564 return false; |
| 566 } | 565 } |
| 567 int64 other_bookmarks_id; | 566 int64 other_bookmarks_id; |
| 568 if (!GetSyncIdForTaggedNode(kOtherBookmarksTag, &other_bookmarks_id)) { | 567 if (!GetSyncIdForTaggedNode(kOtherBookmarksTag, &other_bookmarks_id)) { |
| 569 // We should always be able to find the permanent nodes. | 568 // We should always be able to find the permanent nodes. |
| 570 return false; | 569 return false; |
| 571 } | 570 } |
| 572 int64 mobile_bookmarks_id = -1; | 571 int64 mobile_bookmarks_id = -1; |
| 573 if (!GetSyncIdForTaggedNode(kMobileBookmarksTag, &mobile_bookmarks_id) && | 572 if (!GetSyncIdForTaggedNode(kMobileBookmarksTag, &mobile_bookmarks_id) && |
| 574 CommandLine::ForCurrentProcess()->HasSwitch( | 573 create_mobile_bookmarks_folder_) { |
| 575 switches::kCreateMobileBookmarksFolder)) { | |
| 576 return false; | 574 return false; |
| 577 } | 575 } |
| 578 | 576 |
| 579 // Build a bookmark node ID index since we are going to repeatedly search for | 577 // Build a bookmark node ID index since we are going to repeatedly search for |
| 580 // bookmark nodes by their IDs. | 578 // bookmark nodes by their IDs. |
| 581 BookmarkNodeIdIndex id_index; | 579 BookmarkNodeIdIndex id_index; |
| 582 id_index.AddAll(bookmark_model_->bookmark_bar_node()); | 580 id_index.AddAll(bookmark_model_->bookmark_bar_node()); |
| 583 id_index.AddAll(bookmark_model_->other_node()); | 581 id_index.AddAll(bookmark_model_->other_node()); |
| 584 id_index.AddAll(bookmark_model_->mobile_node()); | 582 id_index.AddAll(bookmark_model_->mobile_node()); |
| 585 | 583 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { | 643 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { |
| 646 // We only access the cryptographer while holding a transaction. | 644 // We only access the cryptographer while holding a transaction. |
| 647 sync_api::ReadTransaction trans(FROM_HERE, user_share_); | 645 sync_api::ReadTransaction trans(FROM_HERE, user_share_); |
| 648 const syncable::ModelTypeSet encrypted_types = | 646 const syncable::ModelTypeSet encrypted_types = |
| 649 sync_api::GetEncryptedTypes(&trans); | 647 sync_api::GetEncryptedTypes(&trans); |
| 650 return !encrypted_types.Has(syncable::BOOKMARKS) || | 648 return !encrypted_types.Has(syncable::BOOKMARKS) || |
| 651 trans.GetCryptographer()->is_ready(); | 649 trans.GetCryptographer()->is_ready(); |
| 652 } | 650 } |
| 653 | 651 |
| 654 } // namespace browser_sync | 652 } // namespace browser_sync |
| OLD | NEW |