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 5 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_ 6 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_ 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "base/files/file_path.h" 11 #include "base/logging.h" 12 #include "base/time/time.h" 13 #include "content/common/content_export.h" 14 #include "content/public/browser/download_danger_type.h" 15 #include "content/public/browser/download_item.h" 16 #include "content/public/browser/save_page_type.h" 17 18 namespace content { 19 20 class BrowserContext; 21 class WebContents; 22 23 // Called by SavePackage when it creates a DownloadItem. 24 typedef base::Callback<void(DownloadItem*)> 25 SavePackageDownloadCreatedCallback; 26 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 typedef base::Callback<void(const base::FilePath&, 32 SavePageType, 33 const SavePackageDownloadCreatedCallback&)> 34 SavePackagePathPickedCallback; 35 36 // Called with the results of DetermineDownloadTarget(). If the delegate decides 37 // to cancel the download, then |target_path| should be set to an empty path. If 38 // |target_path| is non-empty, then |intermediate_path| is required to be 39 // non-empty and specify the path to the intermediate file (which could be the 40 // same as |target_path|). Both |target_path| and |intermediate_path| are 41 // expected to in the same directory. 42 typedef base::Callback<void( 43 const base::FilePath& target_path, 44 DownloadItem::TargetDisposition disposition, 45 DownloadDangerType danger_type, 46 const base::FilePath& intermediate_path)> DownloadTargetCallback; 47 48 // Called when a download delayed by the delegate has completed. 49 typedef base::Callback<void(bool)> DownloadOpenDelayedCallback; 50 51 // Called with the result of CheckForFileExistence(). 52 typedef base::Callback<void(bool result)> CheckForFileExistenceCallback; 53 54 typedef base::Callback<void(uint32)> DownloadIdCallback; 55 56 // Browser's download manager: manages all downloads and destination view. 57 class CONTENT_EXPORT DownloadManagerDelegate { 58 public: 59 // Lets the delegate know that the download manager is shutting down. 60 virtual void Shutdown() {} 61 62 // Runs |callback| with a new download id when possible, perhaps 63 // synchronously. 64 virtual void GetNextId(const DownloadIdCallback& callback); 65 66 // Called to notify the delegate that a new download |item| requires a 67 // download target to be determined. The delegate should return |true| if it 68 // will determine the target information and will invoke |callback|. The 69 // callback may be invoked directly (synchronously). If this function returns 70 // |false|, the download manager will continue the download using a default 71 // target path. 72 // 73 // The state of the |item| shouldn't be modified during the process of 74 // filename determination save for external data (GetExternalData() / 75 // SetExternalData()). 76 // 77 // If the download should be canceled, |callback| should be invoked with an 78 // empty |target_path| argument. 79 virtual bool DetermineDownloadTarget(DownloadItem* item, 80 const DownloadTargetCallback& callback); 81 82 // Tests if a file type should be opened automatically. 83 virtual bool ShouldOpenFileBasedOnExtension(const base::FilePath& path); 84 85 // Allows the delegate to delay completion of the download. This function 86 // will either return true (in which case the download may complete) 87 // or will call the callback passed when the download is ready for 88 // completion. This routine may be called multiple times; once the callback 89 // has been called or the function has returned true for a particular 90 // download it should continue to return true for that download. 91 virtual bool ShouldCompleteDownload( 92 DownloadItem* item, 93 const base::Closure& complete_callback); 94 95 // Allows the delegate to override opening the download. If this function 96 // returns false, the delegate needs to call callback when it's done 97 // with the item, and is responsible for opening it. This function is called 98 // after the final rename, but before the download state is set to COMPLETED. 99 virtual bool ShouldOpenDownload(DownloadItem* item, 100 const DownloadOpenDelayedCallback& callback); 101 102 // Returns true if we need to generate a binary hash for downloads. 103 virtual bool GenerateFileHash(); 104 105 // Retrieve the directories to save html pages and downloads to. 106 virtual void GetSaveDir(BrowserContext* browser_context, 107 base::FilePath* website_save_dir, 108 base::FilePath* download_save_dir, 109 bool* skip_dir_check) {} 110 111 // Asks the user for the path to save a page. The delegate calls the callback 112 // to give the answer. 113 virtual void ChooseSavePath( 114 WebContents* web_contents, 115 const base::FilePath& suggested_path, 116 const base::FilePath::StringType& default_extension, 117 bool can_save_as_complete, 118 const SavePackagePathPickedCallback& callback) { 119 } 120 121 // Opens the file associated with this download. 122 virtual void OpenDownload(DownloadItem* download) {} 123 124 // Shows the download via the OS shell. 125 virtual void ShowDownloadInShell(DownloadItem* download) {} 126 127 // Checks whether a downloaded file still exists. 128 virtual void CheckForFileExistence( 129 DownloadItem* download, 130 const CheckForFileExistenceCallback& callback) {} 131 132 // Return a GUID string used for identifying the application to the 133 // system AV function for scanning downloaded files. If an empty 134 // or invalid GUID string is returned, no client identification 135 // will be given to the AV function. 136 virtual std::string ApplicationClientIdForFileScanning() const; 137 138 protected: 139 virtual ~DownloadManagerDelegate(); 140 }; 141 142 } // namespace content 143 144 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_DELEGATE_H_ 145