| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
| 11 #endif | 11 #endif |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "app/l10n_util.h" | 14 #include "app/l10n_util.h" |
| 15 #include "app/resource_bundle.h" | 15 #include "app/resource_bundle.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/i18n/rtl.h" | 17 #include "base/i18n/rtl.h" |
| 18 #include "base/i18n/time_formatting.h" | 18 #include "base/i18n/time_formatting.h" |
| 19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 20 #include "base/singleton.h" | 20 #include "base/singleton.h" |
| 21 #include "base/string16.h" |
| 21 #include "base/string_number_conversions.h" | 22 #include "base/string_number_conversions.h" |
| 22 #include "base/sys_string_conversions.h" | 23 #include "base/sys_string_conversions.h" |
| 23 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
| 24 #include "base/values.h" | 25 #include "base/values.h" |
| 25 #include "chrome/browser/browser.h" | 26 #include "chrome/browser/browser.h" |
| 26 #include "chrome/browser/browser_list.h" | 27 #include "chrome/browser/browser_list.h" |
| 27 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
| 28 #include "chrome/browser/chrome_thread.h" | 29 #include "chrome/browser/chrome_thread.h" |
| 29 #include "chrome/browser/download/download_item.h" | 30 #include "chrome/browser/download/download_item.h" |
| 30 #include "chrome/browser/download/download_item_model.h" | 31 #include "chrome/browser/download/download_item_model.h" |
| (...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 void DragDownload(const DownloadItem* download, | 531 void DragDownload(const DownloadItem* download, |
| 531 SkBitmap* icon, | 532 SkBitmap* icon, |
| 532 gfx::NativeView view) { | 533 gfx::NativeView view) { |
| 533 DownloadItemDrag::BeginDrag(download, icon); | 534 DownloadItemDrag::BeginDrag(download, icon); |
| 534 } | 535 } |
| 535 #endif // USE_X11 | 536 #endif // USE_X11 |
| 536 | 537 |
| 537 DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { | 538 DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { |
| 538 DictionaryValue* file_value = new DictionaryValue(); | 539 DictionaryValue* file_value = new DictionaryValue(); |
| 539 | 540 |
| 540 file_value->SetInteger(L"started", | 541 file_value->SetInteger("started", |
| 541 static_cast<int>(download->start_time().ToTimeT())); | 542 static_cast<int>(download->start_time().ToTimeT())); |
| 542 file_value->SetString(L"since_string", | 543 file_value->SetString("since_string", |
| 543 TimeFormat::RelativeDate(download->start_time(), NULL)); | 544 TimeFormat::RelativeDate(download->start_time(), NULL)); |
| 544 file_value->SetString(L"date_string", | 545 file_value->SetString("date_string", |
| 545 base::TimeFormatShortDate(download->start_time())); | 546 WideToUTF16Hack(base::TimeFormatShortDate(download->start_time()))); |
| 546 file_value->SetInteger(L"id", id); | 547 file_value->SetInteger("id", id); |
| 547 file_value->SetString(L"file_path", download->full_path().ToWStringHack()); | 548 file_value->SetString("file_path", |
| 549 WideToUTF16Hack(download->full_path().ToWStringHack())); |
| 548 // Keep file names as LTR. | 550 // Keep file names as LTR. |
| 549 std::wstring file_name = download->GetFileName().ToWStringHack(); | 551 std::wstring file_name = download->GetFileName().ToWStringHack(); |
| 550 base::i18n::GetDisplayStringInLTRDirectionality(&file_name); | 552 base::i18n::GetDisplayStringInLTRDirectionality(&file_name); |
| 551 file_value->SetString(L"file_name", file_name); | 553 file_value->SetString("file_name", WideToUTF16Hack(file_name)); |
| 552 file_value->SetString(L"url", download->url().spec()); | 554 file_value->SetString("url", download->url().spec()); |
| 553 file_value->SetBoolean(L"otr", download->is_otr()); | 555 file_value->SetBoolean("otr", download->is_otr()); |
| 554 | 556 |
| 555 if (download->state() == DownloadItem::IN_PROGRESS) { | 557 if (download->state() == DownloadItem::IN_PROGRESS) { |
| 556 if (download->safety_state() == DownloadItem::DANGEROUS) { | 558 if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 557 file_value->SetString(L"state", L"DANGEROUS"); | 559 file_value->SetString("state", "DANGEROUS"); |
| 558 } else if (download->is_paused()) { | 560 } else if (download->is_paused()) { |
| 559 file_value->SetString(L"state", L"PAUSED"); | 561 file_value->SetString("state", "PAUSED"); |
| 560 } else { | 562 } else { |
| 561 file_value->SetString(L"state", L"IN_PROGRESS"); | 563 file_value->SetString("state", "IN_PROGRESS"); |
| 562 } | 564 } |
| 563 | 565 |
| 564 file_value->SetString(L"progress_status_text", | 566 file_value->SetString("progress_status_text", |
| 565 GetProgressStatusText(download)); | 567 WideToUTF16Hack(GetProgressStatusText(download))); |
| 566 | 568 |
| 567 file_value->SetInteger(L"percent", | 569 file_value->SetInteger("percent", |
| 568 static_cast<int>(download->PercentComplete())); | 570 static_cast<int>(download->PercentComplete())); |
| 569 file_value->SetInteger(L"received", | 571 file_value->SetInteger("received", |
| 570 static_cast<int>(download->received_bytes())); | 572 static_cast<int>(download->received_bytes())); |
| 571 } else if (download->state() == DownloadItem::CANCELLED) { | 573 } else if (download->state() == DownloadItem::CANCELLED) { |
| 572 file_value->SetString(L"state", L"CANCELLED"); | 574 file_value->SetString("state", "CANCELLED"); |
| 573 } else if (download->state() == DownloadItem::COMPLETE) { | 575 } else if (download->state() == DownloadItem::COMPLETE) { |
| 574 if (download->safety_state() == DownloadItem::DANGEROUS) { | 576 if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 575 file_value->SetString(L"state", L"DANGEROUS"); | 577 file_value->SetString("state", "DANGEROUS"); |
| 576 } else { | 578 } else { |
| 577 file_value->SetString(L"state", L"COMPLETE"); | 579 file_value->SetString("state", "COMPLETE"); |
| 578 } | 580 } |
| 579 } | 581 } |
| 580 | 582 |
| 581 file_value->SetInteger(L"total", | 583 file_value->SetInteger("total", |
| 582 static_cast<int>(download->total_bytes())); | 584 static_cast<int>(download->total_bytes())); |
| 583 | 585 |
| 584 return file_value; | 586 return file_value; |
| 585 } | 587 } |
| 586 | 588 |
| 587 std::wstring GetProgressStatusText(DownloadItem* download) { | 589 std::wstring GetProgressStatusText(DownloadItem* download) { |
| 588 int64 total = download->total_bytes(); | 590 int64 total = download->total_bytes(); |
| 589 int64 size = download->received_bytes(); | 591 int64 size = download->received_bytes(); |
| 590 DataUnits amount_units = GetByteDisplayUnits(size); | 592 DataUnits amount_units = GetByteDisplayUnits(size); |
| 591 std::wstring received_size = FormatBytes(size, amount_units, true); | 593 std::wstring received_size = FormatBytes(size, amount_units, true); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 615 } | 617 } |
| 616 amount_units = GetByteDisplayUnits(download->CurrentSpeed()); | 618 amount_units = GetByteDisplayUnits(download->CurrentSpeed()); |
| 617 std::wstring speed_text = FormatSpeed(download->CurrentSpeed(), | 619 std::wstring speed_text = FormatSpeed(download->CurrentSpeed(), |
| 618 amount_units, true); | 620 amount_units, true); |
| 619 std::wstring speed_text_localized; | 621 std::wstring speed_text_localized; |
| 620 if (base::i18n::AdjustStringForLocaleDirection(speed_text, | 622 if (base::i18n::AdjustStringForLocaleDirection(speed_text, |
| 621 &speed_text_localized)) | 623 &speed_text_localized)) |
| 622 speed_text.assign(speed_text_localized); | 624 speed_text.assign(speed_text_localized); |
| 623 | 625 |
| 624 base::TimeDelta remaining; | 626 base::TimeDelta remaining; |
| 625 std::wstring time_remaining; | 627 string16 time_remaining; |
| 626 if (download->is_paused()) | 628 if (download->is_paused()) |
| 627 time_remaining = l10n_util::GetString(IDS_DOWNLOAD_PROGRESS_PAUSED); | 629 time_remaining = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); |
| 628 else if (download->TimeRemaining(&remaining)) | 630 else if (download->TimeRemaining(&remaining)) |
| 629 time_remaining = TimeFormat::TimeRemaining(remaining); | 631 time_remaining = TimeFormat::TimeRemaining(remaining); |
| 630 | 632 |
| 631 if (time_remaining.empty()) { | 633 if (time_remaining.empty()) { |
| 632 return l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, | 634 return l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, |
| 633 speed_text, amount); | 635 speed_text, amount); |
| 634 } | 636 } |
| 635 return l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_STATUS, speed_text, | 637 return l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_STATUS, speed_text, |
| 636 amount, time_remaining); | 638 amount, UTF16ToWideHack(time_remaining)); |
| 637 } | 639 } |
| 638 | 640 |
| 639 #if !defined(OS_MACOSX) | 641 #if !defined(OS_MACOSX) |
| 640 void UpdateAppIconDownloadProgress(int download_count, | 642 void UpdateAppIconDownloadProgress(int download_count, |
| 641 bool progress_known, | 643 bool progress_known, |
| 642 float progress) { | 644 float progress) { |
| 643 #if defined(OS_WIN) | 645 #if defined(OS_WIN) |
| 644 // Taskbar progress bar is only supported on Win7. | 646 // Taskbar progress bar is only supported on Win7. |
| 645 if (win_util::GetWinVersion() < win_util::WINVERSION_WIN7) | 647 if (win_util::GetWinVersion() < win_util::WINVERSION_WIN7) |
| 646 return; | 648 return; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 } | 754 } |
| 753 | 755 |
| 754 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 756 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
| 755 FilePath::StringType file_name; | 757 FilePath::StringType file_name; |
| 756 SStringPrintf(&file_name, PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), | 758 SStringPrintf(&file_name, PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), |
| 757 suggested_path.value().c_str()); | 759 suggested_path.value().c_str()); |
| 758 return FilePath(file_name); | 760 return FilePath(file_name); |
| 759 } | 761 } |
| 760 | 762 |
| 761 } // namespace download_util | 763 } // namespace download_util |
| OLD | NEW |