Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / sync / api / attachments / fake_attachment_store.cc
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.
4
5 #include "sync/api/attachments/fake_attachment_store.h"
6
7 #include "base/bind.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"
14
15 namespace syncer {
16
17 // Backend is where all the work happens.
18 class FakeAttachmentStore::Backend
19     : public base::RefCountedThreadSafe<FakeAttachmentStore::Backend> {
20  public:
21   // Construct a Backend that posts its results to |frontend_task_runner|.
22   Backend(
23       const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner);
24
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);
30
31  private:
32   friend class base::RefCountedThreadSafe<Backend>;
33   typedef std::string UniqueId;
34   typedef std::map<UniqueId, Attachment*> AttachmentMap;
35
36   ~Backend();
37   Result RemoveAttachment(const sync_pb::AttachmentId& id);
38
39   scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_;
40   AttachmentMap attachments_;
41   STLValueDeleter<AttachmentMap> attachments_value_deleter_;
42 };
43
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_) {}
48
49 FakeAttachmentStore::Backend::~Backend() {}
50
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));
58     result = SUCCESS;
59   }
60   frontend_task_runner_->PostTask(
61       FROM_HERE, base::Bind(callback, result, base::Passed(&attachment)));
62 }
63
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);
69   RemoveAttachment(id);
70   attachments_.insert(
71       AttachmentMap::value_type(id.unique_id(), attachment.release()));
72   frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
73 }
74
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));
79 }
80
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()) {
86     delete iter->second;
87     attachments_.erase(iter);
88     result = SUCCESS;
89   }
90   return result;
91 }
92
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) {}
97
98 FakeAttachmentStore::~FakeAttachmentStore() {}
99
100 void FakeAttachmentStore::Read(const sync_pb::AttachmentId& id,
101                                const ReadCallback& callback) {
102   backend_task_runner_->PostTask(
103       FROM_HERE,
104       base::Bind(&FakeAttachmentStore::Backend::Read, backend_, id, callback));
105 }
106
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(
112       FROM_HERE,
113       base::Bind(
114           &FakeAttachmentStore::Backend::Write, backend_, id, bytes, callback));
115 }
116
117 void FakeAttachmentStore::Drop(const sync_pb::AttachmentId& id,
118                                const DropCallback& callback) {
119   backend_task_runner_->PostTask(
120       FROM_HERE,
121       base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback));
122 }
123
124 }  // namespace syncer