Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1956)

Unified Diff: chrome/browser/ui/webui/downloads_dom_handler.cc

Issue 722953002: downloads: add the ability to undo download removal. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: dcheck Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/downloads_dom_handler.cc
diff --git a/chrome/browser/ui/webui/downloads_dom_handler.cc b/chrome/browser/ui/webui/downloads_dom_handler.cc
index f347ba33b455b8cfb0f31555b804c19ea5752ca5..f5638e880ac7c9377a068e46d8c5e68c100b1fa4 100644
--- a/chrome/browser/ui/webui/downloads_dom_handler.cc
+++ b/chrome/browser/ui/webui/downloads_dom_handler.cc
@@ -16,6 +16,7 @@
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
+#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
@@ -131,7 +132,7 @@ base::DictionaryValue* CreateDownloadItemValue(
download_item->GetStartTime(), NULL));
file_value->SetString(
"date_string", base::TimeFormatShortDate(download_item->GetStartTime()));
- file_value->SetInteger("id", download_item->GetId());
+ file_value->SetString("id", base::Uint64ToString(download_item->GetId()));
base::FilePath download_path(download_item->GetTargetFilePath());
file_value->Set("file_path", base::CreateFilePathValue(download_path));
@@ -156,7 +157,7 @@ base::DictionaryValue* CreateDownloadItemValue(
// Keep file names as LTR.
base::string16 file_name =
- download_item->GetFileNameToReportUser().LossyDisplayName();
+ download_item->GetFileNameToReportUser().LossyDisplayName();
file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name);
file_value->SetString("file_name", file_name);
file_value->SetString("url", download_item->GetURL().spec());
@@ -230,7 +231,7 @@ base::DictionaryValue* CreateDownloadItemValue(
break;
case content::DownloadItem::MAX_DOWNLOAD_STATE:
- NOTREACHED() << "state undefined";
+ NOTREACHED();
}
return file_value;
@@ -249,6 +250,7 @@ bool IsDownloadDisplayable(const content::DownloadItem& item) {
DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm)
: main_notifier_(dlm, this),
update_scheduled_(false),
+ last_removed_id_(content::DownloadItem::kInvalidId),
weak_ptr_factory_(this) {
// Create our fileicon data source.
Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext());
@@ -262,6 +264,7 @@ DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm)
}
DownloadsDOMHandler::~DownloadsDOMHandler() {
+ FinalizeRemoval();
}
// DownloadsDOMHandler, public: -----------------------------------------------
@@ -310,6 +313,9 @@ void DownloadsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("openDownloadsFolder",
base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder,
weak_ptr_factory_.GetWeakPtr()));
+ web_ui()->RegisterMessageCallback("undoRemove",
+ base::Bind(&DownloadsDOMHandler::HandleUndoRemove,
+ weak_ptr_factory_.GetWeakPtr()));
}
void DownloadsDOMHandler::OnDownloadCreated(
@@ -438,8 +444,11 @@ void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE);
content::DownloadItem* file = GetDownloadByValue(args);
- if (file)
+ if (file) {
+ FinalizeRemoval();
+ last_removed_id_ = file->GetId();
file->Remove();
+ }
}
void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) {
@@ -480,6 +489,18 @@ void DownloadsDOMHandler::HandleOpenDownloadsFolder(
}
}
+void DownloadsDOMHandler::HandleUndoRemove(const base::ListValue* args) {
+ if (last_removed_id_ == content::DownloadItem::kInvalidId)
+ return;
+
+ if (main_notifier_.GetManager())
+ main_notifier_.GetManager()->ReviveDownload(last_removed_id_);
+ if (original_notifier_ && original_notifier_->GetManager())
+ original_notifier_->GetManager()->ReviveDownload(last_removed_id_);
+
+ last_removed_id_ = content::DownloadItem::kInvalidId;
+}
+
// DownloadsDOMHandler, private: ----------------------------------------------
void DownloadsDOMHandler::ScheduleSendCurrentDownloads() {
@@ -561,16 +582,37 @@ bool DownloadsDOMHandler::IsDeletingHistoryAllowed() {
GetPrefs()->GetBoolean(prefs::kAllowDeletingBrowserHistory));
}
+void DownloadsDOMHandler::FinalizeRemoval() {
+ if (last_removed_id_ == content::DownloadItem::kInvalidId)
+ return;
+
+ if (main_notifier_.GetManager())
+ main_notifier_.GetManager()->FinalizeRemoval(last_removed_id_);
+ if (original_notifier_ && original_notifier_->GetManager())
+ original_notifier_->GetManager()->FinalizeRemoval(last_removed_id_);
+
+ last_removed_id_ = content::DownloadItem::kInvalidId;
+}
+
content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue(
const base::ListValue* args) {
- int download_id = -1;
- if (!ExtractIntegerValue(args, &download_id))
+ std::string download_id;
+ if (!args->GetString(0, &download_id)) {
+ NOTREACHED();
return NULL;
+ }
+
+ uint64 id;
+ if (!base::StringToUint64(download_id, &id)) {
+ NOTREACHED();
+ return NULL;
+ }
+
content::DownloadItem* item = NULL;
if (main_notifier_.GetManager())
- item = main_notifier_.GetManager()->GetDownload(download_id);
+ item = main_notifier_.GetManager()->GetDownload(id);
if (!item && original_notifier_.get() && original_notifier_->GetManager())
- item = original_notifier_->GetManager()->GetDownload(download_id);
+ item = original_notifier_->GetManager()->GetDownload(id);
return item;
}

Powered by Google App Engine
This is Rietveld 408576698