| 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 "components/sync/engine/attachments/on_disk_attachment_store.h" | 5 #include "components/sync/engine/attachments/on_disk_attachment_store.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 return false; | 147 return false; |
| 148 } | 148 } |
| 149 | 149 |
| 150 } // namespace | 150 } // namespace |
| 151 | 151 |
| 152 OnDiskAttachmentStore::OnDiskAttachmentStore( | 152 OnDiskAttachmentStore::OnDiskAttachmentStore( |
| 153 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner, | 153 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner, |
| 154 const base::FilePath& path) | 154 const base::FilePath& path) |
| 155 : AttachmentStoreBackend(callback_task_runner), path_(path) {} | 155 : AttachmentStoreBackend(callback_task_runner), path_(path) {} |
| 156 | 156 |
| 157 OnDiskAttachmentStore::~OnDiskAttachmentStore() {} | 157 OnDiskAttachmentStore::~OnDiskAttachmentStore() { |
| 158 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 159 } |
| 158 | 160 |
| 159 void OnDiskAttachmentStore::Init( | 161 void OnDiskAttachmentStore::Init( |
| 160 const AttachmentStore::InitCallback& callback) { | 162 const AttachmentStore::InitCallback& callback) { |
| 161 DCHECK(CalledOnValidThread()); | 163 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 162 AttachmentStore::Result result_code = OpenOrCreate(path_); | 164 AttachmentStore::Result result_code = OpenOrCreate(path_); |
| 163 UMA_HISTOGRAM_ENUMERATION("Sync.Attachments.StoreInitResult", result_code, | 165 UMA_HISTOGRAM_ENUMERATION("Sync.Attachments.StoreInitResult", result_code, |
| 164 AttachmentStore::RESULT_SIZE); | 166 AttachmentStore::RESULT_SIZE); |
| 165 PostCallback(base::Bind(callback, result_code)); | 167 PostCallback(base::Bind(callback, result_code)); |
| 166 } | 168 } |
| 167 | 169 |
| 168 void OnDiskAttachmentStore::Read( | 170 void OnDiskAttachmentStore::Read( |
| 169 AttachmentStore::Component component, | 171 AttachmentStore::Component component, |
| 170 const AttachmentIdList& ids, | 172 const AttachmentIdList& ids, |
| 171 const AttachmentStore::ReadCallback& callback) { | 173 const AttachmentStore::ReadCallback& callback) { |
| 172 DCHECK(CalledOnValidThread()); | 174 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 173 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap()); | 175 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap()); |
| 174 std::unique_ptr<AttachmentIdList> unavailable_attachments( | 176 std::unique_ptr<AttachmentIdList> unavailable_attachments( |
| 175 new AttachmentIdList()); | 177 new AttachmentIdList()); |
| 176 | 178 |
| 177 AttachmentStore::Result result_code = | 179 AttachmentStore::Result result_code = |
| 178 AttachmentStore::STORE_INITIALIZATION_FAILED; | 180 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 179 | 181 |
| 180 if (db_) { | 182 if (db_) { |
| 181 result_code = AttachmentStore::SUCCESS; | 183 result_code = AttachmentStore::SUCCESS; |
| 182 for (const auto& id : ids) { | 184 for (const auto& id : ids) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 196 } | 198 } |
| 197 | 199 |
| 198 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map), | 200 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map), |
| 199 base::Passed(&unavailable_attachments))); | 201 base::Passed(&unavailable_attachments))); |
| 200 } | 202 } |
| 201 | 203 |
| 202 void OnDiskAttachmentStore::Write( | 204 void OnDiskAttachmentStore::Write( |
| 203 AttachmentStore::Component component, | 205 AttachmentStore::Component component, |
| 204 const AttachmentList& attachments, | 206 const AttachmentList& attachments, |
| 205 const AttachmentStore::WriteCallback& callback) { | 207 const AttachmentStore::WriteCallback& callback) { |
| 206 DCHECK(CalledOnValidThread()); | 208 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 207 AttachmentStore::Result result_code = | 209 AttachmentStore::Result result_code = |
| 208 AttachmentStore::STORE_INITIALIZATION_FAILED; | 210 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 209 | 211 |
| 210 if (db_) { | 212 if (db_) { |
| 211 result_code = AttachmentStore::SUCCESS; | 213 result_code = AttachmentStore::SUCCESS; |
| 212 AttachmentList::const_iterator iter = attachments.begin(); | 214 AttachmentList::const_iterator iter = attachments.begin(); |
| 213 const AttachmentList::const_iterator end = attachments.end(); | 215 const AttachmentList::const_iterator end = attachments.end(); |
| 214 for (; iter != end; ++iter) { | 216 for (; iter != end; ++iter) { |
| 215 if (!WriteSingleAttachment(*iter, component)) | 217 if (!WriteSingleAttachment(*iter, component)) |
| 216 result_code = AttachmentStore::UNSPECIFIED_ERROR; | 218 result_code = AttachmentStore::UNSPECIFIED_ERROR; |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 PostCallback(base::Bind(callback, result_code)); | 221 PostCallback(base::Bind(callback, result_code)); |
| 220 } | 222 } |
| 221 | 223 |
| 222 void OnDiskAttachmentStore::SetReference(AttachmentStore::Component component, | 224 void OnDiskAttachmentStore::SetReference(AttachmentStore::Component component, |
| 223 const AttachmentIdList& ids) { | 225 const AttachmentIdList& ids) { |
| 224 DCHECK(CalledOnValidThread()); | 226 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 225 if (!db_) | 227 if (!db_) |
| 226 return; | 228 return; |
| 227 attachment_store_pb::RecordMetadata::Component proto_component = | 229 attachment_store_pb::RecordMetadata::Component proto_component = |
| 228 ComponentToProto(component); | 230 ComponentToProto(component); |
| 229 for (const auto& id : ids) { | 231 for (const auto& id : ids) { |
| 230 attachment_store_pb::RecordMetadata record_metadata; | 232 attachment_store_pb::RecordMetadata record_metadata; |
| 231 if (!ReadSingleRecordMetadata(id, &record_metadata)) | 233 if (!ReadSingleRecordMetadata(id, &record_metadata)) |
| 232 continue; | 234 continue; |
| 233 if (SetReferenceInRecordMetadata(&record_metadata, proto_component)) | 235 if (SetReferenceInRecordMetadata(&record_metadata, proto_component)) |
| 234 WriteSingleRecordMetadata(id, record_metadata); | 236 WriteSingleRecordMetadata(id, record_metadata); |
| 235 } | 237 } |
| 236 } | 238 } |
| 237 | 239 |
| 238 void OnDiskAttachmentStore::DropReference( | 240 void OnDiskAttachmentStore::DropReference( |
| 239 AttachmentStore::Component component, | 241 AttachmentStore::Component component, |
| 240 const AttachmentIdList& ids, | 242 const AttachmentIdList& ids, |
| 241 const AttachmentStore::DropCallback& callback) { | 243 const AttachmentStore::DropCallback& callback) { |
| 242 DCHECK(CalledOnValidThread()); | 244 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 243 AttachmentStore::Result result_code = | 245 AttachmentStore::Result result_code = |
| 244 AttachmentStore::STORE_INITIALIZATION_FAILED; | 246 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 245 if (db_) { | 247 if (db_) { |
| 246 attachment_store_pb::RecordMetadata::Component proto_component = | 248 attachment_store_pb::RecordMetadata::Component proto_component = |
| 247 ComponentToProto(component); | 249 ComponentToProto(component); |
| 248 result_code = AttachmentStore::SUCCESS; | 250 result_code = AttachmentStore::SUCCESS; |
| 249 leveldb::WriteOptions write_options = MakeWriteOptions(); | 251 leveldb::WriteOptions write_options = MakeWriteOptions(); |
| 250 for (const auto& id : ids) { | 252 for (const auto& id : ids) { |
| 251 attachment_store_pb::RecordMetadata record_metadata; | 253 attachment_store_pb::RecordMetadata record_metadata; |
| 252 if (!ReadSingleRecordMetadata(id, &record_metadata)) | 254 if (!ReadSingleRecordMetadata(id, &record_metadata)) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 272 } | 274 } |
| 273 } | 275 } |
| 274 } | 276 } |
| 275 PostCallback(base::Bind(callback, result_code)); | 277 PostCallback(base::Bind(callback, result_code)); |
| 276 } | 278 } |
| 277 | 279 |
| 278 void OnDiskAttachmentStore::ReadMetadataById( | 280 void OnDiskAttachmentStore::ReadMetadataById( |
| 279 AttachmentStore::Component component, | 281 AttachmentStore::Component component, |
| 280 const AttachmentIdList& ids, | 282 const AttachmentIdList& ids, |
| 281 const AttachmentStore::ReadMetadataCallback& callback) { | 283 const AttachmentStore::ReadMetadataCallback& callback) { |
| 282 DCHECK(CalledOnValidThread()); | 284 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 283 AttachmentStore::Result result_code = | 285 AttachmentStore::Result result_code = |
| 284 AttachmentStore::STORE_INITIALIZATION_FAILED; | 286 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 285 std::unique_ptr<AttachmentMetadataList> metadata_list( | 287 std::unique_ptr<AttachmentMetadataList> metadata_list( |
| 286 new AttachmentMetadataList()); | 288 new AttachmentMetadataList()); |
| 287 if (db_) { | 289 if (db_) { |
| 288 result_code = AttachmentStore::SUCCESS; | 290 result_code = AttachmentStore::SUCCESS; |
| 289 for (const auto& id : ids) { | 291 for (const auto& id : ids) { |
| 290 attachment_store_pb::RecordMetadata record_metadata; | 292 attachment_store_pb::RecordMetadata record_metadata; |
| 291 if (!ReadSingleRecordMetadata(id, &record_metadata)) { | 293 if (!ReadSingleRecordMetadata(id, &record_metadata)) { |
| 292 result_code = AttachmentStore::UNSPECIFIED_ERROR; | 294 result_code = AttachmentStore::UNSPECIFIED_ERROR; |
| 293 continue; | 295 continue; |
| 294 } | 296 } |
| 295 if (!AttachmentHasReferenceFromComponent(record_metadata, | 297 if (!AttachmentHasReferenceFromComponent(record_metadata, |
| 296 ComponentToProto(component))) { | 298 ComponentToProto(component))) { |
| 297 result_code = AttachmentStore::UNSPECIFIED_ERROR; | 299 result_code = AttachmentStore::UNSPECIFIED_ERROR; |
| 298 continue; | 300 continue; |
| 299 } | 301 } |
| 300 metadata_list->push_back(MakeAttachmentMetadata(id, record_metadata)); | 302 metadata_list->push_back(MakeAttachmentMetadata(id, record_metadata)); |
| 301 } | 303 } |
| 302 } | 304 } |
| 303 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); | 305 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); |
| 304 } | 306 } |
| 305 | 307 |
| 306 void OnDiskAttachmentStore::ReadMetadata( | 308 void OnDiskAttachmentStore::ReadMetadata( |
| 307 AttachmentStore::Component component, | 309 AttachmentStore::Component component, |
| 308 const AttachmentStore::ReadMetadataCallback& callback) { | 310 const AttachmentStore::ReadMetadataCallback& callback) { |
| 309 DCHECK(CalledOnValidThread()); | 311 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 310 AttachmentStore::Result result_code = | 312 AttachmentStore::Result result_code = |
| 311 AttachmentStore::STORE_INITIALIZATION_FAILED; | 313 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 312 std::unique_ptr<AttachmentMetadataList> metadata_list( | 314 std::unique_ptr<AttachmentMetadataList> metadata_list( |
| 313 new AttachmentMetadataList()); | 315 new AttachmentMetadataList()); |
| 314 | 316 |
| 315 if (db_) { | 317 if (db_) { |
| 316 attachment_store_pb::RecordMetadata::Component proto_component = | 318 attachment_store_pb::RecordMetadata::Component proto_component = |
| 317 ComponentToProto(component); | 319 ComponentToProto(component); |
| 318 result_code = AttachmentStore::SUCCESS; | 320 result_code = AttachmentStore::SUCCESS; |
| 319 std::unique_ptr<leveldb::Iterator> db_iterator( | 321 std::unique_ptr<leveldb::Iterator> db_iterator( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 return key; | 533 return key; |
| 532 } | 534 } |
| 533 | 535 |
| 534 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( | 536 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( |
| 535 const AttachmentId& attachment_id, | 537 const AttachmentId& attachment_id, |
| 536 const attachment_store_pb::RecordMetadata& record_metadata) { | 538 const attachment_store_pb::RecordMetadata& record_metadata) { |
| 537 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); | 539 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); |
| 538 } | 540 } |
| 539 | 541 |
| 540 } // namespace syncer | 542 } // namespace syncer |
| OLD | NEW |