OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/api/attachments/fake_attachment_store.h" | 5 #include "sync/api/attachments/fake_attachment_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 Backend( | 22 Backend( |
23 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner); | 23 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner); |
24 | 24 |
25 void Read(const AttachmentId& id, const ReadCallback& callback); | 25 void Read(const AttachmentId& id, const ReadCallback& callback); |
26 void Write(const scoped_refptr<base::RefCountedMemory>& bytes, | 26 void Write(const scoped_refptr<base::RefCountedMemory>& bytes, |
27 const WriteCallback& callback); | 27 const WriteCallback& callback); |
28 void Drop(const AttachmentId& id, const DropCallback& callback); | 28 void Drop(const AttachmentId& id, const DropCallback& callback); |
29 | 29 |
30 private: | 30 private: |
31 friend class base::RefCountedThreadSafe<Backend>; | 31 friend class base::RefCountedThreadSafe<Backend>; |
32 typedef std::map<AttachmentId, Attachment*> AttachmentMap; | |
33 | 32 |
34 ~Backend(); | 33 ~Backend(); |
35 Result Remove(const AttachmentId& id); | |
36 | 34 |
37 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; | 35 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; |
38 AttachmentMap attachments_; | 36 AttachmentMap attachments_; |
39 STLValueDeleter<AttachmentMap> attachments_value_deleter_; | |
40 }; | 37 }; |
41 | 38 |
42 FakeAttachmentStore::Backend::Backend( | 39 FakeAttachmentStore::Backend::Backend( |
43 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) | 40 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) |
44 : frontend_task_runner_(frontend_task_runner), | 41 : frontend_task_runner_(frontend_task_runner) {} |
45 attachments_value_deleter_(&attachments_) {} | |
46 | 42 |
47 FakeAttachmentStore::Backend::~Backend() {} | 43 FakeAttachmentStore::Backend::~Backend() {} |
48 | 44 |
49 void FakeAttachmentStore::Backend::Read(const AttachmentId& id, | 45 void FakeAttachmentStore::Backend::Read(const AttachmentId& id, |
50 const ReadCallback& callback) { | 46 const ReadCallback& callback) { |
51 AttachmentMap::iterator iter = attachments_.find(id); | 47 AttachmentMap::iterator iter = attachments_.find(id); |
52 scoped_ptr<Attachment> attachment; | 48 scoped_ptr<Attachment> attachment; |
53 Result result = NOT_FOUND; | 49 Result result = NOT_FOUND; |
54 if (iter != attachments_.end()) { | 50 if (iter != attachments_.end()) { |
55 attachment.reset(new Attachment(*iter->second)); | 51 attachment.reset(new Attachment(iter->second)); |
56 result = SUCCESS; | 52 result = SUCCESS; |
57 } | 53 } |
58 frontend_task_runner_->PostTask( | 54 frontend_task_runner_->PostTask( |
59 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment))); | 55 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment))); |
60 } | 56 } |
61 | 57 |
62 void FakeAttachmentStore::Backend::Write( | 58 void FakeAttachmentStore::Backend::Write( |
63 const scoped_refptr<base::RefCountedMemory>& bytes, | 59 const scoped_refptr<base::RefCountedMemory>& bytes, |
64 const WriteCallback& callback) { | 60 const WriteCallback& callback) { |
65 scoped_ptr<Attachment> attachment = Attachment::Create(bytes); | 61 Attachment attachment = Attachment::Create(bytes); |
66 DCHECK(attachment.get()); | 62 AttachmentId attachment_id(attachment.GetId()); |
67 AttachmentId attachment_id(attachment->GetId()); | 63 attachments_.insert(AttachmentMap::value_type(attachment_id, attachment)); |
68 attachments_.insert( | |
69 AttachmentMap::value_type(attachment_id, attachment.release())); | |
70 frontend_task_runner_->PostTask(FROM_HERE, | 64 frontend_task_runner_->PostTask(FROM_HERE, |
71 base::Bind(callback, SUCCESS, attachment_id)); | 65 base::Bind(callback, SUCCESS, attachment_id)); |
72 } | 66 } |
73 | 67 |
74 void FakeAttachmentStore::Backend::Drop(const AttachmentId& id, | 68 void FakeAttachmentStore::Backend::Drop(const AttachmentId& id, |
75 const DropCallback& callback) { | 69 const DropCallback& callback) { |
76 Result result = Remove(id); | 70 Result result = NOT_FOUND; |
| 71 AttachmentMap::iterator iter = attachments_.find(id); |
| 72 if (iter != attachments_.end()) { |
| 73 attachments_.erase(iter); |
| 74 result = SUCCESS; |
| 75 } |
77 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 76 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
78 } | 77 } |
79 | 78 |
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()) { | |
85 delete iter->second; | |
86 attachments_.erase(iter); | |
87 result = SUCCESS; | |
88 } | |
89 return result; | |
90 } | |
91 | |
92 FakeAttachmentStore::FakeAttachmentStore( | 79 FakeAttachmentStore::FakeAttachmentStore( |
93 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) | 80 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) |
94 : backend_(new Backend(base::MessageLoopProxy::current())), | 81 : backend_(new Backend(base::MessageLoopProxy::current())), |
95 backend_task_runner_(backend_task_runner) {} | 82 backend_task_runner_(backend_task_runner) {} |
96 | 83 |
97 FakeAttachmentStore::~FakeAttachmentStore() {} | 84 FakeAttachmentStore::~FakeAttachmentStore() {} |
98 | 85 |
99 void FakeAttachmentStore::Read(const AttachmentId& id, | 86 void FakeAttachmentStore::Read(const AttachmentId& id, |
100 const ReadCallback& callback) { | 87 const ReadCallback& callback) { |
101 backend_task_runner_->PostTask( | 88 backend_task_runner_->PostTask( |
(...skipping 11 matching lines...) Expand all Loading... |
113 } | 100 } |
114 | 101 |
115 void FakeAttachmentStore::Drop(const AttachmentId& id, | 102 void FakeAttachmentStore::Drop(const AttachmentId& id, |
116 const DropCallback& callback) { | 103 const DropCallback& callback) { |
117 backend_task_runner_->PostTask( | 104 backend_task_runner_->PostTask( |
118 FROM_HERE, | 105 FROM_HERE, |
119 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback)); | 106 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback)); |
120 } | 107 } |
121 | 108 |
122 } // namespace syncer | 109 } // namespace syncer |
OLD | NEW |