summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/attachments/in_memory_attachment_store.cc
blob: 99f603087ed6a669fe678e9e434aaeb102a4f001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/internal_api/public/attachments/in_memory_attachment_store.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/sequenced_task_runner.h"

namespace syncer {

namespace {

void AppendMetadata(AttachmentMetadataList* list,
                    const Attachment& attachment) {
  list->push_back(
      AttachmentMetadata(attachment.GetId(), attachment.GetData()->size()));
}

}  // namespace

InMemoryAttachmentStore::InMemoryAttachmentStore(
    const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner)
    : AttachmentStoreBackend(callback_task_runner) {
  // Object is created on one thread but used on another.
  DetachFromThread();
}

InMemoryAttachmentStore::~InMemoryAttachmentStore() {
}

void InMemoryAttachmentStore::Init(
    const AttachmentStore::InitCallback& callback) {
  DCHECK(CalledOnValidThread());
  PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
}

void InMemoryAttachmentStore::Read(
    AttachmentStore::Component component,
    const AttachmentIdList& ids,
    const AttachmentStore::ReadCallback& callback) {
  DCHECK(CalledOnValidThread());
  AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
  scoped_ptr<AttachmentMap> result_map(new AttachmentMap);
  scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList);

  for (const auto& id : ids) {
    AttachmentEntryMap::iterator iter = attachments_.find(id);
    if (iter != attachments_.end() &&
        iter->second.components.count(component) > 0) {
      const Attachment& attachment = iter->second.attachment;
      result_map->insert(std::make_pair(id, attachment));
    } else {
      unavailable_attachments->push_back(id);
    }
  }
  if (!unavailable_attachments->empty()) {
    result_code = AttachmentStore::UNSPECIFIED_ERROR;
  }
  PostCallback(base::Bind(callback, result_code, base::Passed(&result_map),
                          base::Passed(&unavailable_attachments)));
}

void InMemoryAttachmentStore::Write(
    AttachmentStore::Component component,
    const AttachmentList& attachments,
    const AttachmentStore::WriteCallback& callback) {
  DCHECK(CalledOnValidThread());
  for (const auto& attachment : attachments) {
    attachments_.insert(std::make_pair(attachment.GetId(),
                                       AttachmentEntry(attachment, component)));
  }
  PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
}

void InMemoryAttachmentStore::SetReference(AttachmentStore::Component component,
                                           const AttachmentIdList& ids) {
  DCHECK(CalledOnValidThread());
  for (const auto& id : ids) {
    AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
    if (attachments_iter != attachments_.end()) {
      attachments_iter->second.components.insert(component);
    }
  }
}

void InMemoryAttachmentStore::DropReference(
    AttachmentStore::Component component,
    const AttachmentIdList& ids,
    const AttachmentStore::DropCallback& callback) {
  DCHECK(CalledOnValidThread());
  AttachmentStore::Result result = AttachmentStore::SUCCESS;
  for (const auto& id : ids) {
    AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
    if (attachments_iter == attachments_.end()) {
      continue;
    }
    attachments_iter->second.components.erase(component);
    if (attachments_iter->second.components.empty()) {
      attachments_.erase(attachments_iter);
    }
  }
  PostCallback(base::Bind(callback, result));
}

void InMemoryAttachmentStore::ReadMetadataById(
    AttachmentStore::Component component,
    const AttachmentIdList& ids,
    const AttachmentStore::ReadMetadataCallback& callback) {
  DCHECK(CalledOnValidThread());
  AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
  scoped_ptr<AttachmentMetadataList> metadata_list(
      new AttachmentMetadataList());

  for (const auto& id : ids) {
    AttachmentEntryMap::iterator iter = attachments_.find(id);
    if (iter == attachments_.end()) {
      result_code = AttachmentStore::UNSPECIFIED_ERROR;
      continue;
    }
    DCHECK_GT(iter->second.components.size(), 0u);
    if (iter->second.components.count(component) == 0) {
      result_code = AttachmentStore::UNSPECIFIED_ERROR;
      continue;
    }
    AppendMetadata(metadata_list.get(), iter->second.attachment);
  }
  PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
}

void InMemoryAttachmentStore::ReadMetadata(
    AttachmentStore::Component component,
    const AttachmentStore::ReadMetadataCallback& callback) {
  DCHECK(CalledOnValidThread());
  AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
  scoped_ptr<AttachmentMetadataList> metadata_list(
      new AttachmentMetadataList());

  for (AttachmentEntryMap::const_iterator iter = attachments_.begin();
       iter != attachments_.end(); ++iter) {
    DCHECK_GT(iter->second.components.size(), 0u);
    if (iter->second.components.count(component) > 0) {
      AppendMetadata(metadata_list.get(), iter->second.attachment);
    }
  }
  PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
}

InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry(
    const Attachment& attachment,
    AttachmentStore::Component initial_reference_component)
    : attachment(attachment) {
  components.insert(initial_reference_component);
}

InMemoryAttachmentStore::AttachmentEntry::~AttachmentEntry() {
}

}  // namespace syncer