atom_download_manager_delegate.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/atom_download_manager_delegate.h"
  5. #include <string>
  6. #include "atom/browser/api/atom_api_download_item.h"
  7. #include "atom/browser/atom_browser_context.h"
  8. #include "atom/browser/native_window.h"
  9. #include "atom/browser/ui/file_dialog.h"
  10. #include "atom/browser/web_contents_preferences.h"
  11. #include "atom/common/options_switches.h"
  12. #include "base/bind.h"
  13. #include "base/files/file_util.h"
  14. #include "chrome/common/pref_names.h"
  15. #include "components/prefs/pref_service.h"
  16. #include "content/public/browser/browser_context.h"
  17. #include "content/public/browser/browser_thread.h"
  18. #include "content/public/browser/download_manager.h"
  19. #include "net/base/filename_util.h"
  20. namespace atom {
  21. namespace {
  22. // Generate default file path to save the download.
  23. void CreateDownloadPath(
  24. const GURL& url,
  25. const std::string& content_disposition,
  26. const std::string& suggested_filename,
  27. const std::string& mime_type,
  28. const base::FilePath& default_download_path,
  29. const AtomDownloadManagerDelegate::CreateDownloadPathCallback& callback) {
  30. DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
  31. auto generated_name =
  32. net::GenerateFileName(url, content_disposition, std::string(),
  33. suggested_filename, mime_type, "download");
  34. if (!base::PathExists(default_download_path))
  35. base::CreateDirectory(default_download_path);
  36. base::FilePath path(default_download_path.Append(generated_name));
  37. content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
  38. base::BindOnce(callback, path));
  39. }
  40. } // namespace
  41. AtomDownloadManagerDelegate::AtomDownloadManagerDelegate(
  42. content::DownloadManager* manager)
  43. : download_manager_(manager), weak_ptr_factory_(this) {}
  44. AtomDownloadManagerDelegate::~AtomDownloadManagerDelegate() {
  45. if (download_manager_) {
  46. DCHECK_EQ(static_cast<content::DownloadManagerDelegate*>(this),
  47. download_manager_->GetDelegate());
  48. download_manager_->SetDelegate(nullptr);
  49. download_manager_ = nullptr;
  50. }
  51. }
  52. void AtomDownloadManagerDelegate::GetItemSavePath(content::DownloadItem* item,
  53. base::FilePath* path) {
  54. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  55. v8::Locker locker(isolate);
  56. v8::HandleScope handle_scope(isolate);
  57. api::DownloadItem* download =
  58. api::DownloadItem::FromWrappedClass(isolate, item);
  59. if (download)
  60. *path = download->GetSavePath();
  61. }
  62. void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
  63. uint32_t download_id,
  64. const content::DownloadTargetCallback& callback,
  65. const base::FilePath& default_path) {
  66. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  67. auto* item = download_manager_->GetDownload(download_id);
  68. if (!item)
  69. return;
  70. NativeWindow* window = nullptr;
  71. content::WebContents* web_contents = item->GetWebContents();
  72. auto* relay =
  73. web_contents ? NativeWindowRelay::FromWebContents(web_contents) : nullptr;
  74. if (relay)
  75. window = relay->window.get();
  76. auto* web_preferences = WebContentsPreferences::From(web_contents);
  77. bool offscreen =
  78. !web_preferences || web_preferences->IsEnabled(options::kOffscreen);
  79. base::FilePath path;
  80. GetItemSavePath(item, &path);
  81. // Show save dialog if save path was not set already on item
  82. file_dialog::DialogSettings settings;
  83. settings.parent_window = window;
  84. settings.force_detached = offscreen;
  85. settings.title = item->GetURL().spec();
  86. settings.default_path = default_path;
  87. if (path.empty() && file_dialog::ShowSaveDialog(settings, &path)) {
  88. // Remember the last selected download directory.
  89. AtomBrowserContext* browser_context = static_cast<AtomBrowserContext*>(
  90. download_manager_->GetBrowserContext());
  91. browser_context->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
  92. path.DirName());
  93. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  94. v8::Locker locker(isolate);
  95. v8::HandleScope handle_scope(isolate);
  96. api::DownloadItem* download_item =
  97. api::DownloadItem::FromWrappedClass(isolate, item);
  98. if (download_item)
  99. download_item->SetSavePath(path);
  100. }
  101. // Running the DownloadTargetCallback with an empty FilePath signals that the
  102. // download should be cancelled.
  103. // If user cancels the file save dialog, run the callback with empty FilePath.
  104. callback.Run(path, content::DownloadItem::TARGET_DISPOSITION_PROMPT,
  105. content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path,
  106. path.empty() ? content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
  107. : content::DOWNLOAD_INTERRUPT_REASON_NONE);
  108. }
  109. void AtomDownloadManagerDelegate::Shutdown() {
  110. weak_ptr_factory_.InvalidateWeakPtrs();
  111. download_manager_ = nullptr;
  112. }
  113. bool AtomDownloadManagerDelegate::DetermineDownloadTarget(
  114. content::DownloadItem* download,
  115. const content::DownloadTargetCallback& callback) {
  116. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  117. if (!download->GetForcedFilePath().empty()) {
  118. callback.Run(download->GetForcedFilePath(),
  119. content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
  120. content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
  121. download->GetForcedFilePath(),
  122. content::DOWNLOAD_INTERRUPT_REASON_NONE);
  123. return true;
  124. }
  125. // Try to get the save path from JS wrapper.
  126. base::FilePath save_path;
  127. GetItemSavePath(download, &save_path);
  128. if (!save_path.empty()) {
  129. callback.Run(save_path, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
  130. content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, save_path,
  131. content::DOWNLOAD_INTERRUPT_REASON_NONE);
  132. return true;
  133. }
  134. AtomBrowserContext* browser_context =
  135. static_cast<AtomBrowserContext*>(download_manager_->GetBrowserContext());
  136. base::FilePath default_download_path =
  137. browser_context->prefs()->GetFilePath(prefs::kDownloadDefaultDirectory);
  138. CreateDownloadPathCallback download_path_callback =
  139. base::Bind(&AtomDownloadManagerDelegate::OnDownloadPathGenerated,
  140. weak_ptr_factory_.GetWeakPtr(), download->GetId(), callback);
  141. content::BrowserThread::PostTask(
  142. content::BrowserThread::FILE, FROM_HERE,
  143. base::BindOnce(&CreateDownloadPath, download->GetURL(),
  144. download->GetContentDisposition(),
  145. download->GetSuggestedFilename(), download->GetMimeType(),
  146. default_download_path, download_path_callback));
  147. return true;
  148. }
  149. bool AtomDownloadManagerDelegate::ShouldOpenDownload(
  150. content::DownloadItem* download,
  151. const content::DownloadOpenDelayedCallback& callback) {
  152. return true;
  153. }
  154. void AtomDownloadManagerDelegate::GetNextId(
  155. const content::DownloadIdCallback& callback) {
  156. static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
  157. callback.Run(next_id++);
  158. }
  159. } // namespace atom