| 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 <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/sequenced_task_runner.h" | |
| 13 | |
| 14 namespace syncer { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 void AppendMetadata(AttachmentMetadataList* list, | |
| 19 const Attachment& attachment) { | |
| 20 list->push_back( | |
| 21 AttachmentMetadata(attachment.GetId(), attachment.GetData()->size())); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 InMemoryAttachmentStore::InMemoryAttachmentStore( | |
| 27 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) | |
| 28 : AttachmentStoreBackend(callback_task_runner) { | |
| 29 // Object is created on one thread but used on another. | |
| 30 DetachFromThread(); | |
| 31 } | |
| 32 | |
| 33 InMemoryAttachmentStore::~InMemoryAttachmentStore() { | |
| 34 } | |
| 35 | |
| 36 void InMemoryAttachmentStore::Init( | |
| 37 const AttachmentStore::InitCallback& callback) { | |
| 38 DCHECK(CalledOnValidThread()); | |
| 39 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS)); | |
| 40 } | |
| 41 | |
| 42 void InMemoryAttachmentStore::Read( | |
| 43 AttachmentStore::Component component, | |
| 44 const AttachmentIdList& ids, | |
| 45 const AttachmentStore::ReadCallback& callback) { | |
| 46 DCHECK(CalledOnValidThread()); | |
| 47 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; | |
| 48 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap); | |
| 49 std::unique_ptr<AttachmentIdList> unavailable_attachments( | |
| 50 new AttachmentIdList); | |
| 51 | |
| 52 for (const auto& id : ids) { | |
| 53 AttachmentEntryMap::iterator iter = attachments_.find(id); | |
| 54 if (iter != attachments_.end() && | |
| 55 iter->second.components.count(component) > 0) { | |
| 56 const Attachment& attachment = iter->second.attachment; | |
| 57 result_map->insert(std::make_pair(id, attachment)); | |
| 58 } else { | |
| 59 unavailable_attachments->push_back(id); | |
| 60 } | |
| 61 } | |
| 62 if (!unavailable_attachments->empty()) { | |
| 63 result_code = AttachmentStore::UNSPECIFIED_ERROR; | |
| 64 } | |
| 65 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map), | |
| 66 base::Passed(&unavailable_attachments))); | |
| 67 } | |
| 68 | |
| 69 void InMemoryAttachmentStore::Write( | |
| 70 AttachmentStore::Component component, | |
| 71 const AttachmentList& attachments, | |
| 72 const AttachmentStore::WriteCallback& callback) { | |
| 73 DCHECK(CalledOnValidThread()); | |
| 74 for (const auto& attachment : attachments) { | |
| 75 attachments_.insert(std::make_pair(attachment.GetId(), | |
| 76 AttachmentEntry(attachment, component))); | |
| 77 } | |
| 78 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS)); | |
| 79 } | |
| 80 | |
| 81 void InMemoryAttachmentStore::SetReference(AttachmentStore::Component component, | |
| 82 const AttachmentIdList& ids) { | |
| 83 DCHECK(CalledOnValidThread()); | |
| 84 for (const auto& id : ids) { | |
| 85 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id); | |
| 86 if (attachments_iter != attachments_.end()) { | |
| 87 attachments_iter->second.components.insert(component); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void InMemoryAttachmentStore::DropReference( | |
| 93 AttachmentStore::Component component, | |
| 94 const AttachmentIdList& ids, | |
| 95 const AttachmentStore::DropCallback& callback) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 AttachmentStore::Result result = AttachmentStore::SUCCESS; | |
| 98 for (const auto& id : ids) { | |
| 99 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id); | |
| 100 if (attachments_iter == attachments_.end()) { | |
| 101 continue; | |
| 102 } | |
| 103 attachments_iter->second.components.erase(component); | |
| 104 if (attachments_iter->second.components.empty()) { | |
| 105 attachments_.erase(attachments_iter); | |
| 106 } | |
| 107 } | |
| 108 PostCallback(base::Bind(callback, result)); | |
| 109 } | |
| 110 | |
| 111 void InMemoryAttachmentStore::ReadMetadataById( | |
| 112 AttachmentStore::Component component, | |
| 113 const AttachmentIdList& ids, | |
| 114 const AttachmentStore::ReadMetadataCallback& callback) { | |
| 115 DCHECK(CalledOnValidThread()); | |
| 116 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; | |
| 117 std::unique_ptr<AttachmentMetadataList> metadata_list( | |
| 118 new AttachmentMetadataList()); | |
| 119 | |
| 120 for (const auto& id : ids) { | |
| 121 AttachmentEntryMap::iterator iter = attachments_.find(id); | |
| 122 if (iter == attachments_.end()) { | |
| 123 result_code = AttachmentStore::UNSPECIFIED_ERROR; | |
| 124 continue; | |
| 125 } | |
| 126 DCHECK_GT(iter->second.components.size(), 0u); | |
| 127 if (iter->second.components.count(component) == 0) { | |
| 128 result_code = AttachmentStore::UNSPECIFIED_ERROR; | |
| 129 continue; | |
| 130 } | |
| 131 AppendMetadata(metadata_list.get(), iter->second.attachment); | |
| 132 } | |
| 133 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); | |
| 134 } | |
| 135 | |
| 136 void InMemoryAttachmentStore::ReadMetadata( | |
| 137 AttachmentStore::Component component, | |
| 138 const AttachmentStore::ReadMetadataCallback& callback) { | |
| 139 DCHECK(CalledOnValidThread()); | |
| 140 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; | |
| 141 std::unique_ptr<AttachmentMetadataList> metadata_list( | |
| 142 new AttachmentMetadataList()); | |
| 143 | |
| 144 for (AttachmentEntryMap::const_iterator iter = attachments_.begin(); | |
| 145 iter != attachments_.end(); ++iter) { | |
| 146 DCHECK_GT(iter->second.components.size(), 0u); | |
| 147 if (iter->second.components.count(component) > 0) { | |
| 148 AppendMetadata(metadata_list.get(), iter->second.attachment); | |
| 149 } | |
| 150 } | |
| 151 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); | |
| 152 } | |
| 153 | |
| 154 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry( | |
| 155 const Attachment& attachment, | |
| 156 AttachmentStore::Component initial_reference_component) | |
| 157 : attachment(attachment) { | |
| 158 components.insert(initial_reference_component); | |
| 159 } | |
| 160 | |
| 161 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry( | |
| 162 const AttachmentEntry& other) = default; | |
| 163 | |
| 164 InMemoryAttachmentStore::AttachmentEntry::~AttachmentEntry() { | |
| 165 } | |
| 166 | |
| 167 } // namespace syncer | |
| OLD | NEW |