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