| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Download utility implementation | 5 // Download utility implementation |
| 6 | 6 |
| 7 #include "chrome/browser/download/download_util.h" | 7 #include "chrome/browser/download/download_util.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <shobjidl.h> | 10 #include <shobjidl.h> |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 123 } |
| 124 return (download_path == desktop_dir); | 124 return (download_path == desktop_dir); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void GenerateFileNameFromRequest(const DownloadItem& download_item, | 127 void GenerateFileNameFromRequest(const DownloadItem& download_item, |
| 128 FilePath* generated_name) { | 128 FilePath* generated_name) { |
| 129 std::string default_file_name( | 129 std::string default_file_name( |
| 130 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 130 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 131 | 131 |
| 132 *generated_name = net::GenerateFileName(download_item.GetURL(), | 132 *generated_name = net::GenerateFileName(download_item.GetURL(), |
| 133 download_item.content_disposition(), | 133 download_item.GetContentDisposition(), |
| 134 download_item.referrer_charset(), | 134 download_item.GetReferrerCharset(), |
| 135 download_item.suggested_filename(), | 135 download_item.GetSuggestedFilename(), |
| 136 download_item.mime_type(), | 136 download_item.GetMimeType(), |
| 137 default_file_name); | 137 default_file_name); |
| 138 } | 138 } |
| 139 | 139 |
| 140 // Download progress painting -------------------------------------------------- | 140 // Download progress painting -------------------------------------------------- |
| 141 | 141 |
| 142 // Common bitmaps used for download progress animations. We load them once the | 142 // Common bitmaps used for download progress animations. We load them once the |
| 143 // first time we do a progress paint, then reuse them as they are always the | 143 // first time we do a progress paint, then reuse them as they are always the |
| 144 // same. | 144 // same. |
| 145 SkBitmap* g_foreground_16 = NULL; | 145 SkBitmap* g_foreground_16 = NULL; |
| 146 SkBitmap* g_background_16 = NULL; | 146 SkBitmap* g_background_16 = NULL; |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 DCHECK(download); | 346 DCHECK(download); |
| 347 | 347 |
| 348 // Set up our OLE machinery | 348 // Set up our OLE machinery |
| 349 ui::OSExchangeData data; | 349 ui::OSExchangeData data; |
| 350 | 350 |
| 351 if (icon) { | 351 if (icon) { |
| 352 drag_utils::CreateDragImageForFile( | 352 drag_utils::CreateDragImageForFile( |
| 353 download->GetFileNameToReportUser(), *icon, &data); | 353 download->GetFileNameToReportUser(), *icon, &data); |
| 354 } | 354 } |
| 355 | 355 |
| 356 const FilePath full_path = download->full_path(); | 356 const FilePath full_path = download->GetFullPath(); |
| 357 data.SetFilename(full_path); | 357 data.SetFilename(full_path); |
| 358 | 358 |
| 359 std::string mime_type = download->mime_type(); | 359 std::string mime_type = download->GetMimeType(); |
| 360 if (mime_type.empty()) | 360 if (mime_type.empty()) |
| 361 net::GetMimeTypeFromFile(full_path, &mime_type); | 361 net::GetMimeTypeFromFile(full_path, &mime_type); |
| 362 | 362 |
| 363 // Add URL so that we can load supported files when dragged to TabContents. | 363 // Add URL so that we can load supported files when dragged to TabContents. |
| 364 if (net::IsSupportedMimeType(mime_type)) { | 364 if (net::IsSupportedMimeType(mime_type)) { |
| 365 data.SetURL(net::FilePathToFileURL(full_path), | 365 data.SetURL(net::FilePathToFileURL(full_path), |
| 366 download->GetFileNameToReportUser().LossyDisplayName()); | 366 download->GetFileNameToReportUser().LossyDisplayName()); |
| 367 } | 367 } |
| 368 | 368 |
| 369 #if defined(USE_AURA) | 369 #if defined(USE_AURA) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 395 gfx::Image* icon, | 395 gfx::Image* icon, |
| 396 gfx::NativeView view) { | 396 gfx::NativeView view) { |
| 397 DownloadItemDrag::BeginDrag(download, icon); | 397 DownloadItemDrag::BeginDrag(download, icon); |
| 398 } | 398 } |
| 399 #endif // USE_X11 | 399 #endif // USE_X11 |
| 400 | 400 |
| 401 DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { | 401 DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { |
| 402 DictionaryValue* file_value = new DictionaryValue(); | 402 DictionaryValue* file_value = new DictionaryValue(); |
| 403 | 403 |
| 404 file_value->SetInteger("started", | 404 file_value->SetInteger("started", |
| 405 static_cast<int>(download->start_time().ToTimeT())); | 405 static_cast<int>(download->GetStartTime().ToTimeT())); |
| 406 file_value->SetString("since_string", | 406 file_value->SetString("since_string", |
| 407 TimeFormat::RelativeDate(download->start_time(), NULL)); | 407 TimeFormat::RelativeDate(download->GetStartTime(), NULL)); |
| 408 file_value->SetString("date_string", | 408 file_value->SetString("date_string", |
| 409 base::TimeFormatShortDate(download->start_time())); | 409 base::TimeFormatShortDate(download->GetStartTime())); |
| 410 file_value->SetInteger("id", id); | 410 file_value->SetInteger("id", id); |
| 411 | 411 |
| 412 FilePath download_path(download->GetTargetFilePath()); | 412 FilePath download_path(download->GetTargetFilePath()); |
| 413 file_value->Set("file_path", base::CreateFilePathValue(download_path)); | 413 file_value->Set("file_path", base::CreateFilePathValue(download_path)); |
| 414 file_value->SetString("file_url", | 414 file_value->SetString("file_url", |
| 415 net::FilePathToFileURL(download_path).spec()); | 415 net::FilePathToFileURL(download_path).spec()); |
| 416 | 416 |
| 417 // Keep file names as LTR. | 417 // Keep file names as LTR. |
| 418 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); | 418 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); |
| 419 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); | 419 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
| 420 file_value->SetString("file_name", file_name); | 420 file_value->SetString("file_name", file_name); |
| 421 file_value->SetString("url", download->GetURL().spec()); | 421 file_value->SetString("url", download->GetURL().spec()); |
| 422 file_value->SetBoolean("otr", download->is_otr()); | 422 file_value->SetBoolean("otr", download->IsOtr()); |
| 423 file_value->SetInteger("total", static_cast<int>(download->total_bytes())); | 423 file_value->SetInteger("total", static_cast<int>(download->GetTotalBytes())); |
| 424 file_value->SetBoolean("file_externally_removed", | 424 file_value->SetBoolean("file_externally_removed", |
| 425 download->file_externally_removed()); | 425 download->GetFileExternallyRemoved()); |
| 426 | 426 |
| 427 if (download->IsInProgress()) { | 427 if (download->IsInProgress()) { |
| 428 if (download->safety_state() == DownloadItem::DANGEROUS) { | 428 if (download->GetSafetyState() == DownloadItem::DANGEROUS) { |
| 429 file_value->SetString("state", "DANGEROUS"); | 429 file_value->SetString("state", "DANGEROUS"); |
| 430 DCHECK(download->GetDangerType() == DownloadItem::DANGEROUS_FILE || | 430 DCHECK(download->GetDangerType() == DownloadItem::DANGEROUS_FILE || |
| 431 download->GetDangerType() == DownloadItem::DANGEROUS_URL); | 431 download->GetDangerType() == DownloadItem::DANGEROUS_URL); |
| 432 const char* danger_type_value = | 432 const char* danger_type_value = |
| 433 download->GetDangerType() == DownloadItem::DANGEROUS_FILE ? | 433 download->GetDangerType() == DownloadItem::DANGEROUS_FILE ? |
| 434 "DANGEROUS_FILE" : "DANGEROUS_URL"; | 434 "DANGEROUS_FILE" : "DANGEROUS_URL"; |
| 435 file_value->SetString("danger_type", danger_type_value); | 435 file_value->SetString("danger_type", danger_type_value); |
| 436 } else if (download->is_paused()) { | 436 } else if (download->IsPaused()) { |
| 437 file_value->SetString("state", "PAUSED"); | 437 file_value->SetString("state", "PAUSED"); |
| 438 } else { | 438 } else { |
| 439 file_value->SetString("state", "IN_PROGRESS"); | 439 file_value->SetString("state", "IN_PROGRESS"); |
| 440 } | 440 } |
| 441 | 441 |
| 442 file_value->SetString("progress_status_text", | 442 file_value->SetString("progress_status_text", |
| 443 GetProgressStatusText(download)); | 443 GetProgressStatusText(download)); |
| 444 | 444 |
| 445 file_value->SetInteger("percent", | 445 file_value->SetInteger("percent", |
| 446 static_cast<int>(download->PercentComplete())); | 446 static_cast<int>(download->PercentComplete())); |
| 447 file_value->SetInteger("received", | 447 file_value->SetInteger("received", |
| 448 static_cast<int>(download->received_bytes())); | 448 static_cast<int>(download->GetReceivedBytes())); |
| 449 } else if (download->IsInterrupted()) { | 449 } else if (download->IsInterrupted()) { |
| 450 file_value->SetString("state", "INTERRUPTED"); | 450 file_value->SetString("state", "INTERRUPTED"); |
| 451 | 451 |
| 452 file_value->SetString("progress_status_text", | 452 file_value->SetString("progress_status_text", |
| 453 GetProgressStatusText(download)); | 453 GetProgressStatusText(download)); |
| 454 | 454 |
| 455 file_value->SetInteger("percent", | 455 file_value->SetInteger("percent", |
| 456 static_cast<int>(download->PercentComplete())); | 456 static_cast<int>(download->PercentComplete())); |
| 457 file_value->SetInteger("received", | 457 file_value->SetInteger("received", |
| 458 static_cast<int>(download->received_bytes())); | 458 static_cast<int>(download->GetReceivedBytes())); |
| 459 } else if (download->IsCancelled()) { | 459 } else if (download->IsCancelled()) { |
| 460 file_value->SetString("state", "CANCELLED"); | 460 file_value->SetString("state", "CANCELLED"); |
| 461 } else if (download->IsComplete()) { | 461 } else if (download->IsComplete()) { |
| 462 if (download->safety_state() == DownloadItem::DANGEROUS) | 462 if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
| 463 file_value->SetString("state", "DANGEROUS"); | 463 file_value->SetString("state", "DANGEROUS"); |
| 464 else | 464 else |
| 465 file_value->SetString("state", "COMPLETE"); | 465 file_value->SetString("state", "COMPLETE"); |
| 466 } else if (download->state() == DownloadItem::REMOVING) { | 466 } else if (download->GetState() == DownloadItem::REMOVING) { |
| 467 file_value->SetString("state", "REMOVING"); | 467 file_value->SetString("state", "REMOVING"); |
| 468 } else { | 468 } else { |
| 469 NOTREACHED() << "state undefined"; | 469 NOTREACHED() << "state undefined"; |
| 470 } | 470 } |
| 471 | 471 |
| 472 return file_value; | 472 return file_value; |
| 473 } | 473 } |
| 474 | 474 |
| 475 string16 GetProgressStatusText(DownloadItem* download) { | 475 string16 GetProgressStatusText(DownloadItem* download) { |
| 476 int64 total = download->total_bytes(); | 476 int64 total = download->GetTotalBytes(); |
| 477 int64 size = download->received_bytes(); | 477 int64 size = download->GetReceivedBytes(); |
| 478 string16 received_size = ui::FormatBytes(size); | 478 string16 received_size = ui::FormatBytes(size); |
| 479 string16 amount = received_size; | 479 string16 amount = received_size; |
| 480 | 480 |
| 481 // Adjust both strings for the locale direction since we don't yet know which | 481 // Adjust both strings for the locale direction since we don't yet know which |
| 482 // string we'll end up using for constructing the final progress string. | 482 // string we'll end up using for constructing the final progress string. |
| 483 base::i18n::AdjustStringForLocaleDirection(&amount); | 483 base::i18n::AdjustStringForLocaleDirection(&amount); |
| 484 | 484 |
| 485 if (total) { | 485 if (total) { |
| 486 string16 total_text = ui::FormatBytes(total); | 486 string16 total_text = ui::FormatBytes(total); |
| 487 base::i18n::AdjustStringForLocaleDirection(&total_text); | 487 base::i18n::AdjustStringForLocaleDirection(&total_text); |
| 488 | 488 |
| 489 base::i18n::AdjustStringForLocaleDirection(&received_size); | 489 base::i18n::AdjustStringForLocaleDirection(&received_size); |
| 490 amount = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_TAB_PROGRESS_SIZE, | 490 amount = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_TAB_PROGRESS_SIZE, |
| 491 received_size, | 491 received_size, |
| 492 total_text); | 492 total_text); |
| 493 } else { | 493 } else { |
| 494 amount.assign(received_size); | 494 amount.assign(received_size); |
| 495 } | 495 } |
| 496 int64 current_speed = download->CurrentSpeed(); | 496 int64 current_speed = download->CurrentSpeed(); |
| 497 string16 speed_text = ui::FormatSpeed(current_speed); | 497 string16 speed_text = ui::FormatSpeed(current_speed); |
| 498 base::i18n::AdjustStringForLocaleDirection(&speed_text); | 498 base::i18n::AdjustStringForLocaleDirection(&speed_text); |
| 499 | 499 |
| 500 base::TimeDelta remaining; | 500 base::TimeDelta remaining; |
| 501 string16 time_remaining; | 501 string16 time_remaining; |
| 502 if (download->is_paused()) | 502 if (download->IsPaused()) |
| 503 time_remaining = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); | 503 time_remaining = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); |
| 504 else if (download->TimeRemaining(&remaining)) | 504 else if (download->TimeRemaining(&remaining)) |
| 505 time_remaining = TimeFormat::TimeRemaining(remaining); | 505 time_remaining = TimeFormat::TimeRemaining(remaining); |
| 506 | 506 |
| 507 if (time_remaining.empty()) { | 507 if (time_remaining.empty()) { |
| 508 base::i18n::AdjustStringForLocaleDirection(&amount); | 508 base::i18n::AdjustStringForLocaleDirection(&amount); |
| 509 return l10n_util::GetStringFUTF16( | 509 return l10n_util::GetStringFUTF16( |
| 510 IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, speed_text, amount); | 510 IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, speed_text, amount); |
| 511 } | 511 } |
| 512 return l10n_util::GetStringFUTF16(IDS_DOWNLOAD_TAB_PROGRESS_STATUS, | 512 return l10n_util::GetStringFUTF16(IDS_DOWNLOAD_TAB_PROGRESS_STATUS, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 return DownloadFile::GetUniquePathNumberWithSuffix( | 565 return DownloadFile::GetUniquePathNumberWithSuffix( |
| 566 path, FILE_PATH_LITERAL(".crdownload")); | 566 path, FILE_PATH_LITERAL(".crdownload")); |
| 567 } | 567 } |
| 568 | 568 |
| 569 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 569 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
| 570 return DownloadFile::AppendSuffixToPath( | 570 return DownloadFile::AppendSuffixToPath( |
| 571 suggested_path, FILE_PATH_LITERAL(".crdownload")); | 571 suggested_path, FILE_PATH_LITERAL(".crdownload")); |
| 572 } | 572 } |
| 573 | 573 |
| 574 } // namespace download_util | 574 } // namespace download_util |
| OLD | NEW |