| 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 "components/sync/engine_impl/attachments/attachment_downloader_impl.h" | 5 #include "components/sync/engine_impl/attachments/attachment_downloader_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 token_service_provider_(token_service_provider), | 69 token_service_provider_(token_service_provider), |
| 70 raw_store_birthday_(store_birthday), | 70 raw_store_birthday_(store_birthday), |
| 71 model_type_(model_type) { | 71 model_type_(model_type) { |
| 72 DCHECK(url_request_context_getter_.get()); | 72 DCHECK(url_request_context_getter_.get()); |
| 73 DCHECK(!account_id.empty()); | 73 DCHECK(!account_id.empty()); |
| 74 DCHECK(!scopes.empty()); | 74 DCHECK(!scopes.empty()); |
| 75 DCHECK(token_service_provider_.get()); | 75 DCHECK(token_service_provider_.get()); |
| 76 DCHECK(!raw_store_birthday_.empty()); | 76 DCHECK(!raw_store_birthday_.empty()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 AttachmentDownloaderImpl::~AttachmentDownloaderImpl() {} | 79 AttachmentDownloaderImpl::~AttachmentDownloaderImpl() { |
| 80 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 81 } |
| 80 | 82 |
| 81 void AttachmentDownloaderImpl::DownloadAttachment( | 83 void AttachmentDownloaderImpl::DownloadAttachment( |
| 82 const AttachmentId& attachment_id, | 84 const AttachmentId& attachment_id, |
| 83 const DownloadCallback& callback) { | 85 const DownloadCallback& callback) { |
| 84 DCHECK(CalledOnValidThread()); | 86 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 85 | 87 |
| 86 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( | 88 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( |
| 87 sync_service_url_, attachment_id) | 89 sync_service_url_, attachment_id) |
| 88 .spec(); | 90 .spec(); |
| 89 | 91 |
| 90 StateMap::iterator iter = state_map_.find(url); | 92 StateMap::iterator iter = state_map_.find(url); |
| 91 DownloadState* download_state = | 93 DownloadState* download_state = |
| 92 iter != state_map_.end() ? iter->second.get() : nullptr; | 94 iter != state_map_.end() ? iter->second.get() : nullptr; |
| 93 if (!download_state) { | 95 if (!download_state) { |
| 94 // There is no request started for this attachment id. Let's create | 96 // There is no request started for this attachment id. Let's create |
| 95 // DownloadState and request access token for it. | 97 // DownloadState and request access token for it. |
| 96 std::unique_ptr<DownloadState> new_download_state( | 98 std::unique_ptr<DownloadState> new_download_state( |
| 97 new DownloadState(attachment_id, url)); | 99 new DownloadState(attachment_id, url)); |
| 98 download_state = new_download_state.get(); | 100 download_state = new_download_state.get(); |
| 99 state_map_[url] = std::move(new_download_state); | 101 state_map_[url] = std::move(new_download_state); |
| 100 RequestAccessToken(download_state); | 102 RequestAccessToken(download_state); |
| 101 } | 103 } |
| 102 DCHECK(download_state->attachment_id == attachment_id); | 104 DCHECK(download_state->attachment_id == attachment_id); |
| 103 download_state->user_callbacks.push_back(callback); | 105 download_state->user_callbacks.push_back(callback); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void AttachmentDownloaderImpl::OnGetTokenSuccess( | 108 void AttachmentDownloaderImpl::OnGetTokenSuccess( |
| 107 const OAuth2TokenService::Request* request, | 109 const OAuth2TokenService::Request* request, |
| 108 const std::string& access_token, | 110 const std::string& access_token, |
| 109 const base::Time& expiration_time) { | 111 const base::Time& expiration_time) { |
| 110 DCHECK(CalledOnValidThread()); | 112 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 111 DCHECK(request == access_token_request_.get()); | 113 DCHECK(request == access_token_request_.get()); |
| 112 access_token_request_.reset(); | 114 access_token_request_.reset(); |
| 113 StateList::const_iterator iter; | 115 StateList::const_iterator iter; |
| 114 // Start downloads for all download requests waiting for access token. | 116 // Start downloads for all download requests waiting for access token. |
| 115 for (iter = requests_waiting_for_access_token_.begin(); | 117 for (iter = requests_waiting_for_access_token_.begin(); |
| 116 iter != requests_waiting_for_access_token_.end(); ++iter) { | 118 iter != requests_waiting_for_access_token_.end(); ++iter) { |
| 117 DownloadState* download_state = *iter; | 119 DownloadState* download_state = *iter; |
| 118 download_state->access_token = access_token; | 120 download_state->access_token = access_token; |
| 119 download_state->url_fetcher = | 121 download_state->url_fetcher = |
| 120 CreateFetcher(download_state->attachment_url, access_token); | 122 CreateFetcher(download_state->attachment_url, access_token); |
| 121 download_state->start_time = base::TimeTicks::Now(); | 123 download_state->start_time = base::TimeTicks::Now(); |
| 122 download_state->url_fetcher->Start(); | 124 download_state->url_fetcher->Start(); |
| 123 } | 125 } |
| 124 requests_waiting_for_access_token_.clear(); | 126 requests_waiting_for_access_token_.clear(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void AttachmentDownloaderImpl::OnGetTokenFailure( | 129 void AttachmentDownloaderImpl::OnGetTokenFailure( |
| 128 const OAuth2TokenService::Request* request, | 130 const OAuth2TokenService::Request* request, |
| 129 const GoogleServiceAuthError& error) { | 131 const GoogleServiceAuthError& error) { |
| 130 DCHECK(CalledOnValidThread()); | 132 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 131 DCHECK(request == access_token_request_.get()); | 133 DCHECK(request == access_token_request_.get()); |
| 132 access_token_request_.reset(); | 134 access_token_request_.reset(); |
| 133 StateList::const_iterator iter; | 135 StateList::const_iterator iter; |
| 134 // Without access token all downloads fail. | 136 // Without access token all downloads fail. |
| 135 for (iter = requests_waiting_for_access_token_.begin(); | 137 for (iter = requests_waiting_for_access_token_.begin(); |
| 136 iter != requests_waiting_for_access_token_.end(); ++iter) { | 138 iter != requests_waiting_for_access_token_.end(); ++iter) { |
| 137 DownloadState* download_state = *iter; | 139 DownloadState* download_state = *iter; |
| 138 scoped_refptr<base::RefCountedString> null_attachment_data; | 140 scoped_refptr<base::RefCountedString> null_attachment_data; |
| 139 ReportResult(*download_state, DOWNLOAD_TRANSIENT_ERROR, | 141 ReportResult(*download_state, DOWNLOAD_TRANSIENT_ERROR, |
| 140 null_attachment_data); | 142 null_attachment_data); |
| 141 // Don't delete using the URL directly to avoid an access after free error | 143 // Don't delete using the URL directly to avoid an access after free error |
| 142 // due to std::unordered_map's implementation. See crbug.com/603275. | 144 // due to std::unordered_map's implementation. See crbug.com/603275. |
| 143 auto erase_iter = state_map_.find(download_state->attachment_url); | 145 auto erase_iter = state_map_.find(download_state->attachment_url); |
| 144 DCHECK(erase_iter != state_map_.end()); | 146 DCHECK(erase_iter != state_map_.end()); |
| 145 state_map_.erase(erase_iter); | 147 state_map_.erase(erase_iter); |
| 146 } | 148 } |
| 147 requests_waiting_for_access_token_.clear(); | 149 requests_waiting_for_access_token_.clear(); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void AttachmentDownloaderImpl::OnURLFetchComplete( | 152 void AttachmentDownloaderImpl::OnURLFetchComplete( |
| 151 const net::URLFetcher* source) { | 153 const net::URLFetcher* source) { |
| 152 DCHECK(CalledOnValidThread()); | 154 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 153 | 155 |
| 154 // Find DownloadState by url. | 156 // Find DownloadState by url. |
| 155 AttachmentUrl url = source->GetOriginalURL().spec(); | 157 AttachmentUrl url = source->GetOriginalURL().spec(); |
| 156 StateMap::iterator iter = state_map_.find(url); | 158 StateMap::iterator iter = state_map_.find(url); |
| 157 DCHECK(iter != state_map_.end()); | 159 DCHECK(iter != state_map_.end()); |
| 158 const DownloadState& download_state = *iter->second; | 160 const DownloadState& download_state = *iter->second; |
| 159 DCHECK(source == download_state.url_fetcher.get()); | 161 DCHECK(source == download_state.url_fetcher.get()); |
| 160 | 162 |
| 161 DownloadResult result = DOWNLOAD_TRANSIENT_ERROR; | 163 DownloadResult result = DOWNLOAD_TRANSIENT_ERROR; |
| 162 scoped_refptr<base::RefCountedString> attachment_data; | 164 scoped_refptr<base::RefCountedString> attachment_data; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 316 |
| 315 if (crc32c_raw.size() != sizeof(*crc32c)) | 317 if (crc32c_raw.size() != sizeof(*crc32c)) |
| 316 return false; | 318 return false; |
| 317 | 319 |
| 318 *crc32c = | 320 *crc32c = |
| 319 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); | 321 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); |
| 320 return true; | 322 return true; |
| 321 } | 323 } |
| 322 | 324 |
| 323 } // namespace syncer | 325 } // namespace syncer |
| OLD | NEW |