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"
16 void AppendMetadata(AttachmentMetadataList
* list
,
17 const Attachment
& attachment
) {
19 AttachmentMetadata(attachment
.GetId(), attachment
.GetData()->size()));
24 InMemoryAttachmentStore::InMemoryAttachmentStore(
25 const scoped_refptr
<base::SingleThreadTaskRunner
>& callback_task_runner
)
26 : callback_task_runner_(callback_task_runner
) {
27 // Object is created on one thread but used on another.
31 InMemoryAttachmentStore::~InMemoryAttachmentStore() {
34 void InMemoryAttachmentStore::Init(const InitCallback
& callback
) {
35 DCHECK(CalledOnValidThread());
36 callback_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, SUCCESS
));
39 void InMemoryAttachmentStore::Read(const AttachmentIdList
& ids
,
40 const ReadCallback
& callback
) {
41 DCHECK(CalledOnValidThread());
42 Result result_code
= SUCCESS
;
43 AttachmentIdList::const_iterator id_iter
= ids
.begin();
44 AttachmentIdList::const_iterator id_end
= ids
.end();
45 scoped_ptr
<AttachmentMap
> result_map(new AttachmentMap
);
46 scoped_ptr
<AttachmentIdList
> unavailable_attachments(new AttachmentIdList
);
47 for (; id_iter
!= id_end
; ++id_iter
) {
48 const AttachmentId
& id
= *id_iter
;
49 syncer::AttachmentMap::iterator attachment_iter
=
50 attachments_
.find(*id_iter
);
51 if (attachment_iter
!= attachments_
.end()) {
52 const Attachment
& attachment
= attachment_iter
->second
;
53 result_map
->insert(std::make_pair(id
, attachment
));
55 unavailable_attachments
->push_back(id
);
58 if (!unavailable_attachments
->empty()) {
59 result_code
= UNSPECIFIED_ERROR
;
61 callback_task_runner_
->PostTask(
65 base::Passed(&result_map
),
66 base::Passed(&unavailable_attachments
)));
69 void InMemoryAttachmentStore::Write(const AttachmentList
& attachments
,
70 const 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 callback_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, SUCCESS
));
80 void InMemoryAttachmentStore::Drop(const AttachmentIdList
& ids
,
81 const DropCallback
& callback
) {
82 DCHECK(CalledOnValidThread());
83 Result result
= SUCCESS
;
84 AttachmentIdList::const_iterator ids_iter
= ids
.begin();
85 AttachmentIdList::const_iterator ids_end
= ids
.end();
86 for (; ids_iter
!= ids_end
; ++ids_iter
) {
87 AttachmentMap::iterator attachments_iter
= attachments_
.find(*ids_iter
);
88 if (attachments_iter
!= attachments_
.end()) {
89 attachments_
.erase(attachments_iter
);
92 callback_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, result
));
95 void InMemoryAttachmentStore::ReadMetadata(
96 const AttachmentIdList
& ids
,
97 const ReadMetadataCallback
& callback
) {
98 DCHECK(CalledOnValidThread());
99 Result result_code
= SUCCESS
;
100 scoped_ptr
<AttachmentMetadataList
> metadata_list(
101 new AttachmentMetadataList());
102 AttachmentIdList::const_iterator ids_iter
= ids
.begin();
103 AttachmentIdList::const_iterator ids_end
= ids
.end();
105 for (; ids_iter
!= ids_end
; ++ids_iter
) {
106 AttachmentMap::iterator attachments_iter
= attachments_
.find(*ids_iter
);
107 if (attachments_iter
!= attachments_
.end()) {
108 AppendMetadata(metadata_list
.get(), attachments_iter
->second
);
110 result_code
= UNSPECIFIED_ERROR
;
113 callback_task_runner_
->PostTask(
115 base::Bind(callback
, result_code
, base::Passed(&metadata_list
)));
118 void InMemoryAttachmentStore::ReadAllMetadata(
119 const ReadMetadataCallback
& callback
) {
120 DCHECK(CalledOnValidThread());
121 Result result_code
= SUCCESS
;
122 scoped_ptr
<AttachmentMetadataList
> metadata_list(
123 new AttachmentMetadataList());
125 for (AttachmentMap::const_iterator iter
= attachments_
.begin();
126 iter
!= attachments_
.end(); ++iter
) {
127 AppendMetadata(metadata_list
.get(), iter
->second
);
129 callback_task_runner_
->PostTask(
131 base::Bind(callback
, result_code
, base::Passed(&metadata_list
)));
134 } // namespace syncer