PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/content/public/browser/download_manager_delegate.h

https://github.com/chromium/chromium
C Header | 250 lines | 101 code | 36 blank | 113 comment | 0 complexity | d26dc32a2d9fbdbf806977639fc1b5e3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_
  5. #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_
  6. #include <stdint.h>
  7. #include "base/callback.h"
  8. #include "base/files/file_path.h"
  9. #include "components/download/public/common/download_danger_type.h"
  10. #include "components/download/public/common/download_item.h"
  11. #include "components/download/public/common/download_item_rename_handler.h"
  12. #include "components/download/public/common/download_schedule.h"
  13. #include "components/download/public/common/download_url_parameters.h"
  14. #include "components/download/public/common/quarantine_connection.h"
  15. #include "content/common/content_export.h"
  16. #include "content/public/browser/save_page_type.h"
  17. #include "content/public/browser/web_contents.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. #include "url/origin.h"
  20. class GURL;
  21. namespace content {
  22. class BrowserContext;
  23. class WebContents;
  24. // Called by SavePackage when it creates a download::DownloadItem.
  25. using SavePackageDownloadCreatedCallback =
  26. base::OnceCallback<void(download::DownloadItem*)>;
  27. // Will be called asynchronously with the results of the ChooseSavePath
  28. // operation. If the delegate wants notification of the download item created
  29. // in response to this operation, the SavePackageDownloadCreatedCallback will be
  30. // non-null.
  31. using SavePackagePathPickedCallback =
  32. base::OnceCallback<void(const base::FilePath&,
  33. SavePageType,
  34. SavePackageDownloadCreatedCallback)>;
  35. // Called with the results of DetermineDownloadTarget().
  36. //
  37. // |target_path| should be set to a non-empty path which is taken to be the
  38. // final target path for the download. Any file already at this path will be
  39. // overwritten.
  40. //
  41. // |disposition| and |danger_type| are attributes associated with the download
  42. // item and can be accessed via the download::DownloadItem accessors.
  43. //
  44. // |intermediate_path| specifies the path to the intermediate file. The download
  45. // will be written to this path until all the bytes have been written. Upon
  46. // completion, the file will be renamed to |target_path|.
  47. // |intermediate_path| could be the same as |target_path|. Both paths must
  48. // be in the same directory.
  49. //
  50. // |display_name| specifies the suggested file name in case the file name cannot
  51. // be determined from target_path. Could be empty.
  52. //
  53. // |interrupt_reason| should be set to DOWNLOAD_INTERRUPT_REASON_NONE in
  54. // order to proceed with the download. DOWNLOAD_INTERRUPT_REASON_USER_CANCEL
  55. // results in the download being marked cancelled. Any other value results
  56. // in the download being marked as interrupted. The other fields are only
  57. // considered valid if |interrupt_reason| is NONE.
  58. using DownloadTargetCallback = base::OnceCallback<void(
  59. const base::FilePath& target_path,
  60. download::DownloadItem::TargetDisposition disposition,
  61. download::DownloadDangerType danger_type,
  62. download::DownloadItem::MixedContentStatus mixed_content_status,
  63. const base::FilePath& intermediate_path,
  64. const base::FilePath& display_name,
  65. const std::string& mime_type,
  66. absl::optional<download::DownloadSchedule> download_schedule,
  67. download::DownloadInterruptReason interrupt_reason)>;
  68. // Called when a download delayed by the delegate has completed.
  69. using DownloadOpenDelayedCallback = base::OnceCallback<void(bool)>;
  70. // On failure, |next_id| is equal to kInvalidId.
  71. using DownloadIdCallback = base::OnceCallback<void(uint32_t /* next_id */)>;
  72. // Called on whether a download is allowed to continue.
  73. using CheckDownloadAllowedCallback = base::OnceCallback<void(bool /*allow*/)>;
  74. // Called by CheckSavePackageAllowed when the content of a save package is known
  75. // to be allowed or not.
  76. using SavePackageAllowedCallback = base::OnceCallback<void(bool /*allow*/)>;
  77. // Browser's download manager: manages all downloads and destination view.
  78. class CONTENT_EXPORT DownloadManagerDelegate {
  79. public:
  80. // Lets the delegate know that the download manager is shutting down.
  81. virtual void Shutdown() {}
  82. // Runs |callback| with a new download id when possible, perhaps
  83. // synchronously. If this call fails, |callback| will be called with
  84. // kInvalidId.
  85. virtual void GetNextId(DownloadIdCallback callback);
  86. // Called to notify the delegate that a new download |item| requires a
  87. // download target to be determined. The delegate should return |true| if it
  88. // will determine the target information and will invoke |callback|. The
  89. // callback may be invoked directly (synchronously). If this function returns
  90. // |false|, the download manager will continue the download using a default
  91. // target path.
  92. //
  93. // The state of the |item| shouldn't be modified during the process of
  94. // filename determination save for external data (GetExternalData() /
  95. // SetExternalData()).
  96. //
  97. // If the download should be canceled, |callback| should be invoked with an
  98. // empty |target_path| argument.
  99. virtual bool DetermineDownloadTarget(download::DownloadItem* item,
  100. DownloadTargetCallback* callback);
  101. // Tests if a file type should be opened automatically. This consider both
  102. // user and policy settings, and should be called when it doesn't matter
  103. // what set the auto-open, just if it is set.
  104. virtual bool ShouldAutomaticallyOpenFile(const GURL& url,
  105. const base::FilePath& path);
  106. // Tests if a file type should be opened automatically by policy. This
  107. // should only be used if it matters if the file will auto-open by policy.
  108. // Generally used to determine if we need to show UI indicating an active
  109. // policy.
  110. virtual bool ShouldAutomaticallyOpenFileByPolicy(const GURL& url,
  111. const base::FilePath& path);
  112. // Allows the delegate to delay completion of the download. This function
  113. // will either return true (in which case the download may complete)
  114. // or will call the callback passed when the download is ready for
  115. // completion. This routine may be called multiple times; once the callback
  116. // has been called or the function has returned true for a particular
  117. // download it should continue to return true for that download.
  118. virtual bool ShouldCompleteDownload(download::DownloadItem* item,
  119. base::OnceClosure complete_callback);
  120. // Allows the delegate to override opening the download. If this function
  121. // returns false, the delegate needs to call |callback| when it's done
  122. // with the item, and is responsible for opening it. When it returns true,
  123. // the callback will not be used. This function is called after the final
  124. // rename, but before the download state is set to COMPLETED.
  125. virtual bool ShouldOpenDownload(download::DownloadItem* item,
  126. DownloadOpenDelayedCallback callback);
  127. // Checks and hands off the downloading to be handled by another system based
  128. // on mime type. Returns true if the download was intercepted.
  129. virtual bool InterceptDownloadIfApplicable(
  130. const GURL& url,
  131. const std::string& user_agent,
  132. const std::string& content_disposition,
  133. const std::string& mime_type,
  134. const std::string& request_origin,
  135. int64_t content_length,
  136. bool is_transient,
  137. WebContents* web_contents);
  138. // Retrieve the directories to save html pages and downloads to.
  139. virtual void GetSaveDir(BrowserContext* browser_context,
  140. base::FilePath* website_save_dir,
  141. base::FilePath* download_save_dir) {}
  142. // Asks the user for the path to save a page. The delegate calls the callback
  143. // to give the answer.
  144. virtual void ChooseSavePath(
  145. WebContents* web_contents,
  146. const base::FilePath& suggested_path,
  147. const base::FilePath::StringType& default_extension,
  148. bool can_save_as_complete,
  149. SavePackagePathPickedCallback callback) {}
  150. // Sanitize a filename that's going to be used for saving a subresource of a
  151. // SavePackage.
  152. //
  153. // If the delegate does nothing, the default filename already populated in
  154. // |filename| will be used. Otherwise, the delegate can update |filename| to
  155. // the desired filename.
  156. //
  157. // |filename| contains a basename with an extension, but without a path. This
  158. // should be the case on return as well. I.e. |filename| cannot specify a
  159. // relative path.
  160. // |source_url| contains the URL from which the download originates and is
  161. // needed to determine the file's danger level.
  162. virtual void SanitizeSavePackageResourceName(base::FilePath* filename,
  163. const GURL& source_url) {}
  164. // Sanitize a download parameters
  165. //
  166. // If the delegate does nothing, the default parameters already populated in
  167. // |params| will be used. Otherwise, the delegate can update |params| to
  168. // the desired parameters.
  169. virtual void SanitizeDownloadParameters(
  170. download::DownloadUrlParameters* params) {}
  171. // Opens the file associated with this download.
  172. virtual void OpenDownload(download::DownloadItem* download) {}
  173. // Shows the download via the OS shell.
  174. virtual void ShowDownloadInShell(download::DownloadItem* download) {}
  175. // Return a GUID string used for identifying the application to the system AV
  176. // function for scanning downloaded files. If no GUID is provided or if the
  177. // provided GUID is invalid, then the appropriate quarantining will be
  178. // performed manually without passing the download to the system AV function.
  179. //
  180. // This GUID is only used on Windows.
  181. virtual std::string ApplicationClientIdForFileScanning();
  182. // Checks whether download is allowed to continue. |check_download_allowed_cb|
  183. // is called with the decision on completion.
  184. virtual void CheckDownloadAllowed(
  185. const WebContents::Getter& web_contents_getter,
  186. const GURL& url,
  187. const std::string& request_method,
  188. absl::optional<url::Origin> request_initiator,
  189. bool from_download_cross_origin_redirect,
  190. bool content_initiated,
  191. CheckDownloadAllowedCallback check_download_allowed_cb);
  192. // Gets a callback which can connect the download manager to a Quarantine
  193. // Service instance if available.
  194. virtual download::QuarantineConnectionCallback
  195. GetQuarantineConnectionCallback();
  196. // Gets a handler to perform the rename for a download item. If no special
  197. // rename handling is required, don't override this, as the default
  198. // implementation returns null, which indicates that the default rename
  199. // handling should be performed.
  200. virtual std::unique_ptr<download::DownloadItemRenameHandler>
  201. GetRenameHandlerForDownload(download::DownloadItem* download_item);
  202. // Gets a |DownloadItem| from the GUID, or null if no such GUID is available.
  203. virtual download::DownloadItem* GetDownloadByGuid(const std::string& guid);
  204. // Allows the delegate to delay completion of a SavePackage's final renaming
  205. // step so it can be disallowed.
  206. virtual void CheckSavePackageAllowed(
  207. download::DownloadItem* download_item,
  208. base::flat_map<base::FilePath, base::FilePath> save_package_files,
  209. SavePackageAllowedCallback callback);
  210. protected:
  211. virtual ~DownloadManagerDelegate();
  212. };
  213. } // namespace content
  214. #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_