OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // File method ordering: Methods in this file are in the same order as | 5 // File method ordering: Methods in this file are in the same order as |
6 // in download_item_impl.h, with the following exception: The public | 6 // in download_item_impl.h, with the following exception: The public |
7 // interface Start is placed in chronological order with the other | 7 // interface Start is placed in chronological order with the other |
8 // (private) routines that together define a DownloadItem's state | 8 // (private) routines that together define a DownloadItem's state |
9 // transitions as the download progresses. See "Download progression | 9 // transitions as the download progresses. See "Download progression |
10 // cascade" later in this file. | 10 // cascade" later in this file. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
62 | 62 |
63 // Make sure we only delete files. | 63 // Make sure we only delete files. |
64 if (!file_util::DirectoryExists(path)) | 64 if (!file_util::DirectoryExists(path)) |
65 file_util::Delete(path, false); | 65 file_util::Delete(path, false); |
66 } | 66 } |
67 | 67 |
68 // Wrapper around DownloadFile::Detach and DownloadFile::Cancel that | 68 // Wrapper around DownloadFile::Detach and DownloadFile::Cancel that |
69 // takes ownership of the DownloadFile and hence implicitly destroys it | 69 // takes ownership of the DownloadFile and hence implicitly destroys it |
70 // at the end of the function. | 70 // at the end of the function. |
71 static void DownloadFileDetach(scoped_ptr<DownloadFile> download_file) { | 71 static base::FilePath DownloadFileDetach( |
72 scoped_ptr<DownloadFile> download_file) { | |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
74 base::FilePath full_path = download_file->FullPath(); | |
73 download_file->Detach(); | 75 download_file->Detach(); |
76 return full_path; | |
74 } | 77 } |
75 | 78 |
76 static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) { | 79 static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) { |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
78 download_file->Cancel(); | 81 download_file->Cancel(); |
79 } | 82 } |
80 | 83 |
81 bool IsDownloadResumptionEnabled() { | 84 bool IsDownloadResumptionEnabled() { |
82 return CommandLine::ForCurrentProcess()->HasSwitch( | 85 return CommandLine::ForCurrentProcess()->HasSwitch( |
83 switches::kEnableDownloadResumption); | 86 switches::kEnableDownloadResumption); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 | 271 |
269 observers_.RemoveObserver(observer); | 272 observers_.RemoveObserver(observer); |
270 } | 273 } |
271 | 274 |
272 void DownloadItemImpl::UpdateObservers() { | 275 void DownloadItemImpl::UpdateObservers() { |
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
274 | 277 |
275 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 278 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
276 } | 279 } |
277 | 280 |
278 void DownloadItemImpl::DangerousDownloadValidated() { | 281 void DownloadItemImpl::ValidateDangerousDownload() { |
279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
280 DCHECK_EQ(IN_PROGRESS, GetState()); | 283 DCHECK_EQ(IN_PROGRESS, GetState()); |
281 DCHECK(IsDangerous()); | 284 DCHECK(IsDangerous()); |
282 | 285 |
283 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 286 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
284 | 287 |
285 if (GetState() != IN_PROGRESS) | 288 if (GetState() != IN_PROGRESS) |
286 return; | 289 return; |
287 | 290 |
288 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 291 RecordDangerousDownloadAccept(GetDangerType()); |
289 GetDangerType(), | |
290 DOWNLOAD_DANGER_TYPE_MAX); | |
291 | 292 |
292 danger_type_ = DOWNLOAD_DANGER_TYPE_USER_VALIDATED; | 293 danger_type_ = DOWNLOAD_DANGER_TYPE_USER_VALIDATED; |
293 | 294 |
294 bound_net_log_.AddEvent( | 295 bound_net_log_.AddEvent( |
295 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, | 296 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, |
296 base::Bind(&ItemCheckedNetLogCallback, GetDangerType())); | 297 base::Bind(&ItemCheckedNetLogCallback, GetDangerType())); |
297 | 298 |
298 UpdateObservers(); | 299 UpdateObservers(); |
299 | 300 |
300 MaybeCompleteDownload(); | 301 MaybeCompleteDownload(); |
301 } | 302 } |
302 | 303 |
304 void DownloadItemImpl::StealDangerousDownload( | |
305 const AcquireFileCallback& callback) { | |
306 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
308 DCHECK(IsDangerous()); | |
309 DCHECK(download_file_); | |
310 | |
311 BrowserThread::PostTaskAndReplyWithResult( | |
312 BrowserThread::FILE, | |
313 FROM_HERE, | |
314 base::Bind(&DownloadFileDetach, base::Passed(&download_file_)), | |
315 callback); | |
316 current_path_.clear(); | |
317 Remove(); | |
318 // We have now been deleted. | |
319 } | |
320 | |
303 void DownloadItemImpl::Pause() { | 321 void DownloadItemImpl::Pause() { |
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
305 | 323 |
306 // Ignore irrelevant states. | 324 // Ignore irrelevant states. |
307 if (state_ != IN_PROGRESS_INTERNAL || is_paused_) | 325 if (state_ != IN_PROGRESS_INTERNAL || is_paused_) |
308 return; | 326 return; |
309 | 327 |
310 request_handle_->PauseRequest(); | 328 request_handle_->PauseRequest(); |
311 is_paused_ = true; | 329 is_paused_ = true; |
312 UpdateObservers(); | 330 UpdateObservers(); |
(...skipping 30 matching lines...) Expand all Loading... | |
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
344 | 362 |
345 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 363 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
346 if (state_ != IN_PROGRESS_INTERNAL && | 364 if (state_ != IN_PROGRESS_INTERNAL && |
347 state_ != INTERRUPTED_INTERNAL && | 365 state_ != INTERRUPTED_INTERNAL && |
348 state_ != RESUMING_INTERNAL) { | 366 state_ != RESUMING_INTERNAL) { |
349 // Small downloads might be complete before this method has a chance to run. | 367 // Small downloads might be complete before this method has a chance to run. |
350 return; | 368 return; |
351 } | 369 } |
352 | 370 |
353 last_reason_ = user_cancel ? | 371 if (IsDangerous()) { |
354 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | 372 RecordDangerousDownloadDiscard( |
355 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | 373 user_cancel ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION |
Randy Smith (Not in Mondays)
2013/05/24 02:06:16
Hmmm. Is this changing the semantics of the UMA?
asanka
2013/05/24 20:32:47
It's incorrect to do UMA in both Remove() and Canc
| |
374 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, | |
375 GetDangerType()); | |
376 } | |
377 | |
378 last_reason_ = user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED | |
379 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
356 | 380 |
357 RecordDownloadCount(CANCELLED_COUNT); | 381 RecordDownloadCount(CANCELLED_COUNT); |
358 | 382 |
359 // TODO(rdsmith/benjhayden): Remove condition as part of | 383 // TODO(rdsmith/benjhayden): Remove condition as part of |
360 // |SavePackage| integration. | 384 // |SavePackage| integration. |
361 // |download_file_| can be NULL if Interrupt() is called after the | 385 // |download_file_| can be NULL if Interrupt() is called after the |
362 // download file has been released. | 386 // download file has been released. |
363 if (!is_save_package_download_ && download_file_) | 387 if (!is_save_package_download_ && download_file_) |
364 ReleaseDownloadFile(true); | 388 ReleaseDownloadFile(true); |
365 | 389 |
366 if (state_ == IN_PROGRESS_INTERNAL) { | 390 if (state_ == IN_PROGRESS_INTERNAL) { |
367 // Cancel the originating URL request unless it's already been cancelled | 391 // Cancel the originating URL request unless it's already been cancelled |
368 // by interrupt. | 392 // by interrupt. |
369 request_handle_->CancelRequest(); | 393 request_handle_->CancelRequest(); |
370 } | 394 } |
371 | 395 |
372 TransitionTo(CANCELLED_INTERNAL); | 396 TransitionTo(CANCELLED_INTERNAL); |
373 } | 397 } |
374 | 398 |
375 void DownloadItemImpl::Delete(DeleteReason reason) { | |
376 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
377 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
378 | |
379 switch (reason) { | |
380 case DELETE_DUE_TO_USER_DISCARD: | |
381 UMA_HISTOGRAM_ENUMERATION( | |
382 "Download.UserDiscard", GetDangerType(), | |
383 DOWNLOAD_DANGER_TYPE_MAX); | |
384 break; | |
385 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | |
386 UMA_HISTOGRAM_ENUMERATION( | |
387 "Download.Discard", GetDangerType(), | |
388 DOWNLOAD_DANGER_TYPE_MAX); | |
389 break; | |
390 default: | |
391 NOTREACHED(); | |
392 } | |
393 | |
394 // Delete the file if it exists and is not owned by a DownloadFile object. | |
395 // (In the latter case the DownloadFile object will delete it on cancel.) | |
396 if (!current_path_.empty() && download_file_.get() == NULL) { | |
397 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
398 base::Bind(&DeleteDownloadedFile, current_path_)); | |
399 current_path_.clear(); | |
400 } | |
401 Remove(); | |
402 // We have now been deleted. | |
403 } | |
404 | |
405 void DownloadItemImpl::Remove() { | 399 void DownloadItemImpl::Remove() { |
406 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 400 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
408 | 402 |
403 if (IsDangerous()) { | |
404 RecordDangerousDownloadDiscard(DOWNLOAD_DISCARD_DUE_TO_USER_ACTION, | |
405 GetDangerType()); | |
406 } | |
407 | |
409 // Remove the intermediate file if we are removing an interrupted download. | 408 // Remove the intermediate file if we are removing an interrupted download. |
410 // Continuable interruptions leave the intermediate file around. However, the | 409 // Continuable interruptions leave the intermediate file around. However, the |
411 // intermediate file will be unusable if the download item is removed. | 410 // intermediate file will be unusable if the download item is removed. |
412 if (!current_path_.empty() && IsInterrupted() && !download_file_.get()) { | 411 if (!current_path_.empty() && IsInterrupted() && !download_file_.get()) { |
413 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 412 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
414 base::Bind(&DeleteDownloadedFile, current_path_)); | 413 base::Bind(&DeleteDownloadedFile, current_path_)); |
415 current_path_.clear(); | 414 current_path_.clear(); |
416 } | 415 } |
417 | 416 |
418 delegate_->AssertStateConsistent(this); | 417 delegate_->AssertStateConsistent(this); |
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1392 if (destroy_file) { | 1391 if (destroy_file) { |
1393 BrowserThread::PostTask( | 1392 BrowserThread::PostTask( |
1394 BrowserThread::FILE, FROM_HERE, | 1393 BrowserThread::FILE, FROM_HERE, |
1395 // Will be deleted at end of task execution. | 1394 // Will be deleted at end of task execution. |
1396 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); | 1395 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); |
1397 // Avoid attempting to reuse the intermediate file by clearing out | 1396 // Avoid attempting to reuse the intermediate file by clearing out |
1398 // current_path_. | 1397 // current_path_. |
1399 current_path_.clear(); | 1398 current_path_.clear(); |
1400 } else { | 1399 } else { |
1401 BrowserThread::PostTask( | 1400 BrowserThread::PostTask( |
1402 BrowserThread::FILE, FROM_HERE, | 1401 BrowserThread::FILE, |
1403 // Will be deleted at end of task execution. | 1402 FROM_HERE, |
1404 base::Bind(&DownloadFileDetach, base::Passed(&download_file_))); | 1403 base::Bind(base::IgnoreResult(&DownloadFileDetach), |
1404 // Will be deleted at end of task execution. | |
1405 base::Passed(&download_file_))); | |
1405 } | 1406 } |
1406 // Don't accept any more messages from the DownloadFile, and null | 1407 // Don't accept any more messages from the DownloadFile, and null |
1407 // out any previous "all data received". This also breaks links to | 1408 // out any previous "all data received". This also breaks links to |
1408 // other entities we've given out weak pointers to. | 1409 // other entities we've given out weak pointers to. |
1409 weak_ptr_factory_.InvalidateWeakPtrs(); | 1410 weak_ptr_factory_.InvalidateWeakPtrs(); |
1410 } | 1411 } |
1411 | 1412 |
1412 bool DownloadItemImpl::IsDownloadReadyForCompletion( | 1413 bool DownloadItemImpl::IsDownloadReadyForCompletion( |
1413 const base::Closure& state_change_notification) { | 1414 const base::Closure& state_change_notification) { |
1414 // If we don't have all the data, the download is not ready for | 1415 // If we don't have all the data, the download is not ready for |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1678 case RESUME_MODE_USER_CONTINUE: | 1679 case RESUME_MODE_USER_CONTINUE: |
1679 return "USER_CONTINUE"; | 1680 return "USER_CONTINUE"; |
1680 case RESUME_MODE_USER_RESTART: | 1681 case RESUME_MODE_USER_RESTART: |
1681 return "USER_RESTART"; | 1682 return "USER_RESTART"; |
1682 } | 1683 } |
1683 NOTREACHED() << "Unknown resume mode " << mode; | 1684 NOTREACHED() << "Unknown resume mode " << mode; |
1684 return "unknown"; | 1685 return "unknown"; |
1685 } | 1686 } |
1686 | 1687 |
1687 } // namespace content | 1688 } // namespace content |
OLD | NEW |