OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" | |
6 | |
7 #include <stdint.h> | |
8 #include <utility> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/bind.h" | |
12 #include "base/location.h" | |
13 #include "base/metrics/histogram.h" | |
14 #include "base/metrics/histogram_macros.h" | |
15 #include "base/metrics/sparse_histogram.h" | |
16 #include "base/single_thread_task_runner.h" | |
17 #include "base/sys_byteorder.h" | |
18 #include "base/threading/thread_task_runner_handle.h" | |
19 #include "base/time/time.h" | |
20 #include "net/base/load_flags.h" | |
21 #include "net/http/http_response_headers.h" | |
22 #include "net/http/http_status_code.h" | |
23 #include "net/http/http_util.h" | |
24 #include "net/url_request/url_fetcher.h" | |
25 #include "net/url_request/url_request_status.h" | |
26 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h" | |
27 #include "sync/internal_api/public/attachments/attachment_util.h" | |
28 #include "sync/protocol/sync.pb.h" | |
29 #include "url/gurl.h" | |
30 | |
31 namespace syncer { | |
32 | |
33 struct AttachmentDownloaderImpl::DownloadState { | |
34 public: | |
35 DownloadState(const AttachmentId& attachment_id, | |
36 const AttachmentUrl& attachment_url); | |
37 | |
38 AttachmentId attachment_id; | |
39 AttachmentUrl attachment_url; | |
40 // |access_token| needed to invalidate if downloading attachment fails with | |
41 // HTTP_UNAUTHORIZED. | |
42 std::string access_token; | |
43 std::unique_ptr<net::URLFetcher> url_fetcher; | |
44 std::vector<DownloadCallback> user_callbacks; | |
45 base::TimeTicks start_time; | |
46 }; | |
47 | |
48 AttachmentDownloaderImpl::DownloadState::DownloadState( | |
49 const AttachmentId& attachment_id, | |
50 const AttachmentUrl& attachment_url) | |
51 : attachment_id(attachment_id), attachment_url(attachment_url) { | |
52 } | |
53 | |
54 AttachmentDownloaderImpl::AttachmentDownloaderImpl( | |
55 const GURL& sync_service_url, | |
56 const scoped_refptr<net::URLRequestContextGetter>& | |
57 url_request_context_getter, | |
58 const std::string& account_id, | |
59 const OAuth2TokenService::ScopeSet& scopes, | |
60 const scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>& | |
61 token_service_provider, | |
62 const std::string& store_birthday, | |
63 ModelType model_type) | |
64 : OAuth2TokenService::Consumer("attachment-downloader-impl"), | |
65 sync_service_url_(sync_service_url), | |
66 url_request_context_getter_(url_request_context_getter), | |
67 account_id_(account_id), | |
68 oauth2_scopes_(scopes), | |
69 token_service_provider_(token_service_provider), | |
70 raw_store_birthday_(store_birthday), | |
71 model_type_(model_type) { | |
72 DCHECK(url_request_context_getter_.get()); | |
73 DCHECK(!account_id.empty()); | |
74 DCHECK(!scopes.empty()); | |
75 DCHECK(token_service_provider_.get()); | |
76 DCHECK(!raw_store_birthday_.empty()); | |
77 } | |
78 | |
79 AttachmentDownloaderImpl::~AttachmentDownloaderImpl() { | |
80 } | |
81 | |
82 void AttachmentDownloaderImpl::DownloadAttachment( | |
83 const AttachmentId& attachment_id, | |
84 const DownloadCallback& callback) { | |
85 DCHECK(CalledOnValidThread()); | |
86 | |
87 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( | |
88 sync_service_url_, attachment_id).spec(); | |
89 | |
90 StateMap::iterator iter = state_map_.find(url); | |
91 DownloadState* download_state = | |
92 iter != state_map_.end() ? iter->second.get() : nullptr; | |
93 if (!download_state) { | |
94 // There is no request started for this attachment id. Let's create | |
95 // DownloadState and request access token for it. | |
96 std::unique_ptr<DownloadState> new_download_state( | |
97 new DownloadState(attachment_id, url)); | |
98 download_state = new_download_state.get(); | |
99 state_map_[url] = std::move(new_download_state); | |
100 RequestAccessToken(download_state); | |
101 } | |
102 DCHECK(download_state->attachment_id == attachment_id); | |
103 download_state->user_callbacks.push_back(callback); | |
104 } | |
105 | |
106 void AttachmentDownloaderImpl::OnGetTokenSuccess( | |
107 const OAuth2TokenService::Request* request, | |
108 const std::string& access_token, | |
109 const base::Time& expiration_time) { | |
110 DCHECK(CalledOnValidThread()); | |
111 DCHECK(request == access_token_request_.get()); | |
112 access_token_request_.reset(); | |
113 StateList::const_iterator iter; | |
114 // Start downloads for all download requests waiting for access token. | |
115 for (iter = requests_waiting_for_access_token_.begin(); | |
116 iter != requests_waiting_for_access_token_.end(); | |
117 ++iter) { | |
118 DownloadState* download_state = *iter; | |
119 download_state->access_token = access_token; | |
120 download_state->url_fetcher = | |
121 CreateFetcher(download_state->attachment_url, access_token); | |
122 download_state->start_time = base::TimeTicks::Now(); | |
123 download_state->url_fetcher->Start(); | |
124 } | |
125 requests_waiting_for_access_token_.clear(); | |
126 } | |
127 | |
128 void AttachmentDownloaderImpl::OnGetTokenFailure( | |
129 const OAuth2TokenService::Request* request, | |
130 const GoogleServiceAuthError& error) { | |
131 DCHECK(CalledOnValidThread()); | |
132 DCHECK(request == access_token_request_.get()); | |
133 access_token_request_.reset(); | |
134 StateList::const_iterator iter; | |
135 // Without access token all downloads fail. | |
136 for (iter = requests_waiting_for_access_token_.begin(); | |
137 iter != requests_waiting_for_access_token_.end(); | |
138 ++iter) { | |
139 DownloadState* download_state = *iter; | |
140 scoped_refptr<base::RefCountedString> null_attachment_data; | |
141 ReportResult(*download_state, DOWNLOAD_TRANSIENT_ERROR, | |
142 null_attachment_data); | |
143 // Don't delete using the URL directly to avoid an access after free error | |
144 // due to std::unordered_map's implementation. See crbug.com/603275. | |
145 auto erase_iter = state_map_.find(download_state->attachment_url); | |
146 DCHECK(erase_iter != state_map_.end()); | |
147 state_map_.erase(erase_iter); | |
148 } | |
149 requests_waiting_for_access_token_.clear(); | |
150 } | |
151 | |
152 void AttachmentDownloaderImpl::OnURLFetchComplete( | |
153 const net::URLFetcher* source) { | |
154 DCHECK(CalledOnValidThread()); | |
155 | |
156 // Find DownloadState by url. | |
157 AttachmentUrl url = source->GetOriginalURL().spec(); | |
158 StateMap::iterator iter = state_map_.find(url); | |
159 DCHECK(iter != state_map_.end()); | |
160 const DownloadState& download_state = *iter->second; | |
161 DCHECK(source == download_state.url_fetcher.get()); | |
162 | |
163 DownloadResult result = DOWNLOAD_TRANSIENT_ERROR; | |
164 scoped_refptr<base::RefCountedString> attachment_data; | |
165 uint32_t attachment_crc32c = 0; | |
166 | |
167 net::URLRequestStatus status = source->GetStatus(); | |
168 const int response_code = source->GetResponseCode(); | |
169 UMA_HISTOGRAM_SPARSE_SLOWLY("Sync.Attachments.DownloadResponseCode", | |
170 status.is_success() ? response_code : status.error()); | |
171 if (response_code == net::HTTP_OK) { | |
172 std::string data_as_string; | |
173 source->GetResponseAsString(&data_as_string); | |
174 attachment_data = base::RefCountedString::TakeString(&data_as_string); | |
175 | |
176 UMA_HISTOGRAM_LONG_TIMES("Sync.Attachments.DownloadTotalTime", | |
177 base::TimeTicks::Now() - download_state.start_time); | |
178 | |
179 attachment_crc32c = ComputeCrc32c(attachment_data); | |
180 uint32_t crc32c_from_headers = 0; | |
181 if (ExtractCrc32c(source->GetResponseHeaders(), &crc32c_from_headers) && | |
182 attachment_crc32c != crc32c_from_headers) { | |
183 // Fail download only if there is useful crc32c in header and it doesn't | |
184 // match data. All other cases are fine. When crc32c is not in headers | |
185 // locally calculated one will be stored and used for further checks. | |
186 result = DOWNLOAD_TRANSIENT_ERROR; | |
187 } else { | |
188 // If the id's crc32c doesn't match that of the downloaded attachment, | |
189 // then we're stuck and retrying is unlikely to help. | |
190 if (attachment_crc32c != download_state.attachment_id.GetCrc32c()) { | |
191 result = DOWNLOAD_UNSPECIFIED_ERROR; | |
192 } else { | |
193 result = DOWNLOAD_SUCCESS; | |
194 } | |
195 } | |
196 UMA_HISTOGRAM_BOOLEAN("Sync.Attachments.DownloadChecksumResult", | |
197 result == DOWNLOAD_SUCCESS); | |
198 } else if (response_code == net::HTTP_UNAUTHORIZED) { | |
199 // Server tells us we've got a bad token so invalidate it. | |
200 OAuth2TokenServiceRequest::InvalidateToken(token_service_provider_.get(), | |
201 account_id_, | |
202 oauth2_scopes_, | |
203 download_state.access_token); | |
204 // Fail the request, but indicate that it may be successful if retried. | |
205 result = DOWNLOAD_TRANSIENT_ERROR; | |
206 } else if (response_code == net::HTTP_FORBIDDEN) { | |
207 // User is not allowed to use attachments. Retrying won't help. | |
208 result = DOWNLOAD_UNSPECIFIED_ERROR; | |
209 } else if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) { | |
210 result = DOWNLOAD_TRANSIENT_ERROR; | |
211 } | |
212 ReportResult(download_state, result, attachment_data); | |
213 state_map_.erase(iter); | |
214 } | |
215 | |
216 std::unique_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( | |
217 const AttachmentUrl& url, | |
218 const std::string& access_token) { | |
219 std::unique_ptr<net::URLFetcher> url_fetcher = | |
220 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); | |
221 AttachmentUploaderImpl::ConfigureURLFetcherCommon( | |
222 url_fetcher.get(), access_token, raw_store_birthday_, model_type_, | |
223 url_request_context_getter_.get()); | |
224 return url_fetcher; | |
225 } | |
226 | |
227 void AttachmentDownloaderImpl::RequestAccessToken( | |
228 DownloadState* download_state) { | |
229 requests_waiting_for_access_token_.push_back(download_state); | |
230 // Start access token request if there is no active one. | |
231 if (access_token_request_ == NULL) { | |
232 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart( | |
233 token_service_provider_.get(), account_id_, oauth2_scopes_, this); | |
234 } | |
235 } | |
236 | |
237 void AttachmentDownloaderImpl::ReportResult( | |
238 const DownloadState& download_state, | |
239 const DownloadResult& result, | |
240 const scoped_refptr<base::RefCountedString>& attachment_data) { | |
241 std::vector<DownloadCallback>::const_iterator iter; | |
242 for (iter = download_state.user_callbacks.begin(); | |
243 iter != download_state.user_callbacks.end(); | |
244 ++iter) { | |
245 std::unique_ptr<Attachment> attachment; | |
246 if (result == DOWNLOAD_SUCCESS) { | |
247 attachment.reset(new Attachment(Attachment::CreateFromParts( | |
248 download_state.attachment_id, attachment_data))); | |
249 } | |
250 | |
251 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
252 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); | |
253 } | |
254 } | |
255 | |
256 bool AttachmentDownloaderImpl::ExtractCrc32c( | |
257 const net::HttpResponseHeaders* headers, | |
258 uint32_t* crc32c) { | |
259 DCHECK(crc32c); | |
260 if (!headers) { | |
261 return false; | |
262 } | |
263 | |
264 std::string crc32c_encoded; | |
265 std::string header_value; | |
266 size_t iter = 0; | |
267 // Iterate over all matching headers. | |
268 while (headers->EnumerateHeader(&iter, "x-goog-hash", &header_value)) { | |
269 // Because EnumerateHeader is smart about list values, header_value will | |
270 // either be empty or a single name=value pair. | |
271 net::HttpUtil::NameValuePairsIterator pair_iter( | |
272 header_value.begin(), header_value.end(), ','); | |
273 if (pair_iter.GetNext()) { | |
274 if (pair_iter.name() == "crc32c") { | |
275 crc32c_encoded = pair_iter.value(); | |
276 DCHECK(!pair_iter.GetNext()); | |
277 break; | |
278 } | |
279 } | |
280 } | |
281 // Check if header was found | |
282 if (crc32c_encoded.empty()) | |
283 return false; | |
284 std::string crc32c_raw; | |
285 if (!base::Base64Decode(crc32c_encoded, &crc32c_raw)) | |
286 return false; | |
287 | |
288 if (crc32c_raw.size() != sizeof(*crc32c)) | |
289 return false; | |
290 | |
291 *crc32c = | |
292 base::NetToHost32(*reinterpret_cast<const uint32_t*>(crc32c_raw.c_str())); | |
293 return true; | |
294 } | |
295 | |
296 } // namespace syncer | |
OLD | NEW |