We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / sync / internal_api / attachments / attachment_service_impl.cc
blobfb6517292adec7f9e2600d234267dba6fa596aac
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"
7 #include <iterator>
9 #include "base/bind.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"
17 namespace syncer {
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 {
25 public:
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);
41 private:
42 friend class base::RefCounted<GetOrDownloadState>;
43 virtual ~GetOrDownloadState();
45 // If all attachment requests completed then post callback to consumer with
46 // results.
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()) {
64 std::copy(
65 attachment_ids.begin(),
66 attachment_ids.end(),
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();
100 void
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(
107 FROM_HERE,
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,
116 Delegate* delegate,
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()),
122 delegate_(delegate),
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
130 // 411981).
131 upload_task_queue_.reset(new TaskQueue<AttachmentId>(
132 base::Bind(&AttachmentServiceImpl::BeginUpload,
133 weak_ptr_factory_.GetWeakPtr()),
134 initial_backoff_delay,
135 max_backoff_delay));
137 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
140 AttachmentServiceImpl::~AttachmentServiceImpl() {
141 DCHECK(CalledOnValidThread());
142 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
145 // Static.
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 attachment_store_->Read(attachment_ids,
168 base::Bind(&AttachmentServiceImpl::ReadDone,
169 weak_ptr_factory_.GetWeakPtr(), state));
172 void AttachmentServiceImpl::ReadDone(
173 const scoped_refptr<GetOrDownloadState>& state,
174 const AttachmentStore::Result& result,
175 scoped_ptr<AttachmentMap> attachments,
176 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) {
177 // Add read attachments to result.
178 for (AttachmentMap::const_iterator iter = attachments->begin();
179 iter != attachments->end();
180 ++iter) {
181 state->AddAttachment(iter->second);
184 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin();
185 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end();
186 if (result != AttachmentStore::STORE_INITIALIZATION_FAILED &&
187 attachment_downloader_.get()) {
188 // Try to download locally unavailable attachments.
189 for (; iter != end; ++iter) {
190 attachment_downloader_->DownloadAttachment(
191 *iter,
192 base::Bind(&AttachmentServiceImpl::DownloadDone,
193 weak_ptr_factory_.GetWeakPtr(),
194 state,
195 *iter));
197 } else {
198 // No downloader so all locally unavailable attachments are unavailable.
199 for (; iter != end; ++iter) {
200 state->AddUnavailableAttachmentId(*iter);
205 void AttachmentServiceImpl::WriteDone(
206 const scoped_refptr<GetOrDownloadState>& state,
207 const Attachment& attachment,
208 const AttachmentStore::Result& result) {
209 switch (result) {
210 case AttachmentStore::SUCCESS:
211 state->AddAttachment(attachment);
212 break;
213 case AttachmentStore::UNSPECIFIED_ERROR:
214 case AttachmentStore::STORE_INITIALIZATION_FAILED:
215 state->AddUnavailableAttachmentId(attachment.GetId());
216 break;
220 void AttachmentServiceImpl::UploadDone(
221 const AttachmentUploader::UploadResult& result,
222 const AttachmentId& attachment_id) {
223 DCHECK(CalledOnValidThread());
224 AttachmentIdList ids;
225 ids.push_back(attachment_id);
226 switch (result) {
227 case AttachmentUploader::UPLOAD_SUCCESS:
228 attachment_store_->DropSyncReference(ids);
229 upload_task_queue_->MarkAsSucceeded(attachment_id);
230 if (delegate_) {
231 delegate_->OnAttachmentUploaded(attachment_id);
233 break;
234 case AttachmentUploader::UPLOAD_TRANSIENT_ERROR:
235 upload_task_queue_->MarkAsFailed(attachment_id);
236 upload_task_queue_->AddToQueue(attachment_id);
237 break;
238 case AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR:
239 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures.
240 attachment_store_->DropSyncReference(ids);
241 upload_task_queue_->MarkAsFailed(attachment_id);
242 break;
246 void AttachmentServiceImpl::DownloadDone(
247 const scoped_refptr<GetOrDownloadState>& state,
248 const AttachmentId& attachment_id,
249 const AttachmentDownloader::DownloadResult& result,
250 scoped_ptr<Attachment> attachment) {
251 switch (result) {
252 case AttachmentDownloader::DOWNLOAD_SUCCESS: {
253 AttachmentList attachment_list;
254 attachment_list.push_back(*attachment.get());
255 attachment_store_->Write(
256 attachment_list,
257 base::Bind(&AttachmentServiceImpl::WriteDone,
258 weak_ptr_factory_.GetWeakPtr(), state, *attachment.get()));
259 break;
261 case AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR:
262 case AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR:
263 state->AddUnavailableAttachmentId(attachment_id);
264 break;
268 void AttachmentServiceImpl::BeginUpload(const AttachmentId& attachment_id) {
269 DCHECK(CalledOnValidThread());
270 AttachmentIdList attachment_ids;
271 attachment_ids.push_back(attachment_id);
272 attachment_store_->Read(attachment_ids,
273 base::Bind(&AttachmentServiceImpl::ReadDoneNowUpload,
274 weak_ptr_factory_.GetWeakPtr()));
277 void AttachmentServiceImpl::UploadAttachments(
278 const AttachmentIdList& attachment_ids) {
279 DCHECK(CalledOnValidThread());
280 if (!attachment_uploader_.get()) {
281 return;
283 attachment_store_->SetSyncReference(attachment_ids);
285 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end();
286 ++iter) {
287 upload_task_queue_->AddToQueue(*iter);
291 void AttachmentServiceImpl::OnNetworkChanged(
292 net::NetworkChangeNotifier::ConnectionType type) {
293 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) {
294 upload_task_queue_->ResetBackoff();
298 void AttachmentServiceImpl::ReadDoneNowUpload(
299 const AttachmentStore::Result& result,
300 scoped_ptr<AttachmentMap> attachments,
301 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) {
302 DCHECK(CalledOnValidThread());
303 if (!unavailable_attachment_ids->empty()) {
304 // TODO(maniscalco): We failed to read some attachments. What should we do
305 // now?
306 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin();
307 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end();
308 for (; iter != end; ++iter) {
309 upload_task_queue_->Cancel(*iter);
311 attachment_store_->DropSyncReference(*unavailable_attachment_ids);
314 AttachmentMap::const_iterator iter = attachments->begin();
315 AttachmentMap::const_iterator end = attachments->end();
316 for (; iter != end; ++iter) {
317 attachment_uploader_->UploadAttachment(
318 iter->second,
319 base::Bind(&AttachmentServiceImpl::UploadDone,
320 weak_ptr_factory_.GetWeakPtr()));
324 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) {
325 upload_task_queue_->SetTimerForTest(timer.Pass());
328 } // namespace syncer