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 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // If all attachment requests completed then post callback to consumer with | 47 // If all attachment requests completed then post callback to consumer with |
48 // results. | 48 // results. |
49 void PostResultIfAllRequestsCompleted(); | 49 void PostResultIfAllRequestsCompleted(); |
50 | 50 |
51 GetOrDownloadCallback callback_; | 51 GetOrDownloadCallback callback_; |
52 | 52 |
53 // Requests for these attachments are still in progress. | 53 // Requests for these attachments are still in progress. |
54 AttachmentIdSet in_progress_attachments_; | 54 AttachmentIdSet in_progress_attachments_; |
55 | 55 |
56 AttachmentIdSet unavailable_attachments_; | 56 AttachmentIdSet unavailable_attachments_; |
57 scoped_ptr<AttachmentMap> retrieved_attachments_; | 57 std::unique_ptr<AttachmentMap> retrieved_attachments_; |
58 | 58 |
59 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState); | 59 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState); |
60 }; | 60 }; |
61 | 61 |
62 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState( | 62 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState( |
63 const AttachmentIdList& attachment_ids, | 63 const AttachmentIdList& attachment_ids, |
64 const GetOrDownloadCallback& callback) | 64 const GetOrDownloadCallback& callback) |
65 : callback_(callback), retrieved_attachments_(new AttachmentMap()) { | 65 : callback_(callback), retrieved_attachments_(new AttachmentMap()) { |
66 std::copy( | 66 std::copy( |
67 attachment_ids.begin(), | 67 attachment_ids.begin(), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // All requests completed. Let's notify consumer. | 105 // All requests completed. Let's notify consumer. |
106 GetOrDownloadResult result = | 106 GetOrDownloadResult result = |
107 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; | 107 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; |
108 base::MessageLoop::current()->PostTask( | 108 base::MessageLoop::current()->PostTask( |
109 FROM_HERE, | 109 FROM_HERE, |
110 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); | 110 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 AttachmentServiceImpl::AttachmentServiceImpl( | 114 AttachmentServiceImpl::AttachmentServiceImpl( |
115 scoped_ptr<AttachmentStoreForSync> attachment_store, | 115 std::unique_ptr<AttachmentStoreForSync> attachment_store, |
116 scoped_ptr<AttachmentUploader> attachment_uploader, | 116 std::unique_ptr<AttachmentUploader> attachment_uploader, |
117 scoped_ptr<AttachmentDownloader> attachment_downloader, | 117 std::unique_ptr<AttachmentDownloader> attachment_downloader, |
118 Delegate* delegate, | 118 Delegate* delegate, |
119 const base::TimeDelta& initial_backoff_delay, | 119 const base::TimeDelta& initial_backoff_delay, |
120 const base::TimeDelta& max_backoff_delay) | 120 const base::TimeDelta& max_backoff_delay) |
121 : attachment_store_(std::move(attachment_store)), | 121 : attachment_store_(std::move(attachment_store)), |
122 attachment_uploader_(std::move(attachment_uploader)), | 122 attachment_uploader_(std::move(attachment_uploader)), |
123 attachment_downloader_(std::move(attachment_downloader)), | 123 attachment_downloader_(std::move(attachment_downloader)), |
124 delegate_(delegate), | 124 delegate_(delegate), |
125 weak_ptr_factory_(this) { | 125 weak_ptr_factory_(this) { |
126 DCHECK(CalledOnValidThread()); | 126 DCHECK(CalledOnValidThread()); |
127 DCHECK(attachment_store_.get()); | 127 DCHECK(attachment_store_.get()); |
(...skipping 10 matching lines...) Expand all Loading... |
138 | 138 |
139 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 139 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
140 } | 140 } |
141 | 141 |
142 AttachmentServiceImpl::~AttachmentServiceImpl() { | 142 AttachmentServiceImpl::~AttachmentServiceImpl() { |
143 DCHECK(CalledOnValidThread()); | 143 DCHECK(CalledOnValidThread()); |
144 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 144 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
145 } | 145 } |
146 | 146 |
147 // Static. | 147 // Static. |
148 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { | 148 std::unique_ptr<syncer::AttachmentService> |
149 scoped_ptr<syncer::AttachmentStore> attachment_store = | 149 AttachmentServiceImpl::CreateForTest() { |
| 150 std::unique_ptr<syncer::AttachmentStore> attachment_store = |
150 AttachmentStore::CreateInMemoryStore(); | 151 AttachmentStore::CreateInMemoryStore(); |
151 scoped_ptr<AttachmentUploader> attachment_uploader( | 152 std::unique_ptr<AttachmentUploader> attachment_uploader( |
152 new FakeAttachmentUploader); | 153 new FakeAttachmentUploader); |
153 scoped_ptr<AttachmentDownloader> attachment_downloader( | 154 std::unique_ptr<AttachmentDownloader> attachment_downloader( |
154 new FakeAttachmentDownloader()); | 155 new FakeAttachmentDownloader()); |
155 scoped_ptr<syncer::AttachmentService> attachment_service( | 156 std::unique_ptr<syncer::AttachmentService> attachment_service( |
156 new syncer::AttachmentServiceImpl( | 157 new syncer::AttachmentServiceImpl( |
157 attachment_store->CreateAttachmentStoreForSync(), | 158 attachment_store->CreateAttachmentStoreForSync(), |
158 std::move(attachment_uploader), std::move(attachment_downloader), | 159 std::move(attachment_uploader), std::move(attachment_downloader), |
159 NULL, base::TimeDelta(), base::TimeDelta())); | 160 NULL, base::TimeDelta(), base::TimeDelta())); |
160 return attachment_service; | 161 return attachment_service; |
161 } | 162 } |
162 | 163 |
163 void AttachmentServiceImpl::GetOrDownloadAttachments( | 164 void AttachmentServiceImpl::GetOrDownloadAttachments( |
164 const AttachmentIdList& attachment_ids, | 165 const AttachmentIdList& attachment_ids, |
165 const GetOrDownloadCallback& callback) { | 166 const GetOrDownloadCallback& callback) { |
166 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
167 scoped_refptr<GetOrDownloadState> state( | 168 scoped_refptr<GetOrDownloadState> state( |
168 new GetOrDownloadState(attachment_ids, callback)); | 169 new GetOrDownloadState(attachment_ids, callback)); |
169 // SetModelTypeReference() makes attachments visible for model type. | 170 // SetModelTypeReference() makes attachments visible for model type. |
170 // Needed when attachment doesn't have model type reference, but still | 171 // Needed when attachment doesn't have model type reference, but still |
171 // available in local store. | 172 // available in local store. |
172 attachment_store_->SetModelTypeReference(attachment_ids); | 173 attachment_store_->SetModelTypeReference(attachment_ids); |
173 attachment_store_->Read(attachment_ids, | 174 attachment_store_->Read(attachment_ids, |
174 base::Bind(&AttachmentServiceImpl::ReadDone, | 175 base::Bind(&AttachmentServiceImpl::ReadDone, |
175 weak_ptr_factory_.GetWeakPtr(), state)); | 176 weak_ptr_factory_.GetWeakPtr(), state)); |
176 } | 177 } |
177 | 178 |
178 void AttachmentServiceImpl::ReadDone( | 179 void AttachmentServiceImpl::ReadDone( |
179 const scoped_refptr<GetOrDownloadState>& state, | 180 const scoped_refptr<GetOrDownloadState>& state, |
180 const AttachmentStore::Result& result, | 181 const AttachmentStore::Result& result, |
181 scoped_ptr<AttachmentMap> attachments, | 182 std::unique_ptr<AttachmentMap> attachments, |
182 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 183 std::unique_ptr<AttachmentIdList> unavailable_attachment_ids) { |
183 // Add read attachments to result. | 184 // Add read attachments to result. |
184 for (AttachmentMap::const_iterator iter = attachments->begin(); | 185 for (AttachmentMap::const_iterator iter = attachments->begin(); |
185 iter != attachments->end(); | 186 iter != attachments->end(); |
186 ++iter) { | 187 ++iter) { |
187 state->AddAttachment(iter->second); | 188 state->AddAttachment(iter->second); |
188 } | 189 } |
189 | 190 |
190 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); | 191 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); |
191 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); | 192 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); |
192 if (result != AttachmentStore::STORE_INITIALIZATION_FAILED && | 193 if (result != AttachmentStore::STORE_INITIALIZATION_FAILED && |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 attachment_store_->DropSyncReference(ids); | 247 attachment_store_->DropSyncReference(ids); |
247 upload_task_queue_->MarkAsFailed(attachment_id); | 248 upload_task_queue_->MarkAsFailed(attachment_id); |
248 break; | 249 break; |
249 } | 250 } |
250 } | 251 } |
251 | 252 |
252 void AttachmentServiceImpl::DownloadDone( | 253 void AttachmentServiceImpl::DownloadDone( |
253 const scoped_refptr<GetOrDownloadState>& state, | 254 const scoped_refptr<GetOrDownloadState>& state, |
254 const AttachmentId& attachment_id, | 255 const AttachmentId& attachment_id, |
255 const AttachmentDownloader::DownloadResult& result, | 256 const AttachmentDownloader::DownloadResult& result, |
256 scoped_ptr<Attachment> attachment) { | 257 std::unique_ptr<Attachment> attachment) { |
257 switch (result) { | 258 switch (result) { |
258 case AttachmentDownloader::DOWNLOAD_SUCCESS: { | 259 case AttachmentDownloader::DOWNLOAD_SUCCESS: { |
259 AttachmentList attachment_list; | 260 AttachmentList attachment_list; |
260 attachment_list.push_back(*attachment.get()); | 261 attachment_list.push_back(*attachment.get()); |
261 attachment_store_->Write( | 262 attachment_store_->Write( |
262 attachment_list, | 263 attachment_list, |
263 base::Bind(&AttachmentServiceImpl::WriteDone, | 264 base::Bind(&AttachmentServiceImpl::WriteDone, |
264 weak_ptr_factory_.GetWeakPtr(), state, *attachment.get())); | 265 weak_ptr_factory_.GetWeakPtr(), state, *attachment.get())); |
265 break; | 266 break; |
266 } | 267 } |
(...skipping 29 matching lines...) Expand all Loading... |
296 | 297 |
297 void AttachmentServiceImpl::OnNetworkChanged( | 298 void AttachmentServiceImpl::OnNetworkChanged( |
298 net::NetworkChangeNotifier::ConnectionType type) { | 299 net::NetworkChangeNotifier::ConnectionType type) { |
299 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { | 300 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
300 upload_task_queue_->ResetBackoff(); | 301 upload_task_queue_->ResetBackoff(); |
301 } | 302 } |
302 } | 303 } |
303 | 304 |
304 void AttachmentServiceImpl::ReadDoneNowUpload( | 305 void AttachmentServiceImpl::ReadDoneNowUpload( |
305 const AttachmentStore::Result& result, | 306 const AttachmentStore::Result& result, |
306 scoped_ptr<AttachmentMap> attachments, | 307 std::unique_ptr<AttachmentMap> attachments, |
307 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 308 std::unique_ptr<AttachmentIdList> unavailable_attachment_ids) { |
308 DCHECK(CalledOnValidThread()); | 309 DCHECK(CalledOnValidThread()); |
309 if (!unavailable_attachment_ids->empty()) { | 310 if (!unavailable_attachment_ids->empty()) { |
310 // TODO(maniscalco): We failed to read some attachments. What should we do | 311 // TODO(maniscalco): We failed to read some attachments. What should we do |
311 // now? | 312 // now? |
312 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); | 313 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); |
313 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); | 314 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); |
314 for (; iter != end; ++iter) { | 315 for (; iter != end; ++iter) { |
315 upload_task_queue_->Cancel(*iter); | 316 upload_task_queue_->Cancel(*iter); |
316 } | 317 } |
317 attachment_store_->DropSyncReference(*unavailable_attachment_ids); | 318 attachment_store_->DropSyncReference(*unavailable_attachment_ids); |
318 } | 319 } |
319 | 320 |
320 AttachmentMap::const_iterator iter = attachments->begin(); | 321 AttachmentMap::const_iterator iter = attachments->begin(); |
321 AttachmentMap::const_iterator end = attachments->end(); | 322 AttachmentMap::const_iterator end = attachments->end(); |
322 for (; iter != end; ++iter) { | 323 for (; iter != end; ++iter) { |
323 attachment_uploader_->UploadAttachment( | 324 attachment_uploader_->UploadAttachment( |
324 iter->second, | 325 iter->second, |
325 base::Bind(&AttachmentServiceImpl::UploadDone, | 326 base::Bind(&AttachmentServiceImpl::UploadDone, |
326 weak_ptr_factory_.GetWeakPtr())); | 327 weak_ptr_factory_.GetWeakPtr())); |
327 } | 328 } |
328 } | 329 } |
329 | 330 |
330 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 331 void AttachmentServiceImpl::SetTimerForTest( |
| 332 std::unique_ptr<base::Timer> timer) { |
331 upload_task_queue_->SetTimerForTest(std::move(timer)); | 333 upload_task_queue_->SetTimerForTest(std::move(timer)); |
332 } | 334 } |
333 | 335 |
334 } // namespace syncer | 336 } // namespace syncer |
OLD | NEW |