OLD | NEW |
| (Empty) |
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/internal_api/public/attachments/in_memory_attachment_store.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/location.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 | |
12 namespace syncer { | |
13 | |
14 InMemoryAttachmentStore::InMemoryAttachmentStore( | |
15 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner) | |
16 : callback_task_runner_(callback_task_runner) { | |
17 // Object is created on one thread but used on another. | |
18 DetachFromThread(); | |
19 } | |
20 | |
21 InMemoryAttachmentStore::~InMemoryAttachmentStore() { | |
22 } | |
23 | |
24 void InMemoryAttachmentStore::Read(const AttachmentIdList& ids, | |
25 const ReadCallback& callback) { | |
26 DCHECK(CalledOnValidThread()); | |
27 Result result_code = SUCCESS; | |
28 AttachmentIdList::const_iterator id_iter = ids.begin(); | |
29 AttachmentIdList::const_iterator id_end = ids.end(); | |
30 scoped_ptr<AttachmentMap> result_map(new AttachmentMap); | |
31 scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList); | |
32 for (; id_iter != id_end; ++id_iter) { | |
33 const AttachmentId& id = *id_iter; | |
34 syncer::AttachmentMap::iterator attachment_iter = | |
35 attachments_.find(*id_iter); | |
36 if (attachment_iter != attachments_.end()) { | |
37 const Attachment& attachment = attachment_iter->second; | |
38 result_map->insert(std::make_pair(id, attachment)); | |
39 } else { | |
40 unavailable_attachments->push_back(id); | |
41 } | |
42 } | |
43 if (!unavailable_attachments->empty()) { | |
44 result_code = UNSPECIFIED_ERROR; | |
45 } | |
46 callback_task_runner_->PostTask( | |
47 FROM_HERE, | |
48 base::Bind(callback, | |
49 result_code, | |
50 base::Passed(&result_map), | |
51 base::Passed(&unavailable_attachments))); | |
52 } | |
53 | |
54 void InMemoryAttachmentStore::Write(const AttachmentList& attachments, | |
55 const WriteCallback& callback) { | |
56 DCHECK(CalledOnValidThread()); | |
57 AttachmentList::const_iterator iter = attachments.begin(); | |
58 AttachmentList::const_iterator end = attachments.end(); | |
59 for (; iter != end; ++iter) { | |
60 attachments_.insert(std::make_pair(iter->GetId(), *iter)); | |
61 } | |
62 callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); | |
63 } | |
64 | |
65 void InMemoryAttachmentStore::Drop(const AttachmentIdList& ids, | |
66 const DropCallback& callback) { | |
67 DCHECK(CalledOnValidThread()); | |
68 Result result = SUCCESS; | |
69 AttachmentIdList::const_iterator ids_iter = ids.begin(); | |
70 AttachmentIdList::const_iterator ids_end = ids.end(); | |
71 for (; ids_iter != ids_end; ++ids_iter) { | |
72 AttachmentMap::iterator attachments_iter = attachments_.find(*ids_iter); | |
73 if (attachments_iter != attachments_.end()) { | |
74 attachments_.erase(attachments_iter); | |
75 } | |
76 } | |
77 callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | |
78 } | |
79 | |
80 } // namespace syncer | |
OLD | NEW |