fake_attachment_store.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (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/message_loop/message_loop_proxy.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/sequenced_task_runner.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/single_thread_task_runner.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);
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (; id_iter != id_end; ++id_iter) {
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const AttachmentId& id = *id_iter;
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    syncer::AttachmentMap::iterator attachment_iter =
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        attachments_.find(*id_iter);
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (attachment_iter != attachments_.end()) {
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      const Attachment& attachment = attachment_iter->second;
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      result_map->insert(std::make_pair(id, attachment));
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    } else {
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      result_code = UNSPECIFIED_ERROR;
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      break;
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frontend_task_runner_->PostTask(
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      FROM_HERE, base::Bind(callback, result_code, base::Passed(&result_map)));
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Backend::Write(const AttachmentList& attachments,
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                         const WriteCallback& callback) {
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AttachmentList::const_iterator iter = attachments.begin();
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AttachmentList::const_iterator end = attachments.end();
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (; iter != end; ++iter) {
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    attachments_.insert(std::make_pair(iter->GetId(), *iter));
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Backend::Drop(const AttachmentIdList& ids,
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                        const DropCallback& callback) {
780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  Result result = SUCCESS;
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AttachmentIdList::const_iterator ids_iter = ids.begin();
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AttachmentIdList::const_iterator ids_end = ids.end();
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (; ids_iter != ids_end; ++ids_iter) {
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AttachmentMap::iterator attachments_iter = attachments_.find(*ids_iter);
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (attachments_iter != attachments_.end()) {
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      attachments_.erase(attachments_iter);
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  frontend_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::FakeAttachmentStore(
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : backend_(new Backend(base::MessageLoopProxy::current())),
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      backend_task_runner_(backend_task_runner) {}
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeAttachmentStore::~FakeAttachmentStore() {}
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Read(const AttachmentIdList& ids,
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const ReadCallback& callback) {
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  backend_task_runner_->PostTask(
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::Bind(&FakeAttachmentStore::Backend::Read, backend_, ids, callback));
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Write(const AttachmentList& attachments,
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                const WriteCallback& callback) {
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  backend_task_runner_->PostTask(
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::Bind(&FakeAttachmentStore::Backend::Write,
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 backend_,
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 attachments,
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 callback));
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid FakeAttachmentStore::Drop(const AttachmentIdList& ids,
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const DropCallback& callback) {
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  backend_task_runner_->PostTask(
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::Bind(&FakeAttachmentStore::Backend::Drop, backend_, ids, callback));
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace syncer
122