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 sync_pb::AttachmentId& id, const ReadCallback& callback);
26 void Write(const sync_pb::AttachmentId& id,
27 const scoped_refptr<base::RefCountedMemory>& bytes,
28 const WriteCallback& callback);
29 void Drop(const sync_pb::AttachmentId& id, const DropCallback& callback);
32 friend class base::RefCountedThreadSafe<Backend>;
33 typedef std::string UniqueId;
34 typedef std::map<UniqueId, Attachment*> AttachmentMap;
37 Result RemoveAttachment(const sync_pb::AttachmentId& id);
39 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_;
40 AttachmentMap attachments_;
41 STLValueDeleter<AttachmentMap> attachments_value_deleter_;
44 FakeAttachmentStore::Backend::Backend(
45 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner)
46 : frontend_task_runner_(frontend_task_runner),
47 attachments_value_deleter_(&attachments_) {}
49 FakeAttachmentStore::Backend::~Backend() {}
51 void FakeAttachmentStore::Backend::Read(const sync_pb::AttachmentId& id,
52 const ReadCallback& callback) {
53 AttachmentMap::iterator iter = attachments_.find(id.unique_id());
54 scoped_ptr<Attachment> attachment;
55 Result result = NOT_FOUND;
56 if (iter != attachments_.end()) {
57 attachment.reset(new Attachment(*iter->second));
60 frontend_task_runner_->PostTask(
61 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment)));
64 void FakeAttachmentStore::Backend::Write(
65 const sync_pb::AttachmentId& id,
66 const scoped_refptr<base::RefCountedMemory>& bytes,
67 const WriteCallback& callback) {
68 scoped_ptr<Attachment> attachment = Attachment::CreateWithId(id, bytes);
71 AttachmentMap::value_type(id.unique_id(), attachment.release()));
72 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
75 void FakeAttachmentStore::Backend::Drop(const sync_pb::AttachmentId& id,
76 const DropCallback& callback) {
77 Result result = RemoveAttachment(id);
78 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
81 AttachmentStore::Result FakeAttachmentStore::Backend::RemoveAttachment(
82 const sync_pb::AttachmentId& id) {
83 Result result = NOT_FOUND;
84 AttachmentMap::iterator iter = attachments_.find(id.unique_id());
85 if (iter != attachments_.end()) {
87 attachments_.erase(iter);
93 FakeAttachmentStore::FakeAttachmentStore(
94 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner)
95 : backend_(new Backend(base::MessageLoopProxy::current())),
96 backend_task_runner_(backend_task_runner) {}
98 FakeAttachmentStore::~FakeAttachmentStore() {}
100 void FakeAttachmentStore::Read(const sync_pb::AttachmentId& id,
101 const ReadCallback& callback) {
102 backend_task_runner_->PostTask(
104 base::Bind(&FakeAttachmentStore::Backend::Read, backend_, id, callback));
107 void FakeAttachmentStore::Write(
108 const sync_pb::AttachmentId& id,
109 const scoped_refptr<base::RefCountedMemory>& bytes,
110 const WriteCallback& callback) {
111 backend_task_runner_->PostTask(
114 &FakeAttachmentStore::Backend::Write, backend_, id, bytes, callback));
117 void FakeAttachmentStore::Drop(const sync_pb::AttachmentId& id,
118 const DropCallback& callback) {
119 backend_task_runner_->PostTask(
121 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback));
124 } // namespace syncer