Chromium Code Reviews| 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" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "sync/api/attachments/attachment.h" | 13 #include "sync/api/attachments/attachment.h" |
| 14 #include "sync/internal_api/public/attachments/attachment_store_frontend.h" | |
|
maniscalco
2015/03/06 23:15:03
Maybe I overlooked something, but is this include
pavely
2015/03/09 18:09:29
Done.
| |
| 14 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 15 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" |
| 15 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 16 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
| 16 | 17 |
| 17 namespace syncer { | 18 namespace syncer { |
| 18 | 19 |
| 19 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls. | 20 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls. |
| 20 // GetOrDownloadState tracks completion of these calls and posts callback for | 21 // GetOrDownloadState tracks completion of these calls and posts callback for |
| 21 // consumer once all attachments are either retrieved or reported unavailable. | 22 // consumer once all attachments are either retrieved or reported unavailable. |
| 22 class AttachmentServiceImpl::GetOrDownloadState | 23 class AttachmentServiceImpl::GetOrDownloadState |
| 23 : public base::RefCounted<GetOrDownloadState>, | 24 : public base::RefCounted<GetOrDownloadState>, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 // All requests completed. Let's notify consumer. | 104 // All requests completed. Let's notify consumer. |
| 104 GetOrDownloadResult result = | 105 GetOrDownloadResult result = |
| 105 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; | 106 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; |
| 106 base::MessageLoop::current()->PostTask( | 107 base::MessageLoop::current()->PostTask( |
| 107 FROM_HERE, | 108 FROM_HERE, |
| 108 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); | 109 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); |
| 109 } | 110 } |
| 110 } | 111 } |
| 111 | 112 |
| 112 AttachmentServiceImpl::AttachmentServiceImpl( | 113 AttachmentServiceImpl::AttachmentServiceImpl( |
| 113 scoped_refptr<AttachmentStore> attachment_store, | 114 scoped_ptr<AttachmentStore> attachment_store, |
| 114 scoped_ptr<AttachmentUploader> attachment_uploader, | 115 scoped_ptr<AttachmentUploader> attachment_uploader, |
| 115 scoped_ptr<AttachmentDownloader> attachment_downloader, | 116 scoped_ptr<AttachmentDownloader> attachment_downloader, |
| 116 Delegate* delegate, | 117 Delegate* delegate, |
| 117 const base::TimeDelta& initial_backoff_delay, | 118 const base::TimeDelta& initial_backoff_delay, |
| 118 const base::TimeDelta& max_backoff_delay) | 119 const base::TimeDelta& max_backoff_delay) |
| 119 : attachment_store_(attachment_store), | 120 : attachment_store_(attachment_store.Pass()), |
| 120 attachment_uploader_(attachment_uploader.Pass()), | 121 attachment_uploader_(attachment_uploader.Pass()), |
| 121 attachment_downloader_(attachment_downloader.Pass()), | 122 attachment_downloader_(attachment_downloader.Pass()), |
| 122 delegate_(delegate), | 123 delegate_(delegate), |
| 123 weak_ptr_factory_(this) { | 124 weak_ptr_factory_(this) { |
| 124 DCHECK(CalledOnValidThread()); | 125 DCHECK(CalledOnValidThread()); |
| 125 DCHECK(attachment_store_.get()); | 126 DCHECK(attachment_store_.get()); |
| 126 | 127 |
| 127 // TODO(maniscalco): Observe network connectivity change events. When the | 128 // TODO(maniscalco): Observe network connectivity change events. When the |
| 128 // network becomes disconnected, consider suspending queue dispatch. When | 129 // network becomes disconnected, consider suspending queue dispatch. When |
| 129 // connectivity is restored, consider clearing any dispatch backoff (bug | 130 // connectivity is restored, consider clearing any dispatch backoff (bug |
| 130 // 411981). | 131 // 411981). |
| 131 upload_task_queue_.reset(new TaskQueue<AttachmentId>( | 132 upload_task_queue_.reset(new TaskQueue<AttachmentId>( |
| 132 base::Bind(&AttachmentServiceImpl::BeginUpload, | 133 base::Bind(&AttachmentServiceImpl::BeginUpload, |
| 133 weak_ptr_factory_.GetWeakPtr()), | 134 weak_ptr_factory_.GetWeakPtr()), |
| 134 initial_backoff_delay, | 135 initial_backoff_delay, |
| 135 max_backoff_delay)); | 136 max_backoff_delay)); |
| 136 | 137 |
| 137 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 138 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 138 } | 139 } |
| 139 | 140 |
| 140 AttachmentServiceImpl::~AttachmentServiceImpl() { | 141 AttachmentServiceImpl::~AttachmentServiceImpl() { |
| 141 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
| 142 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 143 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 143 } | 144 } |
| 144 | 145 |
| 145 // Static. | 146 // Static. |
| 146 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { | 147 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { |
| 147 scoped_refptr<syncer::AttachmentStore> attachment_store = | 148 scoped_ptr<syncer::AttachmentStore> attachment_store = |
| 148 AttachmentStore::CreateInMemoryStore(); | 149 AttachmentStore::CreateInMemoryStore(); |
| 149 scoped_ptr<AttachmentUploader> attachment_uploader( | 150 scoped_ptr<AttachmentUploader> attachment_uploader( |
| 150 new FakeAttachmentUploader); | 151 new FakeAttachmentUploader); |
| 151 scoped_ptr<AttachmentDownloader> attachment_downloader( | 152 scoped_ptr<AttachmentDownloader> attachment_downloader( |
| 152 new FakeAttachmentDownloader()); | 153 new FakeAttachmentDownloader()); |
| 153 scoped_ptr<syncer::AttachmentService> attachment_service( | 154 scoped_ptr<syncer::AttachmentService> attachment_service( |
| 154 new syncer::AttachmentServiceImpl(attachment_store, | 155 new syncer::AttachmentServiceImpl(attachment_store.Pass(), |
| 155 attachment_uploader.Pass(), | 156 attachment_uploader.Pass(), |
| 156 attachment_downloader.Pass(), | 157 attachment_downloader.Pass(), |
| 157 NULL, | 158 NULL, |
| 158 base::TimeDelta(), | 159 base::TimeDelta(), |
| 159 base::TimeDelta())); | 160 base::TimeDelta())); |
| 160 return attachment_service.Pass(); | 161 return attachment_service.Pass(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 AttachmentStore* AttachmentServiceImpl::GetStore() { | |
| 164 return attachment_store_.get(); | |
| 165 } | |
| 166 | |
| 167 void AttachmentServiceImpl::GetOrDownloadAttachments( | 164 void AttachmentServiceImpl::GetOrDownloadAttachments( |
| 168 const AttachmentIdList& attachment_ids, | 165 const AttachmentIdList& attachment_ids, |
| 169 const GetOrDownloadCallback& callback) { | 166 const GetOrDownloadCallback& callback) { |
| 170 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
| 171 scoped_refptr<GetOrDownloadState> state( | 168 scoped_refptr<GetOrDownloadState> state( |
| 172 new GetOrDownloadState(attachment_ids, callback)); | 169 new GetOrDownloadState(attachment_ids, callback)); |
| 173 attachment_store_->Read(attachment_ids, | 170 attachment_store_->Read(attachment_ids, |
| 174 base::Bind(&AttachmentServiceImpl::ReadDone, | 171 base::Bind(&AttachmentServiceImpl::ReadDone, |
| 175 weak_ptr_factory_.GetWeakPtr(), | 172 weak_ptr_factory_.GetWeakPtr(), state)); |
| 176 state)); | |
| 177 } | 173 } |
| 178 | 174 |
| 179 void AttachmentServiceImpl::ReadDone( | 175 void AttachmentServiceImpl::ReadDone( |
| 180 const scoped_refptr<GetOrDownloadState>& state, | 176 const scoped_refptr<GetOrDownloadState>& state, |
| 181 const AttachmentStore::Result& result, | 177 const AttachmentStore::Result& result, |
| 182 scoped_ptr<AttachmentMap> attachments, | 178 scoped_ptr<AttachmentMap> attachments, |
| 183 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 179 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { |
| 184 // Add read attachments to result. | 180 // Add read attachments to result. |
| 185 for (AttachmentMap::const_iterator iter = attachments->begin(); | 181 for (AttachmentMap::const_iterator iter = attachments->begin(); |
| 186 iter != attachments->end(); | 182 iter != attachments->end(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 base::Bind(&AttachmentServiceImpl::UploadDone, | 316 base::Bind(&AttachmentServiceImpl::UploadDone, |
| 321 weak_ptr_factory_.GetWeakPtr())); | 317 weak_ptr_factory_.GetWeakPtr())); |
| 322 } | 318 } |
| 323 } | 319 } |
| 324 | 320 |
| 325 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 321 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { |
| 326 upload_task_queue_->SetTimerForTest(timer.Pass()); | 322 upload_task_queue_->SetTimerForTest(timer.Pass()); |
| 327 } | 323 } |
| 328 | 324 |
| 329 } // namespace syncer | 325 } // namespace syncer |
| OLD | NEW |