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/attachment_service_impl.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "sync/api/attachments/attachment.h"
14 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h"
15 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h"
19 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls.
20 // GetOrDownloadState tracks completion of these calls and posts callback for
21 // consumer once all attachments are either retrieved or reported unavailable.
22 class AttachmentServiceImpl::GetOrDownloadState
23 : public base::RefCounted
<GetOrDownloadState
>,
24 public base::NonThreadSafe
{
26 // GetOrDownloadState gets parameter from values passed to
27 // AttachmentService::GetOrDownloadAttachments.
28 // |attachment_ids| is a list of attachmens to retrieve.
29 // |callback| will be posted on current thread when all attachments retrieved
30 // or confirmed unavailable.
31 GetOrDownloadState(const AttachmentIdList
& attachment_ids
,
32 const GetOrDownloadCallback
& callback
);
34 // Attachment was just retrieved. Add it to retrieved attachments.
35 void AddAttachment(const Attachment
& attachment
);
37 // Both reading from local store and downloading attachment failed.
38 // Add it to unavailable set.
39 void AddUnavailableAttachmentId(const AttachmentId
& attachment_id
);
42 friend class base::RefCounted
<GetOrDownloadState
>;
43 virtual ~GetOrDownloadState();
45 // If all attachment requests completed then post callback to consumer with
47 void PostResultIfAllRequestsCompleted();
49 GetOrDownloadCallback callback_
;
51 // Requests for these attachments are still in progress.
52 AttachmentIdSet in_progress_attachments_
;
54 AttachmentIdSet unavailable_attachments_
;
55 scoped_ptr
<AttachmentMap
> retrieved_attachments_
;
57 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState
);
60 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState(
61 const AttachmentIdList
& attachment_ids
,
62 const GetOrDownloadCallback
& callback
)
63 : callback_(callback
), retrieved_attachments_(new AttachmentMap()) {
65 attachment_ids
.begin(),
67 std::inserter(in_progress_attachments_
, in_progress_attachments_
.end()));
68 PostResultIfAllRequestsCompleted();
71 AttachmentServiceImpl::GetOrDownloadState::~GetOrDownloadState() {
72 DCHECK(CalledOnValidThread());
75 void AttachmentServiceImpl::GetOrDownloadState::AddAttachment(
76 const Attachment
& attachment
) {
77 DCHECK(CalledOnValidThread());
78 DCHECK(retrieved_attachments_
->find(attachment
.GetId()) ==
79 retrieved_attachments_
->end());
80 retrieved_attachments_
->insert(
81 std::make_pair(attachment
.GetId(), attachment
));
82 DCHECK(in_progress_attachments_
.find(attachment
.GetId()) !=
83 in_progress_attachments_
.end());
84 in_progress_attachments_
.erase(attachment
.GetId());
85 PostResultIfAllRequestsCompleted();
88 void AttachmentServiceImpl::GetOrDownloadState::AddUnavailableAttachmentId(
89 const AttachmentId
& attachment_id
) {
90 DCHECK(CalledOnValidThread());
91 DCHECK(unavailable_attachments_
.find(attachment_id
) ==
92 unavailable_attachments_
.end());
93 unavailable_attachments_
.insert(attachment_id
);
94 DCHECK(in_progress_attachments_
.find(attachment_id
) !=
95 in_progress_attachments_
.end());
96 in_progress_attachments_
.erase(attachment_id
);
97 PostResultIfAllRequestsCompleted();
101 AttachmentServiceImpl::GetOrDownloadState::PostResultIfAllRequestsCompleted() {
102 if (in_progress_attachments_
.empty()) {
103 // All requests completed. Let's notify consumer.
104 GetOrDownloadResult result
=
105 unavailable_attachments_
.empty() ? GET_SUCCESS
: GET_UNSPECIFIED_ERROR
;
106 base::MessageLoop::current()->PostTask(
108 base::Bind(callback_
, result
, base::Passed(&retrieved_attachments_
)));
112 AttachmentServiceImpl::AttachmentServiceImpl(
113 scoped_ptr
<AttachmentStoreForSync
> attachment_store
,
114 scoped_ptr
<AttachmentUploader
> attachment_uploader
,
115 scoped_ptr
<AttachmentDownloader
> attachment_downloader
,
117 const base::TimeDelta
& initial_backoff_delay
,
118 const base::TimeDelta
& max_backoff_delay
)
119 : attachment_store_(attachment_store
.Pass()),
120 attachment_uploader_(attachment_uploader
.Pass()),
121 attachment_downloader_(attachment_downloader
.Pass()),
123 weak_ptr_factory_(this) {
124 DCHECK(CalledOnValidThread());
125 DCHECK(attachment_store_
.get());
127 // TODO(maniscalco): Observe network connectivity change events. When the
128 // network becomes disconnected, consider suspending queue dispatch. When
129 // connectivity is restored, consider clearing any dispatch backoff (bug
131 upload_task_queue_
.reset(new TaskQueue
<AttachmentId
>(
132 base::Bind(&AttachmentServiceImpl::BeginUpload
,
133 weak_ptr_factory_
.GetWeakPtr()),
134 initial_backoff_delay
,
137 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
140 AttachmentServiceImpl::~AttachmentServiceImpl() {
141 DCHECK(CalledOnValidThread());
142 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
146 scoped_ptr
<syncer::AttachmentService
> AttachmentServiceImpl::CreateForTest() {
147 scoped_ptr
<syncer::AttachmentStore
> attachment_store
=
148 AttachmentStore::CreateInMemoryStore();
149 scoped_ptr
<AttachmentUploader
> attachment_uploader(
150 new FakeAttachmentUploader
);
151 scoped_ptr
<AttachmentDownloader
> attachment_downloader(
152 new FakeAttachmentDownloader());
153 scoped_ptr
<syncer::AttachmentService
> attachment_service(
154 new syncer::AttachmentServiceImpl(
155 attachment_store
->CreateAttachmentStoreForSync(),
156 attachment_uploader
.Pass(), attachment_downloader
.Pass(), NULL
,
157 base::TimeDelta(), base::TimeDelta()));
158 return attachment_service
.Pass();
161 void AttachmentServiceImpl::GetOrDownloadAttachments(
162 const AttachmentIdList
& attachment_ids
,
163 const GetOrDownloadCallback
& callback
) {
164 DCHECK(CalledOnValidThread());
165 scoped_refptr
<GetOrDownloadState
> state(
166 new GetOrDownloadState(attachment_ids
, callback
));
167 // SetModelTypeReference() makes attachments visible for model type.
168 // Needed when attachment doesn't have model type reference, but still
169 // available in local store.
170 attachment_store_
->SetModelTypeReference(attachment_ids
);
171 attachment_store_
->Read(attachment_ids
,
172 base::Bind(&AttachmentServiceImpl::ReadDone
,
173 weak_ptr_factory_
.GetWeakPtr(), state
));
176 void AttachmentServiceImpl::ReadDone(
177 const scoped_refptr
<GetOrDownloadState
>& state
,
178 const AttachmentStore::Result
& result
,
179 scoped_ptr
<AttachmentMap
> attachments
,
180 scoped_ptr
<AttachmentIdList
> unavailable_attachment_ids
) {
181 // Add read attachments to result.
182 for (AttachmentMap::const_iterator iter
= attachments
->begin();
183 iter
!= attachments
->end();
185 state
->AddAttachment(iter
->second
);
188 AttachmentIdList::const_iterator iter
= unavailable_attachment_ids
->begin();
189 AttachmentIdList::const_iterator end
= unavailable_attachment_ids
->end();
190 if (result
!= AttachmentStore::STORE_INITIALIZATION_FAILED
&&
191 attachment_downloader_
.get()) {
192 // Try to download locally unavailable attachments.
193 for (; iter
!= end
; ++iter
) {
194 attachment_downloader_
->DownloadAttachment(
196 base::Bind(&AttachmentServiceImpl::DownloadDone
,
197 weak_ptr_factory_
.GetWeakPtr(),
202 // No downloader so all locally unavailable attachments are unavailable.
203 for (; iter
!= end
; ++iter
) {
204 state
->AddUnavailableAttachmentId(*iter
);
209 void AttachmentServiceImpl::WriteDone(
210 const scoped_refptr
<GetOrDownloadState
>& state
,
211 const Attachment
& attachment
,
212 const AttachmentStore::Result
& result
) {
214 case AttachmentStore::SUCCESS
:
215 state
->AddAttachment(attachment
);
217 case AttachmentStore::UNSPECIFIED_ERROR
:
218 case AttachmentStore::STORE_INITIALIZATION_FAILED
:
219 state
->AddUnavailableAttachmentId(attachment
.GetId());
224 void AttachmentServiceImpl::UploadDone(
225 const AttachmentUploader::UploadResult
& result
,
226 const AttachmentId
& attachment_id
) {
227 DCHECK(CalledOnValidThread());
228 AttachmentIdList ids
;
229 ids
.push_back(attachment_id
);
231 case AttachmentUploader::UPLOAD_SUCCESS
:
232 attachment_store_
->DropSyncReference(ids
);
233 upload_task_queue_
->MarkAsSucceeded(attachment_id
);
235 delegate_
->OnAttachmentUploaded(attachment_id
);
238 case AttachmentUploader::UPLOAD_TRANSIENT_ERROR
:
239 upload_task_queue_
->MarkAsFailed(attachment_id
);
240 upload_task_queue_
->AddToQueue(attachment_id
);
242 case AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR
:
243 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures.
244 attachment_store_
->DropSyncReference(ids
);
245 upload_task_queue_
->MarkAsFailed(attachment_id
);
250 void AttachmentServiceImpl::DownloadDone(
251 const scoped_refptr
<GetOrDownloadState
>& state
,
252 const AttachmentId
& attachment_id
,
253 const AttachmentDownloader::DownloadResult
& result
,
254 scoped_ptr
<Attachment
> attachment
) {
256 case AttachmentDownloader::DOWNLOAD_SUCCESS
: {
257 AttachmentList attachment_list
;
258 attachment_list
.push_back(*attachment
.get());
259 attachment_store_
->Write(
261 base::Bind(&AttachmentServiceImpl::WriteDone
,
262 weak_ptr_factory_
.GetWeakPtr(), state
, *attachment
.get()));
265 case AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR
:
266 case AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR
:
267 state
->AddUnavailableAttachmentId(attachment_id
);
272 void AttachmentServiceImpl::BeginUpload(const AttachmentId
& attachment_id
) {
273 DCHECK(CalledOnValidThread());
274 AttachmentIdList attachment_ids
;
275 attachment_ids
.push_back(attachment_id
);
276 attachment_store_
->Read(attachment_ids
,
277 base::Bind(&AttachmentServiceImpl::ReadDoneNowUpload
,
278 weak_ptr_factory_
.GetWeakPtr()));
281 void AttachmentServiceImpl::UploadAttachments(
282 const AttachmentIdList
& attachment_ids
) {
283 DCHECK(CalledOnValidThread());
284 if (!attachment_uploader_
.get()) {
287 attachment_store_
->SetSyncReference(attachment_ids
);
289 for (auto iter
= attachment_ids
.begin(); iter
!= attachment_ids
.end();
291 upload_task_queue_
->AddToQueue(*iter
);
295 void AttachmentServiceImpl::OnNetworkChanged(
296 net::NetworkChangeNotifier::ConnectionType type
) {
297 if (type
!= net::NetworkChangeNotifier::CONNECTION_NONE
) {
298 upload_task_queue_
->ResetBackoff();
302 void AttachmentServiceImpl::ReadDoneNowUpload(
303 const AttachmentStore::Result
& result
,
304 scoped_ptr
<AttachmentMap
> attachments
,
305 scoped_ptr
<AttachmentIdList
> unavailable_attachment_ids
) {
306 DCHECK(CalledOnValidThread());
307 if (!unavailable_attachment_ids
->empty()) {
308 // TODO(maniscalco): We failed to read some attachments. What should we do
310 AttachmentIdList::const_iterator iter
= unavailable_attachment_ids
->begin();
311 AttachmentIdList::const_iterator end
= unavailable_attachment_ids
->end();
312 for (; iter
!= end
; ++iter
) {
313 upload_task_queue_
->Cancel(*iter
);
315 attachment_store_
->DropSyncReference(*unavailable_attachment_ids
);
318 AttachmentMap::const_iterator iter
= attachments
->begin();
319 AttachmentMap::const_iterator end
= attachments
->end();
320 for (; iter
!= end
; ++iter
) {
321 attachment_uploader_
->UploadAttachment(
323 base::Bind(&AttachmentServiceImpl::UploadDone
,
324 weak_ptr_factory_
.GetWeakPtr()));
328 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr
<base::Timer
> timer
) {
329 upload_task_queue_
->SetTimerForTest(timer
.Pass());
332 } // namespace syncer