| 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_refptr<AttachmentStore> attachment_store, | 113 scoped_ptr<AttachmentStore> 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), | 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) { |
| 124 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
| 125 DCHECK(attachment_store_.get()); | 125 DCHECK(attachment_store_.get()); |
| 126 | 126 |
| 127 // TODO(maniscalco): Observe network connectivity change events. When the | 127 // TODO(maniscalco): Observe network connectivity change events. When the |
| 128 // network becomes disconnected, consider suspending queue dispatch. When | 128 // network becomes disconnected, consider suspending queue dispatch. When |
| 129 // connectivity is restored, consider clearing any dispatch backoff (bug | 129 // connectivity is restored, consider clearing any dispatch backoff (bug |
| 130 // 411981). | 130 // 411981). |
| 131 upload_task_queue_.reset(new TaskQueue<AttachmentId>( | 131 upload_task_queue_.reset(new TaskQueue<AttachmentId>( |
| 132 base::Bind(&AttachmentServiceImpl::BeginUpload, | 132 base::Bind(&AttachmentServiceImpl::BeginUpload, |
| 133 weak_ptr_factory_.GetWeakPtr()), | 133 weak_ptr_factory_.GetWeakPtr()), |
| 134 initial_backoff_delay, | 134 initial_backoff_delay, |
| 135 max_backoff_delay)); | 135 max_backoff_delay)); |
| 136 | 136 |
| 137 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 137 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 138 } | 138 } |
| 139 | 139 |
| 140 AttachmentServiceImpl::~AttachmentServiceImpl() { | 140 AttachmentServiceImpl::~AttachmentServiceImpl() { |
| 141 DCHECK(CalledOnValidThread()); | 141 DCHECK(CalledOnValidThread()); |
| 142 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 142 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Static. | 145 // Static. |
| 146 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { | 146 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { |
| 147 scoped_refptr<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, | 154 new syncer::AttachmentServiceImpl(attachment_store.Pass(), |
| 155 attachment_uploader.Pass(), | 155 attachment_uploader.Pass(), |
| 156 attachment_downloader.Pass(), | 156 attachment_downloader.Pass(), |
| 157 NULL, | 157 NULL, |
| 158 base::TimeDelta(), | 158 base::TimeDelta(), |
| 159 base::TimeDelta())); | 159 base::TimeDelta())); |
| 160 return attachment_service.Pass(); | 160 return attachment_service.Pass(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 AttachmentStore* AttachmentServiceImpl::GetStore() { | |
| 164 return attachment_store_.get(); | |
| 165 } | |
| 166 | |
| 167 void AttachmentServiceImpl::GetOrDownloadAttachments( | 163 void AttachmentServiceImpl::GetOrDownloadAttachments( |
| 168 const AttachmentIdList& attachment_ids, | 164 const AttachmentIdList& attachment_ids, |
| 169 const GetOrDownloadCallback& callback) { | 165 const GetOrDownloadCallback& callback) { |
| 170 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 171 scoped_refptr<GetOrDownloadState> state( | 167 scoped_refptr<GetOrDownloadState> state( |
| 172 new GetOrDownloadState(attachment_ids, callback)); | 168 new GetOrDownloadState(attachment_ids, callback)); |
| 173 attachment_store_->Read(attachment_ids, | 169 attachment_store_->Read(attachment_ids, |
| 174 base::Bind(&AttachmentServiceImpl::ReadDone, | 170 base::Bind(&AttachmentServiceImpl::ReadDone, |
| 175 weak_ptr_factory_.GetWeakPtr(), | 171 weak_ptr_factory_.GetWeakPtr(), state)); |
| 176 state)); | |
| 177 } | 172 } |
| 178 | 173 |
| 179 void AttachmentServiceImpl::ReadDone( | 174 void AttachmentServiceImpl::ReadDone( |
| 180 const scoped_refptr<GetOrDownloadState>& state, | 175 const scoped_refptr<GetOrDownloadState>& state, |
| 181 const AttachmentStore::Result& result, | 176 const AttachmentStore::Result& result, |
| 182 scoped_ptr<AttachmentMap> attachments, | 177 scoped_ptr<AttachmentMap> attachments, |
| 183 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 178 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { |
| 184 // Add read attachments to result. | 179 // Add read attachments to result. |
| 185 for (AttachmentMap::const_iterator iter = attachments->begin(); | 180 for (AttachmentMap::const_iterator iter = attachments->begin(); |
| 186 iter != attachments->end(); | 181 iter != attachments->end(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 base::Bind(&AttachmentServiceImpl::UploadDone, | 315 base::Bind(&AttachmentServiceImpl::UploadDone, |
| 321 weak_ptr_factory_.GetWeakPtr())); | 316 weak_ptr_factory_.GetWeakPtr())); |
| 322 } | 317 } |
| 323 } | 318 } |
| 324 | 319 |
| 325 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 320 void AttachmentServiceImpl::SetTimerForTest(scoped_ptr<base::Timer> timer) { |
| 326 upload_task_queue_->SetTimerForTest(timer.Pass()); | 321 upload_task_queue_->SetTimerForTest(timer.Pass()); |
| 327 } | 322 } |
| 328 | 323 |
| 329 } // namespace syncer | 324 } // namespace syncer |
| OLD | NEW |