| 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" |
| 11 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
| 12 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 14 #include "base/time/time.h" |
| 13 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 14 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 16 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
| 17 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 19 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h" | 21 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h" |
| 20 #include "sync/internal_api/public/attachments/attachment_util.h" | 22 #include "sync/internal_api/public/attachments/attachment_util.h" |
| 21 #include "sync/protocol/sync.pb.h" | 23 #include "sync/protocol/sync.pb.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 namespace syncer { | 26 namespace syncer { |
| 25 | 27 |
| 26 struct AttachmentDownloaderImpl::DownloadState { | 28 struct AttachmentDownloaderImpl::DownloadState { |
| 27 public: | 29 public: |
| 28 DownloadState(const AttachmentId& attachment_id, | 30 DownloadState(const AttachmentId& attachment_id, |
| 29 const AttachmentUrl& attachment_url); | 31 const AttachmentUrl& attachment_url); |
| 30 | 32 |
| 31 AttachmentId attachment_id; | 33 AttachmentId attachment_id; |
| 32 AttachmentUrl attachment_url; | 34 AttachmentUrl attachment_url; |
| 33 // |access_token| needed to invalidate if downloading attachment fails with | 35 // |access_token| needed to invalidate if downloading attachment fails with |
| 34 // HTTP_UNAUTHORIZED. | 36 // HTTP_UNAUTHORIZED. |
| 35 std::string access_token; | 37 std::string access_token; |
| 36 scoped_ptr<net::URLFetcher> url_fetcher; | 38 scoped_ptr<net::URLFetcher> url_fetcher; |
| 37 std::vector<DownloadCallback> user_callbacks; | 39 std::vector<DownloadCallback> user_callbacks; |
| 40 base::TimeTicks start_time; |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 AttachmentDownloaderImpl::DownloadState::DownloadState( | 43 AttachmentDownloaderImpl::DownloadState::DownloadState( |
| 41 const AttachmentId& attachment_id, | 44 const AttachmentId& attachment_id, |
| 42 const AttachmentUrl& attachment_url) | 45 const AttachmentUrl& attachment_url) |
| 43 : attachment_id(attachment_id), attachment_url(attachment_url) { | 46 : attachment_id(attachment_id), attachment_url(attachment_url) { |
| 44 } | 47 } |
| 45 | 48 |
| 46 AttachmentDownloaderImpl::AttachmentDownloaderImpl( | 49 AttachmentDownloaderImpl::AttachmentDownloaderImpl( |
| 47 const GURL& sync_service_url, | 50 const GURL& sync_service_url, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 access_token_request_.reset(); | 105 access_token_request_.reset(); |
| 103 StateList::const_iterator iter; | 106 StateList::const_iterator iter; |
| 104 // Start downloads for all download requests waiting for access token. | 107 // Start downloads for all download requests waiting for access token. |
| 105 for (iter = requests_waiting_for_access_token_.begin(); | 108 for (iter = requests_waiting_for_access_token_.begin(); |
| 106 iter != requests_waiting_for_access_token_.end(); | 109 iter != requests_waiting_for_access_token_.end(); |
| 107 ++iter) { | 110 ++iter) { |
| 108 DownloadState* download_state = *iter; | 111 DownloadState* download_state = *iter; |
| 109 download_state->access_token = access_token; | 112 download_state->access_token = access_token; |
| 110 download_state->url_fetcher = | 113 download_state->url_fetcher = |
| 111 CreateFetcher(download_state->attachment_url, access_token).Pass(); | 114 CreateFetcher(download_state->attachment_url, access_token).Pass(); |
| 115 download_state->start_time = base::TimeTicks::Now(); |
| 112 download_state->url_fetcher->Start(); | 116 download_state->url_fetcher->Start(); |
| 113 } | 117 } |
| 114 requests_waiting_for_access_token_.clear(); | 118 requests_waiting_for_access_token_.clear(); |
| 115 } | 119 } |
| 116 | 120 |
| 117 void AttachmentDownloaderImpl::OnGetTokenFailure( | 121 void AttachmentDownloaderImpl::OnGetTokenFailure( |
| 118 const OAuth2TokenService::Request* request, | 122 const OAuth2TokenService::Request* request, |
| 119 const GoogleServiceAuthError& error) { | 123 const GoogleServiceAuthError& error) { |
| 120 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
| 121 DCHECK(request == access_token_request_.get()); | 125 DCHECK(request == access_token_request_.get()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 152 | 156 |
| 153 net::URLRequestStatus status = source->GetStatus(); | 157 net::URLRequestStatus status = source->GetStatus(); |
| 154 const int response_code = source->GetResponseCode(); | 158 const int response_code = source->GetResponseCode(); |
| 155 UMA_HISTOGRAM_SPARSE_SLOWLY("Sync.Attachments.DownloadResponseCode", | 159 UMA_HISTOGRAM_SPARSE_SLOWLY("Sync.Attachments.DownloadResponseCode", |
| 156 status.is_success() ? response_code : status.error()); | 160 status.is_success() ? response_code : status.error()); |
| 157 if (response_code == net::HTTP_OK) { | 161 if (response_code == net::HTTP_OK) { |
| 158 std::string data_as_string; | 162 std::string data_as_string; |
| 159 source->GetResponseAsString(&data_as_string); | 163 source->GetResponseAsString(&data_as_string); |
| 160 attachment_data = base::RefCountedString::TakeString(&data_as_string); | 164 attachment_data = base::RefCountedString::TakeString(&data_as_string); |
| 161 | 165 |
| 166 UMA_HISTOGRAM_LONG_TIMES("Sync.Attachments.DownloadTotalTime", |
| 167 base::TimeTicks::Now() - download_state.start_time); |
| 168 |
| 162 attachment_crc32c = ComputeCrc32c(attachment_data); | 169 attachment_crc32c = ComputeCrc32c(attachment_data); |
| 163 uint32_t crc32c_from_headers = 0; | 170 uint32_t crc32c_from_headers = 0; |
| 164 if (ExtractCrc32c(source->GetResponseHeaders(), &crc32c_from_headers) && | 171 if (ExtractCrc32c(source->GetResponseHeaders(), &crc32c_from_headers) && |
| 165 attachment_crc32c != crc32c_from_headers) { | 172 attachment_crc32c != crc32c_from_headers) { |
| 166 // Fail download only if there is useful crc32c in header and it doesn't | 173 // Fail download only if there is useful crc32c in header and it doesn't |
| 167 // match data. All other cases are fine. When crc32c is not in headers | 174 // match data. All other cases are fine. When crc32c is not in headers |
| 168 // locally calculated one will be stored and used for further checks. | 175 // locally calculated one will be stored and used for further checks. |
| 169 result = DOWNLOAD_TRANSIENT_ERROR; | 176 result = DOWNLOAD_TRANSIENT_ERROR; |
| 170 } else { | 177 } else { |
| 171 result = DOWNLOAD_SUCCESS; | 178 result = DOWNLOAD_SUCCESS; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 272 |
| 266 if (crc32c_raw.size() != sizeof(*crc32c)) | 273 if (crc32c_raw.size() != sizeof(*crc32c)) |
| 267 return false; | 274 return false; |
| 268 | 275 |
| 269 *crc32c = | 276 *crc32c = |
| 270 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); | 277 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); |
| 271 return true; | 278 return true; |
| 272 } | 279 } |
| 273 | 280 |
| 274 } // namespace syncer | 281 } // namespace syncer |
| OLD | NEW |