Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: components/sync/engine/attachments/in_memory_attachment_store.cc

Issue 2915453002: Deprecate NonThreadSafe in components/sync in favor of SequenceChecker. (Closed)
Patch Set: fix comment Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/sync/engine/attachments/in_memory_attachment_store.h" 5 #include "components/sync/engine/attachments/in_memory_attachment_store.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 10 matching lines...) Expand all
21 list->push_back( 21 list->push_back(
22 AttachmentMetadata(attachment.GetId(), attachment.GetData()->size())); 22 AttachmentMetadata(attachment.GetId(), attachment.GetData()->size()));
23 } 23 }
24 24
25 } // namespace 25 } // namespace
26 26
27 InMemoryAttachmentStore::InMemoryAttachmentStore( 27 InMemoryAttachmentStore::InMemoryAttachmentStore(
28 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) 28 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner)
29 : AttachmentStoreBackend(callback_task_runner) { 29 : AttachmentStoreBackend(callback_task_runner) {
30 // Object is created on one thread but used on another. 30 // Object is created on one thread but used on another.
31 DetachFromThread(); 31 DETACH_FROM_SEQUENCE(sequence_checker_);
32 } 32 }
33 33
34 InMemoryAttachmentStore::~InMemoryAttachmentStore() {} 34 InMemoryAttachmentStore::~InMemoryAttachmentStore() {
35 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
36 }
35 37
36 void InMemoryAttachmentStore::Init( 38 void InMemoryAttachmentStore::Init(
37 const AttachmentStore::InitCallback& callback) { 39 const AttachmentStore::InitCallback& callback) {
38 DCHECK(CalledOnValidThread()); 40 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
39 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS)); 41 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
40 } 42 }
41 43
42 void InMemoryAttachmentStore::Read( 44 void InMemoryAttachmentStore::Read(
43 AttachmentStore::Component component, 45 AttachmentStore::Component component,
44 const AttachmentIdList& ids, 46 const AttachmentIdList& ids,
45 const AttachmentStore::ReadCallback& callback) { 47 const AttachmentStore::ReadCallback& callback) {
46 DCHECK(CalledOnValidThread()); 48 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
47 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; 49 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
48 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap); 50 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap);
49 std::unique_ptr<AttachmentIdList> unavailable_attachments( 51 std::unique_ptr<AttachmentIdList> unavailable_attachments(
50 new AttachmentIdList); 52 new AttachmentIdList);
51 53
52 for (const auto& id : ids) { 54 for (const auto& id : ids) {
53 AttachmentEntryMap::iterator iter = attachments_.find(id); 55 AttachmentEntryMap::iterator iter = attachments_.find(id);
54 if (iter != attachments_.end() && 56 if (iter != attachments_.end() &&
55 iter->second.components.count(component) > 0) { 57 iter->second.components.count(component) > 0) {
56 const Attachment& attachment = iter->second.attachment; 58 const Attachment& attachment = iter->second.attachment;
57 result_map->insert(std::make_pair(id, attachment)); 59 result_map->insert(std::make_pair(id, attachment));
58 } else { 60 } else {
59 unavailable_attachments->push_back(id); 61 unavailable_attachments->push_back(id);
60 } 62 }
61 } 63 }
62 if (!unavailable_attachments->empty()) { 64 if (!unavailable_attachments->empty()) {
63 result_code = AttachmentStore::UNSPECIFIED_ERROR; 65 result_code = AttachmentStore::UNSPECIFIED_ERROR;
64 } 66 }
65 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map), 67 PostCallback(base::Bind(callback, result_code, base::Passed(&result_map),
66 base::Passed(&unavailable_attachments))); 68 base::Passed(&unavailable_attachments)));
67 } 69 }
68 70
69 void InMemoryAttachmentStore::Write( 71 void InMemoryAttachmentStore::Write(
70 AttachmentStore::Component component, 72 AttachmentStore::Component component,
71 const AttachmentList& attachments, 73 const AttachmentList& attachments,
72 const AttachmentStore::WriteCallback& callback) { 74 const AttachmentStore::WriteCallback& callback) {
73 DCHECK(CalledOnValidThread()); 75 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
74 for (const auto& attachment : attachments) { 76 for (const auto& attachment : attachments) {
75 attachments_.insert(std::make_pair(attachment.GetId(), 77 attachments_.insert(std::make_pair(attachment.GetId(),
76 AttachmentEntry(attachment, component))); 78 AttachmentEntry(attachment, component)));
77 } 79 }
78 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS)); 80 PostCallback(base::Bind(callback, AttachmentStore::SUCCESS));
79 } 81 }
80 82
81 void InMemoryAttachmentStore::SetReference(AttachmentStore::Component component, 83 void InMemoryAttachmentStore::SetReference(AttachmentStore::Component component,
82 const AttachmentIdList& ids) { 84 const AttachmentIdList& ids) {
83 DCHECK(CalledOnValidThread()); 85 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
84 for (const auto& id : ids) { 86 for (const auto& id : ids) {
85 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id); 87 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
86 if (attachments_iter != attachments_.end()) { 88 if (attachments_iter != attachments_.end()) {
87 attachments_iter->second.components.insert(component); 89 attachments_iter->second.components.insert(component);
88 } 90 }
89 } 91 }
90 } 92 }
91 93
92 void InMemoryAttachmentStore::DropReference( 94 void InMemoryAttachmentStore::DropReference(
93 AttachmentStore::Component component, 95 AttachmentStore::Component component,
94 const AttachmentIdList& ids, 96 const AttachmentIdList& ids,
95 const AttachmentStore::DropCallback& callback) { 97 const AttachmentStore::DropCallback& callback) {
96 DCHECK(CalledOnValidThread()); 98 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
97 AttachmentStore::Result result = AttachmentStore::SUCCESS; 99 AttachmentStore::Result result = AttachmentStore::SUCCESS;
98 for (const auto& id : ids) { 100 for (const auto& id : ids) {
99 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id); 101 AttachmentEntryMap::iterator attachments_iter = attachments_.find(id);
100 if (attachments_iter == attachments_.end()) { 102 if (attachments_iter == attachments_.end()) {
101 continue; 103 continue;
102 } 104 }
103 attachments_iter->second.components.erase(component); 105 attachments_iter->second.components.erase(component);
104 if (attachments_iter->second.components.empty()) { 106 if (attachments_iter->second.components.empty()) {
105 attachments_.erase(attachments_iter); 107 attachments_.erase(attachments_iter);
106 } 108 }
107 } 109 }
108 PostCallback(base::Bind(callback, result)); 110 PostCallback(base::Bind(callback, result));
109 } 111 }
110 112
111 void InMemoryAttachmentStore::ReadMetadataById( 113 void InMemoryAttachmentStore::ReadMetadataById(
112 AttachmentStore::Component component, 114 AttachmentStore::Component component,
113 const AttachmentIdList& ids, 115 const AttachmentIdList& ids,
114 const AttachmentStore::ReadMetadataCallback& callback) { 116 const AttachmentStore::ReadMetadataCallback& callback) {
115 DCHECK(CalledOnValidThread()); 117 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
116 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; 118 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
117 std::unique_ptr<AttachmentMetadataList> metadata_list( 119 std::unique_ptr<AttachmentMetadataList> metadata_list(
118 new AttachmentMetadataList()); 120 new AttachmentMetadataList());
119 121
120 for (const auto& id : ids) { 122 for (const auto& id : ids) {
121 AttachmentEntryMap::iterator iter = attachments_.find(id); 123 AttachmentEntryMap::iterator iter = attachments_.find(id);
122 if (iter == attachments_.end()) { 124 if (iter == attachments_.end()) {
123 result_code = AttachmentStore::UNSPECIFIED_ERROR; 125 result_code = AttachmentStore::UNSPECIFIED_ERROR;
124 continue; 126 continue;
125 } 127 }
126 DCHECK_GT(iter->second.components.size(), 0u); 128 DCHECK_GT(iter->second.components.size(), 0u);
127 if (iter->second.components.count(component) == 0) { 129 if (iter->second.components.count(component) == 0) {
128 result_code = AttachmentStore::UNSPECIFIED_ERROR; 130 result_code = AttachmentStore::UNSPECIFIED_ERROR;
129 continue; 131 continue;
130 } 132 }
131 AppendMetadata(metadata_list.get(), iter->second.attachment); 133 AppendMetadata(metadata_list.get(), iter->second.attachment);
132 } 134 }
133 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); 135 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
134 } 136 }
135 137
136 void InMemoryAttachmentStore::ReadMetadata( 138 void InMemoryAttachmentStore::ReadMetadata(
137 AttachmentStore::Component component, 139 AttachmentStore::Component component,
138 const AttachmentStore::ReadMetadataCallback& callback) { 140 const AttachmentStore::ReadMetadataCallback& callback) {
139 DCHECK(CalledOnValidThread()); 141 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
140 AttachmentStore::Result result_code = AttachmentStore::SUCCESS; 142 AttachmentStore::Result result_code = AttachmentStore::SUCCESS;
141 std::unique_ptr<AttachmentMetadataList> metadata_list( 143 std::unique_ptr<AttachmentMetadataList> metadata_list(
142 new AttachmentMetadataList()); 144 new AttachmentMetadataList());
143 145
144 for (AttachmentEntryMap::const_iterator iter = attachments_.begin(); 146 for (AttachmentEntryMap::const_iterator iter = attachments_.begin();
145 iter != attachments_.end(); ++iter) { 147 iter != attachments_.end(); ++iter) {
146 DCHECK_GT(iter->second.components.size(), 0u); 148 DCHECK_GT(iter->second.components.size(), 0u);
147 if (iter->second.components.count(component) > 0) { 149 if (iter->second.components.count(component) > 0) {
148 AppendMetadata(metadata_list.get(), iter->second.attachment); 150 AppendMetadata(metadata_list.get(), iter->second.attachment);
149 } 151 }
150 } 152 }
151 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); 153 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list)));
152 } 154 }
153 155
154 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry( 156 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry(
155 const Attachment& attachment, 157 const Attachment& attachment,
156 AttachmentStore::Component initial_reference_component) 158 AttachmentStore::Component initial_reference_component)
157 : attachment(attachment) { 159 : attachment(attachment) {
158 components.insert(initial_reference_component); 160 components.insert(initial_reference_component);
159 } 161 }
160 162
161 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry( 163 InMemoryAttachmentStore::AttachmentEntry::AttachmentEntry(
162 const AttachmentEntry& other) = default; 164 const AttachmentEntry& other) = default;
163 165
164 InMemoryAttachmentStore::AttachmentEntry::~AttachmentEntry() {} 166 InMemoryAttachmentStore::AttachmentEntry::~AttachmentEntry() {}
165 167
166 } // namespace syncer 168 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698