| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 struct AttachmentDownloaderImpl::DownloadState { | 31 struct AttachmentDownloaderImpl::DownloadState { |
| 32 public: | 32 public: |
| 33 DownloadState(const AttachmentId& attachment_id, | 33 DownloadState(const AttachmentId& attachment_id, |
| 34 const AttachmentUrl& attachment_url); | 34 const AttachmentUrl& attachment_url); |
| 35 | 35 |
| 36 AttachmentId attachment_id; | 36 AttachmentId attachment_id; |
| 37 AttachmentUrl attachment_url; | 37 AttachmentUrl attachment_url; |
| 38 // |access_token| needed to invalidate if downloading attachment fails with | 38 // |access_token| needed to invalidate if downloading attachment fails with |
| 39 // HTTP_UNAUTHORIZED. | 39 // HTTP_UNAUTHORIZED. |
| 40 std::string access_token; | 40 std::string access_token; |
| 41 scoped_ptr<net::URLFetcher> url_fetcher; | 41 std::unique_ptr<net::URLFetcher> url_fetcher; |
| 42 std::vector<DownloadCallback> user_callbacks; | 42 std::vector<DownloadCallback> user_callbacks; |
| 43 base::TimeTicks start_time; | 43 base::TimeTicks start_time; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 AttachmentDownloaderImpl::DownloadState::DownloadState( | 46 AttachmentDownloaderImpl::DownloadState::DownloadState( |
| 47 const AttachmentId& attachment_id, | 47 const AttachmentId& attachment_id, |
| 48 const AttachmentUrl& attachment_url) | 48 const AttachmentUrl& attachment_url) |
| 49 : attachment_id(attachment_id), attachment_url(attachment_url) { | 49 : attachment_id(attachment_id), attachment_url(attachment_url) { |
| 50 } | 50 } |
| 51 | 51 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 82 const DownloadCallback& callback) { | 82 const DownloadCallback& callback) { |
| 83 DCHECK(CalledOnValidThread()); | 83 DCHECK(CalledOnValidThread()); |
| 84 | 84 |
| 85 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( | 85 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( |
| 86 sync_service_url_, attachment_id).spec(); | 86 sync_service_url_, attachment_id).spec(); |
| 87 | 87 |
| 88 StateMap::iterator iter = state_map_.find(url); | 88 StateMap::iterator iter = state_map_.find(url); |
| 89 if (iter == state_map_.end()) { | 89 if (iter == state_map_.end()) { |
| 90 // There is no request started for this attachment id. Let's create | 90 // There is no request started for this attachment id. Let's create |
| 91 // DownloadState and request access token for it. | 91 // DownloadState and request access token for it. |
| 92 scoped_ptr<DownloadState> new_download_state( | 92 std::unique_ptr<DownloadState> new_download_state( |
| 93 new DownloadState(attachment_id, url)); | 93 new DownloadState(attachment_id, url)); |
| 94 iter = state_map_.add(url, std::move(new_download_state)).first; | 94 iter = state_map_.add(url, std::move(new_download_state)).first; |
| 95 RequestAccessToken(iter->second); | 95 RequestAccessToken(iter->second); |
| 96 } | 96 } |
| 97 DownloadState* download_state = iter->second; | 97 DownloadState* download_state = iter->second; |
| 98 DCHECK(download_state->attachment_id == attachment_id); | 98 DCHECK(download_state->attachment_id == attachment_id); |
| 99 download_state->user_callbacks.push_back(callback); | 99 download_state->user_callbacks.push_back(callback); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void AttachmentDownloaderImpl::OnGetTokenSuccess( | 102 void AttachmentDownloaderImpl::OnGetTokenSuccess( |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } else if (response_code == net::HTTP_FORBIDDEN) { | 199 } else if (response_code == net::HTTP_FORBIDDEN) { |
| 200 // User is not allowed to use attachments. Retrying won't help. | 200 // User is not allowed to use attachments. Retrying won't help. |
| 201 result = DOWNLOAD_UNSPECIFIED_ERROR; | 201 result = DOWNLOAD_UNSPECIFIED_ERROR; |
| 202 } else if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) { | 202 } else if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) { |
| 203 result = DOWNLOAD_TRANSIENT_ERROR; | 203 result = DOWNLOAD_TRANSIENT_ERROR; |
| 204 } | 204 } |
| 205 ReportResult(download_state, result, attachment_data); | 205 ReportResult(download_state, result, attachment_data); |
| 206 state_map_.erase(iter); | 206 state_map_.erase(iter); |
| 207 } | 207 } |
| 208 | 208 |
| 209 scoped_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( | 209 std::unique_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( |
| 210 const AttachmentUrl& url, | 210 const AttachmentUrl& url, |
| 211 const std::string& access_token) { | 211 const std::string& access_token) { |
| 212 scoped_ptr<net::URLFetcher> url_fetcher = | 212 std::unique_ptr<net::URLFetcher> url_fetcher = |
| 213 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); | 213 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); |
| 214 AttachmentUploaderImpl::ConfigureURLFetcherCommon( | 214 AttachmentUploaderImpl::ConfigureURLFetcherCommon( |
| 215 url_fetcher.get(), access_token, raw_store_birthday_, model_type_, | 215 url_fetcher.get(), access_token, raw_store_birthday_, model_type_, |
| 216 url_request_context_getter_.get()); | 216 url_request_context_getter_.get()); |
| 217 return url_fetcher; | 217 return url_fetcher; |
| 218 } | 218 } |
| 219 | 219 |
| 220 void AttachmentDownloaderImpl::RequestAccessToken( | 220 void AttachmentDownloaderImpl::RequestAccessToken( |
| 221 DownloadState* download_state) { | 221 DownloadState* download_state) { |
| 222 requests_waiting_for_access_token_.push_back(download_state); | 222 requests_waiting_for_access_token_.push_back(download_state); |
| 223 // Start access token request if there is no active one. | 223 // Start access token request if there is no active one. |
| 224 if (access_token_request_ == NULL) { | 224 if (access_token_request_ == NULL) { |
| 225 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart( | 225 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart( |
| 226 token_service_provider_.get(), account_id_, oauth2_scopes_, this); | 226 token_service_provider_.get(), account_id_, oauth2_scopes_, this); |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 | 229 |
| 230 void AttachmentDownloaderImpl::ReportResult( | 230 void AttachmentDownloaderImpl::ReportResult( |
| 231 const DownloadState& download_state, | 231 const DownloadState& download_state, |
| 232 const DownloadResult& result, | 232 const DownloadResult& result, |
| 233 const scoped_refptr<base::RefCountedString>& attachment_data) { | 233 const scoped_refptr<base::RefCountedString>& attachment_data) { |
| 234 std::vector<DownloadCallback>::const_iterator iter; | 234 std::vector<DownloadCallback>::const_iterator iter; |
| 235 for (iter = download_state.user_callbacks.begin(); | 235 for (iter = download_state.user_callbacks.begin(); |
| 236 iter != download_state.user_callbacks.end(); | 236 iter != download_state.user_callbacks.end(); |
| 237 ++iter) { | 237 ++iter) { |
| 238 scoped_ptr<Attachment> attachment; | 238 std::unique_ptr<Attachment> attachment; |
| 239 if (result == DOWNLOAD_SUCCESS) { | 239 if (result == DOWNLOAD_SUCCESS) { |
| 240 attachment.reset(new Attachment(Attachment::CreateFromParts( | 240 attachment.reset(new Attachment(Attachment::CreateFromParts( |
| 241 download_state.attachment_id, attachment_data))); | 241 download_state.attachment_id, attachment_data))); |
| 242 } | 242 } |
| 243 | 243 |
| 244 base::MessageLoop::current()->PostTask( | 244 base::MessageLoop::current()->PostTask( |
| 245 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); | 245 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 | 248 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 280 |
| 281 if (crc32c_raw.size() != sizeof(*crc32c)) | 281 if (crc32c_raw.size() != sizeof(*crc32c)) |
| 282 return false; | 282 return false; |
| 283 | 283 |
| 284 *crc32c = | 284 *crc32c = |
| 285 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); | 285 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); |
| 286 return true; | 286 return true; |
| 287 } | 287 } |
| 288 | 288 |
| 289 } // namespace syncer | 289 } // namespace syncer |
| OLD | NEW |