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/api/attachments/attachment_service_impl.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "sync/api/attachments/attachment.h"
10 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h"
11 #include "sync/internal_api/public/attachments/fake_attachment_store.h"
12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h"
13
14 namespace syncer {
15
16 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls.
17 // GetOrDownloadState tracks completion of these calls and posts callback for
18 // consumer once all attachments are either retrieved or reported unavailable.
19 class AttachmentServiceImpl::GetOrDownloadState
20 : public base::RefCounted<GetOrDownloadState>,
21 public base::NonThreadSafe {
22 public:
23 // GetOrDownloadState gets parameter from values passed to
24 // AttachmentService::GetOrDownloadAttachments.
25 // |attachment_ids| is a list of attachmens to retrieve.
26 // |callback| will be posted on current thread when all attachments retrieved
27 // or confirmed unavailable.
28 GetOrDownloadState(const AttachmentIdList& attachment_ids,
29 const GetOrDownloadCallback& callback);
30
31 // Attachment was just retrieved. Add it to retrieved attachments.
32 void AddAttachment(const Attachment& attachment);
33
34 // Both reading from local store and downloading attachment failed.
35 // Add it to unavailable set.
36 void AddUnavailableAttachmentId(const AttachmentId& attachment_id);
37
38 private:
39 friend class base::RefCounted<GetOrDownloadState>;
40 virtual ~GetOrDownloadState();
41
42 // If all attachment requests completed then post callback to consumer with
43 // results.
44 void PostResultIfAllRequestsCompleted();
45
46 GetOrDownloadCallback callback_;
47
48 // Requests for these attachments are still in progress.
49 AttachmentIdSet in_progress_attachments_;
50
51 AttachmentIdSet unavailable_attachments_;
52 scoped_ptr<AttachmentMap> retrieved_attachments_;
53
54 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState);
55 };
56
GetOrDownloadState(const AttachmentIdList & attachment_ids,const GetOrDownloadCallback & callback)57 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState(
58 const AttachmentIdList& attachment_ids,
59 const GetOrDownloadCallback& callback)
60 : callback_(callback), retrieved_attachments_(new AttachmentMap()) {
61 std::copy(
62 attachment_ids.begin(),
63 attachment_ids.end(),
64 std::inserter(in_progress_attachments_, in_progress_attachments_.end()));
65 PostResultIfAllRequestsCompleted();
66 }
67
~GetOrDownloadState()68 AttachmentServiceImpl::GetOrDownloadState::~GetOrDownloadState() {
69 DCHECK(CalledOnValidThread());
70 }
71
AddAttachment(const Attachment & attachment)72 void AttachmentServiceImpl::GetOrDownloadState::AddAttachment(
73 const Attachment& attachment) {
74 DCHECK(CalledOnValidThread());
75 DCHECK(retrieved_attachments_->find(attachment.GetId()) ==
76 retrieved_attachments_->end());
77 retrieved_attachments_->insert(
78 std::make_pair(attachment.GetId(), attachment));
79 DCHECK(in_progress_attachments_.find(attachment.GetId()) !=
80 in_progress_attachments_.end());
81 in_progress_attachments_.erase(attachment.GetId());
82 PostResultIfAllRequestsCompleted();
83 }
84
AddUnavailableAttachmentId(const AttachmentId & attachment_id)85 void AttachmentServiceImpl::GetOrDownloadState::AddUnavailableAttachmentId(
86 const AttachmentId& attachment_id) {
87 DCHECK(CalledOnValidThread());
88 DCHECK(unavailable_attachments_.find(attachment_id) ==
89 unavailable_attachments_.end());
90 unavailable_attachments_.insert(attachment_id);
91 DCHECK(in_progress_attachments_.find(attachment_id) !=
92 in_progress_attachments_.end());
93 in_progress_attachments_.erase(attachment_id);
94 PostResultIfAllRequestsCompleted();
95 }
96
97 void
PostResultIfAllRequestsCompleted()98 AttachmentServiceImpl::GetOrDownloadState::PostResultIfAllRequestsCompleted() {
99 if (in_progress_attachments_.empty()) {
100 // All requests completed. Let's notify consumer.
101 GetOrDownloadResult result =
102 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR;
103 base::MessageLoop::current()->PostTask(
104 FROM_HERE,
105 base::Bind(callback_, result, base::Passed(&retrieved_attachments_)));
106 }
107 }
108
AttachmentServiceImpl(scoped_ptr<AttachmentStore> attachment_store,scoped_ptr<AttachmentUploader> attachment_uploader,scoped_ptr<AttachmentDownloader> attachment_downloader,Delegate * delegate)109 AttachmentServiceImpl::AttachmentServiceImpl(
110 scoped_ptr<AttachmentStore> attachment_store,
111 scoped_ptr<AttachmentUploader> attachment_uploader,
112 scoped_ptr<AttachmentDownloader> attachment_downloader,
113 Delegate* delegate)
114 : attachment_store_(attachment_store.Pass()),
115 attachment_uploader_(attachment_uploader.Pass()),
116 attachment_downloader_(attachment_downloader.Pass()),
117 delegate_(delegate),
118 weak_ptr_factory_(this) {
119 DCHECK(CalledOnValidThread());
120 DCHECK(attachment_store_);
121 DCHECK(attachment_uploader_);
122 }
123
~AttachmentServiceImpl()124 AttachmentServiceImpl::~AttachmentServiceImpl() {
125 DCHECK(CalledOnValidThread());
126 }
127
128 // Static.
CreateForTest()129 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() {
130 scoped_ptr<syncer::AttachmentStore> attachment_store(
131 new syncer::FakeAttachmentStore(base::MessageLoopProxy::current()));
132 scoped_ptr<AttachmentUploader> attachment_uploader(
133 new FakeAttachmentUploader);
134 scoped_ptr<AttachmentDownloader> attachment_downloader(
135 new FakeAttachmentDownloader());
136 scoped_ptr<syncer::AttachmentService> attachment_service(
137 new syncer::AttachmentServiceImpl(attachment_store.Pass(),
138 attachment_uploader.Pass(),
139 attachment_downloader.Pass(),
140 NULL));
141 return attachment_service.Pass();
142 }
143
GetOrDownloadAttachments(const AttachmentIdList & attachment_ids,const GetOrDownloadCallback & callback)144 void AttachmentServiceImpl::GetOrDownloadAttachments(
145 const AttachmentIdList& attachment_ids,
146 const GetOrDownloadCallback& callback) {
147 DCHECK(CalledOnValidThread());
148 scoped_refptr<GetOrDownloadState> state(
149 new GetOrDownloadState(attachment_ids, callback));
150 attachment_store_->Read(attachment_ids,
151 base::Bind(&AttachmentServiceImpl::ReadDone,
152 weak_ptr_factory_.GetWeakPtr(),
153 state));
154 }
155
DropAttachments(const AttachmentIdList & attachment_ids,const DropCallback & callback)156 void AttachmentServiceImpl::DropAttachments(
157 const AttachmentIdList& attachment_ids,
158 const DropCallback& callback) {
159 DCHECK(CalledOnValidThread());
160 attachment_store_->Drop(attachment_ids,
161 base::Bind(&AttachmentServiceImpl::DropDone,
162 weak_ptr_factory_.GetWeakPtr(),
163 callback));
164 }
165
StoreAttachments(const AttachmentList & attachments,const StoreCallback & callback)166 void AttachmentServiceImpl::StoreAttachments(const AttachmentList& attachments,
167 const StoreCallback& callback) {
168 DCHECK(CalledOnValidThread());
169 attachment_store_->Write(attachments,
170 base::Bind(&AttachmentServiceImpl::WriteDone,
171 weak_ptr_factory_.GetWeakPtr(),
172 callback));
173 for (AttachmentList::const_iterator iter = attachments.begin();
174 iter != attachments.end();
175 ++iter) {
176 attachment_uploader_->UploadAttachment(
177 *iter,
178 base::Bind(&AttachmentServiceImpl::UploadDone,
179 weak_ptr_factory_.GetWeakPtr()));
180 }
181 }
182
OnSyncDataDelete(const SyncData & sync_data)183 void AttachmentServiceImpl::OnSyncDataDelete(const SyncData& sync_data) {
184 DCHECK(CalledOnValidThread());
185 // TODO(maniscalco): One or more of sync_data's attachments may no longer be
186 // referenced anywhere. We should probably delete them at this point (bug
187 // 356351).
188 }
189
OnSyncDataUpdate(const AttachmentIdList & old_attachment_ids,const SyncData & updated_sync_data)190 void AttachmentServiceImpl::OnSyncDataUpdate(
191 const AttachmentIdList& old_attachment_ids,
192 const SyncData& updated_sync_data) {
193 DCHECK(CalledOnValidThread());
194 // TODO(maniscalco): At this point we need to ensure we write all new
195 // attachments referenced by updated_sync_data to local storage and schedule
196 // them up upload to the server. We also need to remove any no unreferenced
197 // attachments from local storage (bug 356351).
198 }
199
ReadDone(const scoped_refptr<GetOrDownloadState> & state,const AttachmentStore::Result & result,scoped_ptr<AttachmentMap> attachments,scoped_ptr<AttachmentIdList> unavailable_attachment_ids)200 void AttachmentServiceImpl::ReadDone(
201 const scoped_refptr<GetOrDownloadState>& state,
202 const AttachmentStore::Result& result,
203 scoped_ptr<AttachmentMap> attachments,
204 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) {
205 // Add read attachments to result.
206 for (AttachmentMap::const_iterator iter = attachments->begin();
207 iter != attachments->end();
208 ++iter) {
209 state->AddAttachment(iter->second);
210 }
211 // Try to download locally unavailable attachments.
212 for (AttachmentIdList::const_iterator iter =
213 unavailable_attachment_ids->begin();
214 iter != unavailable_attachment_ids->end();
215 ++iter) {
216 attachment_downloader_->DownloadAttachment(
217 *iter,
218 base::Bind(&AttachmentServiceImpl::DownloadDone,
219 weak_ptr_factory_.GetWeakPtr(),
220 state,
221 *iter));
222 ;
223 }
224 }
225
DropDone(const DropCallback & callback,const AttachmentStore::Result & result)226 void AttachmentServiceImpl::DropDone(const DropCallback& callback,
227 const AttachmentStore::Result& result) {
228 AttachmentService::DropResult drop_result =
229 AttachmentService::DROP_UNSPECIFIED_ERROR;
230 if (result == AttachmentStore::SUCCESS) {
231 drop_result = AttachmentService::DROP_SUCCESS;
232 }
233 // TODO(maniscalco): Deal with case where an error occurred (bug 361251).
234 base::MessageLoop::current()->PostTask(FROM_HERE,
235 base::Bind(callback, drop_result));
236 }
237
WriteDone(const StoreCallback & callback,const AttachmentStore::Result & result)238 void AttachmentServiceImpl::WriteDone(const StoreCallback& callback,
239 const AttachmentStore::Result& result) {
240 AttachmentService::StoreResult store_result =
241 AttachmentService::STORE_UNSPECIFIED_ERROR;
242 if (result == AttachmentStore::SUCCESS) {
243 store_result = AttachmentService::STORE_SUCCESS;
244 }
245 // TODO(maniscalco): Deal with case where an error occurred (bug 361251).
246 base::MessageLoop::current()->PostTask(FROM_HERE,
247 base::Bind(callback, store_result));
248 }
249
UploadDone(const AttachmentUploader::UploadResult & result,const AttachmentId & attachment_id)250 void AttachmentServiceImpl::UploadDone(
251 const AttachmentUploader::UploadResult& result,
252 const AttachmentId& attachment_id) {
253 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures.
254 if (result != AttachmentUploader::UPLOAD_SUCCESS)
255 return;
256 if (delegate_) {
257 delegate_->OnAttachmentUploaded(attachment_id);
258 }
259 }
260
DownloadDone(const scoped_refptr<GetOrDownloadState> & state,const AttachmentId & attachment_id,const AttachmentDownloader::DownloadResult & result,scoped_ptr<Attachment> attachment)261 void AttachmentServiceImpl::DownloadDone(
262 const scoped_refptr<GetOrDownloadState>& state,
263 const AttachmentId& attachment_id,
264 const AttachmentDownloader::DownloadResult& result,
265 scoped_ptr<Attachment> attachment) {
266 if (result == AttachmentDownloader::DOWNLOAD_SUCCESS) {
267 state->AddAttachment(*attachment.get());
268 } else {
269 state->AddUnavailableAttachmentId(attachment_id);
270 }
271 }
272
273 } // namespace syncer
274