| OLD | NEW |
| 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 "sync/internal_api/public/attachments/attachment_service_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_service_impl.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // All requests completed. Let's notify consumer. | 103 // All requests completed. Let's notify consumer. |
| 104 GetOrDownloadResult result = | 104 GetOrDownloadResult result = |
| 105 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; | 105 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; |
| 106 base::MessageLoop::current()->PostTask( | 106 base::MessageLoop::current()->PostTask( |
| 107 FROM_HERE, | 107 FROM_HERE, |
| 108 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); | 108 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 AttachmentServiceImpl::AttachmentServiceImpl( | 112 AttachmentServiceImpl::AttachmentServiceImpl( |
| 113 scoped_ptr<AttachmentStore> attachment_store, | 113 scoped_ptr<AttachmentStoreForSync> attachment_store, |
| 114 scoped_ptr<AttachmentUploader> attachment_uploader, | 114 scoped_ptr<AttachmentUploader> attachment_uploader, |
| 115 scoped_ptr<AttachmentDownloader> attachment_downloader, | 115 scoped_ptr<AttachmentDownloader> attachment_downloader, |
| 116 Delegate* delegate, | 116 Delegate* delegate, |
| 117 const base::TimeDelta& initial_backoff_delay, | 117 const base::TimeDelta& initial_backoff_delay, |
| 118 const base::TimeDelta& max_backoff_delay) | 118 const base::TimeDelta& max_backoff_delay) |
| 119 : attachment_store_(attachment_store.Pass()), | 119 : attachment_store_(attachment_store.Pass()), |
| 120 attachment_uploader_(attachment_uploader.Pass()), | 120 attachment_uploader_(attachment_uploader.Pass()), |
| 121 attachment_downloader_(attachment_downloader.Pass()), | 121 attachment_downloader_(attachment_downloader.Pass()), |
| 122 delegate_(delegate), | 122 delegate_(delegate), |
| 123 weak_ptr_factory_(this) { | 123 weak_ptr_factory_(this) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 144 | 144 |
| 145 // Static. | 145 // Static. |
| 146 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { | 146 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { |
| 147 scoped_ptr<syncer::AttachmentStore> attachment_store = | 147 scoped_ptr<syncer::AttachmentStore> attachment_store = |
| 148 AttachmentStore::CreateInMemoryStore(); | 148 AttachmentStore::CreateInMemoryStore(); |
| 149 scoped_ptr<AttachmentUploader> attachment_uploader( | 149 scoped_ptr<AttachmentUploader> attachment_uploader( |
| 150 new FakeAttachmentUploader); | 150 new FakeAttachmentUploader); |
| 151 scoped_ptr<AttachmentDownloader> attachment_downloader( | 151 scoped_ptr<AttachmentDownloader> attachment_downloader( |
| 152 new FakeAttachmentDownloader()); | 152 new FakeAttachmentDownloader()); |
| 153 scoped_ptr<syncer::AttachmentService> attachment_service( | 153 scoped_ptr<syncer::AttachmentService> attachment_service( |
| 154 new syncer::AttachmentServiceImpl(attachment_store.Pass(), | 154 new syncer::AttachmentServiceImpl( |
| 155 attachment_uploader.Pass(), | 155 attachment_store->CreateAttachmentStoreForSync(), |
| 156 attachment_downloader.Pass(), | 156 attachment_uploader.Pass(), attachment_downloader.Pass(), NULL, |
| 157 NULL, | 157 base::TimeDelta(), base::TimeDelta())); |
| 158 base::TimeDelta(), | |
| 159 base::TimeDelta())); | |
| 160 return attachment_service.Pass(); | 158 return attachment_service.Pass(); |
| 161 } | 159 } |
| 162 | 160 |
| 163 void AttachmentServiceImpl::GetOrDownloadAttachments( | 161 void AttachmentServiceImpl::GetOrDownloadAttachments( |
| 164 const AttachmentIdList& attachment_ids, | 162 const AttachmentIdList& attachment_ids, |
| 165 const GetOrDownloadCallback& callback) { | 163 const GetOrDownloadCallback& callback) { |
| 166 DCHECK(CalledOnValidThread()); | 164 DCHECK(CalledOnValidThread()); |
| 167 scoped_refptr<GetOrDownloadState> state( | 165 scoped_refptr<GetOrDownloadState> state( |
| 168 new GetOrDownloadState(attachment_ids, callback)); | 166 new GetOrDownloadState(attachment_ids, callback)); |
| 169 attachment_store_->Read(attachment_ids, | 167 attachment_store_->Read(attachment_ids, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 case AttachmentStore::STORE_INITIALIZATION_FAILED: | 214 case AttachmentStore::STORE_INITIALIZATION_FAILED: |
| 217 state->AddUnavailableAttachmentId(attachment.GetId()); | 215 state->AddUnavailableAttachmentId(attachment.GetId()); |
| 218 break; | 216 break; |
| 219 } | 217 } |
| 220 } | 218 } |
| 221 | 219 |
| 222 void AttachmentServiceImpl::UploadDone( | 220 void AttachmentServiceImpl::UploadDone( |
| 223 const AttachmentUploader::UploadResult& result, | 221 const AttachmentUploader::UploadResult& result, |
| 224 const AttachmentId& attachment_id) { | 222 const AttachmentId& attachment_id) { |
| 225 DCHECK(CalledOnValidThread()); | 223 DCHECK(CalledOnValidThread()); |
| 224 AttachmentIdList ids; |
| 225 ids.push_back(attachment_id); |
| 226 switch (result) { | 226 switch (result) { |
| 227 case AttachmentUploader::UPLOAD_SUCCESS: | 227 case AttachmentUploader::UPLOAD_SUCCESS: |
| 228 attachment_store_->DropSyncReference(ids); |
| 228 upload_task_queue_->MarkAsSucceeded(attachment_id); | 229 upload_task_queue_->MarkAsSucceeded(attachment_id); |
| 229 if (delegate_) { | 230 if (delegate_) { |
| 230 delegate_->OnAttachmentUploaded(attachment_id); | 231 delegate_->OnAttachmentUploaded(attachment_id); |
| 231 } | 232 } |
| 232 break; | 233 break; |
| 233 case AttachmentUploader::UPLOAD_TRANSIENT_ERROR: | 234 case AttachmentUploader::UPLOAD_TRANSIENT_ERROR: |
| 234 upload_task_queue_->MarkAsFailed(attachment_id); | 235 upload_task_queue_->MarkAsFailed(attachment_id); |
| 235 upload_task_queue_->AddToQueue(attachment_id); | 236 upload_task_queue_->AddToQueue(attachment_id); |
| 236 break; | 237 break; |
| 237 case AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR: | 238 case AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR: |
| 238 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures. | 239 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures. |
| 240 attachment_store_->DropSyncReference(ids); |
| 239 upload_task_queue_->MarkAsFailed(attachment_id); | 241 upload_task_queue_->MarkAsFailed(attachment_id); |
| 240 break; | 242 break; |
| 241 } | 243 } |
| 242 } | 244 } |
| 243 | 245 |
| 244 void AttachmentServiceImpl::DownloadDone( | 246 void AttachmentServiceImpl::DownloadDone( |
| 245 const scoped_refptr<GetOrDownloadState>& state, | 247 const scoped_refptr<GetOrDownloadState>& state, |
| 246 const AttachmentId& attachment_id, | 248 const AttachmentId& attachment_id, |
| 247 const AttachmentDownloader::DownloadResult& result, | 249 const AttachmentDownloader::DownloadResult& result, |
| 248 scoped_ptr<Attachment> attachment) { | 250 scoped_ptr<Attachment> attachment) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 271 base::Bind(&AttachmentServiceImpl::ReadDoneNowUpload, | 273 base::Bind(&AttachmentServiceImpl::ReadDoneNowUpload, |
| 272 weak_ptr_factory_.GetWeakPtr())); | 274 weak_ptr_factory_.GetWeakPtr())); |
| 273 } | 275 } |
| 274 | 276 |
| 275 void AttachmentServiceImpl::UploadAttachments( | 277 void AttachmentServiceImpl::UploadAttachments( |
| 276 const AttachmentIdList& attachment_ids) { | 278 const AttachmentIdList& attachment_ids) { |
| 277 DCHECK(CalledOnValidThread()); | 279 DCHECK(CalledOnValidThread()); |
| 278 if (!attachment_uploader_.get()) { | 280 if (!attachment_uploader_.get()) { |
| 279 return; | 281 return; |
| 280 } | 282 } |
| 283 attachment_store_->SetSyncReference(attachment_ids); |
| 284 |
| 281 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); | 285 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); |
| 282 ++iter) { | 286 ++iter) { |
| 283 upload_task_queue_->AddToQueue(*iter); | 287 upload_task_queue_->AddToQueue(*iter); |
| 284 } | 288 } |
| 285 } | 289 } |
| 286 | 290 |
| 287 void AttachmentServiceImpl::OnNetworkChanged( | 291 void AttachmentServiceImpl::OnNetworkChanged( |
| 288 net::NetworkChangeNotifier::ConnectionType type) { | 292 net::NetworkChangeNotifier::ConnectionType type) { |
| 289 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { | 293 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 290 upload_task_queue_->ResetBackoff(); | 294 upload_task_queue_->ResetBackoff(); |
| 291 } | 295 } |
| 292 } | 296 } |
| 293 | 297 |
| 294 void AttachmentServiceImpl::ReadDoneNowUpload( | 298 void AttachmentServiceImpl::ReadDoneNowUpload( |
| 295 const AttachmentStore::Result& result, | 299 const AttachmentStore::Result& result, |
| 296 scoped_ptr<AttachmentMap> attachments, | 300 scoped_ptr<AttachmentMap> attachments, |
| 297 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 301 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { |
| 298 DCHECK(CalledOnValidThread()); | 302 DCHECK(CalledOnValidThread()); |
| 299 if (!unavailable_attachment_ids->empty()) { | 303 if (!unavailable_attachment_ids->empty()) { |
| 300 // TODO(maniscalco): We failed to read some attachments. What should we do | 304 // TODO(maniscalco): We failed to read some attachments. What should we do |
| 301 // now? | 305 // now? |
| 302 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); | 306 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); |
| 303 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); | 307 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); |
| 304 for (; iter != end; ++iter) { | 308 for (; iter != end; ++iter) { |
| 305 upload_task_queue_->Cancel(*iter); | 309 upload_task_queue_->Cancel(*iter); |
| 306 } | 310 } |
| 311 attachment_store_->DropSyncReference(*unavailable_attachment_ids); |
| 307 } | 312 } |
| 308 | 313 |
| 309 AttachmentMap::const_iterator iter = attachments->begin(); | 314 AttachmentMap::const_iterator iter = attachments->begin(); |
| 310 AttachmentMap::const_iterator end = attachments->end(); | 315 AttachmentMap::const_iterator end = attachments->end(); |
| 311 for (; iter != end; ++iter) { | 316 for (; iter != end; ++iter) { |
| 312 attachment_uploader_->UploadAttachment( | 317 attachment_uploader_->UploadAttachment( |
| 313 iter->second, | 318 iter->second, |
| 314 base::Bind(&AttachmentServiceImpl::UploadDone, | 319 base::Bind(&AttachmentServiceImpl::UploadDone, |
| 315 weak_ptr_factory_.GetWeakPtr())); | 320 weak_ptr_factory_.GetWeakPtr())); |
| 316 } | 321 } |
| 317 } | 322 } |
| 318 | 323 |
| 319 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 324 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { |
| 320 upload_task_queue_->SetTimerForTest(timer.Pass()); | 325 upload_task_queue_->SetTimerForTest(timer.Pass()); |
| 321 } | 326 } |
| 322 | 327 |
| 323 } // namespace syncer | 328 } // namespace syncer |
| OLD | NEW |