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 "components/sync/core/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" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "sync/api/attachments/attachment.h" | 16 #include "components/sync/api/attachments/attachment.h" |
17 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 17 #include "components/sync/core/attachments/fake_attachment_downloader.h" |
18 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 18 #include "components/sync/core/attachments/fake_attachment_uploader.h" |
19 | 19 |
20 namespace syncer { | 20 namespace syncer { |
21 | 21 |
22 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls. | 22 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls. |
23 // GetOrDownloadState tracks completion of these calls and posts callback for | 23 // GetOrDownloadState tracks completion of these calls and posts callback for |
24 // consumer once all attachments are either retrieved or reported unavailable. | 24 // consumer once all attachments are either retrieved or reported unavailable. |
25 class AttachmentServiceImpl::GetOrDownloadState | 25 class AttachmentServiceImpl::GetOrDownloadState |
26 : public base::RefCounted<GetOrDownloadState>, | 26 : public base::RefCounted<GetOrDownloadState>, |
27 public base::NonThreadSafe { | 27 public base::NonThreadSafe { |
28 public: | 28 public: |
(...skipping 29 matching lines...) Expand all Loading... |
58 std::unique_ptr<AttachmentMap> retrieved_attachments_; | 58 std::unique_ptr<AttachmentMap> retrieved_attachments_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState); | 60 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState); |
61 }; | 61 }; |
62 | 62 |
63 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState( | 63 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState( |
64 const AttachmentIdList& attachment_ids, | 64 const AttachmentIdList& attachment_ids, |
65 const GetOrDownloadCallback& callback) | 65 const GetOrDownloadCallback& callback) |
66 : callback_(callback), retrieved_attachments_(new AttachmentMap()) { | 66 : callback_(callback), retrieved_attachments_(new AttachmentMap()) { |
67 std::copy( | 67 std::copy( |
68 attachment_ids.begin(), | 68 attachment_ids.begin(), attachment_ids.end(), |
69 attachment_ids.end(), | |
70 std::inserter(in_progress_attachments_, in_progress_attachments_.end())); | 69 std::inserter(in_progress_attachments_, in_progress_attachments_.end())); |
71 PostResultIfAllRequestsCompleted(); | 70 PostResultIfAllRequestsCompleted(); |
72 } | 71 } |
73 | 72 |
74 AttachmentServiceImpl::GetOrDownloadState::~GetOrDownloadState() { | 73 AttachmentServiceImpl::GetOrDownloadState::~GetOrDownloadState() { |
75 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
76 } | 75 } |
77 | 76 |
78 void AttachmentServiceImpl::GetOrDownloadState::AddAttachment( | 77 void AttachmentServiceImpl::GetOrDownloadState::AddAttachment( |
79 const Attachment& attachment) { | 78 const Attachment& attachment) { |
(...skipping 13 matching lines...) Expand all Loading... |
93 DCHECK(CalledOnValidThread()); | 92 DCHECK(CalledOnValidThread()); |
94 DCHECK(unavailable_attachments_.find(attachment_id) == | 93 DCHECK(unavailable_attachments_.find(attachment_id) == |
95 unavailable_attachments_.end()); | 94 unavailable_attachments_.end()); |
96 unavailable_attachments_.insert(attachment_id); | 95 unavailable_attachments_.insert(attachment_id); |
97 DCHECK(in_progress_attachments_.find(attachment_id) != | 96 DCHECK(in_progress_attachments_.find(attachment_id) != |
98 in_progress_attachments_.end()); | 97 in_progress_attachments_.end()); |
99 in_progress_attachments_.erase(attachment_id); | 98 in_progress_attachments_.erase(attachment_id); |
100 PostResultIfAllRequestsCompleted(); | 99 PostResultIfAllRequestsCompleted(); |
101 } | 100 } |
102 | 101 |
103 void | 102 void AttachmentServiceImpl::GetOrDownloadState:: |
104 AttachmentServiceImpl::GetOrDownloadState::PostResultIfAllRequestsCompleted() { | 103 PostResultIfAllRequestsCompleted() { |
105 if (in_progress_attachments_.empty()) { | 104 if (in_progress_attachments_.empty()) { |
106 // All requests completed. Let's notify consumer. | 105 // All requests completed. Let's notify consumer. |
107 GetOrDownloadResult result = | 106 GetOrDownloadResult result = |
108 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; | 107 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; |
109 base::ThreadTaskRunnerHandle::Get()->PostTask( | 108 base::ThreadTaskRunnerHandle::Get()->PostTask( |
110 FROM_HERE, | 109 FROM_HERE, |
111 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); | 110 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); |
112 } | 111 } |
113 } | 112 } |
114 | 113 |
(...skipping 12 matching lines...) Expand all Loading... |
127 DCHECK(CalledOnValidThread()); | 126 DCHECK(CalledOnValidThread()); |
128 DCHECK(attachment_store_.get()); | 127 DCHECK(attachment_store_.get()); |
129 | 128 |
130 // TODO(maniscalco): Observe network connectivity change events. When the | 129 // TODO(maniscalco): Observe network connectivity change events. When the |
131 // network becomes disconnected, consider suspending queue dispatch. When | 130 // network becomes disconnected, consider suspending queue dispatch. When |
132 // connectivity is restored, consider clearing any dispatch backoff (bug | 131 // connectivity is restored, consider clearing any dispatch backoff (bug |
133 // 411981). | 132 // 411981). |
134 upload_task_queue_.reset(new TaskQueue<AttachmentId>( | 133 upload_task_queue_.reset(new TaskQueue<AttachmentId>( |
135 base::Bind(&AttachmentServiceImpl::BeginUpload, | 134 base::Bind(&AttachmentServiceImpl::BeginUpload, |
136 weak_ptr_factory_.GetWeakPtr()), | 135 weak_ptr_factory_.GetWeakPtr()), |
137 initial_backoff_delay, | 136 initial_backoff_delay, max_backoff_delay)); |
138 max_backoff_delay)); | |
139 | 137 |
140 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 138 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
141 } | 139 } |
142 | 140 |
143 AttachmentServiceImpl::~AttachmentServiceImpl() { | 141 AttachmentServiceImpl::~AttachmentServiceImpl() { |
144 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
145 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 143 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
146 } | 144 } |
147 | 145 |
148 // Static. | 146 // Static. |
(...skipping 28 matching lines...) Expand all Loading... |
177 weak_ptr_factory_.GetWeakPtr(), state)); | 175 weak_ptr_factory_.GetWeakPtr(), state)); |
178 } | 176 } |
179 | 177 |
180 void AttachmentServiceImpl::ReadDone( | 178 void AttachmentServiceImpl::ReadDone( |
181 const scoped_refptr<GetOrDownloadState>& state, | 179 const scoped_refptr<GetOrDownloadState>& state, |
182 const AttachmentStore::Result& result, | 180 const AttachmentStore::Result& result, |
183 std::unique_ptr<AttachmentMap> attachments, | 181 std::unique_ptr<AttachmentMap> attachments, |
184 std::unique_ptr<AttachmentIdList> unavailable_attachment_ids) { | 182 std::unique_ptr<AttachmentIdList> unavailable_attachment_ids) { |
185 // Add read attachments to result. | 183 // Add read attachments to result. |
186 for (AttachmentMap::const_iterator iter = attachments->begin(); | 184 for (AttachmentMap::const_iterator iter = attachments->begin(); |
187 iter != attachments->end(); | 185 iter != attachments->end(); ++iter) { |
188 ++iter) { | |
189 state->AddAttachment(iter->second); | 186 state->AddAttachment(iter->second); |
190 } | 187 } |
191 | 188 |
192 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); | 189 AttachmentIdList::const_iterator iter = unavailable_attachment_ids->begin(); |
193 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); | 190 AttachmentIdList::const_iterator end = unavailable_attachment_ids->end(); |
194 if (result != AttachmentStore::STORE_INITIALIZATION_FAILED && | 191 if (result != AttachmentStore::STORE_INITIALIZATION_FAILED && |
195 attachment_downloader_.get()) { | 192 attachment_downloader_.get()) { |
196 // Try to download locally unavailable attachments. | 193 // Try to download locally unavailable attachments. |
197 for (; iter != end; ++iter) { | 194 for (; iter != end; ++iter) { |
198 attachment_downloader_->DownloadAttachment( | 195 attachment_downloader_->DownloadAttachment( |
199 *iter, | 196 *iter, base::Bind(&AttachmentServiceImpl::DownloadDone, |
200 base::Bind(&AttachmentServiceImpl::DownloadDone, | 197 weak_ptr_factory_.GetWeakPtr(), state, *iter)); |
201 weak_ptr_factory_.GetWeakPtr(), | |
202 state, | |
203 *iter)); | |
204 } | 198 } |
205 } else { | 199 } else { |
206 // No downloader so all locally unavailable attachments are unavailable. | 200 // No downloader so all locally unavailable attachments are unavailable. |
207 for (; iter != end; ++iter) { | 201 for (; iter != end; ++iter) { |
208 state->AddUnavailableAttachmentId(*iter); | 202 state->AddUnavailableAttachmentId(*iter); |
209 } | 203 } |
210 } | 204 } |
211 } | 205 } |
212 | 206 |
213 void AttachmentServiceImpl::WriteDone( | 207 void AttachmentServiceImpl::WriteDone( |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 for (; iter != end; ++iter) { | 310 for (; iter != end; ++iter) { |
317 upload_task_queue_->Cancel(*iter); | 311 upload_task_queue_->Cancel(*iter); |
318 } | 312 } |
319 attachment_store_->DropSyncReference(*unavailable_attachment_ids); | 313 attachment_store_->DropSyncReference(*unavailable_attachment_ids); |
320 } | 314 } |
321 | 315 |
322 AttachmentMap::const_iterator iter = attachments->begin(); | 316 AttachmentMap::const_iterator iter = attachments->begin(); |
323 AttachmentMap::const_iterator end = attachments->end(); | 317 AttachmentMap::const_iterator end = attachments->end(); |
324 for (; iter != end; ++iter) { | 318 for (; iter != end; ++iter) { |
325 attachment_uploader_->UploadAttachment( | 319 attachment_uploader_->UploadAttachment( |
326 iter->second, | 320 iter->second, base::Bind(&AttachmentServiceImpl::UploadDone, |
327 base::Bind(&AttachmentServiceImpl::UploadDone, | 321 weak_ptr_factory_.GetWeakPtr())); |
328 weak_ptr_factory_.GetWeakPtr())); | |
329 } | 322 } |
330 } | 323 } |
331 | 324 |
332 void AttachmentServiceImpl::SetTimerForTest( | 325 void AttachmentServiceImpl::SetTimerForTest( |
333 std::unique_ptr<base::Timer> timer) { | 326 std::unique_ptr<base::Timer> timer) { |
334 upload_task_queue_->SetTimerForTest(std::move(timer)); | 327 upload_task_queue_->SetTimerForTest(std::move(timer)); |
335 } | 328 } |
336 | 329 |
337 } // namespace syncer | 330 } // namespace syncer |
OLD | NEW |