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/api/attachments/attachment_service_impl.h" | 5 #include "sync/api/attachments/attachment_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "sync/api/attachments/attachment.h" | 9 #include "sync/api/attachments/attachment.h" |
10 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | |
10 #include "sync/internal_api/public/attachments/fake_attachment_store.h" | 11 #include "sync/internal_api/public/attachments/fake_attachment_store.h" |
11 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
12 | 13 |
13 namespace syncer { | 14 namespace syncer { |
14 | 15 |
16 // GetOrDownloadAttachments starts multiple parallel DownloadAttachment calls. | |
17 // GetOrDownloadState tracks completion of these calls and posts callback for | |
18 // consumer once all attachments are either retrieved or reported unavailable. | |
19 class AttachmentServiceImpl::GetOrDownloadState | |
20 : public base::RefCounted<GetOrDownloadState>, | |
21 public base::NonThreadSafe { | |
22 public: | |
23 GetOrDownloadState(const AttachmentIdList& attachment_ids, | |
maniscalco
2014/05/29 22:35:54
Can you document the parameters?
pavely
2014/05/30 00:50:07
Done.
| |
24 const GetOrDownloadCallback& callback); | |
25 virtual ~GetOrDownloadState(); | |
maniscalco
2014/05/29 22:35:54
Probably should make the dtor private. Hmm, I tho
pavely
2014/05/30 00:50:07
Done.
| |
26 | |
27 // Attachment was just retrieved. Add it to retrieved attachments. | |
28 void AddAttachment(Attachment attachment); | |
maniscalco
2014/05/29 22:35:54
attachment can be made const ref: const Attachment
pavely
2014/05/30 00:50:07
Done.
| |
29 | |
30 // Both reading from local store and downloading attachment failed. | |
31 // Add it to unavailable set. | |
32 void AddUnavailableAttachmentId(const AttachmentId& attachment_id); | |
33 | |
34 private: | |
35 // Request regarding attachment id completed. There is going to be no more | |
36 // calls about this attachment. If all attachment requests completed then post | |
37 // callback to consumer with results. | |
38 void AttachmentRequestCompleted(const AttachmentId& attachment_id); | |
39 | |
40 GetOrDownloadCallback callback_; | |
41 | |
42 // Requests for these attachments are still in porgress. | |
maniscalco
2014/05/29 22:35:54
s/porgress/progress/
pavely
2014/05/30 00:50:07
Done.
| |
43 AttachmentIdSet incomplete_attachments_; | |
maniscalco
2014/05/29 22:35:54
Consider renaming to in_progress_attachments_ for
pavely
2014/05/30 00:50:07
in_progress_attachments_ is better.
I first rename
| |
44 | |
45 AttachmentIdSet unavailable_attachments_; | |
46 scoped_ptr<AttachmentMap> retrieved_attachments_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(GetOrDownloadState); | |
49 }; | |
50 | |
51 AttachmentServiceImpl::GetOrDownloadState::GetOrDownloadState( | |
52 const AttachmentIdList& attachment_ids, | |
53 const GetOrDownloadCallback& callback) | |
54 : callback_(callback), retrieved_attachments_(new AttachmentMap()) { | |
55 std::copy( | |
56 attachment_ids.begin(), | |
57 attachment_ids.end(), | |
58 std::inserter(incomplete_attachments_, incomplete_attachments_.end())); | |
maniscalco
2014/05/29 22:35:54
Should this be std::back_inserter instead? I can'
pavely
2014/05/30 00:50:07
back_inserter needs push_back(). inserter needs in
maniscalco
2014/05/30 16:08:14
SGTM, std::inserter seems like the way to go.
On
| |
59 } | |
60 | |
61 AttachmentServiceImpl::GetOrDownloadState::~GetOrDownloadState() { | |
62 DCHECK(CalledOnValidThread()); | |
63 } | |
64 | |
65 void AttachmentServiceImpl::GetOrDownloadState::AddAttachment( | |
66 Attachment attachment) { | |
67 DCHECK(CalledOnValidThread()); | |
68 DCHECK(retrieved_attachments_->find(attachment.GetId()) == | |
maniscalco
2014/05/29 22:35:54
FYI, there is a DCHECK_EQ macro. It may or may no
pavely
2014/05/30 00:50:07
The reason for DCHECK_EQ macro is that it will als
| |
69 retrieved_attachments_->end()); | |
70 retrieved_attachments_->insert( | |
71 std::make_pair(attachment.GetId(), attachment)); | |
72 AttachmentRequestCompleted(attachment.GetId()); | |
73 } | |
74 | |
75 void AttachmentServiceImpl::GetOrDownloadState::AddUnavailableAttachmentId( | |
76 const AttachmentId& attachment_id) { | |
77 DCHECK(CalledOnValidThread()); | |
78 DCHECK(unavailable_attachments_.find(attachment_id) == | |
79 unavailable_attachments_.end()); | |
80 unavailable_attachments_.insert(attachment_id); | |
81 AttachmentRequestCompleted(attachment_id); | |
82 } | |
83 | |
84 void AttachmentServiceImpl::GetOrDownloadState::AttachmentRequestCompleted( | |
85 const AttachmentId& attachment_id) { | |
86 DCHECK(incomplete_attachments_.find(attachment_id) != | |
87 incomplete_attachments_.end()); | |
88 incomplete_attachments_.erase(attachment_id); | |
89 | |
90 if (incomplete_attachments_.empty()) { | |
91 // All requests completed. Let's notify consumer. | |
92 GetOrDownloadResult result = | |
93 unavailable_attachments_.empty() ? GET_SUCCESS : GET_UNSPECIFIED_ERROR; | |
94 base::MessageLoop::current()->PostTask( | |
95 FROM_HERE, | |
96 base::Bind(callback_, result, base::Passed(&retrieved_attachments_))); | |
97 } | |
98 } | |
99 | |
15 AttachmentServiceImpl::AttachmentServiceImpl( | 100 AttachmentServiceImpl::AttachmentServiceImpl( |
16 scoped_ptr<AttachmentStore> attachment_store, | 101 scoped_ptr<AttachmentStore> attachment_store, |
17 scoped_ptr<AttachmentUploader> attachment_uploader, | 102 scoped_ptr<AttachmentUploader> attachment_uploader, |
103 scoped_ptr<AttachmentDownloader> attachment_downloader, | |
18 Delegate* delegate) | 104 Delegate* delegate) |
19 : attachment_store_(attachment_store.Pass()), | 105 : attachment_store_(attachment_store.Pass()), |
20 attachment_uploader_(attachment_uploader.Pass()), | 106 attachment_uploader_(attachment_uploader.Pass()), |
107 attachment_downloader_(attachment_downloader.Pass()), | |
21 delegate_(delegate), | 108 delegate_(delegate), |
22 weak_ptr_factory_(this) { | 109 weak_ptr_factory_(this) { |
23 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
24 DCHECK(attachment_store_); | 111 DCHECK(attachment_store_); |
25 DCHECK(attachment_uploader_); | 112 DCHECK(attachment_uploader_); |
26 } | 113 } |
27 | 114 |
28 AttachmentServiceImpl::~AttachmentServiceImpl() { | 115 AttachmentServiceImpl::~AttachmentServiceImpl() { |
29 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
30 } | 117 } |
31 | 118 |
32 // Static. | 119 // Static. |
33 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { | 120 scoped_ptr<syncer::AttachmentService> AttachmentServiceImpl::CreateForTest() { |
34 scoped_ptr<syncer::AttachmentStore> attachment_store( | 121 scoped_ptr<syncer::AttachmentStore> attachment_store( |
35 new syncer::FakeAttachmentStore(base::MessageLoopProxy::current())); | 122 new syncer::FakeAttachmentStore(base::MessageLoopProxy::current())); |
36 scoped_ptr<AttachmentUploader> attachment_uploader( | 123 scoped_ptr<AttachmentUploader> attachment_uploader( |
37 new FakeAttachmentUploader); | 124 new FakeAttachmentUploader); |
125 scoped_ptr<AttachmentDownloader> attachment_downloader( | |
126 new FakeAttachmentDownloader()); | |
38 scoped_ptr<syncer::AttachmentService> attachment_service( | 127 scoped_ptr<syncer::AttachmentService> attachment_service( |
39 new syncer::AttachmentServiceImpl( | 128 new syncer::AttachmentServiceImpl(attachment_store.Pass(), |
40 attachment_store.Pass(), attachment_uploader.Pass(), NULL)); | 129 attachment_uploader.Pass(), |
130 attachment_downloader.Pass(), | |
131 NULL)); | |
41 return attachment_service.Pass(); | 132 return attachment_service.Pass(); |
42 } | 133 } |
43 | 134 |
44 void AttachmentServiceImpl::GetOrDownloadAttachments( | 135 void AttachmentServiceImpl::GetOrDownloadAttachments( |
45 const AttachmentIdList& attachment_ids, | 136 const AttachmentIdList& attachment_ids, |
46 const GetOrDownloadCallback& callback) { | 137 const GetOrDownloadCallback& callback) { |
47 DCHECK(CalledOnValidThread()); | 138 DCHECK(CalledOnValidThread()); |
139 scoped_refptr<GetOrDownloadState> state( | |
140 new GetOrDownloadState(attachment_ids, callback)); | |
48 attachment_store_->Read(attachment_ids, | 141 attachment_store_->Read(attachment_ids, |
49 base::Bind(&AttachmentServiceImpl::ReadDone, | 142 base::Bind(&AttachmentServiceImpl::ReadDone, |
50 weak_ptr_factory_.GetWeakPtr(), | 143 weak_ptr_factory_.GetWeakPtr(), |
51 callback)); | 144 state)); |
52 } | 145 } |
53 | 146 |
54 void AttachmentServiceImpl::DropAttachments( | 147 void AttachmentServiceImpl::DropAttachments( |
55 const AttachmentIdList& attachment_ids, | 148 const AttachmentIdList& attachment_ids, |
56 const DropCallback& callback) { | 149 const DropCallback& callback) { |
57 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
58 attachment_store_->Drop(attachment_ids, | 151 attachment_store_->Drop(attachment_ids, |
59 base::Bind(&AttachmentServiceImpl::DropDone, | 152 base::Bind(&AttachmentServiceImpl::DropDone, |
60 weak_ptr_factory_.GetWeakPtr(), | 153 weak_ptr_factory_.GetWeakPtr(), |
61 callback)); | 154 callback)); |
(...skipping 27 matching lines...) Expand all Loading... | |
89 const AttachmentIdList& old_attachment_ids, | 182 const AttachmentIdList& old_attachment_ids, |
90 const SyncData& updated_sync_data) { | 183 const SyncData& updated_sync_data) { |
91 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
92 // TODO(maniscalco): At this point we need to ensure we write all new | 185 // TODO(maniscalco): At this point we need to ensure we write all new |
93 // attachments referenced by updated_sync_data to local storage and schedule | 186 // attachments referenced by updated_sync_data to local storage and schedule |
94 // them up upload to the server. We also need to remove any no unreferenced | 187 // them up upload to the server. We also need to remove any no unreferenced |
95 // attachments from local storage (bug 356351). | 188 // attachments from local storage (bug 356351). |
96 } | 189 } |
97 | 190 |
98 void AttachmentServiceImpl::ReadDone( | 191 void AttachmentServiceImpl::ReadDone( |
99 const GetOrDownloadCallback& callback, | 192 const scoped_refptr<GetOrDownloadState>& state, |
100 const AttachmentStore::Result& result, | 193 const AttachmentStore::Result& result, |
101 scoped_ptr<AttachmentMap> attachments, | 194 scoped_ptr<AttachmentMap> attachments, |
102 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { | 195 scoped_ptr<AttachmentIdList> unavailable_attachment_ids) { |
103 AttachmentService::GetOrDownloadResult get_result = | 196 // Add read attachments to result. |
104 AttachmentService::GET_UNSPECIFIED_ERROR; | 197 for (AttachmentMap::const_iterator iter = attachments->begin(); |
105 if (result == AttachmentStore::SUCCESS) { | 198 iter != attachments->end(); |
106 get_result = AttachmentService::GET_SUCCESS; | 199 ++iter) { |
200 state->AddAttachment(iter->second); | |
107 } | 201 } |
108 // TODO(maniscalco): Deal with case where an error occurred (bug 361251). | 202 // Try to download locally unavailable attachments. |
109 base::MessageLoop::current()->PostTask( | 203 for (AttachmentIdList::const_iterator iter = |
110 FROM_HERE, base::Bind(callback, get_result, base::Passed(&attachments))); | 204 unavailable_attachment_ids->begin(); |
205 iter != unavailable_attachment_ids->end(); | |
206 ++iter) { | |
207 attachment_downloader_->DownloadAttachment( | |
208 *iter, | |
209 base::Bind(&AttachmentServiceImpl::DownloadDone, | |
210 weak_ptr_factory_.GetWeakPtr(), | |
211 state, | |
212 *iter)); | |
213 ; | |
214 } | |
111 } | 215 } |
112 | 216 |
113 void AttachmentServiceImpl::DropDone(const DropCallback& callback, | 217 void AttachmentServiceImpl::DropDone(const DropCallback& callback, |
114 const AttachmentStore::Result& result) { | 218 const AttachmentStore::Result& result) { |
115 AttachmentService::DropResult drop_result = | 219 AttachmentService::DropResult drop_result = |
116 AttachmentService::DROP_UNSPECIFIED_ERROR; | 220 AttachmentService::DROP_UNSPECIFIED_ERROR; |
117 if (result == AttachmentStore::SUCCESS) { | 221 if (result == AttachmentStore::SUCCESS) { |
118 drop_result = AttachmentService::DROP_SUCCESS; | 222 drop_result = AttachmentService::DROP_SUCCESS; |
119 } | 223 } |
120 // TODO(maniscalco): Deal with case where an error occurred (bug 361251). | 224 // TODO(maniscalco): Deal with case where an error occurred (bug 361251). |
(...skipping 17 matching lines...) Expand all Loading... | |
138 const AttachmentUploader::UploadResult& result, | 242 const AttachmentUploader::UploadResult& result, |
139 const AttachmentId& attachment_id) { | 243 const AttachmentId& attachment_id) { |
140 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures. | 244 // TODO(pavely): crbug/372622: Deal with UploadAttachment failures. |
141 if (result != AttachmentUploader::UPLOAD_SUCCESS) | 245 if (result != AttachmentUploader::UPLOAD_SUCCESS) |
142 return; | 246 return; |
143 if (delegate_) { | 247 if (delegate_) { |
144 delegate_->OnAttachmentUploaded(attachment_id); | 248 delegate_->OnAttachmentUploaded(attachment_id); |
145 } | 249 } |
146 } | 250 } |
147 | 251 |
252 void AttachmentServiceImpl::DownloadDone( | |
253 const scoped_refptr<GetOrDownloadState>& state, | |
254 const AttachmentId& attachment_id, | |
255 const AttachmentDownloader::DownloadResult& result, | |
256 scoped_ptr<Attachment> attachment) { | |
257 if (result == AttachmentDownloader::DOWNLOAD_SUCCESS) { | |
258 state->AddAttachment(*attachment.get()); | |
259 } else { | |
260 state->AddUnavailableAttachmentId(attachment_id); | |
261 } | |
262 } | |
263 | |
148 } // namespace syncer | 264 } // namespace syncer |
OLD | NEW |