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