Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate.h

Issue 12850002: Move download filename determintion into a separate class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ 5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ 6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
7 7
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/hash_tables.h" 9 #include "base/hash_tables.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/common/cancelable_request.h"
13 #include "chrome/browser/download/download_path_reservation_tracker.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" 14 #include "chrome/browser/safe_browsing/download_protection_service.h"
15 #include "content/public/browser/download_danger_type.h" 15 #include "content/public/browser/download_danger_type.h"
16 #include "content/public/browser/download_item.h" 16 #include "content/public/browser/download_item.h"
17 #include "content/public/browser/download_manager_delegate.h" 17 #include "content/public/browser/download_manager_delegate.h"
18 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
20 20
21 class DownloadFilePickerFactory;
21 class DownloadPrefs; 22 class DownloadPrefs;
22 class PrefRegistrySyncable; 23 class PrefRegistrySyncable;
23 class Profile; 24 class Profile;
24 25
25 namespace content { 26 namespace content {
26 class DownloadManager; 27 class DownloadManager;
27 } 28 }
28 29
29 namespace extensions { 30 namespace extensions {
30 class CrxInstaller; 31 class CrxInstaller;
31 } 32 }
32 33
33 #if defined(COMPILER_GCC) 34 #if defined(COMPILER_GCC)
34 namespace BASE_HASH_NAMESPACE { 35 namespace BASE_HASH_NAMESPACE {
35 template<> 36 template<>
36 struct hash<extensions::CrxInstaller*> { 37 struct hash<extensions::CrxInstaller*> {
37 std::size_t operator()(extensions::CrxInstaller* const& p) const { 38 std::size_t operator()(extensions::CrxInstaller* const& p) const {
38 return reinterpret_cast<std::size_t>(p); 39 return reinterpret_cast<std::size_t>(p);
39 } 40 }
40 }; 41 };
41 } // namespace BASE_HASH_NAMESPACE 42 } // namespace BASE_HASH_NAMESPACE
42 #endif 43 #endif
43 44
44 // This is the Chrome side helper for the download system. 45 // This is the Chrome side helper for the download system.
45 class ChromeDownloadManagerDelegate 46 class ChromeDownloadManagerDelegate
46 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>, 47 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>,
47 public content::DownloadManagerDelegate, 48 public content::DownloadManagerDelegate,
48 public content::NotificationObserver { 49 public content::NotificationObserver,
50 public DownloadTargetDeterminerDelegate {
49 public: 51 public:
50 // Callback type used with ChooseDownloadPath(). The callback should be
51 // invoked with the user-selected path as the argument. If the file selection
52 // was canceled, the argument should be the empty path.
53 typedef base::Callback<void(const base::FilePath&)> FileSelectedCallback;
54
55 explicit ChromeDownloadManagerDelegate(Profile* profile); 52 explicit ChromeDownloadManagerDelegate(Profile* profile);
56 53
57 static void RegisterUserPrefs(PrefRegistrySyncable* registry); 54 static void RegisterUserPrefs(PrefRegistrySyncable* registry);
58 55
59 // Should be called before the first call to ShouldCompleteDownload() to 56 // Should be called before the first call to ShouldCompleteDownload() to
60 // disable SafeBrowsing checks for |item|. 57 // disable SafeBrowsing checks for |item|.
61 static void DisableSafeBrowsing(content::DownloadItem* item); 58 static void DisableSafeBrowsing(content::DownloadItem* item);
62 59
63 void SetDownloadManager(content::DownloadManager* dm); 60 void SetDownloadManager(content::DownloadManager* dm);
64 61
(...skipping 25 matching lines...) Expand all
90 virtual void OpenDownload(content::DownloadItem* download) OVERRIDE; 87 virtual void OpenDownload(content::DownloadItem* download) OVERRIDE;
91 virtual void ShowDownloadInShell(content::DownloadItem* download) OVERRIDE; 88 virtual void ShowDownloadInShell(content::DownloadItem* download) OVERRIDE;
92 virtual void CheckForFileExistence( 89 virtual void CheckForFileExistence(
93 content::DownloadItem* download, 90 content::DownloadItem* download,
94 const content::CheckForFileExistenceCallback& callback) OVERRIDE; 91 const content::CheckForFileExistenceCallback& callback) OVERRIDE;
95 92
96 // Clears the last directory chosen by the user in response to a file chooser 93 // Clears the last directory chosen by the user in response to a file chooser
97 // prompt. Called when clearing recent history. 94 // prompt. Called when clearing recent history.
98 void ClearLastDownloadPath(); 95 void ClearLastDownloadPath();
99 96
97 void SetFilePickerFactoryForTesting(
98 scoped_ptr<DownloadFilePickerFactory> factory);
99
100 DownloadPrefs* download_prefs() { return download_prefs_.get(); } 100 DownloadPrefs* download_prefs() { return download_prefs_.get(); }
101 101
102 protected: 102 protected:
103 // So that test classes can inherit from this for override purposes. 103 // So that test classes can inherit from this for override purposes.
104 virtual ~ChromeDownloadManagerDelegate(); 104 virtual ~ChromeDownloadManagerDelegate();
105 105
106 // Returns the SafeBrowsing download protection service if it's
107 // enabled. Returns NULL otherwise. Protected virtual for testing.
108 virtual safe_browsing::DownloadProtectionService*
109 GetDownloadProtectionService();
110
111 // Returns true if this download should show the "dangerous file" warning.
112 // Various factors are considered, such as the type of the file, whether a
113 // user action initiated the download, and whether the user has explicitly
114 // marked the file type as "auto open". Protected virtual for testing.
115 virtual bool IsDangerousFile(const content::DownloadItem& download,
116 const base::FilePath& suggested_path,
117 bool visited_referrer_before);
118
119 // Obtains a path reservation by calling
120 // DownloadPathReservationTracker::GetReservedPath(). Protected virtual for
121 // testing.
122 virtual void GetReservedPath(
123 content::DownloadItem& download,
124 const base::FilePath& target_path,
125 const base::FilePath& default_download_path,
126 bool should_uniquify_path,
127 const DownloadPathReservationTracker::ReservedPathCallback& callback);
128
129 // Displays the file chooser dialog to prompt the user for the download
130 // location for |item|. |suggested_path| will be used as the initial download
131 // path. Once a location is available |callback| will be invoked with the
132 // selected full path. If the user cancels the dialog, then an empty FilePath
133 // will be passed into |callback|. Protected virtual for testing.
134 virtual void ChooseDownloadPath(content::DownloadItem* item,
135 const base::FilePath& suggested_path,
136 const FileSelectedCallback& callback);
137
138 // So that test classes that inherit from this for override purposes 106 // So that test classes that inherit from this for override purposes
139 // can call back into the DownloadManager. 107 // can call back into the DownloadManager.
140 scoped_refptr<content::DownloadManager> download_manager_; 108 scoped_refptr<content::DownloadManager> download_manager_;
141 109
110 // DownloadTargetDeterminerDelegate. Protected for testing.
111 virtual safe_browsing::DownloadProtectionService*
112 GetDownloadProtectionService() OVERRIDE;
113 virtual void DetermineLocalPath(content::DownloadItem* download,
114 const base::FilePath& virtual_path,
115 const LocalPathCallback& callback) OVERRIDE;
116 virtual void ReserveVirtualPath(
117 content::DownloadItem* download,
118 const base::FilePath& virtual_path,
119 bool should_uniquify_path,
120 const DownloadTargetDeterminerDelegate::ReservedPathCallback&
121 callback) OVERRIDE;
122
142 private: 123 private:
143 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>; 124 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>;
144 125
145 struct ContinueFilenameDeterminationInfo;
146
147 // content::NotificationObserver implementation. 126 // content::NotificationObserver implementation.
148 virtual void Observe(int type, 127 virtual void Observe(int type,
149 const content::NotificationSource& source, 128 const content::NotificationSource& source,
150 const content::NotificationDetails& details) OVERRIDE; 129 const content::NotificationDetails& details) OVERRIDE;
151 130
152 // Callback function after url is checked with safebrowsing service. 131 // Callback invoked by DownloadTargetDeterminer on completion.
153 void CheckDownloadUrlDone( 132 void OnDetermineDownloadTargetDone(
154 int32 download_id,
155 const content::DownloadTargetCallback& callback, 133 const content::DownloadTargetCallback& callback,
156 safe_browsing::DownloadProtectionService::DownloadCheckResult result); 134 const base::FilePath& virtual_path,
135 const base::FilePath& local_path,
136 const base::FilePath& intermediate_path,
137 content::DownloadItem::TargetDisposition target_disposition,
138 content::DownloadDangerType danger_type);
157 139
158 // Callback function after the DownloadProtectionService completes. 140 // Callback function after the DownloadProtectionService completes.
159 void CheckClientDownloadDone( 141 void CheckClientDownloadDone(
160 int32 download_id, 142 int32 download_id,
161 safe_browsing::DownloadProtectionService::DownloadCheckResult result); 143 safe_browsing::DownloadProtectionService::DownloadCheckResult result);
162 144
163 // Callback function after we check whether the referrer URL has been visited
164 // before today. Determines the danger state of the download based on the file
165 // type and |visited_referrer_before|. Generates a target path for the
166 // download. Invokes |DownloadPathReservationTracker::GetReservedPath| to get
167 // a reserved path for the download. The path is then passed into
168 // OnPathReservationAvailable().
169 void CheckVisitedReferrerBeforeDone(
170 int32 download_id,
171 const content::DownloadTargetCallback& callback,
172 content::DownloadDangerType danger_type,
173 bool visited_referrer_before);
174
175 #if defined (OS_CHROMEOS)
176 // DriveDownloadObserver::SubstituteDriveDownloadPath callback. Calls
177 // |DownloadPathReservationTracker::GetReservedPath| to get a reserved path
178 // for the download. The path is then passed into
179 // OnPathReservationAvailable().
180 void SubstituteDriveDownloadPathCallback(
181 int32 download_id,
182 const content::DownloadTargetCallback& callback,
183 bool should_prompt,
184 bool is_forced_path,
185 content::DownloadDangerType danger_type,
186 const base::FilePath& unverified_path);
187 #endif
188
189 // Called on the UI thread once a reserved path is available. Updates the
190 // download identified by |download_id| with the |target_path|, target
191 // disposition and |danger_type|.
192 void OnPathReservationAvailable(
193 int32 download_id,
194 const content::DownloadTargetCallback& callback,
195 bool should_prompt,
196 content::DownloadDangerType danger_type,
197 const base::FilePath& reserved_path,
198 bool reserved_path_verified);
199
200 // When an extension opts to change a download's target filename, this
201 // sanitizes it before continuing with the filename determination process.
202 void OnExtensionOverridingFilename(
203 const ContinueFilenameDeterminationInfo& continue_info,
204 const base::FilePath& changed_filename,
205 bool overwrite);
206
207 // When extensions either opt not to change a download's target filename, or
208 // the changed filename has been sanitized, this method continues with the
209 // filename determination process, optionally prompting the user to manually
210 // set the filename.
211 void ContinueDeterminingFilename(
212 const ContinueFilenameDeterminationInfo& continue_info,
213 const base::FilePath& suggested_path,
214 bool is_forced_path);
215
216 // Called on the UI thread once the final target path is available.
217 void OnTargetPathDetermined(
218 int32 download_id,
219 const content::DownloadTargetCallback& callback,
220 content::DownloadItem::TargetDisposition disposition,
221 content::DownloadDangerType danger_type,
222 const base::FilePath& target_path);
223
224 // Internal gateways for ShouldCompleteDownload(). 145 // Internal gateways for ShouldCompleteDownload().
225 bool IsDownloadReadyForCompletion( 146 bool IsDownloadReadyForCompletion(
226 content::DownloadItem* item, 147 content::DownloadItem* item,
227 const base::Closure& internal_complete_callback); 148 const base::Closure& internal_complete_callback);
228 void ShouldCompleteDownloadInternal( 149 void ShouldCompleteDownloadInternal(
229 int download_id, 150 int download_id,
230 const base::Closure& user_complete_callback); 151 const base::Closure& user_complete_callback);
231 152
232 Profile* profile_; 153 Profile* profile_;
233 int next_download_id_; 154 int next_download_id_;
234 scoped_ptr<DownloadPrefs> download_prefs_; 155 scoped_ptr<DownloadPrefs> download_prefs_;
156 scoped_ptr<DownloadFilePickerFactory> file_picker_factory_;
235 157
236 // Maps from pending extension installations to DownloadItem IDs. 158 // Maps from pending extension installations to DownloadItem IDs.
237 typedef base::hash_map<extensions::CrxInstaller*, 159 typedef base::hash_map<extensions::CrxInstaller*,
238 content::DownloadOpenDelayedCallback> CrxInstallerMap; 160 content::DownloadOpenDelayedCallback> CrxInstallerMap;
239 CrxInstallerMap crx_installers_; 161 CrxInstallerMap crx_installers_;
240 162
241 CancelableRequestConsumer history_consumer_;
242
243 content::NotificationRegistrar registrar_; 163 content::NotificationRegistrar registrar_;
244 164
245 // The directory most recently chosen by the user in response to a Save As 165 // The directory most recently chosen by the user in response to a Save As
246 // dialog for a regular download. 166 // dialog for a regular download.
247 base::FilePath last_download_path_; 167 base::FilePath last_download_path_;
248 168
249 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); 169 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate);
250 }; 170 };
251 171
252 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ 172 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698