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/scoped_ptr.h"
11 #include "base/memory/weak_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/download/download_target_info.h"
15 #include "chrome/browser/safe_browsing/download_protection_service.h"
16 #include "content/public/browser/download_danger_type.h"
17 #include "content/public/browser/download_item.h"
18 #include "content/public/browser/download_manager_delegate.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
26 class DownloadManager
;
29 namespace extensions
{
33 namespace user_prefs
{
34 class PrefRegistrySyncable
;
37 #if defined(COMPILER_GCC)
38 namespace BASE_HASH_NAMESPACE
{
40 struct hash
<extensions::CrxInstaller
*> {
41 std::size_t operator()(extensions::CrxInstaller
* const& p
) const {
42 return reinterpret_cast<std::size_t>(p
);
45 } // namespace BASE_HASH_NAMESPACE
48 // This is the Chrome side helper for the download system.
49 class ChromeDownloadManagerDelegate
50 : public content::DownloadManagerDelegate
,
51 public content::NotificationObserver
,
52 public DownloadTargetDeterminerDelegate
{
54 explicit ChromeDownloadManagerDelegate(Profile
* profile
);
55 virtual ~ChromeDownloadManagerDelegate();
57 // Should be called before the first call to ShouldCompleteDownload() to
58 // disable SafeBrowsing checks for |item|.
59 static void DisableSafeBrowsing(content::DownloadItem
* item
);
61 void SetDownloadManager(content::DownloadManager
* dm
);
63 // Callbacks passed to GetNextId() will not be called until the returned
64 // callback is called.
65 content::DownloadIdCallback
GetDownloadIdReceiverCallback();
67 // content::DownloadManagerDelegate
68 virtual void Shutdown() OVERRIDE
;
69 virtual void GetNextId(const content::DownloadIdCallback
& callback
) OVERRIDE
;
70 virtual bool DetermineDownloadTarget(
71 content::DownloadItem
* item
,
72 const content::DownloadTargetCallback
& callback
) OVERRIDE
;
73 virtual bool ShouldOpenFileBasedOnExtension(
74 const base::FilePath
& path
) OVERRIDE
;
75 virtual bool ShouldCompleteDownload(
76 content::DownloadItem
* item
,
77 const base::Closure
& complete_callback
) OVERRIDE
;
78 virtual bool ShouldOpenDownload(
79 content::DownloadItem
* item
,
80 const content::DownloadOpenDelayedCallback
& callback
) OVERRIDE
;
81 virtual bool GenerateFileHash() OVERRIDE
;
82 virtual void GetSaveDir(content::BrowserContext
* browser_context
,
83 base::FilePath
* website_save_dir
,
84 base::FilePath
* download_save_dir
,
85 bool* skip_dir_check
) OVERRIDE
;
86 virtual void ChooseSavePath(
87 content::WebContents
* web_contents
,
88 const base::FilePath
& suggested_path
,
89 const base::FilePath::StringType
& default_extension
,
90 bool can_save_as_complete
,
91 const content::SavePackagePathPickedCallback
& callback
) OVERRIDE
;
92 virtual void OpenDownload(content::DownloadItem
* download
) OVERRIDE
;
93 virtual void ShowDownloadInShell(content::DownloadItem
* download
) OVERRIDE
;
94 virtual void CheckForFileExistence(
95 content::DownloadItem
* download
,
96 const content::CheckForFileExistenceCallback
& callback
) OVERRIDE
;
97 virtual std::string
ApplicationClientIdForFileScanning() const OVERRIDE
;
99 // Opens a download using the platform handler. DownloadItem::OpenDownload,
100 // which ends up being handled by OpenDownload(), will open a download in the
101 // browser if doing so is preferred.
102 void OpenDownloadUsingPlatformHandler(content::DownloadItem
* download
);
104 DownloadPrefs
* download_prefs() { return download_prefs_
.get(); }
107 // So that test classes that inherit from this for override purposes
108 // can call back into the DownloadManager.
109 content::DownloadManager
* download_manager_
;
111 virtual safe_browsing::DownloadProtectionService
*
112 GetDownloadProtectionService();
114 // DownloadTargetDeterminerDelegate. Protected for testing.
115 virtual void NotifyExtensions(
116 content::DownloadItem
* download
,
117 const base::FilePath
& suggested_virtual_path
,
118 const NotifyExtensionsCallback
& callback
) OVERRIDE
;
119 virtual void ReserveVirtualPath(
120 content::DownloadItem
* download
,
121 const base::FilePath
& virtual_path
,
122 bool create_directory
,
123 DownloadPathReservationTracker::FilenameConflictAction conflict_action
,
124 const ReservedPathCallback
& callback
) OVERRIDE
;
125 virtual void PromptUserForDownloadPath(
126 content::DownloadItem
* download
,
127 const base::FilePath
& suggested_virtual_path
,
128 const FileSelectedCallback
& callback
) OVERRIDE
;
129 virtual void DetermineLocalPath(
130 content::DownloadItem
* download
,
131 const base::FilePath
& virtual_path
,
132 const LocalPathCallback
& callback
) OVERRIDE
;
133 virtual void CheckDownloadUrl(
134 content::DownloadItem
* download
,
135 const base::FilePath
& suggested_virtual_path
,
136 const CheckDownloadUrlCallback
& callback
) OVERRIDE
;
137 virtual void GetFileMimeType(
138 const base::FilePath
& path
,
139 const GetFileMimeTypeCallback
& callback
) OVERRIDE
;
142 friend class base::RefCountedThreadSafe
<ChromeDownloadManagerDelegate
>;
144 typedef std::vector
<content::DownloadIdCallback
> IdCallbackVector
;
146 // content::NotificationObserver implementation.
147 virtual void Observe(int type
,
148 const content::NotificationSource
& source
,
149 const content::NotificationDetails
& details
) OVERRIDE
;
151 // Callback function after the DownloadProtectionService completes.
152 void CheckClientDownloadDone(
154 safe_browsing::DownloadProtectionService::DownloadCheckResult result
);
156 // Internal gateways for ShouldCompleteDownload().
157 bool IsDownloadReadyForCompletion(
158 content::DownloadItem
* item
,
159 const base::Closure
& internal_complete_callback
);
160 void ShouldCompleteDownloadInternal(
162 const base::Closure
& user_complete_callback
);
164 void SetNextId(uint32 id
);
166 void ReturnNextId(const content::DownloadIdCallback
& callback
);
168 void OnDownloadTargetDetermined(
170 const content::DownloadTargetCallback
& callback
,
171 scoped_ptr
<DownloadTargetInfo
> target_info
);
174 uint32 next_download_id_
;
175 IdCallbackVector id_callbacks_
;
176 scoped_ptr
<DownloadPrefs
> download_prefs_
;
178 // Maps from pending extension installations to DownloadItem IDs.
179 typedef base::hash_map
<extensions::CrxInstaller
*,
180 content::DownloadOpenDelayedCallback
> CrxInstallerMap
;
181 CrxInstallerMap crx_installers_
;
183 content::NotificationRegistrar registrar_
;
185 base::WeakPtrFactory
<ChromeDownloadManagerDelegate
> weak_ptr_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate
);
190 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_