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/message_loop/message_loop_proxy.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.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 AttachmentId& id, const ReadCallback& callback);
26 void Write(const scoped_refptr<base::RefCountedMemory>& bytes,
27 const WriteCallback& callback);
28 void Drop(const AttachmentId& id, const DropCallback& callback);
31 friend class base::RefCountedThreadSafe<Backend>;
32 typedef std::map<AttachmentId, Attachment*> AttachmentMap;
35 Result Remove(const AttachmentId& id);
37 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_;
38 AttachmentMap attachments_;
39 STLValueDeleter<AttachmentMap> attachments_value_deleter_;
42 FakeAttachmentStore::Backend::Backend(
43 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner)
44 : frontend_task_runner_(frontend_task_runner),
45 attachments_value_deleter_(&attachments_) {}
47 FakeAttachmentStore::Backend::~Backend() {}
49 void FakeAttachmentStore::Backend::Read(const AttachmentId& id,
50 const ReadCallback& callback) {
51 AttachmentMap::iterator iter = attachments_.find(id);
52 scoped_ptr<Attachment> attachment;
53 Result result = NOT_FOUND;
54 if (iter != attachments_.end()) {
55 attachment.reset(new Attachment(*iter->second));
58 frontend_task_runner_->PostTask(
59 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment)));
62 void FakeAttachmentStore::Backend::Write(
63 const scoped_refptr<base::RefCountedMemory>& bytes,
64 const WriteCallback& callback) {
65 scoped_ptr<Attachment> attachment = Attachment::Create(bytes);
66 DCHECK(attachment.get());
67 AttachmentId attachment_id(attachment->GetId());
69 AttachmentMap::value_type(attachment_id, attachment.release()));
70 frontend_task_runner_->PostTask(FROM_HERE,
71 base::Bind(callback, SUCCESS, attachment_id));
74 void FakeAttachmentStore::Backend::Drop(const AttachmentId& id,
75 const DropCallback& callback) {
76 Result result = Remove(id);
77 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
80 AttachmentStore::Result FakeAttachmentStore::Backend::Remove(
81 const AttachmentId& id) {
82 Result result = NOT_FOUND;
83 AttachmentMap::iterator iter = attachments_.find(id);
84 if (iter != attachments_.end()) {
86 attachments_.erase(iter);
92 FakeAttachmentStore::FakeAttachmentStore(
93 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner)
94 : backend_(new Backend(base::MessageLoopProxy::current())),
95 backend_task_runner_(backend_task_runner) {}
97 FakeAttachmentStore::~FakeAttachmentStore() {}
99 void FakeAttachmentStore::Read(const AttachmentId& id,
100 const ReadCallback& callback) {
101 backend_task_runner_->PostTask(
103 base::Bind(&FakeAttachmentStore::Backend::Read, backend_, id, callback));
106 void FakeAttachmentStore::Write(
107 const scoped_refptr<base::RefCountedMemory>& bytes,
108 const WriteCallback& callback) {
109 backend_task_runner_->PostTask(
112 &FakeAttachmentStore::Backend::Write, backend_, bytes, callback));
115 void FakeAttachmentStore::Drop(const AttachmentId& id,
116 const DropCallback& callback) {
117 backend_task_runner_->PostTask(
119 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback));
122 } // namespace syncer