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.
5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
8 #include "base/compiler_specific.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/download/download_path_reservation_tracker.h"
13 #include "chrome/browser/download/download_target_determiner_delegate.h"
14 #include "chrome/browser/safe_browsing/download_protection_service.h"
15 #include "content/public/browser/download_danger_type.h"
16 #include "content/public/browser/download_item.h"
17 #include "content/public/browser/download_manager_delegate.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
25 class DownloadManager;
28 namespace extensions {
32 namespace user_prefs {
33 class PrefRegistrySyncable;
36 #if defined(COMPILER_GCC)
37 namespace BASE_HASH_NAMESPACE {
39 struct hash<extensions::CrxInstaller*> {
40 std::size_t operator()(extensions::CrxInstaller* const& p) const {
41 return reinterpret_cast<std::size_t>(p);
44 } // namespace BASE_HASH_NAMESPACE
47 // This is the Chrome side helper for the download system.
48 class ChromeDownloadManagerDelegate
49 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>,
50 public content::DownloadManagerDelegate,
51 public content::NotificationObserver,
52 public DownloadTargetDeterminerDelegate {
54 explicit ChromeDownloadManagerDelegate(Profile* profile);
56 // Should be called before the first call to ShouldCompleteDownload() to
57 // disable SafeBrowsing checks for |item|.
58 static void DisableSafeBrowsing(content::DownloadItem* item);
60 void SetDownloadManager(content::DownloadManager* dm);
62 // Callbacks passed to GetNextId() will not be called until SetNextId() is
64 void SetNextId(uint32 next_id);
66 // content::DownloadManagerDelegate
67 virtual void Shutdown() OVERRIDE;
68 virtual void GetNextId(const content::DownloadIdCallback& callback) OVERRIDE;
69 virtual bool DetermineDownloadTarget(
70 content::DownloadItem* item,
71 const content::DownloadTargetCallback& callback) OVERRIDE;
72 virtual bool ShouldOpenFileBasedOnExtension(
73 const base::FilePath& path) OVERRIDE;
74 virtual bool ShouldCompleteDownload(
75 content::DownloadItem* item,
76 const base::Closure& complete_callback) OVERRIDE;
77 virtual bool ShouldOpenDownload(
78 content::DownloadItem* item,
79 const content::DownloadOpenDelayedCallback& callback) OVERRIDE;
80 virtual bool GenerateFileHash() OVERRIDE;
81 virtual void GetSaveDir(content::BrowserContext* browser_context,
82 base::FilePath* website_save_dir,
83 base::FilePath* download_save_dir,
84 bool* skip_dir_check) OVERRIDE;
85 virtual void ChooseSavePath(
86 content::WebContents* web_contents,
87 const base::FilePath& suggested_path,
88 const base::FilePath::StringType& default_extension,
89 bool can_save_as_complete,
90 const content::SavePackagePathPickedCallback& callback) OVERRIDE;
91 virtual void OpenDownload(content::DownloadItem* download) OVERRIDE;
92 virtual void ShowDownloadInShell(content::DownloadItem* download) OVERRIDE;
93 virtual void CheckForFileExistence(
94 content::DownloadItem* download,
95 const content::CheckForFileExistenceCallback& callback) OVERRIDE;
96 virtual std::string ApplicationClientIdForFileScanning() const OVERRIDE;
98 DownloadPrefs* download_prefs() { return download_prefs_.get(); }
101 // So that test classes can inherit from this for override purposes.
102 virtual ~ChromeDownloadManagerDelegate();
104 // So that test classes that inherit from this for override purposes
105 // can call back into the DownloadManager.
106 content::DownloadManager* download_manager_;
108 virtual safe_browsing::DownloadProtectionService*
109 GetDownloadProtectionService();
111 // DownloadTargetDeterminerDelegate. Protected for testing.
112 virtual void NotifyExtensions(
113 content::DownloadItem* download,
114 const base::FilePath& suggested_virtual_path,
115 const NotifyExtensionsCallback& callback) OVERRIDE;
116 virtual void ReserveVirtualPath(
117 content::DownloadItem* download,
118 const base::FilePath& virtual_path,
119 bool create_directory,
120 DownloadPathReservationTracker::FilenameConflictAction conflict_action,
121 const ReservedPathCallback& callback) OVERRIDE;
122 virtual void PromptUserForDownloadPath(
123 content::DownloadItem* download,
124 const base::FilePath& suggested_virtual_path,
125 const FileSelectedCallback& callback) OVERRIDE;
126 virtual void DetermineLocalPath(
127 content::DownloadItem* download,
128 const base::FilePath& virtual_path,
129 const LocalPathCallback& callback) OVERRIDE;
130 virtual void CheckDownloadUrl(
131 content::DownloadItem* download,
132 const base::FilePath& suggested_virtual_path,
133 const CheckDownloadUrlCallback& callback) OVERRIDE;
136 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>;
138 typedef std::vector<content::DownloadIdCallback> IdCallbackVector;
140 // content::NotificationObserver implementation.
141 virtual void Observe(int type,
142 const content::NotificationSource& source,
143 const content::NotificationDetails& details) OVERRIDE;
145 // Callback function after the DownloadProtectionService completes.
146 void CheckClientDownloadDone(
148 safe_browsing::DownloadProtectionService::DownloadCheckResult result);
150 // Internal gateways for ShouldCompleteDownload().
151 bool IsDownloadReadyForCompletion(
152 content::DownloadItem* item,
153 const base::Closure& internal_complete_callback);
154 void ShouldCompleteDownloadInternal(
156 const base::Closure& user_complete_callback);
158 void ReturnNextId(const content::DownloadIdCallback& callback);
161 uint32 next_download_id_;
162 IdCallbackVector id_callbacks_;
163 scoped_ptr<DownloadPrefs> download_prefs_;
165 // Maps from pending extension installations to DownloadItem IDs.
166 typedef base::hash_map<extensions::CrxInstaller*,
167 content::DownloadOpenDelayedCallback> CrxInstallerMap;
168 CrxInstallerMap crx_installers_;
170 content::NotificationRegistrar registrar_;
172 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate);
175 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_