fake_attachment_store.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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 15namespace syncer { 16 17// Backend is where all the work happens. 18class 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 AttachmentId& id, const ReadCallback& callback); 26 void Write(const scoped_refptr<base::RefCountedMemory>& bytes, 27 const WriteCallback& callback); 28 void Drop(const AttachmentId& id, const DropCallback& callback); 29 30 private: 31 friend class base::RefCountedThreadSafe<Backend>; 32 typedef std::map<AttachmentId, Attachment*> AttachmentMap; 33 34 ~Backend(); 35 Result Remove(const AttachmentId& id); 36 37 scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; 38 AttachmentMap attachments_; 39 STLValueDeleter<AttachmentMap> attachments_value_deleter_; 40}; 41 42FakeAttachmentStore::Backend::Backend( 43 const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) 44 : frontend_task_runner_(frontend_task_runner), 45 attachments_value_deleter_(&attachments_) {} 46 47FakeAttachmentStore::Backend::~Backend() {} 48 49void FakeAttachmentStore::Backend::Read(const AttachmentId& id, 50 const ReadCallback& callback) { 51 AttachmentMap::iterator iter = attachments_.find(id); 52 scoped_ptr<Attachment> attachment; 53 Result result = NOT_FOUND; 54 if (iter != attachments_.end()) { 55 attachment.reset(new Attachment(*iter->second)); 56 result = SUCCESS; 57 } 58 frontend_task_runner_->PostTask( 59 FROM_HERE, base::Bind(callback, result, base::Passed(&attachment))); 60} 61 62void FakeAttachmentStore::Backend::Write( 63 const scoped_refptr<base::RefCountedMemory>& bytes, 64 const WriteCallback& callback) { 65 scoped_ptr<Attachment> attachment = Attachment::Create(bytes); 66 DCHECK(attachment.get()); 67 AttachmentId attachment_id(attachment->GetId()); 68 attachments_.insert( 69 AttachmentMap::value_type(attachment_id, attachment.release())); 70 frontend_task_runner_->PostTask(FROM_HERE, 71 base::Bind(callback, SUCCESS, attachment_id)); 72} 73 74void FakeAttachmentStore::Backend::Drop(const AttachmentId& id, 75 const DropCallback& callback) { 76 Result result = Remove(id); 77 frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); 78} 79 80AttachmentStore::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 92FakeAttachmentStore::FakeAttachmentStore( 93 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) 94 : backend_(new Backend(base::MessageLoopProxy::current())), 95 backend_task_runner_(backend_task_runner) {} 96 97FakeAttachmentStore::~FakeAttachmentStore() {} 98 99void FakeAttachmentStore::Read(const AttachmentId& id, 100 const ReadCallback& callback) { 101 backend_task_runner_->PostTask( 102 FROM_HERE, 103 base::Bind(&FakeAttachmentStore::Backend::Read, backend_, id, callback)); 104} 105 106void FakeAttachmentStore::Write( 107 const scoped_refptr<base::RefCountedMemory>& bytes, 108 const WriteCallback& callback) { 109 backend_task_runner_->PostTask( 110 FROM_HERE, 111 base::Bind( 112 &FakeAttachmentStore::Backend::Write, backend_, bytes, callback)); 113} 114 115void FakeAttachmentStore::Drop(const AttachmentId& id, 116 const DropCallback& callback) { 117 backend_task_runner_->PostTask( 118 FROM_HERE, 119 base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, id, callback)); 120} 121 122} // namespace syncer 123