| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/internal_api/public/attachments/on_disk_attachment_store.h" | 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | |
| 9 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | 17 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" |
| 18 #include "sync/internal_api/public/attachments/attachment_util.h" | 18 #include "sync/internal_api/public/attachments/attachment_util.h" |
| 19 #include "sync/protocol/attachments.pb.h" | 19 #include "sync/protocol/attachments.pb.h" |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 } | 392 } |
| 393 DCHECK(status.ok()); | 393 DCHECK(status.ok()); |
| 394 | 394 |
| 395 // Upgrade code goes here. | 395 // Upgrade code goes here. |
| 396 | 396 |
| 397 if (metadata.schema_version() != kCurrentSchemaVersion) { | 397 if (metadata.schema_version() != kCurrentSchemaVersion) { |
| 398 DVLOG(1) << "Unknown schema version: " << metadata.schema_version(); | 398 DVLOG(1) << "Unknown schema version: " << metadata.schema_version(); |
| 399 return AttachmentStore::UNSPECIFIED_ERROR; | 399 return AttachmentStore::UNSPECIFIED_ERROR; |
| 400 } | 400 } |
| 401 | 401 |
| 402 db_ = db.Pass(); | 402 db_ = std::move(db); |
| 403 return AttachmentStore::SUCCESS; | 403 return AttachmentStore::SUCCESS; |
| 404 } | 404 } |
| 405 | 405 |
| 406 scoped_ptr<Attachment> OnDiskAttachmentStore::ReadSingleAttachment( | 406 scoped_ptr<Attachment> OnDiskAttachmentStore::ReadSingleAttachment( |
| 407 const AttachmentId& attachment_id, | 407 const AttachmentId& attachment_id, |
| 408 AttachmentStore::Component component) { | 408 AttachmentStore::Component component) { |
| 409 scoped_ptr<Attachment> attachment; | 409 scoped_ptr<Attachment> attachment; |
| 410 attachment_store_pb::RecordMetadata record_metadata; | 410 attachment_store_pb::RecordMetadata record_metadata; |
| 411 if (!ReadSingleRecordMetadata(attachment_id, &record_metadata)) { | 411 if (!ReadSingleRecordMetadata(attachment_id, &record_metadata)) { |
| 412 return attachment.Pass(); | 412 return attachment; |
| 413 } | 413 } |
| 414 if (!AttachmentHasReferenceFromComponent(record_metadata, | 414 if (!AttachmentHasReferenceFromComponent(record_metadata, |
| 415 ComponentToProto(component))) | 415 ComponentToProto(component))) |
| 416 return attachment.Pass(); | 416 return attachment; |
| 417 | 417 |
| 418 const std::string key = MakeDataKeyFromAttachmentId(attachment_id); | 418 const std::string key = MakeDataKeyFromAttachmentId(attachment_id); |
| 419 std::string data_str; | 419 std::string data_str; |
| 420 leveldb::Status status = db_->Get( | 420 leveldb::Status status = db_->Get( |
| 421 MakeNonCachingReadOptions(), key, &data_str); | 421 MakeNonCachingReadOptions(), key, &data_str); |
| 422 if (!status.ok()) { | 422 if (!status.ok()) { |
| 423 DVLOG(1) << "DB::Get for data failed: status=" << status.ToString(); | 423 DVLOG(1) << "DB::Get for data failed: status=" << status.ToString(); |
| 424 return attachment.Pass(); | 424 return attachment; |
| 425 } | 425 } |
| 426 scoped_refptr<base::RefCountedMemory> data = | 426 scoped_refptr<base::RefCountedMemory> data = |
| 427 base::RefCountedString::TakeString(&data_str); | 427 base::RefCountedString::TakeString(&data_str); |
| 428 uint32_t crc32c = ComputeCrc32c(data); | 428 uint32_t crc32c = ComputeCrc32c(data); |
| 429 if (record_metadata.has_crc32c()) { | 429 if (record_metadata.has_crc32c()) { |
| 430 if (record_metadata.crc32c() != crc32c) { | 430 if (record_metadata.crc32c() != crc32c) { |
| 431 DVLOG(1) << "Attachment crc32c does not match value read from store"; | 431 DVLOG(1) << "Attachment crc32c does not match value read from store"; |
| 432 return attachment.Pass(); | 432 return attachment; |
| 433 } | 433 } |
| 434 if (record_metadata.crc32c() != attachment_id.GetCrc32c()) { | 434 if (record_metadata.crc32c() != attachment_id.GetCrc32c()) { |
| 435 DVLOG(1) << "Attachment crc32c does not match value in AttachmentId"; | 435 DVLOG(1) << "Attachment crc32c does not match value in AttachmentId"; |
| 436 return attachment.Pass(); | 436 return attachment; |
| 437 } | 437 } |
| 438 } | 438 } |
| 439 attachment.reset( | 439 attachment.reset( |
| 440 new Attachment(Attachment::CreateFromParts(attachment_id, data))); | 440 new Attachment(Attachment::CreateFromParts(attachment_id, data))); |
| 441 return attachment.Pass(); | 441 return attachment; |
| 442 } | 442 } |
| 443 | 443 |
| 444 bool OnDiskAttachmentStore::WriteSingleAttachment( | 444 bool OnDiskAttachmentStore::WriteSingleAttachment( |
| 445 const Attachment& attachment, | 445 const Attachment& attachment, |
| 446 AttachmentStore::Component component) { | 446 AttachmentStore::Component component) { |
| 447 const std::string metadata_key = | 447 const std::string metadata_key = |
| 448 MakeMetadataKeyFromAttachmentId(attachment.GetId()); | 448 MakeMetadataKeyFromAttachmentId(attachment.GetId()); |
| 449 const std::string data_key = MakeDataKeyFromAttachmentId(attachment.GetId()); | 449 const std::string data_key = MakeDataKeyFromAttachmentId(attachment.GetId()); |
| 450 | 450 |
| 451 std::string metadata_str; | 451 std::string metadata_str; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 return key; | 532 return key; |
| 533 } | 533 } |
| 534 | 534 |
| 535 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( | 535 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( |
| 536 const AttachmentId& attachment_id, | 536 const AttachmentId& attachment_id, |
| 537 const attachment_store_pb::RecordMetadata& record_metadata) { | 537 const attachment_store_pb::RecordMetadata& record_metadata) { |
| 538 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); | 538 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); |
| 539 } | 539 } |
| 540 | 540 |
| 541 } // namespace syncer | 541 } // namespace syncer |
| OLD | NEW |