Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / sync / internal_api / attachments / in_memory_attachment_store.cc
blob340daaecf167f8a05afae6061f2ee26a77612a49
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.
5 #include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/sequenced_task_runner.h"
13 namespace syncer {
15 namespace {
17 void AppendMetadata(AttachmentMetadataList* list,
18 const Attachment& attachment) {
19 list->push_back(
20 AttachmentMetadata(attachment.GetId(), attachment.GetData()->size()));
23 } // namespace
25 InMemoryAttachmentStore::InMemoryAttachmentStore(
26 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner)
27 : AttachmentStoreBackend(callback_task_runner) {
28 // Object is created on one thread but used on another.
29 DetachFromThread();
32 InMemoryAttachmentStore::~InMemoryAttachmentStore() {
35 void InMemoryAttachmentStore::Init(
36 const AttachmentStore::InitCallback& callback) {
37 DCHECK(CalledOnValidThread());
38 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
41 void InMemoryAttachmentStore::Read(
42 AttachmentStore::Component component,
43 const AttachmentIdList& ids,
44 const AttachmentStore::ReadCallback& callback) {
45 DCHECK(CalledOnValidThread());
46 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
47 scoped_ptr<AttachmentMap> result_map(new AttachmentMap);
48 scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList);
50 for (const auto& id : ids) {
51 AttachmentEntryMap::iterator iter = attachments_.find(id);
52 if (iter != attachments_.end() &&
53 iter->second.components.count(component) > 0) {
54 const Attachment& attachment = iter->second.attachment;
55 result_map->insert(std::make_pair(id, attachment));
56 } else {
57 unavailable_attachments->push_back(id);
60 if (!unavailable_attachments->empty()) {
61 result_code = AttachmentStore::UNSPECIFIED_ERROR;
63 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map),
64 base::Passed(&unavailable_attachments)));
67 void InMemoryAttachmentStore::Write(
68 AttachmentStore::Component component,
69 const AttachmentList& attachments,
70 const AttachmentStore::WriteCallback& callback) {
71 DCHECK(CalledOnValidThread());
72 for (const auto& attachment : attachments) {
73 attachments_.insert(std::make_pair(attachment.GetId(),
74 AttachmentEntry(attachment, component)));
76 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
79 void InMemoryAttachmentStore::SetReference(AttachmentStore::Component component,
80 const AttachmentIdList& ids) {
81 DCHECK(CalledOnValidThread());
82 for (const auto& id : ids) {
83 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
84 if (attachments_iter != attachments_.end()) {
85 attachments_iter->second.components.insert(component);
90 void InMemoryAttachmentStore::DropReference(
91 AttachmentStore::Component component,
92 const AttachmentIdList& ids,
93 const AttachmentStore::DropCallback& callback) {
94 DCHECK(CalledOnValidThread());
95 AttachmentStore::Result result = AttachmentStore::SUCCESS;
96 for (const auto& id : ids) {
97 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
98 if (attachments_iter == attachments_.end()) {
99 continue;
101 attachments_iter->second.components.erase(component);
102 if (attachments_iter->second.components.empty()) {
103 attachments_.erase(attachments_iter);
106 PostCallback(base::Bind(callback, result));
109 void InMemoryAttachmentStore::ReadMetadataById(
110 AttachmentStore::Component component,
111 const AttachmentIdList& ids,
112 const AttachmentStore::ReadMetadataCallback& callback) {
113 DCHECK(CalledOnValidThread());
114 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
115 scoped_ptr<AttachmentMetadataList> metadata_list(
116 new AttachmentMetadataList());
118 for (const auto& id : ids) {
119 AttachmentEntryMap::iterator iter = attachments_.find(id);
120 if (iter == attachments_.end()) {
121 result_code = AttachmentStore::UNSPECIFIED_ERROR;
122 continue;
124 DCHECK(iter->second.components.size() > 0);
125 if (iter->second.components.count(component) == 0) {
126 result_code = AttachmentStore::UNSPECIFIED_ERROR;
127 continue;
129 AppendMetadata(metadata_list.get(), iter->second.attachment);
131 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
134 void InMemoryAttachmentStore::ReadMetadata(
135 AttachmentStore::Component component,
136 const AttachmentStore::ReadMetadataCallback& callback) {
137 DCHECK(CalledOnValidThread());
138 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
139 scoped_ptr<AttachmentMetadataList> metadata_list(
140 new AttachmentMetadataList());
142 for (AttachmentEntryMap::const_iterator iter = attachments_.begin();
143 iter != attachments_.end(); ++iter) {
144 DCHECK(iter->second.components.size() > 0);
145 if (iter->second.components.count(component) > 0) {
146 AppendMetadata(metadata_list.get(), iter->second.attachment);
149 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
152 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry(
153 const Attachment& attachment,
154 AttachmentStore::Component initial_reference_component)
155 : attachment(attachment) {
156 components.insert(initial_reference_component);
159 InMemoryAttachmentStore::AttachmentEntry::~AttachmentEntry() {
162 } // namespace syncer