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/api/attachments/fake_attachment_store.h"
8 #include "base/location.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "sync/api/attachments/attachment.h"
17 // Backend is where all the work happens.
18 class FakeAttachmentStore::Backend
19 : public base::RefCountedThreadSafe
<FakeAttachmentStore::Backend
> {
21 // Construct a Backend that posts its results to |frontend_task_runner|.
23 const scoped_refptr
<base::SingleThreadTaskRunner
>& frontend_task_runner
);
25 void Read(const AttachmentIdList
& ids
, const ReadCallback
& callback
);
26 void Write(const AttachmentList
& attachments
, const WriteCallback
& callback
);
27 void Drop(const AttachmentIdList
& ids
, const DropCallback
& callback
);
30 friend class base::RefCountedThreadSafe
<Backend
>;
34 scoped_refptr
<base::SingleThreadTaskRunner
> frontend_task_runner_
;
35 AttachmentMap attachments_
;
38 FakeAttachmentStore::Backend::Backend(
39 const scoped_refptr
<base::SingleThreadTaskRunner
>& frontend_task_runner
)
40 : frontend_task_runner_(frontend_task_runner
) {}
42 FakeAttachmentStore::Backend::~Backend() {}
44 void FakeAttachmentStore::Backend::Read(const AttachmentIdList
& ids
,
45 const ReadCallback
& callback
) {
46 Result result_code
= SUCCESS
;
47 AttachmentIdList::const_iterator id_iter
= ids
.begin();
48 AttachmentIdList::const_iterator id_end
= ids
.end();
49 scoped_ptr
<AttachmentMap
> result_map(new AttachmentMap
);
50 scoped_ptr
<AttachmentIdList
> unavailable_attachments(new AttachmentIdList
);
51 for (; id_iter
!= id_end
; ++id_iter
) {
52 const AttachmentId
& id
= *id_iter
;
53 syncer::AttachmentMap::iterator attachment_iter
=
54 attachments_
.find(*id_iter
);
55 if (attachment_iter
!= attachments_
.end()) {
56 const Attachment
& attachment
= attachment_iter
->second
;
57 result_map
->insert(std::make_pair(id
, attachment
));
59 unavailable_attachments
->push_back(id
);
62 if (!unavailable_attachments
->empty()) {
63 result_code
= UNSPECIFIED_ERROR
;
65 frontend_task_runner_
->PostTask(
69 base::Passed(&result_map
),
70 base::Passed(&unavailable_attachments
)));
73 void FakeAttachmentStore::Backend::Write(const AttachmentList
& attachments
,
74 const WriteCallback
& callback
) {
75 AttachmentList::const_iterator iter
= attachments
.begin();
76 AttachmentList::const_iterator end
= attachments
.end();
77 for (; iter
!= end
; ++iter
) {
78 attachments_
.insert(std::make_pair(iter
->GetId(), *iter
));
80 frontend_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, SUCCESS
));
83 void FakeAttachmentStore::Backend::Drop(const AttachmentIdList
& ids
,
84 const DropCallback
& callback
) {
85 Result result
= SUCCESS
;
86 AttachmentIdList::const_iterator ids_iter
= ids
.begin();
87 AttachmentIdList::const_iterator ids_end
= ids
.end();
88 for (; ids_iter
!= ids_end
; ++ids_iter
) {
89 AttachmentMap::iterator attachments_iter
= attachments_
.find(*ids_iter
);
90 if (attachments_iter
!= attachments_
.end()) {
91 attachments_
.erase(attachments_iter
);
94 frontend_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, result
));
97 FakeAttachmentStore::FakeAttachmentStore(
98 const scoped_refptr
<base::SequencedTaskRunner
>& backend_task_runner
)
99 : backend_(new Backend(base::ThreadTaskRunnerHandle::Get())),
100 backend_task_runner_(backend_task_runner
) {}
102 FakeAttachmentStore::~FakeAttachmentStore() {}
104 void FakeAttachmentStore::Read(const AttachmentIdList
& ids
,
105 const ReadCallback
& callback
) {
106 backend_task_runner_
->PostTask(
108 base::Bind(&FakeAttachmentStore::Backend::Read
, backend_
, ids
, callback
));
111 void FakeAttachmentStore::Write(const AttachmentList
& attachments
,
112 const WriteCallback
& callback
) {
113 backend_task_runner_
->PostTask(
115 base::Bind(&FakeAttachmentStore::Backend::Write
,
121 void FakeAttachmentStore::Drop(const AttachmentIdList
& ids
,
122 const DropCallback
& callback
) {
123 backend_task_runner_
->PostTask(
125 base::Bind(&FakeAttachmentStore::Backend::Drop
, backend_
, ids
, callback
));
128 } // namespace syncer