1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/downloads_dom_handler.h"
#include <algorithm>
#include <functional>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/i18n/rtl.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/singleton.h"
#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/supports_user_data.h"
#include "base/threading/thread.h"
#include "base/value_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_crx_util.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_query.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/download/drag_download_item.h"
#include "chrome/browser/extensions/api/downloads/downloads_api.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/fileicon_source.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "extensions/browser/extension_system.h"
#include "net/base/filename_util.h"
#include "ui/base/l10n/time_format.h"
#include "ui/gfx/image/image.h"
using base::UserMetricsAction;
using content::BrowserContext;
using content::BrowserThread;
namespace {
// Maximum number of downloads to show. TODO(glen): Remove this and instead
// stuff the downloads down the pipe slowly.
static const size_t kMaxDownloads = 150;
enum DownloadsDOMEvent {
DOWNLOADS_DOM_EVENT_GET_DOWNLOADS = 0,
DOWNLOADS_DOM_EVENT_OPEN_FILE = 1,
DOWNLOADS_DOM_EVENT_DRAG = 2,
DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS = 3,
DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS = 4,
DOWNLOADS_DOM_EVENT_SHOW = 5,
DOWNLOADS_DOM_EVENT_PAUSE = 6,
DOWNLOADS_DOM_EVENT_REMOVE = 7,
DOWNLOADS_DOM_EVENT_CANCEL = 8,
DOWNLOADS_DOM_EVENT_CLEAR_ALL = 9,
DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10,
DOWNLOADS_DOM_EVENT_RESUME = 11,
DOWNLOADS_DOM_EVENT_MAX
};
static const char kKey[] = "DownloadsDOMHandlerData";
class DownloadsDOMHandlerData : public base::SupportsUserData::Data {
public:
static DownloadsDOMHandlerData* Get(content::DownloadItem* item) {
return static_cast<DownloadsDOMHandlerData*>(item->GetUserData(kKey));
}
static const DownloadsDOMHandlerData* Get(const content::DownloadItem* item) {
return static_cast<DownloadsDOMHandlerData*>(item->GetUserData(kKey));
}
static void Set(content::DownloadItem* item, DownloadsDOMHandlerData* data) {
item->SetUserData(kKey, data);
}
static DownloadsDOMHandlerData* Create(content::DownloadItem* item) {
DownloadsDOMHandlerData* data = new DownloadsDOMHandlerData;
item->SetUserData(kKey, data);
return data;
}
void set_is_removed(bool is_removed) { is_removed_ = is_removed; }
bool is_removed() const { return is_removed_; }
private:
bool is_removed_;
};
void CountDownloadsDOMEvents(DownloadsDOMEvent event) {
UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent",
event,
DOWNLOADS_DOM_EVENT_MAX);
}
// Returns a string constant to be used as the |danger_type| value in
// CreateDownloadItemValue(). Only return strings for DANGEROUS_FILE,
// DANGEROUS_URL, DANGEROUS_CONTENT, and UNCOMMON_CONTENT because the
// |danger_type| value is only defined if the value of |state| is |DANGEROUS|.
const char* GetDangerTypeString(content::DownloadDangerType danger_type) {
switch (danger_type) {
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE:
return "DANGEROUS_FILE";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
return "DANGEROUS_URL";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
return "DANGEROUS_CONTENT";
case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT:
return "UNCOMMON_CONTENT";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
return "DANGEROUS_HOST";
case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED:
return "POTENTIALLY_UNWANTED";
default:
// Don't return a danger type string if it is NOT_DANGEROUS or
// MAYBE_DANGEROUS_CONTENT.
NOTREACHED();
return "";
}
}
// Returns a JSON dictionary containing some of the attributes of |download|.
// The JSON dictionary will also have a field "id" set to |id|, and a field
// "otr" set to |incognito|.
base::DictionaryValue* CreateDownloadItemValue(
content::DownloadItem* download_item,
bool incognito) {
// TODO(asanka): Move towards using download_model here for getting status and
// progress. The difference currently only matters to Drive downloads and
// those don't show up on the downloads page, but should.
DownloadItemModel download_model(download_item);
// The items which are to be written into file_value are also described in
// chrome/browser/resources/downloads/downloads.js in @typedef for
// BackendDownloadObject. Please update it whenever you add or remove
// any keys in file_value.
base::DictionaryValue* file_value = new base::DictionaryValue();
file_value->SetInteger(
"started", static_cast<int>(download_item->GetStartTime().ToTimeT()));
file_value->SetString(
"since_string", ui::TimeFormat::RelativeDate(
download_item->GetStartTime(), NULL));
file_value->SetString(
"date_string", base::TimeFormatShortDate(download_item->GetStartTime()));
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));
file_value->SetString("file_url",
net::FilePathToFileURL(download_path).spec());
extensions::DownloadedByExtension* by_ext =
extensions::DownloadedByExtension::Get(download_item);
if (by_ext) {
file_value->SetString("by_ext_id", by_ext->id());
file_value->SetString("by_ext_name", by_ext->name());
// Lookup the extension's current name() in case the user changed their
// language. This won't work if the extension was uninstalled, so the name
// might be the wrong language.
bool include_disabled = true;
const extensions::Extension* extension = extensions::ExtensionSystem::Get(
Profile::FromBrowserContext(download_item->GetBrowserContext()))->
extension_service()->GetExtensionById(by_ext->id(), include_disabled);
if (extension)
file_value->SetString("by_ext_name", extension->name());
}
// Keep file names as LTR.
base::string16 file_name =
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());
file_value->SetBoolean("otr", incognito);
file_value->SetInteger("total", static_cast<int>(
download_item->GetTotalBytes()));
file_value->SetBoolean("file_externally_removed",
download_item->GetFileExternallyRemoved());
file_value->SetBoolean("retry", false); // Overridden below if needed.
file_value->SetBoolean("resume", download_item->CanResume());
switch (download_item->GetState()) {
case content::DownloadItem::IN_PROGRESS:
if (download_item->IsDangerous()) {
file_value->SetString("state", "DANGEROUS");
// These are the only danger states that the UI is equipped to handle.
DCHECK(download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED);
const char* danger_type_value =
GetDangerTypeString(download_item->GetDangerType());
file_value->SetString("danger_type", danger_type_value);
} else if (download_item->IsPaused()) {
file_value->SetString("state", "PAUSED");
} else {
file_value->SetString("state", "IN_PROGRESS");
}
file_value->SetString("progress_status_text",
download_model.GetTabProgressStatusText());
file_value->SetInteger("percent",
static_cast<int>(download_item->PercentComplete()));
file_value->SetInteger("received",
static_cast<int>(download_item->GetReceivedBytes()));
break;
case content::DownloadItem::INTERRUPTED:
file_value->SetString("state", "INTERRUPTED");
file_value->SetString("progress_status_text",
download_model.GetTabProgressStatusText());
file_value->SetInteger("percent",
static_cast<int>(download_item->PercentComplete()));
file_value->SetInteger("received",
static_cast<int>(download_item->GetReceivedBytes()));
file_value->SetString("last_reason_text",
download_model.GetInterruptReasonText());
if (content::DOWNLOAD_INTERRUPT_REASON_CRASH ==
download_item->GetLastReason() && !download_item->CanResume())
file_value->SetBoolean("retry", true);
break;
case content::DownloadItem::CANCELLED:
file_value->SetString("state", "CANCELLED");
file_value->SetBoolean("retry", true);
break;
case content::DownloadItem::COMPLETE:
DCHECK(!download_item->IsDangerous());
file_value->SetString("state", "COMPLETE");
break;
case content::DownloadItem::MAX_DOWNLOAD_STATE:
NOTREACHED();
}
return file_value;
}
bool IsRemoved(const content::DownloadItem& item) {
const DownloadsDOMHandlerData* data = DownloadsDOMHandlerData::Get(&item);
return data && data->is_removed();
}
// Filters out extension downloads and downloads that don't have a filename yet.
bool IsDownloadDisplayable(const content::DownloadItem& item) {
return !download_crx_util::IsExtensionDownload(item) &&
!item.IsTemporary() &&
!item.GetFileNameToReportUser().empty() &&
!item.GetTargetFilePath().empty() &&
!IsRemoved(item);
}
} // namespace
DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm)
: main_notifier_(dlm, this),
update_scheduled_(false),
weak_ptr_factory_(this) {
// Create our fileicon data source.
Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext());
content::URLDataSource::Add(profile, new FileIconSource());
if (profile->IsOffTheRecord()) {
original_notifier_.reset(new AllDownloadItemNotifier(
BrowserContext::GetDownloadManager(profile->GetOriginalProfile()),
this));
}
}
DownloadsDOMHandler::~DownloadsDOMHandler() {
while (!removes_.empty()) {
const std::set<uint32> remove = removes_.back();
removes_.pop_back();
for (const auto id : remove) {
content::DownloadItem* download = GetDownloadById(id);
if (download)
download->Remove();
}
}
}
// DownloadsDOMHandler, public: -----------------------------------------------
void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) {
SendCurrentDownloads();
}
void DownloadsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("onPageLoaded",
base::Bind(&DownloadsDOMHandler::OnPageLoaded,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("getDownloads",
base::Bind(&DownloadsDOMHandler::HandleGetDownloads,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("openFile",
base::Bind(&DownloadsDOMHandler::HandleOpenFile,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("drag",
base::Bind(&DownloadsDOMHandler::HandleDrag,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("saveDangerous",
base::Bind(&DownloadsDOMHandler::HandleSaveDangerous,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("discardDangerous",
base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("show",
base::Bind(&DownloadsDOMHandler::HandleShow,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("pause",
base::Bind(&DownloadsDOMHandler::HandlePause,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("resume",
base::Bind(&DownloadsDOMHandler::HandleResume,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("remove",
base::Bind(&DownloadsDOMHandler::HandleRemove,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("undo",
base::Bind(&DownloadsDOMHandler::HandleUndo,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("cancel",
base::Bind(&DownloadsDOMHandler::HandleCancel,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("clearAll",
base::Bind(&DownloadsDOMHandler::HandleClearAll,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("openDownloadsFolder",
base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder,
weak_ptr_factory_.GetWeakPtr()));
}
void DownloadsDOMHandler::OnDownloadCreated(
content::DownloadManager* manager, content::DownloadItem* download_item) {
if (IsDownloadDisplayable(*download_item))
ScheduleSendCurrentDownloads();
}
void DownloadsDOMHandler::OnDownloadUpdated(
content::DownloadManager* manager,
content::DownloadItem* download_item) {
if (IsDownloadDisplayable(*download_item)) {
if (search_terms_ && !search_terms_->empty()) {
// Don't CallDownloadUpdated() if download_item doesn't match
// search_terms_.
// TODO(benjhayden): Consider splitting MatchesQuery() out to a function.
content::DownloadManager::DownloadVector all_items, filtered_items;
all_items.push_back(download_item);
DownloadQuery query;
query.AddFilter(DownloadQuery::FILTER_QUERY, *search_terms_.get());
query.Search(all_items.begin(), all_items.end(), &filtered_items);
if (filtered_items.empty())
return;
}
base::ListValue results_value;
results_value.Append(CreateDownloadItemValue(
download_item,
(original_notifier_.get() &&
(manager == main_notifier_.GetManager()))));
CallDownloadUpdated(results_value);
} else {
ScheduleSendCurrentDownloads();
}
}
void DownloadsDOMHandler::OnDownloadRemoved(
content::DownloadManager* manager,
content::DownloadItem* download_item) {
if (IsRemoved(*download_item))
return;
// This relies on |download_item| being removed from DownloadManager in this
// MessageLoop iteration. |download_item| may not have been removed from
// DownloadManager when OnDownloadRemoved() is fired, so bounce off the
// MessageLoop to give it a chance to be removed. SendCurrentDownloads() looks
// at all downloads, and we do not tell it that |download_item| is being
// removed. If DownloadManager is ever changed to not immediately remove
// |download_item| from its map when OnDownloadRemoved is sent, then
// DownloadsDOMHandler::OnDownloadRemoved() will need to explicitly tell
// SendCurrentDownloads() that |download_item| was removed. A
// SupportsUserData::Data would be the correct way to do this.
ScheduleSendCurrentDownloads();
}
void DownloadsDOMHandler::HandleGetDownloads(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS);
search_terms_.reset((args && !args->empty()) ? args->DeepCopy() : NULL);
SendCurrentDownloads();
}
void DownloadsDOMHandler::HandleOpenFile(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->OpenDownload();
}
void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG);
content::DownloadItem* file = GetDownloadByValue(args);
if (!file)
return;
content::WebContents* web_contents = GetWebUIWebContents();
// |web_contents| is only NULL in the test.
if (!web_contents)
return;
if (file->GetState() != content::DownloadItem::COMPLETE)
return;
gfx::Image* icon = g_browser_process->icon_manager()->LookupIconFromFilepath(
file->GetTargetFilePath(), IconLoader::NORMAL);
gfx::NativeView view = web_contents->GetNativeView();
{
// Enable nested tasks during DnD, while |DragDownload()| blocks.
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
DragDownloadItem(file, icon, view);
}
}
void DownloadsDOMHandler::HandleSaveDangerous(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
ShowDangerPrompt(file);
}
void DownloadsDOMHandler::HandleDiscardDangerous(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Remove();
}
void DownloadsDOMHandler::HandleShow(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->ShowDownloadInShell();
}
void DownloadsDOMHandler::HandlePause(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Pause();
}
void DownloadsDOMHandler::HandleResume(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_RESUME);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Resume();
}
void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) {
if (!IsDeletingHistoryAllowed())
return;
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE);
content::DownloadItem* file = GetDownloadByValue(args);
if (!file)
return;
std::vector<content::DownloadItem*> downloads;
downloads.push_back(file);
RemoveDownloads(downloads);
}
void DownloadsDOMHandler::HandleUndo(const base::ListValue* args) {
// TODO(dbeam): handle more than removed downloads someday?
if (removes_.empty())
return;
const std::set<uint32> last_removed_ids = removes_.back();
removes_.pop_back();
for (auto id : last_removed_ids) {
content::DownloadItem* download = GetDownloadById(id);
if (!download)
continue;
DownloadsDOMHandlerData::Set(download, nullptr);
download->UpdateObservers();
}
}
void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Cancel(true);
}
void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) {
if (!IsDeletingHistoryAllowed())
return;
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL);
std::vector<content::DownloadItem*> downloads;
if (GetMainNotifierManager())
GetMainNotifierManager()->GetAllDownloads(&downloads);
if (original_notifier_ && original_notifier_->GetManager())
original_notifier_->GetManager()->GetAllDownloads(&downloads);
RemoveDownloads(downloads);
}
void DownloadsDOMHandler::RemoveDownloads(
const std::vector<content::DownloadItem*>& to_remove) {
std::set<uint32> ids;
for (auto* download : to_remove) {
if (IsRemoved(*download) ||
download->GetState() == content::DownloadItem::IN_PROGRESS) {
continue;
}
DownloadsDOMHandlerData::Create(download)->set_is_removed(true);
ids.insert(download->GetId());
download->UpdateObservers();
}
removes_.push_back(ids);
}
void DownloadsDOMHandler::HandleOpenDownloadsFolder(
const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER);
content::DownloadManager* manager = main_notifier_.GetManager();
if (manager) {
platform_util::OpenItem(
Profile::FromBrowserContext(manager->GetBrowserContext()),
DownloadPrefs::FromDownloadManager(manager)->DownloadPath());
}
}
// DownloadsDOMHandler, private: ----------------------------------------------
void DownloadsDOMHandler::ScheduleSendCurrentDownloads() {
// Don't call SendCurrentDownloads() every time anything changes. Batch them
// together instead. This may handle hundreds of OnDownloadDestroyed() calls
// in a single UI message loop iteration when the user Clears All downloads.
if (update_scheduled_)
return;
update_scheduled_ = true;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DownloadsDOMHandler::SendCurrentDownloads,
weak_ptr_factory_.GetWeakPtr()));
}
content::DownloadManager* DownloadsDOMHandler::GetMainNotifierManager() {
return main_notifier_.GetManager();
}
void DownloadsDOMHandler::SendCurrentDownloads() {
update_scheduled_ = false;
content::DownloadManager::DownloadVector all_items, filtered_items;
if (main_notifier_.GetManager()) {
main_notifier_.GetManager()->GetAllDownloads(&all_items);
main_notifier_.GetManager()->CheckForHistoryFilesRemoval();
}
if (original_notifier_.get() && original_notifier_->GetManager()) {
original_notifier_->GetManager()->GetAllDownloads(&all_items);
original_notifier_->GetManager()->CheckForHistoryFilesRemoval();
}
DownloadQuery query;
if (search_terms_ && !search_terms_->empty()) {
query.AddFilter(DownloadQuery::FILTER_QUERY, *search_terms_.get());
}
query.AddFilter(base::Bind(&IsDownloadDisplayable));
query.AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
query.Limit(kMaxDownloads);
query.Search(all_items.begin(), all_items.end(), &filtered_items);
base::ListValue results_value;
for (content::DownloadManager::DownloadVector::iterator
iter = filtered_items.begin(); iter != filtered_items.end(); ++iter) {
results_value.Append(CreateDownloadItemValue(
*iter,
(original_notifier_.get() &&
main_notifier_.GetManager() &&
(main_notifier_.GetManager()->GetDownload((*iter)->GetId()) ==
*iter))));
}
CallDownloadsList(results_value);
}
void DownloadsDOMHandler::ShowDangerPrompt(
content::DownloadItem* dangerous_item) {
DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create(
dangerous_item,
GetWebUIWebContents(),
false,
base::Bind(&DownloadsDOMHandler::DangerPromptDone,
weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()));
// danger_prompt will delete itself.
DCHECK(danger_prompt);
}
void DownloadsDOMHandler::DangerPromptDone(
int download_id, DownloadDangerPrompt::Action action) {
if (action != DownloadDangerPrompt::ACCEPT)
return;
content::DownloadItem* item = NULL;
if (main_notifier_.GetManager())
item = main_notifier_.GetManager()->GetDownload(download_id);
if (!item && original_notifier_.get() && original_notifier_->GetManager())
item = original_notifier_->GetManager()->GetDownload(download_id);
if (!item || item->IsDone())
return;
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS);
item->ValidateDangerousDownload();
}
bool DownloadsDOMHandler::IsDeletingHistoryAllowed() {
content::DownloadManager* manager = main_notifier_.GetManager();
return (manager &&
Profile::FromBrowserContext(manager->GetBrowserContext())->
GetPrefs()->GetBoolean(prefs::kAllowDeletingBrowserHistory));
}
content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue(
const base::ListValue* args) {
std::string download_id;
if (!args->GetString(0, &download_id)) {
NOTREACHED();
return nullptr;
}
uint64 id;
if (!base::StringToUint64(download_id, &id)) {
NOTREACHED();
return nullptr;
}
return GetDownloadById(static_cast<uint32>(id));
}
content::DownloadItem* DownloadsDOMHandler::GetDownloadById(uint32 id) {
content::DownloadItem* item = NULL;
if (main_notifier_.GetManager())
item = main_notifier_.GetManager()->GetDownload(id);
if (!item && original_notifier_.get() && original_notifier_->GetManager())
item = original_notifier_->GetManager()->GetDownload(id);
return item;
}
content::WebContents* DownloadsDOMHandler::GetWebUIWebContents() {
return web_ui()->GetWebContents();
}
void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) {
web_ui()->CallJavascriptFunction("downloadsList", downloads);
}
void DownloadsDOMHandler::CallDownloadUpdated(
const base::ListValue& download_item) {
web_ui()->CallJavascriptFunction("downloadUpdated", download_item);
}
|