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
3 // found in the LICENSE file.
5 #include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/sequenced_task_runner.h"
17 void AppendMetadata(AttachmentMetadataList
* list
,
18 const Attachment
& attachment
) {
20 AttachmentMetadata(attachment
.GetId(), attachment
.GetData()->size()));
25 InMemoryAttachmentStore::InMemoryAttachmentStore(
26 const scoped_refptr
<base::SequencedTaskRunner
>& callback_task_runner
)
27 : AttachmentStoreBackend(callback_task_runner
) {
28 // Object is created on one thread but used on another.
32 InMemoryAttachmentStore::~InMemoryAttachmentStore() {
35 void InMemoryAttachmentStore::Init(
36 const AttachmentStore::InitCallback
& callback
) {
37 DCHECK(CalledOnValidThread());
38 PostCallback(base::Bind(callback
, AttachmentStore::SUCCESS
));
41 void InMemoryAttachmentStore::Read(
42 const AttachmentIdList
& ids
,
43 const AttachmentStore::ReadCallback
& callback
) {
44 DCHECK(CalledOnValidThread());
45 AttachmentStore::Result result_code
= AttachmentStore::SUCCESS
;
46 AttachmentIdList::const_iterator id_iter
= ids
.begin();
47 AttachmentIdList::const_iterator id_end
= ids
.end();
48 scoped_ptr
<AttachmentMap
> result_map(new AttachmentMap
);
49 scoped_ptr
<AttachmentIdList
> unavailable_attachments(new AttachmentIdList
);
50 for (; id_iter
!= id_end
; ++id_iter
) {
51 const AttachmentId
& id
= *id_iter
;
52 syncer::AttachmentMap::iterator attachment_iter
=
53 attachments_
.find(*id_iter
);
54 if (attachment_iter
!= attachments_
.end()) {
55 const Attachment
& attachment
= attachment_iter
->second
;
56 result_map
->insert(std::make_pair(id
, attachment
));
58 unavailable_attachments
->push_back(id
);
61 if (!unavailable_attachments
->empty()) {
62 result_code
= AttachmentStore::UNSPECIFIED_ERROR
;
64 PostCallback(base::Bind(callback
, result_code
, base::Passed(&result_map
),
65 base::Passed(&unavailable_attachments
)));
68 void InMemoryAttachmentStore::Write(
69 const AttachmentList
& attachments
,
70 const AttachmentStore::WriteCallback
& callback
) {
71 DCHECK(CalledOnValidThread());
72 AttachmentList::const_iterator iter
= attachments
.begin();
73 AttachmentList::const_iterator end
= attachments
.end();
74 for (; iter
!= end
; ++iter
) {
75 attachments_
.insert(std::make_pair(iter
->GetId(), *iter
));
77 PostCallback(base::Bind(callback
, AttachmentStore::SUCCESS
));
80 void InMemoryAttachmentStore::Drop(
81 const AttachmentIdList
& ids
,
82 const AttachmentStore::DropCallback
& callback
) {
83 DCHECK(CalledOnValidThread());
84 AttachmentStore::Result result
= AttachmentStore::SUCCESS
;
85 AttachmentIdList::const_iterator ids_iter
= ids
.begin();
86 AttachmentIdList::const_iterator ids_end
= ids
.end();
87 for (; ids_iter
!= ids_end
; ++ids_iter
) {
88 AttachmentMap::iterator attachments_iter
= attachments_
.find(*ids_iter
);
89 if (attachments_iter
!= attachments_
.end()) {
90 attachments_
.erase(attachments_iter
);
93 PostCallback(base::Bind(callback
, result
));
96 void InMemoryAttachmentStore::ReadMetadata(
97 const AttachmentIdList
& ids
,
98 const AttachmentStore::ReadMetadataCallback
& callback
) {
99 DCHECK(CalledOnValidThread());
100 AttachmentStore::Result result_code
= AttachmentStore::SUCCESS
;
101 scoped_ptr
<AttachmentMetadataList
> metadata_list(
102 new AttachmentMetadataList());
103 AttachmentIdList::const_iterator ids_iter
= ids
.begin();
104 AttachmentIdList::const_iterator ids_end
= ids
.end();
106 for (; ids_iter
!= ids_end
; ++ids_iter
) {
107 AttachmentMap::iterator attachments_iter
= attachments_
.find(*ids_iter
);
108 if (attachments_iter
!= attachments_
.end()) {
109 AppendMetadata(metadata_list
.get(), attachments_iter
->second
);
111 result_code
= AttachmentStore::UNSPECIFIED_ERROR
;
114 PostCallback(base::Bind(callback
, result_code
, base::Passed(&metadata_list
)));
117 void InMemoryAttachmentStore::ReadAllMetadata(
118 const AttachmentStore::ReadMetadataCallback
& callback
) {
119 DCHECK(CalledOnValidThread());
120 AttachmentStore::Result result_code
= AttachmentStore::SUCCESS
;
121 scoped_ptr
<AttachmentMetadataList
> metadata_list(
122 new AttachmentMetadataList());
124 for (AttachmentMap::const_iterator iter
= attachments_
.begin();
125 iter
!= attachments_
.end(); ++iter
) {
126 AppendMetadata(metadata_list
.get(), iter
->second
);
128 PostCallback(base::Bind(callback
, result_code
, base::Passed(&metadata_list
)));
131 } // namespace syncer