OLD | NEW |
| (Empty) |
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 AttachmentIdList& ids, const ReadCallback& callback); | |
26 void Write(const AttachmentList& attachments, const WriteCallback& callback); | |
27 void Drop(const AttachmentIdList& ids, const DropCallback& callback); | |
28 | |
29 private: | |
30 friend class base::RefCountedThreadSafe<Backend>; | |
31 | |
32 ~Backend(); | |
33 | |
34 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; | |
35 AttachmentMap attachments_; | |
36 }; | |
37 | |
38 FakeAttachmentStore::Backend::Backend( | |
39 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) | |
40 : frontend_task_runner_(frontend_task_runner) {} | |
41 | |
42 FakeAttachmentStore::Backend::~Backend() {} | |
43 | |
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 for (; id_iter != id_end; ++id_iter) { | |
51 const AttachmentId& id = *id_iter; | |
52 syncer::AttachmentMap::iterator attachment_iter = | |
53 attachments_.find(*id_iter); | |
54 if (attachment_iter != attachments_.end()) { | |
55 const Attachment& attachment = attachment_iter->second; | |
56 result_map->insert(std::make_pair(id, attachment)); | |
57 } else { | |
58 result_code = UNSPECIFIED_ERROR; | |
59 break; | |
60 } | |
61 } | |
62 frontend_task_runner_->PostTask( | |
63 FROM_HERE, base::Bind(callback, result_code, base::Passed(&result_map))); | |
64 } | |
65 | |
66 void FakeAttachmentStore::Backend::Write(const AttachmentList& attachments, | |
67 const WriteCallback& callback) { | |
68 AttachmentList::const_iterator iter = attachments.begin(); | |
69 AttachmentList::const_iterator end = attachments.end(); | |
70 for (; iter != end; ++iter) { | |
71 attachments_.insert(std::make_pair(iter->GetId(), *iter)); | |
72 } | |
73 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); | |
74 } | |
75 | |
76 void FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids, | |
77 const DropCallback& callback) { | |
78 Result result = SUCCESS; | |
79 AttachmentIdList::const_iterator ids_iter = ids.begin(); | |
80 AttachmentIdList::const_iterator ids_end = ids.end(); | |
81 for (; ids_iter != ids_end; ++ids_iter) { | |
82 AttachmentMap::iterator attachments_iter = attachments_.find(*ids_iter); | |
83 if (attachments_iter != attachments_.end()) { | |
84 attachments_.erase(attachments_iter); | |
85 } | |
86 } | |
87 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | |
88 } | |
89 | |
90 FakeAttachmentStore::FakeAttachmentStore( | |
91 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) | |
92 : backend_(new Backend(base::MessageLoopProxy::current())), | |
93 backend_task_runner_(backend_task_runner) {} | |
94 | |
95 FakeAttachmentStore::~FakeAttachmentStore() {} | |
96 | |
97 void FakeAttachmentStore::Read(const AttachmentIdList& ids, | |
98 const ReadCallback& callback) { | |
99 backend_task_runner_->PostTask( | |
100 FROM_HERE, | |
101 base::Bind(&FakeAttachmentStore::Backend::Read, backend_, ids, callback)); | |
102 } | |
103 | |
104 void FakeAttachmentStore::Write(const AttachmentList& attachments, | |
105 const WriteCallback& callback) { | |
106 backend_task_runner_->PostTask( | |
107 FROM_HERE, | |
108 base::Bind(&FakeAttachmentStore::Backend::Write, | |
109 backend_, | |
110 attachments, | |
111 callback)); | |
112 } | |
113 | |
114 void FakeAttachmentStore::Drop(const AttachmentIdList& ids, | |
115 const DropCallback& callback) { | |
116 backend_task_runner_->PostTask( | |
117 FROM_HERE, | |
118 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, ids, callback)); | |
119 } | |
120 | |
121 } // namespace syncer | |
OLD | NEW |