| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 <stack> | 7 #include <stack> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Re-encode the BookmarkNode's favicon as a PNG, and pass the data to the | 70 // Re-encode the BookmarkNode's favicon as a PNG, and pass the data to the |
| 71 // sync subsystem. | 71 // sync subsystem. |
| 72 if (!gfx::PNGCodec::EncodeBGRASkBitmap(favicon, false, dst)) | 72 if (!gfx::PNGCodec::EncodeBGRASkBitmap(favicon, false, dst)) |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void BookmarkChangeProcessor::RemoveOneSyncNode( | 76 void BookmarkChangeProcessor::RemoveOneSyncNode( |
| 77 sync_api::WriteTransaction* trans, const BookmarkNode* node) { | 77 sync_api::WriteTransaction* trans, const BookmarkNode* node) { |
| 78 sync_api::WriteNode sync_node(trans); | 78 sync_api::WriteNode sync_node(trans); |
| 79 if (!model_associator_->InitSyncNodeFromChromeId(node->id(), &sync_node)) { | 79 if (!model_associator_->InitSyncNodeFromChromeId(node->id(), &sync_node)) { |
| 80 error_handler()->OnUnrecoverableError(); | 80 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 // This node should have no children. | 83 // This node should have no children. |
| 84 DCHECK(sync_node.GetFirstChildId() == sync_api::kInvalidId); | 84 DCHECK(sync_node.GetFirstChildId() == sync_api::kInvalidId); |
| 85 // Remove association and delete the sync node. | 85 // Remove association and delete the sync node. |
| 86 model_associator_->Disassociate(sync_node.GetId()); | 86 model_associator_->Disassociate(sync_node.GetId()); |
| 87 sync_node.Remove(); | 87 sync_node.Remove(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void BookmarkChangeProcessor::RemoveSyncNodeHierarchy( | 90 void BookmarkChangeProcessor::RemoveSyncNodeHierarchy( |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 UnrecoverableErrorHandler* error_handler) { | 151 UnrecoverableErrorHandler* error_handler) { |
| 152 const BookmarkNode* child = parent->GetChild(index); | 152 const BookmarkNode* child = parent->GetChild(index); |
| 153 DCHECK(child); | 153 DCHECK(child); |
| 154 | 154 |
| 155 // Create a WriteNode container to hold the new node. | 155 // Create a WriteNode container to hold the new node. |
| 156 sync_api::WriteNode sync_child(trans); | 156 sync_api::WriteNode sync_child(trans); |
| 157 | 157 |
| 158 // Actually create the node with the appropriate initial position. | 158 // Actually create the node with the appropriate initial position. |
| 159 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator, | 159 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator, |
| 160 error_handler)) { | 160 error_handler)) { |
| 161 LOG(WARNING) << "Sync node creation failed; recovery unlikely"; | 161 error_handler->OnUnrecoverableError(FROM_HERE, |
| 162 error_handler->OnUnrecoverableError(); | 162 "Sync node creation failed; recovery unlikely"); |
| 163 return sync_api::kInvalidId; | 163 return sync_api::kInvalidId; |
| 164 } | 164 } |
| 165 | 165 |
| 166 UpdateSyncNodeProperties(child, model, &sync_child); | 166 UpdateSyncNodeProperties(child, model, &sync_child); |
| 167 | 167 |
| 168 // Associate the ID from the sync domain with the bookmark node, so that we | 168 // Associate the ID from the sync domain with the bookmark node, so that we |
| 169 // can refer back to this item later. | 169 // can refer back to this item later. |
| 170 associator->Associate(child, sync_child.GetId()); | 170 associator->Associate(child, sync_child.GetId()); |
| 171 | 171 |
| 172 return sync_child.GetId(); | 172 return sync_child.GetId(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 189 NOTREACHED() << "Saw update to permanent node!"; | 189 NOTREACHED() << "Saw update to permanent node!"; |
| 190 return; | 190 return; |
| 191 } | 191 } |
| 192 | 192 |
| 193 // Acquire a scoped write lock via a transaction. | 193 // Acquire a scoped write lock via a transaction. |
| 194 sync_api::WriteTransaction trans(share_handle()); | 194 sync_api::WriteTransaction trans(share_handle()); |
| 195 | 195 |
| 196 // Lookup the sync node that's associated with |node|. | 196 // Lookup the sync node that's associated with |node|. |
| 197 sync_api::WriteNode sync_node(&trans); | 197 sync_api::WriteNode sync_node(&trans); |
| 198 if (!model_associator_->InitSyncNodeFromChromeId(node->id(), &sync_node)) { | 198 if (!model_associator_->InitSyncNodeFromChromeId(node->id(), &sync_node)) { |
| 199 error_handler()->OnUnrecoverableError(); | 199 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 200 return; | 200 return; |
| 201 } | 201 } |
| 202 | 202 |
| 203 UpdateSyncNodeProperties(node, model, &sync_node); | 203 UpdateSyncNodeProperties(node, model, &sync_node); |
| 204 | 204 |
| 205 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); | 205 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); |
| 206 DCHECK_EQ(model_associator_->GetChromeNodeFromSyncId( | 206 DCHECK_EQ(model_associator_->GetChromeNodeFromSyncId( |
| 207 sync_node.GetParentId()), | 207 sync_node.GetParentId()), |
| 208 node->GetParent()); | 208 node->GetParent()); |
| 209 // This node's index should be one more than the predecessor's index. | 209 // This node's index should be one more than the predecessor's index. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 223 NOTREACHED() << "Saw update to permanent node!"; | 223 NOTREACHED() << "Saw update to permanent node!"; |
| 224 return; | 224 return; |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Acquire a scoped write lock via a transaction. | 227 // Acquire a scoped write lock via a transaction. |
| 228 sync_api::WriteTransaction trans(share_handle()); | 228 sync_api::WriteTransaction trans(share_handle()); |
| 229 | 229 |
| 230 // Lookup the sync node that's associated with |child|. | 230 // Lookup the sync node that's associated with |child|. |
| 231 sync_api::WriteNode sync_node(&trans); | 231 sync_api::WriteNode sync_node(&trans); |
| 232 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { | 232 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { |
| 233 error_handler()->OnUnrecoverableError(); | 233 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 234 return; | 234 return; |
| 235 } | 235 } |
| 236 | 236 |
| 237 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, | 237 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, |
| 238 model_associator_, error_handler())) { | 238 model_associator_, error_handler())) { |
| 239 error_handler()->OnUnrecoverableError(); | 239 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 240 return; | 240 return; |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 void BookmarkChangeProcessor::BookmarkNodeFavIconLoaded(BookmarkModel* model, | 244 void BookmarkChangeProcessor::BookmarkNodeFavIconLoaded(BookmarkModel* model, |
| 245 const BookmarkNode* node) { | 245 const BookmarkNode* node) { |
| 246 DCHECK(running()); | 246 DCHECK(running()); |
| 247 BookmarkNodeChanged(model, node); | 247 BookmarkNodeChanged(model, node); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void BookmarkChangeProcessor::BookmarkNodeChildrenReordered( | 250 void BookmarkChangeProcessor::BookmarkNodeChildrenReordered( |
| 251 BookmarkModel* model, const BookmarkNode* node) { | 251 BookmarkModel* model, const BookmarkNode* node) { |
| 252 | 252 |
| 253 // Acquire a scoped write lock via a transaction. | 253 // Acquire a scoped write lock via a transaction. |
| 254 sync_api::WriteTransaction trans(share_handle()); | 254 sync_api::WriteTransaction trans(share_handle()); |
| 255 | 255 |
| 256 // The given node's children got reordered. We need to reorder all the | 256 // The given node's children got reordered. We need to reorder all the |
| 257 // children of the corresponding sync node. | 257 // children of the corresponding sync node. |
| 258 for (int i = 0; i < node->GetChildCount(); ++i) { | 258 for (int i = 0; i < node->GetChildCount(); ++i) { |
| 259 sync_api::WriteNode sync_child(&trans); | 259 sync_api::WriteNode sync_child(&trans); |
| 260 if (!model_associator_->InitSyncNodeFromChromeId(node->GetChild(i)->id(), | 260 if (!model_associator_->InitSyncNodeFromChromeId(node->GetChild(i)->id(), |
| 261 &sync_child)) { | 261 &sync_child)) { |
| 262 error_handler()->OnUnrecoverableError(); | 262 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 263 return; | 263 return; |
| 264 } | 264 } |
| 265 DCHECK_EQ(sync_child.GetParentId(), | 265 DCHECK_EQ(sync_child.GetParentId(), |
| 266 model_associator_->GetSyncIdFromChromeId(node->id())); | 266 model_associator_->GetSyncIdFromChromeId(node->id())); |
| 267 | 267 |
| 268 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, | 268 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, |
| 269 model_associator_, error_handler())) { | 269 model_associator_, error_handler())) { |
| 270 error_handler()->OnUnrecoverableError(); | 270 error_handler()->OnUnrecoverableError(FROM_HERE, std::string()); |
| 271 return; | 271 return; |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 } | 274 } |
| 275 | 275 |
| 276 // static | 276 // static |
| 277 bool BookmarkChangeProcessor::PlaceSyncNode(MoveOrCreate operation, | 277 bool BookmarkChangeProcessor::PlaceSyncNode(MoveOrCreate operation, |
| 278 const BookmarkNode* parent, int index, sync_api::WriteTransaction* trans, | 278 const BookmarkNode* parent, int index, sync_api::WriteTransaction* trans, |
| 279 sync_api::WriteNode* dst, BookmarkModelAssociator* associator, | 279 sync_api::WriteNode* dst, BookmarkModelAssociator* associator, |
| 280 UnrecoverableErrorHandler* error_handler) { | 280 UnrecoverableErrorHandler* error_handler) { |
| 281 sync_api::ReadNode sync_parent(trans); | 281 sync_api::ReadNode sync_parent(trans); |
| 282 if (!associator->InitSyncNodeFromChromeId(parent->id(), &sync_parent)) { | 282 if (!associator->InitSyncNodeFromChromeId(parent->id(), &sync_parent)) { |
| 283 LOG(WARNING) << "Parent lookup failed"; | 283 LOG(WARNING) << "Parent lookup failed"; |
| 284 error_handler->OnUnrecoverableError(); | 284 error_handler->OnUnrecoverableError(FROM_HERE, std::string()); |
| 285 return false; | 285 return false; |
| 286 } | 286 } |
| 287 | 287 |
| 288 bool success = false; | 288 bool success = false; |
| 289 if (index == 0) { | 289 if (index == 0) { |
| 290 // Insert into first position. | 290 // Insert into first position. |
| 291 success = (operation == CREATE) ? | 291 success = (operation == CREATE) ? |
| 292 dst->InitByCreation(syncable::BOOKMARKS, sync_parent, NULL) : | 292 dst->InitByCreation(syncable::BOOKMARKS, sync_parent, NULL) : |
| 293 dst->SetPosition(sync_parent, NULL); | 293 dst->SetPosition(sync_parent, NULL); |
| 294 if (success) { | 294 if (success) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 model_associator_->Disassociate(changes[i].id); | 405 model_associator_->Disassociate(changes[i].id); |
| 406 model->Remove(parent, parent->IndexOfChild(dst)); | 406 model->Remove(parent, parent->IndexOfChild(dst)); |
| 407 dst = NULL; | 407 dst = NULL; |
| 408 } else { | 408 } else { |
| 409 DCHECK_EQ((changes[i].action == | 409 DCHECK_EQ((changes[i].action == |
| 410 sync_api::SyncManager::ChangeRecord::ACTION_ADD), (dst == NULL)) | 410 sync_api::SyncManager::ChangeRecord::ACTION_ADD), (dst == NULL)) |
| 411 << "ACTION_ADD should be seen if and only if the node is unknown."; | 411 << "ACTION_ADD should be seen if and only if the node is unknown."; |
| 412 | 412 |
| 413 sync_api::ReadNode src(trans); | 413 sync_api::ReadNode src(trans); |
| 414 if (!src.InitByIdLookup(changes[i].id)) { | 414 if (!src.InitByIdLookup(changes[i].id)) { |
| 415 LOG(ERROR) << "ApplyModelChanges was passed a bad ID"; | 415 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 416 error_handler()->OnUnrecoverableError(); | 416 "ApplyModelChanges was passed a bad ID"); |
| 417 return; | 417 return; |
| 418 } | 418 } |
| 419 | 419 |
| 420 CreateOrUpdateBookmarkNode(&src, model); | 420 CreateOrUpdateBookmarkNode(&src, model); |
| 421 } | 421 } |
| 422 } | 422 } |
| 423 // Clean up the temporary node. | 423 // Clean up the temporary node. |
| 424 if (foster_parent) { | 424 if (foster_parent) { |
| 425 // There should be no nodes left under the foster parent. | 425 // There should be no nodes left under the foster parent. |
| 426 DCHECK_EQ(foster_parent->GetChildCount(), 0); | 426 DCHECK_EQ(foster_parent->GetChildCount(), 0); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 const BookmarkNode* bookmark_node, | 528 const BookmarkNode* bookmark_node, |
| 529 BookmarkModel* model, | 529 BookmarkModel* model, |
| 530 sync_api::WriteNode* sync_node) { | 530 sync_api::WriteNode* sync_node) { |
| 531 std::vector<unsigned char> favicon_bytes; | 531 std::vector<unsigned char> favicon_bytes; |
| 532 EncodeFavicon(bookmark_node, model, &favicon_bytes); | 532 EncodeFavicon(bookmark_node, model, &favicon_bytes); |
| 533 if (!favicon_bytes.empty()) | 533 if (!favicon_bytes.empty()) |
| 534 sync_node->SetFaviconBytes(favicon_bytes); | 534 sync_node->SetFaviconBytes(favicon_bytes); |
| 535 } | 535 } |
| 536 | 536 |
| 537 } // namespace browser_sync | 537 } // namespace browser_sync |
| OLD | NEW |