OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
180 DCHECK(bookmark_model_); | 180 DCHECK(bookmark_model_); |
181 DCHECK(user_share_); | 181 DCHECK(user_share_); |
182 DCHECK(unrecoverable_error_handler_); | 182 DCHECK(unrecoverable_error_handler_); |
183 } | 183 } |
184 | 184 |
185 BookmarkModelAssociator::~BookmarkModelAssociator() { | 185 BookmarkModelAssociator::~BookmarkModelAssociator() { |
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
187 } | 187 } |
188 | 188 |
189 void BookmarkModelAssociator::UpdateMobileNodeVisibility() { | |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
191 DCHECK(bookmark_model_->IsLoaded()); | |
192 | |
193 bookmark_model_->SetMobileFolderVisible( | |
194 id_map_.find(bookmark_model_->mobile_node()->id()) != id_map_.end()); | |
195 } | |
196 | |
197 bool BookmarkModelAssociator::DisassociateModels(SyncError* error) { | 189 bool BookmarkModelAssociator::DisassociateModels(SyncError* error) { |
198 id_map_.clear(); | 190 id_map_.clear(); |
199 id_map_inverse_.clear(); | 191 id_map_inverse_.clear(); |
200 dirty_associations_sync_ids_.clear(); | 192 dirty_associations_sync_ids_.clear(); |
201 return true; | 193 return true; |
202 } | 194 } |
203 | 195 |
204 int64 BookmarkModelAssociator::GetSyncIdFromChromeId(const int64& node_id) { | 196 int64 BookmarkModelAssociator::GetSyncIdFromChromeId(const int64& node_id) { |
205 BookmarkIdToSyncIdMap::const_iterator iter = id_map_.find(node_id); | 197 BookmarkIdToSyncIdMap::const_iterator iter = id_map_.find(node_id); |
206 return iter == id_map_.end() ? sync_api::kInvalidId : iter->second; | 198 return iter == id_map_.end() ? sync_api::kInvalidId : iter->second; |
(...skipping 22 matching lines...) Expand all Loading... |
229 int64 sync_id) { | 221 int64 sync_id) { |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
231 int64 node_id = node->id(); | 223 int64 node_id = node->id(); |
232 DCHECK_NE(sync_id, sync_api::kInvalidId); | 224 DCHECK_NE(sync_id, sync_api::kInvalidId); |
233 DCHECK(id_map_.find(node_id) == id_map_.end()); | 225 DCHECK(id_map_.find(node_id) == id_map_.end()); |
234 DCHECK(id_map_inverse_.find(sync_id) == id_map_inverse_.end()); | 226 DCHECK(id_map_inverse_.find(sync_id) == id_map_inverse_.end()); |
235 id_map_[node_id] = sync_id; | 227 id_map_[node_id] = sync_id; |
236 id_map_inverse_[sync_id] = node; | 228 id_map_inverse_[sync_id] = node; |
237 dirty_associations_sync_ids_.insert(sync_id); | 229 dirty_associations_sync_ids_.insert(sync_id); |
238 PostPersistAssociationsTask(); | 230 PostPersistAssociationsTask(); |
239 UpdateMobileNodeVisibility(); | |
240 } | 231 } |
241 | 232 |
242 void BookmarkModelAssociator::Disassociate(int64 sync_id) { | 233 void BookmarkModelAssociator::Disassociate(int64 sync_id) { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
244 SyncIdToBookmarkNodeMap::iterator iter = id_map_inverse_.find(sync_id); | 235 SyncIdToBookmarkNodeMap::iterator iter = id_map_inverse_.find(sync_id); |
245 if (iter == id_map_inverse_.end()) | 236 if (iter == id_map_inverse_.end()) |
246 return; | 237 return; |
247 id_map_.erase(iter->second->id()); | 238 id_map_.erase(iter->second->id()); |
248 id_map_inverse_.erase(iter); | 239 id_map_inverse_.erase(iter); |
249 dirty_associations_sync_ids_.erase(sync_id); | 240 dirty_associations_sync_ids_.erase(sync_id); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 if (!AssociateTaggedPermanentNode(bookmark_model_->other_node(), | 361 if (!AssociateTaggedPermanentNode(bookmark_model_->other_node(), |
371 kOtherBookmarksTag)) { | 362 kOtherBookmarksTag)) { |
372 error->Reset(FROM_HERE, kServerError, model_type()); | 363 error->Reset(FROM_HERE, kServerError, model_type()); |
373 return false; | 364 return false; |
374 } | 365 } |
375 if (!AssociateTaggedPermanentNode(bookmark_model_->bookmark_bar_node(), | 366 if (!AssociateTaggedPermanentNode(bookmark_model_->bookmark_bar_node(), |
376 kBookmarkBarTag)) { | 367 kBookmarkBarTag)) { |
377 error->Reset(FROM_HERE, kServerError, model_type()); | 368 error->Reset(FROM_HERE, kServerError, model_type()); |
378 return false; | 369 return false; |
379 } | 370 } |
380 if (!AssociateTaggedPermanentNode(bookmark_model_->mobile_node(), | 371 // The mobile folder isn't always present on the backend, so we don't fail if |
381 kMobileBookmarksTag) && | 372 // it doesn't exist. |
382 // The mobile folder only need exist if kCreateMobileBookmarksFolder is | 373 ignore_result(AssociateTaggedPermanentNode(bookmark_model_->mobile_node(), |
383 // set. | 374 kMobileBookmarksTag)); |
384 CommandLine::ForCurrentProcess()->HasSwitch( | |
385 switches::kCreateMobileBookmarksFolder)) { | |
386 error->Reset(FROM_HERE, kServerError, model_type()); | |
387 return false; | |
388 } | |
389 int64 bookmark_bar_sync_id = GetSyncIdFromChromeId( | 375 int64 bookmark_bar_sync_id = GetSyncIdFromChromeId( |
390 bookmark_model_->bookmark_bar_node()->id()); | 376 bookmark_model_->bookmark_bar_node()->id()); |
391 DCHECK_NE(bookmark_bar_sync_id, sync_api::kInvalidId); | 377 DCHECK_NE(bookmark_bar_sync_id, sync_api::kInvalidId); |
392 int64 other_bookmarks_sync_id = GetSyncIdFromChromeId( | 378 int64 other_bookmarks_sync_id = GetSyncIdFromChromeId( |
393 bookmark_model_->other_node()->id()); | 379 bookmark_model_->other_node()->id()); |
394 DCHECK_NE(other_bookmarks_sync_id, sync_api::kInvalidId); | 380 DCHECK_NE(other_bookmarks_sync_id, sync_api::kInvalidId); |
395 int64 mobile_bookmarks_sync_id = GetSyncIdFromChromeId( | 381 int64 mobile_bookmarks_sync_id = GetSyncIdFromChromeId( |
396 bookmark_model_->mobile_node()->id()); | 382 bookmark_model_->mobile_node()->id()); |
397 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
398 switches::kCreateMobileBookmarksFolder)) { | |
399 DCHECK_NE(sync_api::kInvalidId, mobile_bookmarks_sync_id); | |
400 } | |
401 | 383 |
402 std::stack<int64> dfs_stack; | 384 std::stack<int64> dfs_stack; |
403 if (mobile_bookmarks_sync_id != sync_api::kInvalidId) | 385 if (mobile_bookmarks_sync_id != sync_api::kInvalidId) |
404 dfs_stack.push(mobile_bookmarks_sync_id); | 386 dfs_stack.push(mobile_bookmarks_sync_id); |
405 dfs_stack.push(other_bookmarks_sync_id); | 387 dfs_stack.push(other_bookmarks_sync_id); |
406 dfs_stack.push(bookmark_bar_sync_id); | 388 dfs_stack.push(bookmark_bar_sync_id); |
407 | 389 |
408 sync_api::WriteTransaction trans(FROM_HERE, user_share_); | 390 sync_api::WriteTransaction trans(FROM_HERE, user_share_); |
409 | 391 |
410 while (!dfs_stack.empty()) { | 392 while (!dfs_stack.empty()) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 if (!GetSyncIdForTaggedNode(kBookmarkBarTag, &bookmark_bar_id)) { | 516 if (!GetSyncIdForTaggedNode(kBookmarkBarTag, &bookmark_bar_id)) { |
535 // We should always be able to find the permanent nodes. | 517 // We should always be able to find the permanent nodes. |
536 return false; | 518 return false; |
537 } | 519 } |
538 int64 other_bookmarks_id; | 520 int64 other_bookmarks_id; |
539 if (!GetSyncIdForTaggedNode(kOtherBookmarksTag, &other_bookmarks_id)) { | 521 if (!GetSyncIdForTaggedNode(kOtherBookmarksTag, &other_bookmarks_id)) { |
540 // We should always be able to find the permanent nodes. | 522 // We should always be able to find the permanent nodes. |
541 return false; | 523 return false; |
542 } | 524 } |
543 int64 mobile_bookmarks_id = -1; | 525 int64 mobile_bookmarks_id = -1; |
544 if (!GetSyncIdForTaggedNode(kMobileBookmarksTag, &mobile_bookmarks_id) && | 526 // Can't fail here as the mobile folder may not exist. |
545 CommandLine::ForCurrentProcess()->HasSwitch( | 527 ignore_result( |
546 switches::kCreateMobileBookmarksFolder)) { | 528 GetSyncIdForTaggedNode(kMobileBookmarksTag, &mobile_bookmarks_id)); |
547 return false; | |
548 } | |
549 | 529 |
550 // Build a bookmark node ID index since we are going to repeatedly search for | 530 // Build a bookmark node ID index since we are going to repeatedly search for |
551 // bookmark nodes by their IDs. | 531 // bookmark nodes by their IDs. |
552 BookmarkNodeIdIndex id_index; | 532 BookmarkNodeIdIndex id_index; |
553 id_index.AddAll(bookmark_model_->bookmark_bar_node()); | 533 id_index.AddAll(bookmark_model_->bookmark_bar_node()); |
554 id_index.AddAll(bookmark_model_->other_node()); | 534 id_index.AddAll(bookmark_model_->other_node()); |
555 id_index.AddAll(bookmark_model_->mobile_node()); | 535 id_index.AddAll(bookmark_model_->mobile_node()); |
556 | 536 |
557 std::stack<int64> dfs_stack; | 537 std::stack<int64> dfs_stack; |
558 if (mobile_bookmarks_id != -1) | 538 if (mobile_bookmarks_id != -1) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { | 596 bool BookmarkModelAssociator::CryptoReadyIfNecessary() { |
617 // We only access the cryptographer while holding a transaction. | 597 // We only access the cryptographer while holding a transaction. |
618 sync_api::ReadTransaction trans(FROM_HERE, user_share_); | 598 sync_api::ReadTransaction trans(FROM_HERE, user_share_); |
619 const syncable::ModelTypeSet& encrypted_types = | 599 const syncable::ModelTypeSet& encrypted_types = |
620 sync_api::GetEncryptedTypes(&trans); | 600 sync_api::GetEncryptedTypes(&trans); |
621 return encrypted_types.count(syncable::BOOKMARKS) == 0 || | 601 return encrypted_types.count(syncable::BOOKMARKS) == 0 || |
622 trans.GetCryptographer()->is_ready(); | 602 trans.GetCryptographer()->is_ready(); |
623 } | 603 } |
624 | 604 |
625 } // namespace browser_sync | 605 } // namespace browser_sync |
OLD | NEW |