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 // This is the Chrome side helper for the download system.
38 class ChromeDownloadManagerDelegate
39 : public content::DownloadManagerDelegate
,
40 public content::NotificationObserver
,
41 public DownloadTargetDeterminerDelegate
{
43 explicit ChromeDownloadManagerDelegate(Profile
* profile
);
44 ~ChromeDownloadManagerDelegate() override
;
46 // Should be called before the first call to ShouldCompleteDownload() to
47 // disable SafeBrowsing checks for |item|.
48 static void DisableSafeBrowsing(content::DownloadItem
* item
);
50 void SetDownloadManager(content::DownloadManager
* dm
);
52 // Callbacks passed to GetNextId() will not be called until the returned
53 // callback is called.
54 content::DownloadIdCallback
GetDownloadIdReceiverCallback();
56 // content::DownloadManagerDelegate
57 void Shutdown() override
;
58 void GetNextId(const content::DownloadIdCallback
& callback
) override
;
59 bool DetermineDownloadTarget(
60 content::DownloadItem
* item
,
61 const content::DownloadTargetCallback
& callback
) override
;
62 bool ShouldOpenFileBasedOnExtension(const base::FilePath
& path
) override
;
63 bool ShouldCompleteDownload(content::DownloadItem
* item
,
64 const base::Closure
& complete_callback
) override
;
65 bool ShouldOpenDownload(
66 content::DownloadItem
* item
,
67 const content::DownloadOpenDelayedCallback
& callback
) override
;
68 bool GenerateFileHash() override
;
69 void GetSaveDir(content::BrowserContext
* browser_context
,
70 base::FilePath
* website_save_dir
,
71 base::FilePath
* download_save_dir
,
72 bool* skip_dir_check
) override
;
74 content::WebContents
* web_contents
,
75 const base::FilePath
& suggested_path
,
76 const base::FilePath::StringType
& default_extension
,
77 bool can_save_as_complete
,
78 const content::SavePackagePathPickedCallback
& callback
) override
;
79 void OpenDownload(content::DownloadItem
* download
) override
;
80 void ShowDownloadInShell(content::DownloadItem
* download
) override
;
81 void CheckForFileExistence(
82 content::DownloadItem
* download
,
83 const content::CheckForFileExistenceCallback
& callback
) override
;
84 std::string
ApplicationClientIdForFileScanning() const override
;
86 // Opens a download using the platform handler. DownloadItem::OpenDownload,
87 // which ends up being handled by OpenDownload(), will open a download in the
88 // browser if doing so is preferred.
89 void OpenDownloadUsingPlatformHandler(content::DownloadItem
* download
);
91 DownloadPrefs
* download_prefs() { return download_prefs_
.get(); }
94 // So that test classes that inherit from this for override purposes
95 // can call back into the DownloadManager.
96 content::DownloadManager
* download_manager_
;
98 virtual safe_browsing::DownloadProtectionService
*
99 GetDownloadProtectionService();
101 // DownloadTargetDeterminerDelegate. Protected for testing.
102 void NotifyExtensions(content::DownloadItem
* download
,
103 const base::FilePath
& suggested_virtual_path
,
104 const NotifyExtensionsCallback
& callback
) override
;
105 void ReserveVirtualPath(
106 content::DownloadItem
* download
,
107 const base::FilePath
& virtual_path
,
108 bool create_directory
,
109 DownloadPathReservationTracker::FilenameConflictAction conflict_action
,
110 const ReservedPathCallback
& callback
) override
;
111 void PromptUserForDownloadPath(content::DownloadItem
* download
,
112 const base::FilePath
& suggested_virtual_path
,
113 const FileSelectedCallback
& callback
) override
;
114 void DetermineLocalPath(content::DownloadItem
* download
,
115 const base::FilePath
& virtual_path
,
116 const LocalPathCallback
& callback
) override
;
117 void CheckDownloadUrl(content::DownloadItem
* download
,
118 const base::FilePath
& suggested_virtual_path
,
119 const CheckDownloadUrlCallback
& callback
) override
;
120 void GetFileMimeType(const base::FilePath
& path
,
121 const GetFileMimeTypeCallback
& callback
) override
;
124 friend class base::RefCountedThreadSafe
<ChromeDownloadManagerDelegate
>;
126 typedef std::vector
<content::DownloadIdCallback
> IdCallbackVector
;
128 // content::NotificationObserver implementation.
129 void Observe(int type
,
130 const content::NotificationSource
& source
,
131 const content::NotificationDetails
& details
) override
;
133 // Callback function after the DownloadProtectionService completes.
134 void CheckClientDownloadDone(
136 safe_browsing::DownloadProtectionService::DownloadCheckResult result
);
138 // Internal gateways for ShouldCompleteDownload().
139 bool IsDownloadReadyForCompletion(
140 content::DownloadItem
* item
,
141 const base::Closure
& internal_complete_callback
);
142 void ShouldCompleteDownloadInternal(
144 const base::Closure
& user_complete_callback
);
146 void SetNextId(uint32 id
);
148 void ReturnNextId(const content::DownloadIdCallback
& callback
);
150 void OnDownloadTargetDetermined(
152 const content::DownloadTargetCallback
& callback
,
153 scoped_ptr
<DownloadTargetInfo
> target_info
);
155 // Returns true if |path| should open in the browser.
156 bool IsOpenInBrowserPreferreredForFile(const base::FilePath
& path
);
159 uint32 next_download_id_
;
160 IdCallbackVector id_callbacks_
;
161 scoped_ptr
<DownloadPrefs
> download_prefs_
;
163 #if defined(ENABLE_EXTENSIONS)
164 // Maps from pending extension installations to DownloadItem IDs.
165 typedef base::hash_map
<extensions::CrxInstaller
*,
166 content::DownloadOpenDelayedCallback
> CrxInstallerMap
;
167 CrxInstallerMap crx_installers_
;
170 content::NotificationRegistrar registrar_
;
172 base::WeakPtrFactory
<ChromeDownloadManagerDelegate
> weak_ptr_factory_
;
174 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate
);
177 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_