| 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 sync_pb::AttachmentId& id, const ReadCallback& callback); | 25 void Read(const sync_pb::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 sync_pb::AttachmentId& id, const DropCallback& callback); | 28 void Drop(const sync_pb::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::string UniqueId; | |
| 33 typedef std::map<UniqueId, Attachment*> AttachmentMap; | |
| 34 | 32 |
| 35 ~Backend(); | 33 ~Backend(); |
| 36 Result Remove(const sync_pb::AttachmentId& id); | |
| 37 | 34 |
| 38 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; | 35 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; |
| 39 AttachmentMap attachments_; | 36 AttachmentMap attachments_; |
| 40 STLValueDeleter<AttachmentMap> attachments_value_deleter_; | |
| 41 }; | 37 }; |
| 42 | 38 |
| 43 FakeAttachmentStore::Backend::Backend( | 39 FakeAttachmentStore::Backend::Backend( |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) | 40 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) |
| 45 : frontend_task_runner_(frontend_task_runner), | 41 : frontend_task_runner_(frontend_task_runner) {} |
| 46 attachments_value_deleter_(&attachments_) {} | |
| 47 | 42 |
| 48 FakeAttachmentStore::Backend::~Backend() {} | 43 FakeAttachmentStore::Backend::~Backend() {} |
| 49 | 44 |
| 50 void FakeAttachmentStore::Backend::Read(const sync_pb::AttachmentId& id, | 45 void FakeAttachmentStore::Backend::Read(const sync_pb::AttachmentId& id, |
| 51 const ReadCallback& callback) { | 46 const ReadCallback& callback) { |
| 52 AttachmentMap::iterator iter = attachments_.find(id.unique_id()); | 47 AttachmentMap::iterator iter = attachments_.find(id.unique_id()); |
| 53 scoped_ptr<Attachment> attachment; | 48 scoped_ptr<Attachment> attachment; |
| 54 Result result = NOT_FOUND; | 49 Result result = NOT_FOUND; |
| 55 if (iter != attachments_.end()) { | 50 if (iter != attachments_.end()) { |
| 56 attachment.reset(new Attachment(*iter->second)); | 51 attachment.reset(new Attachment(iter->second)); |
| 57 result = SUCCESS; | 52 result = SUCCESS; |
| 58 } | 53 } |
| 59 frontend_task_runner_->PostTask( | 54 frontend_task_runner_->PostTask( |
| 60 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment))); | 55 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment))); |
| 61 } | 56 } |
| 62 | 57 |
| 63 void FakeAttachmentStore::Backend::Write( | 58 void FakeAttachmentStore::Backend::Write( |
| 64 const scoped_refptr<base::RefCountedMemory>& bytes, | 59 const scoped_refptr<base::RefCountedMemory>& bytes, |
| 65 const WriteCallback& callback) { | 60 const WriteCallback& callback) { |
| 66 scoped_ptr<Attachment> attachment = Attachment::Create(bytes); | 61 Attachment attachment = Attachment::Create(bytes); |
| 67 DCHECK(attachment.get()); | 62 sync_pb::AttachmentId attachment_id(attachment.GetId()); |
| 68 sync_pb::AttachmentId attachment_id(attachment->GetId()); | 63 attachments_.insert( |
| 69 attachments_.insert(AttachmentMap::value_type(attachment_id.unique_id(), | 64 AttachmentMap::value_type(attachment_id.unique_id(), attachment)); |
| 70 attachment.release())); | |
| 71 frontend_task_runner_->PostTask(FROM_HERE, | 65 frontend_task_runner_->PostTask(FROM_HERE, |
| 72 base::Bind(callback, SUCCESS, attachment_id)); | 66 base::Bind(callback, SUCCESS, attachment_id)); |
| 73 } | 67 } |
| 74 | 68 |
| 75 void FakeAttachmentStore::Backend::Drop(const sync_pb::AttachmentId& id, | 69 void FakeAttachmentStore::Backend::Drop(const sync_pb::AttachmentId& id, |
| 76 const DropCallback& callback) { | 70 const DropCallback& callback) { |
| 77 Result result = Remove(id); | 71 Result result = NOT_FOUND; |
| 72 AttachmentMap::iterator iter = attachments_.find(id.unique_id()); |
| 73 if (iter != attachments_.end()) { |
| 74 attachments_.erase(iter); |
| 75 result = SUCCESS; |
| 76 } |
| 78 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 77 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 79 } | 78 } |
| 80 | 79 |
| 81 AttachmentStore::Result FakeAttachmentStore::Backend::Remove( | |
| 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( | 80 FakeAttachmentStore::FakeAttachmentStore( |
| 94 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) | 81 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) |
| 95 : backend_(new Backend(base::MessageLoopProxy::current())), | 82 : backend_(new Backend(base::MessageLoopProxy::current())), |
| 96 backend_task_runner_(backend_task_runner) {} | 83 backend_task_runner_(backend_task_runner) {} |
| 97 | 84 |
| 98 FakeAttachmentStore::~FakeAttachmentStore() {} | 85 FakeAttachmentStore::~FakeAttachmentStore() {} |
| 99 | 86 |
| 100 void FakeAttachmentStore::Read(const sync_pb::AttachmentId& id, | 87 void FakeAttachmentStore::Read(const sync_pb::AttachmentId& id, |
| 101 const ReadCallback& callback) { | 88 const ReadCallback& callback) { |
| 102 backend_task_runner_->PostTask( | 89 backend_task_runner_->PostTask( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 } | 101 } |
| 115 | 102 |
| 116 void FakeAttachmentStore::Drop(const sync_pb::AttachmentId& id, | 103 void FakeAttachmentStore::Drop(const sync_pb::AttachmentId& id, |
| 117 const DropCallback& callback) { | 104 const DropCallback& callback) { |
| 118 backend_task_runner_->PostTask( | 105 backend_task_runner_->PostTask( |
| 119 FROM_HERE, | 106 FROM_HERE, |
| 120 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback)); | 107 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback)); |
| 121 } | 108 } |
| 122 | 109 |
| 123 } // namespace syncer | 110 } // namespace syncer |
| OLD | NEW |