fake_attachment_store.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/api/attachments/fake_attachment_store.h" 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind.h" 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/location.h" 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/ref_counted_memory.h" 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/sequenced_task_runner.h" 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/single_thread_task_runner.h" 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/thread_task_runner_handle.h" 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "sync/api/attachments/attachment.h" 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace syncer { 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Backend is where all the work happens. 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FakeAttachmentStore::Backend 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) : public base::RefCountedThreadSafe<FakeAttachmentStore::Backend> { 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Construct a Backend that posts its results to |frontend_task_runner|. 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Backend( 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner); 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch void Read(const AttachmentIdList& ids, const ReadCallback& callback); 260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch void Write(const AttachmentList& attachments, const WriteCallback& callback); 270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch void Drop(const AttachmentIdList& ids, const DropCallback& callback); 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) friend class base::RefCountedThreadSafe<Backend>; 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ~Backend(); 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_refptr<base::SingleThreadTaskRunner> frontend_task_runner_; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AttachmentMap attachments_; 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::Backend::Backend( 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const scoped_refptr<base::SingleThreadTaskRunner>& frontend_task_runner) 40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch : frontend_task_runner_(frontend_task_runner) {} 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::Backend::~Backend() {} 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Backend::Read(const AttachmentIdList& ids, 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const ReadCallback& callback) { 460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch Result result_code = SUCCESS; 470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentIdList::const_iterator id_iter = ids.begin(); 480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentIdList::const_iterator id_end = ids.end(); 490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch scoped_ptr<AttachmentMap> result_map(new AttachmentMap); 50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList); 510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch for (; id_iter != id_end; ++id_iter) { 520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const AttachmentId& id = *id_iter; 530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch syncer::AttachmentMap::iterator attachment_iter = 540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch attachments_.find(*id_iter); 550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch if (attachment_iter != attachments_.end()) { 560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const Attachment& attachment = attachment_iter->second; 570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch result_map->insert(std::make_pair(id, attachment)); 580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } else { 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) unavailable_attachments->push_back(id); 600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (!unavailable_attachments->empty()) { 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) result_code = UNSPECIFIED_ERROR; 64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) frontend_task_runner_->PostTask( 66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) FROM_HERE, 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Bind(callback, 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) result_code, 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Passed(&result_map), 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Passed(&unavailable_attachments))); 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Backend::Write(const AttachmentList& attachments, 740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const WriteCallback& callback) { 750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentList::const_iterator iter = attachments.begin(); 760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentList::const_iterator end = attachments.end(); 770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch for (; iter != end; ++iter) { 780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch attachments_.insert(std::make_pair(iter->GetId(), *iter)); 790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } 800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids, 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const DropCallback& callback) { 850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch Result result = SUCCESS; 860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentIdList::const_iterator ids_iter = ids.begin(); 870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentIdList::const_iterator ids_end = ids.end(); 880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch for (; ids_iter != ids_end; ++ids_iter) { 890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch AttachmentMap::iterator attachments_iter = attachments_.find(*ids_iter); 900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch if (attachments_iter != attachments_.end()) { 910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch attachments_.erase(attachments_iter); 920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch } 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::FakeAttachmentStore( 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch : backend_(new Backend(base::ThreadTaskRunnerHandle::Get())), 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) backend_task_runner_(backend_task_runner) {} 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::~FakeAttachmentStore() {} 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Read(const AttachmentIdList& ids, 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const ReadCallback& callback) { 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) backend_task_runner_->PostTask( 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FROM_HERE, 1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::Bind(&FakeAttachmentStore::Backend::Read, backend_, ids, callback)); 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Write(const AttachmentList& attachments, 1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const WriteCallback& callback) { 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) backend_task_runner_->PostTask( 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FROM_HERE, 1150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::Bind(&FakeAttachmentStore::Backend::Write, 1160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch backend_, 1170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch attachments, 1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch callback)); 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Drop(const AttachmentIdList& ids, 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const DropCallback& callback) { 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) backend_task_runner_->PostTask( 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FROM_HERE, 1250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, ids, callback)); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace syncer 129