blob: bae409afd1f90f01eb4f34cb9e4f86e77bd4f96d [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]98d6f152011-09-29 19:35:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_download_manager_delegate.h"
[email protected]98d6f152011-09-29 19:35:516
David Vallet11683522017-08-23 03:20:467#include <string>
8
[email protected]5c12f4a2011-09-30 21:59:049#if defined(OS_WIN)
10#include <windows.h>
11#include <commdlg.h>
12#endif
13
[email protected]98d6f152011-09-29 19:35:5114#include "base/bind.h"
Hans Wennborg0917de892020-04-28 20:21:1515#include "base/check_op.h"
[email protected]e6669302013-03-25 14:31:1616#include "base/command_line.h"
thestigb7aad54f2014-09-05 18:25:3917#include "base/files/file_util.h"
Hans Wennborg0917de892020-04-28 20:21:1518#include "base/notreached.h"
Avi Drissman9d3ded92018-12-25 20:50:2119#include "base/stl_util.h"
[email protected]21aa99682013-06-11 07:17:0120#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0021#include "base/strings/utf_string_conversions.h"
Gabriel Charette5e2e72042020-02-25 01:04:0122#include "base/task/thread_pool.h"
avi66a07722015-12-25 23:38:1223#include "build/build_config.h"
[email protected]ccb797302011-12-15 16:55:1124#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2825#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4926#include "content/public/browser/browser_thread.h"
Min Qina9f487872018-02-09 20:43:2327#include "content/public/browser/download_item_utils.h"
[email protected]e582fdd2011-12-20 16:48:1728#include "content/public/browser/download_manager.h"
[email protected]0b659b32012-03-26 21:29:3229#include "content/public/browser/web_contents.h"
[email protected]b7c504c2013-05-07 14:42:1230#include "content/shell/common/shell_switches.h"
[email protected]d96cf752014-04-09 04:05:2831#include "net/base/filename_util.h"
[email protected]98d6f152011-09-29 19:35:5132
[email protected]517fce722014-01-15 05:16:1433#if defined(OS_WIN)
[email protected]517fce722014-01-15 05:16:1434#include "ui/aura/window.h"
[email protected]7a60cd3a2014-03-20 20:54:5735#include "ui/aura/window_tree_host.h"
[email protected]517fce722014-01-15 05:16:1436#endif
37
[email protected]98d6f152011-09-29 19:35:5138namespace content {
39
40ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
Jeremy Roman3bca4bf2019-07-11 03:41:2541 : download_manager_(nullptr), suppress_prompting_(false) {}
[email protected]98d6f152011-09-29 19:35:5142
Asanka Herath1ba0e9f2017-04-03 18:48:5343ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3744 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2845 download_manager_->SetDelegate(nullptr);
46 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3747 }
[email protected]98d6f152011-09-29 19:35:5148}
49
50
51void ShellDownloadManagerDelegate::SetDownloadManager(
52 DownloadManager* download_manager) {
53 download_manager_ = download_manager;
54}
55
[email protected]854e1312012-07-30 17:26:3056void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3757 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
58 // to access after this point.
59 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2860 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3061}
62
[email protected]47665442012-07-27 02:31:2263bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2364 download::DownloadItem* download,
danakja3cfb8332019-12-10 21:13:3365 DownloadTargetCallback* callback) {
mostynb4e363892015-03-23 14:35:0566 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2767 // This assignment needs to be here because even at the call to
68 // SetDownloadManager, the system is not fully initialized.
69 if (default_download_path_.empty()) {
70 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
71 Append(FILE_PATH_LITERAL("Downloads"));
72 }
73
[email protected]3d833de2012-05-30 23:32:0674 if (!download->GetForcedFilePath().empty()) {
danakja3cfb8332019-12-10 21:13:3375 std::move(*callback).Run(
76 download->GetForcedFilePath(),
77 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
78 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Joe DeBlasioc427ca032020-01-17 19:15:43