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_downloader_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 DCHECK(request == access_token_request_.get()); | 125 DCHECK(request == access_token_request_.get()); |
126 access_token_request_.reset(); | 126 access_token_request_.reset(); |
127 StateList::const_iterator iter; | 127 StateList::const_iterator iter; |
128 // Without access token all downloads fail. | 128 // Without access token all downloads fail. |
129 for (iter = requests_waiting_for_access_token_.begin(); | 129 for (iter = requests_waiting_for_access_token_.begin(); |
130 iter != requests_waiting_for_access_token_.end(); | 130 iter != requests_waiting_for_access_token_.end(); |
131 ++iter) { | 131 ++iter) { |
132 DownloadState* download_state = *iter; | 132 DownloadState* download_state = *iter; |
133 scoped_refptr<base::RefCountedString> null_attachment_data; | 133 scoped_refptr<base::RefCountedString> null_attachment_data; |
134 ReportResult(*download_state, DOWNLOAD_TRANSIENT_ERROR, | 134 ReportResult(*download_state, DOWNLOAD_TRANSIENT_ERROR, |
135 null_attachment_data, 0); | 135 null_attachment_data); |
136 DCHECK(state_map_.find(download_state->attachment_url) != state_map_.end()); | 136 DCHECK(state_map_.find(download_state->attachment_url) != state_map_.end()); |
137 state_map_.erase(download_state->attachment_url); | 137 state_map_.erase(download_state->attachment_url); |
138 } | 138 } |
139 requests_waiting_for_access_token_.clear(); | 139 requests_waiting_for_access_token_.clear(); |
140 } | 140 } |
141 | 141 |
142 void AttachmentDownloaderImpl::OnURLFetchComplete( | 142 void AttachmentDownloaderImpl::OnURLFetchComplete( |
143 const net::URLFetcher* source) { | 143 const net::URLFetcher* source) { |
144 DCHECK(CalledOnValidThread()); | 144 DCHECK(CalledOnValidThread()); |
145 | 145 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 oauth2_scopes_, | 186 oauth2_scopes_, |
187 download_state.access_token); | 187 download_state.access_token); |
188 // Fail the request, but indicate that it may be successful if retried. | 188 // Fail the request, but indicate that it may be successful if retried. |
189 result = DOWNLOAD_TRANSIENT_ERROR; | 189 result = DOWNLOAD_TRANSIENT_ERROR; |
190 } else if (response_code == net::HTTP_FORBIDDEN) { | 190 } else if (response_code == net::HTTP_FORBIDDEN) { |
191 // User is not allowed to use attachments. Retrying won't help. | 191 // User is not allowed to use attachments. Retrying won't help. |
192 result = DOWNLOAD_UNSPECIFIED_ERROR; | 192 result = DOWNLOAD_UNSPECIFIED_ERROR; |
193 } else if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) { | 193 } else if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) { |
194 result = DOWNLOAD_TRANSIENT_ERROR; | 194 result = DOWNLOAD_TRANSIENT_ERROR; |
195 } | 195 } |
196 ReportResult(download_state, result, attachment_data, attachment_crc32c); | 196 ReportResult(download_state, result, attachment_data); |
pavely
2015/03/07 00:07:25
I feel there should be a check here to make sure t
maniscalco
2015/03/09 17:09:00
Done and added test case to verify.
| |
197 state_map_.erase(iter); | 197 state_map_.erase(iter); |
198 } | 198 } |
199 | 199 |
200 scoped_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( | 200 scoped_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( |
201 const AttachmentUrl& url, | 201 const AttachmentUrl& url, |
202 const std::string& access_token) { | 202 const std::string& access_token) { |
203 scoped_ptr<net::URLFetcher> url_fetcher( | 203 scoped_ptr<net::URLFetcher> url_fetcher( |
204 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this)); | 204 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this)); |
205 AttachmentUploaderImpl::ConfigureURLFetcherCommon( | 205 AttachmentUploaderImpl::ConfigureURLFetcherCommon( |
206 url_fetcher.get(), access_token, raw_store_birthday_, model_type_, | 206 url_fetcher.get(), access_token, raw_store_birthday_, model_type_, |
207 url_request_context_getter_.get()); | 207 url_request_context_getter_.get()); |
208 return url_fetcher.Pass(); | 208 return url_fetcher.Pass(); |
209 } | 209 } |
210 | 210 |
211 void AttachmentDownloaderImpl::RequestAccessToken( | 211 void AttachmentDownloaderImpl::RequestAccessToken( |
212 DownloadState* download_state) { | 212 DownloadState* download_state) { |
213 requests_waiting_for_access_token_.push_back(download_state); | 213 requests_waiting_for_access_token_.push_back(download_state); |
214 // Start access token request if there is no active one. | 214 // Start access token request if there is no active one. |
215 if (access_token_request_ == NULL) { | 215 if (access_token_request_ == NULL) { |
216 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart( | 216 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart( |
217 token_service_provider_.get(), account_id_, oauth2_scopes_, this); | 217 token_service_provider_.get(), account_id_, oauth2_scopes_, this); |
218 } | 218 } |
219 } | 219 } |
220 | 220 |
221 void AttachmentDownloaderImpl::ReportResult( | 221 void AttachmentDownloaderImpl::ReportResult( |
222 const DownloadState& download_state, | 222 const DownloadState& download_state, |
223 const DownloadResult& result, | 223 const DownloadResult& result, |
224 const scoped_refptr<base::RefCountedString>& attachment_data, | 224 const scoped_refptr<base::RefCountedString>& attachment_data) { |
225 uint32_t attachment_crc32c) { | |
226 std::vector<DownloadCallback>::const_iterator iter; | 225 std::vector<DownloadCallback>::const_iterator iter; |
227 for (iter = download_state.user_callbacks.begin(); | 226 for (iter = download_state.user_callbacks.begin(); |
228 iter != download_state.user_callbacks.end(); | 227 iter != download_state.user_callbacks.end(); |
229 ++iter) { | 228 ++iter) { |
230 scoped_ptr<Attachment> attachment; | 229 scoped_ptr<Attachment> attachment; |
231 if (result == DOWNLOAD_SUCCESS) { | 230 if (result == DOWNLOAD_SUCCESS) { |
232 attachment.reset(new Attachment(Attachment::CreateFromParts( | 231 attachment.reset(new Attachment(Attachment::CreateFromParts( |
233 download_state.attachment_id, attachment_data, attachment_crc32c))); | 232 download_state.attachment_id, attachment_data))); |
234 } | 233 } |
235 | 234 |
236 base::MessageLoop::current()->PostTask( | 235 base::MessageLoop::current()->PostTask( |
237 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); | 236 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); |
238 } | 237 } |
239 } | 238 } |
240 | 239 |
241 bool AttachmentDownloaderImpl::ExtractCrc32c( | 240 bool AttachmentDownloaderImpl::ExtractCrc32c( |
242 const net::HttpResponseHeaders* headers, | 241 const net::HttpResponseHeaders* headers, |
243 uint32_t* crc32c) { | 242 uint32_t* crc32c) { |
(...skipping 28 matching lines...) Expand all Loading... | |
272 | 271 |
273 if (crc32c_raw.size() != sizeof(*crc32c)) | 272 if (crc32c_raw.size() != sizeof(*crc32c)) |
274 return false; | 273 return false; |
275 | 274 |
276 *crc32c = | 275 *crc32c = |
277 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); | 276 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); |
278 return true; | 277 return true; |
279 } | 278 } |
280 | 279 |
281 } // namespace syncer | 280 } // namespace syncer |
OLD | NEW |