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

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

Issue 3071005: Download code cleanup patch of death: (Closed)
Patch Set: inline the dtor Created 10 years, 4 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
« no previous file with comments | « chrome/browser/download/download_item.cc ('k') | chrome/browser/download/download_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 // The DownloadManager object manages the process of downloading, including 5 // The DownloadManager object manages the process of downloading, including
6 // updates to the history system and providing the information for displaying 6 // updates to the history system and providing the information for displaying
7 // the downloads view in the Destinations tab. There is one DownloadManager per 7 // the downloads view in the Destinations tab. There is one DownloadManager per
8 // active profile in Chrome. 8 // active profile in Chrome.
9 // 9 //
10 // Download observers: 10 // Download observers:
(...skipping 19 matching lines...) Expand all
30 30
31 #include <map> 31 #include <map>
32 #include <set> 32 #include <set>
33 #include <string> 33 #include <string>
34 #include <vector> 34 #include <vector>
35 35
36 #include "base/basictypes.h" 36 #include "base/basictypes.h"
37 #include "base/file_path.h" 37 #include "base/file_path.h"
38 #include "base/observer_list.h" 38 #include "base/observer_list.h"
39 #include "base/ref_counted.h" 39 #include "base/ref_counted.h"
40 #include "base/scoped_ptr.h"
40 #include "base/time.h" 41 #include "base/time.h"
41 #include "chrome/browser/cancelable_request.h" 42 #include "chrome/browser/download/download_history.h"
42 #include "chrome/browser/history/history.h"
43 #include "chrome/browser/pref_member.h" 43 #include "chrome/browser/pref_member.h"
44 #include "chrome/browser/shell_dialogs.h" 44 #include "chrome/browser/shell_dialogs.h"
45 45
46 class DownloadFileManager; 46 class DownloadFileManager;
47 class DownloadItem; 47 class DownloadItem;
48 class GURL; 48 class GURL;
49 class PrefService; 49 class PrefService;
50 class Profile; 50 class Profile;
51 class ResourceDispatcherHost; 51 class ResourceDispatcherHost;
52 class URLRequestContextGetter; 52 class URLRequestContextGetter;
53 class TabContents; 53 class TabContents;
54 struct DownloadCreateInfo;
54 struct DownloadSaveInfo; 55 struct DownloadSaveInfo;
55 56
56 // Browser's download manager: manages all downloads and destination view. 57 // Browser's download manager: manages all downloads and destination view.
57 class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, 58 class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>,
59 public DownloadHistory::DownloadItemMapper,
58 public SelectFileDialog::Listener { 60 public SelectFileDialog::Listener {
59 // For testing. 61 // For testing.
60 friend class DownloadManagerTest; 62 friend class DownloadManagerTest;
61 friend class MockDownloadManager; 63 friend class MockDownloadManager;
62 64
63 public: 65 public:
64 // A fake download table ID which representas a download that has started,
65 // but is not yet in the table.
66 static const int kUninitializedHandle;
67
68 DownloadManager(); 66 DownloadManager();
69 67
70 static void RegisterUserPrefs(PrefService* prefs); 68 static void RegisterUserPrefs(PrefService* prefs);
71 69
72 // Interface to implement for observers that wish to be informed of changes 70 // Interface to implement for observers that wish to be informed of changes
73 // to the DownloadManager's collection of downloads. 71 // to the DownloadManager's collection of downloads.
74 class Observer { 72 class Observer {
75 public: 73 public:
76 // New or deleted download, observers should query us for the current set 74 // New or deleted download, observers should query us for the current set
77 // of downloads. 75 // of downloads.
78 virtual void ModelChanged() = 0; 76 virtual void ModelChanged() = 0;
79 77
80 // A callback once the DownloadManager has retrieved the requested set of
81 // downloads. The DownloadManagerObserver must copy the vector, but does not
82 // own the individual DownloadItems, when this call is made.
83 virtual void SetDownloads(std::vector<DownloadItem*>& downloads) = 0;
84
85 // Called when the DownloadManager is being destroyed to prevent Observers 78 // Called when the DownloadManager is being destroyed to prevent Observers
86 // from calling back to a stale pointer. 79 // from calling back to a stale pointer.
87 virtual void ManagerGoingDown() {} 80 virtual void ManagerGoingDown() {}
88 81
89 protected: 82 protected:
90 virtual ~Observer() {} 83 virtual ~Observer() {}
91 }; 84 };
92 85
93 // Public API
94
95 // If this download manager has an incognito profile, find all incognito
96 // downloads and pass them along to the parent profile's download manager
97 // via DoGetDownloads. Otherwise, just call DoGetDownloads().
98 void GetDownloads(Observer* observer,
99 const std::wstring& search_text);
100
101 // Begin a search for all downloads matching 'search_text'. If 'search_text'
102 // is empty, return all known downloads. The results are returned in the
103 // 'SetDownloads' observer callback.
104 void DoGetDownloads(Observer* observer,
105 const std::wstring& search_text,
106 std::vector<DownloadItem*>& otr_downloads);
107
108 // Return all temporary downloads that reside in the specified directory. 86 // Return all temporary downloads that reside in the specified directory.
109 void GetTemporaryDownloads(Observer* observer, 87 void GetTemporaryDownloads(const FilePath& dir_path,
110 const FilePath& dir_path); 88 std::vector<DownloadItem*>* result);
111 89
112 // Return all non-temporary downloads in the specified directory that are 90 // Return all non-temporary downloads in the specified directory that are
113 // are in progress or have finished. 91 // are in progress or have finished.
114 void GetAllDownloads(Observer* observer, const FilePath& dir_path); 92 void GetAllDownloads(const FilePath& dir_path,
93 std::vector<DownloadItem*>* result);
115 94
116 // Return all non-temporary downloads in the specified directory that are 95 // Return all non-temporary downloads in the specified directory that are
117 // either in-progress or finished but still waiting for user confirmation. 96 // either in-progress or finished but still waiting for user confirmation.
118 void GetCurrentDownloads(Observer* observer, const FilePath& dir_path); 97 void GetCurrentDownloads(const FilePath& dir_path,
98 std::vector<DownloadItem*>* result);
119 99
120 // Returns true if initialized properly. 100 // Returns true if initialized properly.
121 bool Init(Profile* profile); 101 bool Init(Profile* profile);
122 102
123 // Schedule a query of the history service to retrieve all downloads.
124 void QueryHistoryForDownloads();
125
126 // Cleans up IN_PROGRESS history entries as these entries are corrupt because
127 // of the sudden exit. Changes them to CANCELED. Executed only when called
128 // first time, subsequent calls a no op.
129 void CleanUpInProgressHistoryEntries();
130
131 // Notifications sent from the download thread to the UI thread 103 // Notifications sent from the download thread to the UI thread
132 void StartDownload(DownloadCreateInfo* info); 104 void StartDownload(DownloadCreateInfo* info);
133 void UpdateDownload(int32 download_id, int64 size); 105 void UpdateDownload(int32 download_id, int64 size);
134 void DownloadFinished(int32 download_id, int64 size); 106 void DownloadFinished(int32 download_id, int64 size);
135 107
136 // Called from a view when a user clicks a UI button or link. 108 // Called from a view when a user clicks a UI button or link.
137 void DownloadCancelled(int32 download_id); 109 void DownloadCancelled(int32 download_id);
138 void PauseDownload(int32 download_id, bool pause); 110 void PauseDownload(int32 download_id, bool pause);
139 void RemoveDownload(int64 download_handle); 111 void RemoveDownload(int64 download_handle);
140 112
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 // Allow objects to observe the download creation process. 146 // Allow objects to observe the download creation process.
175 void AddObserver(Observer* observer); 147 void AddObserver(Observer* observer);
176 148
177 // Remove a download observer from ourself. 149 // Remove a download observer from ourself.
178 void RemoveObserver(Observer* observer); 150 void RemoveObserver(Observer* observer);
179 151
180 // Methods called on completion of a query sent to the history system. 152 // Methods called on completion of a query sent to the history system.
181 void OnQueryDownloadEntriesComplete( 153 void OnQueryDownloadEntriesComplete(
182 std::vector<DownloadCreateInfo>* entries); 154 std::vector<DownloadCreateInfo>* entries);
183 void OnCreateDownloadEntryComplete(DownloadCreateInfo info, int64 db_handle); 155 void OnCreateDownloadEntryComplete(DownloadCreateInfo info, int64 db_handle);
184 void OnSearchComplete(HistoryService::Handle handle,
185 std::vector<int64>* results);
186 156
187 // Display a new download in the appropriate browser UI. 157 // Display a new download in the appropriate browser UI.
188 void ShowDownloadInBrowser(const DownloadCreateInfo& info, 158 void ShowDownloadInBrowser(const DownloadCreateInfo& info,
189 DownloadItem* download); 159 DownloadItem* download);
190 160
191 // Opens a download. For Chrome extensions call 161 // Opens a download. For Chrome extensions call
192 // ExtensionsServices::InstallExtension, for everything else call 162 // ExtensionsServices::InstallExtension, for everything else call
193 // OpenDownloadInShell. 163 // OpenDownloadInShell.
194 void OpenDownload(const DownloadItem* download, 164 void OpenDownload(const DownloadItem* download,
195 gfx::NativeView parent_window); 165 gfx::NativeView parent_window);
196 166
197 // Show a download via the Windows shell. 167 // Show a download via the Windows shell.
198 void ShowDownloadInShell(const DownloadItem* download); 168 void ShowDownloadInShell(const DownloadItem* download);
199 169
200 // The number of in progress (including paused) downloads. 170 // The number of in progress (including paused) downloads.
201 int in_progress_count() const { 171 int in_progress_count() const {
202 return static_cast<int>(in_progress_.size()); 172 return static_cast<int>(in_progress_.size());
203 } 173 }
204 174
205 FilePath download_path() { return *download_path_; } 175 FilePath download_path() { return *download_path_; }
206 176
177 DownloadHistory* download_history() { return download_history_.get(); }
178
207 // Clears the last download path, used to initialize "save as" dialogs. 179 // Clears the last download path, used to initialize "save as" dialogs.
208 void ClearLastDownloadPath(); 180 void ClearLastDownloadPath();
209 181
210 // Registers this file extension for automatic opening upon download 182 // Registers this file extension for automatic opening upon download
211 // completion if 'open' is true, or prevents the extension from automatic 183 // completion if 'open' is true, or prevents the extension from automatic
212 // opening if 'open' is false. 184 // opening if 'open' is false.
213 void OpenFilesBasedOnExtension(const FilePath& path, bool open); 185 void OpenFilesBasedOnExtension(const FilePath& path, bool open);
214 186
215 // Tests if a file type should be opened automatically. 187 // Tests if a file type should be opened automatically.
216 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; 188 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const;
217 189
218 // Tests if we think the server means for this mime_type to be executable. 190 // Tests if we think the server means for this mime_type to be executable.
219 static bool IsExecutableMimeType(const std::string& mime_type); 191 static bool IsExecutableMimeType(const std::string& mime_type);
220 192
221 // Tests if a file is considered executable, based on its type. 193 // Tests if a file is considered executable, based on its type.
222 bool IsExecutableFile(const FilePath& path) const; 194 bool IsExecutableFile(const FilePath& path) const;
223 195
224 // Tests if a file type is considered executable. 196 // Tests if a file type is considered executable.
225 static bool IsExecutableExtension(const FilePath::StringType& extension); 197 static bool IsExecutableExtension(const FilePath::StringType& extension);
226 198
227 // Resets the automatic open preference. 199 // Resets the automatic open preference.
228 void ResetAutoOpenFiles(); 200 void ResetAutoOpenFiles();
229 201
230 // Returns true if there are automatic handlers registered for any file 202 // Returns true if there are automatic handlers registered for any file
231 // types. 203 // types.
232 bool HasAutoOpenFileTypesRegistered() const; 204 bool HasAutoOpenFileTypesRegistered() const;
233 205
206 // Overridden from DownloadHistory::DownloadItemMapper:
207 virtual DownloadItem* GetDownloadItemFromDbHandle(int64 db_handle);
208
234 // Overridden from SelectFileDialog::Listener: 209 // Overridden from SelectFileDialog::Listener:
235 virtual void FileSelected(const FilePath& path, int index, void* params); 210 virtual void FileSelected(const FilePath& path, int index, void* params);
236 virtual void FileSelectionCanceled(void* params); 211 virtual void FileSelectionCanceled(void* params);
237 212
238 // Deletes the specified path on the file thread. 213 // Deletes the specified path on the file thread.
239 void DeleteDownload(const FilePath& path); 214 void DeleteDownload(const FilePath& path);
240 215
241 // Called when the user has validated the download of a dangerous file. 216 // Called when the user has validated the download of a dangerous file.
242 void DangerousDownloadValidated(DownloadItem* download); 217 void DangerousDownloadValidated(DownloadItem* download);
243 218
244 // Used to make sure we have a safe file extension and filename for a 219 // Used to make sure we have a safe file extension and filename for a
245 // download. |file_name| can either be just the file name or it can be a 220 // download. |file_name| can either be just the file name or it can be a
246 // full path to a file. 221 // full path to a file.
247 static void GenerateSafeFileName(const std::string& mime_type, 222 static void GenerateSafeFileName(const std::string& mime_type,
248 FilePath* file_name); 223 FilePath* file_name);
249 224
250 // Create a file name based on the response from the server. 225 // Create a file name based on the response from the server.
251 static void GenerateFileName(const GURL& url, 226 static void GenerateFileName(const GURL& url,
252 const std::string& content_disposition, 227 const std::string& content_disposition,
253 const std::string& referrer_charset, 228 const std::string& referrer_charset,
254 const std::string& mime_type, 229 const std::string& mime_type,
255 FilePath* generated_name); 230 FilePath* generated_name);
256 231
257 private: 232 private:
258 class FakeDbHandleGenerator {
259 public:
260 explicit FakeDbHandleGenerator(int64 start_value) : value_(start_value) {}
261
262 int64 GetNext() { return value_--; }
263 private:
264 int64 value_;
265 };
266
267 // This class is used to let an incognito DownloadManager observe changes to 233 // This class is used to let an incognito DownloadManager observe changes to
268 // a normal DownloadManager, to propagate ModelChanged() calls from the parent 234 // a normal DownloadManager, to propagate ModelChanged() calls from the parent
269 // DownloadManager to the observers of the incognito DownloadManager. 235 // DownloadManager to the observers of the incognito DownloadManager.
270 class OtherDownloadManagerObserver : public Observer { 236 class OtherDownloadManagerObserver : public Observer {
271 public: 237 public:
272 explicit OtherDownloadManagerObserver( 238 explicit OtherDownloadManagerObserver(
273 DownloadManager* observing_download_manager); 239 DownloadManager* observing_download_manager);
274 virtual ~OtherDownloadManagerObserver(); 240 virtual ~OtherDownloadManagerObserver();
275 241
276 // Observer interface. 242 // Observer interface.
277 virtual void ModelChanged(); 243 virtual void ModelChanged();
278 virtual void SetDownloads(std::vector<DownloadItem*>& downloads);
279 virtual void ManagerGoingDown(); 244 virtual void ManagerGoingDown();
280 245
281 private: 246 private:
282 // The incognito download manager. 247 // The incognito download manager.
283 DownloadManager* observing_download_manager_; 248 DownloadManager* observing_download_manager_;
284 249
285 // The original profile's download manager. 250 // The original profile's download manager.
286 DownloadManager* observed_download_manager_; 251 DownloadManager* observed_download_manager_;
287 }; 252 };
288 253
(...skipping 23 matching lines...) Expand all
312 // Called on the UI thread once the DownloadManager has determined whether the 277 // Called on the UI thread once the DownloadManager has determined whether the
313 // suggested file path exists. 278 // suggested file path exists.
314 void OnPathExistenceAvailable(DownloadCreateInfo* info); 279 void OnPathExistenceAvailable(DownloadCreateInfo* info);
315 280
316 // Called back after a target path for the file to be downloaded to has been 281 // Called back after a target path for the file to be downloaded to has been
317 // determined, either automatically based on the suggested file name, or by 282 // determined, either automatically based on the suggested file name, or by
318 // the user in a Save As dialog box. 283 // the user in a Save As dialog box.
319 void ContinueStartDownload(DownloadCreateInfo* info, 284 void ContinueStartDownload(DownloadCreateInfo* info,
320 const FilePath& target_path); 285 const FilePath& target_path);
321 286
322 // Update the history service for a particular download.
323 // Marked virtual for testing.
324 virtual void UpdateHistoryForDownload(DownloadItem* download);
325 void RemoveDownloadFromHistory(DownloadItem* download);
326 void RemoveDownloadsFromHistoryBetween(const base::Time remove_begin,
327 const base::Time remove_before);
328
329 // Create an extension based on the file name and mime type. 287 // Create an extension based on the file name and mime type.
330 static void GenerateExtension(const FilePath& file_name, 288 static void GenerateExtension(const FilePath& file_name,
331 const std::string& mime_type, 289 const std::string& mime_type,
332 FilePath::StringType* generated_extension); 290 FilePath::StringType* generated_extension);
333 291
334 // Create a file name based on the response from the server. 292 // Create a file name based on the response from the server.
335 static void GenerateFileNameFromInfo(DownloadCreateInfo* info, 293 static void GenerateFileNameFromInfo(DownloadCreateInfo* info,
336 FilePath* generated_name); 294 FilePath* generated_name);
337 295
338 // Persist the automatic opening preference. 296 // Persist the automatic opening preference.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 // True if the download manager has been initialized and requires a shutdown. 376 // True if the download manager has been initialized and requires a shutdown.
419 bool shutdown_needed_; 377 bool shutdown_needed_;
420 378
421 // Observers that want to be notified of changes to the set of downloads. 379 // Observers that want to be notified of changes to the set of downloads.
422 ObserverList<Observer> observers_; 380 ObserverList<Observer> observers_;
423 381
424 // The current active profile. 382 // The current active profile.
425 Profile* profile_; 383 Profile* profile_;
426 scoped_refptr<URLRequestContextGetter> request_context_getter_; 384 scoped_refptr<URLRequestContextGetter> request_context_getter_;
427 385
428 // Used for history service request management. 386 scoped_ptr<DownloadHistory> download_history_;
429 CancelableRequestConsumerTSimple<Observer*> cancelable_consumer_;
430 387
431 // Non-owning pointer for handling file writing on the download_thread_. 388 // Non-owning pointer for handling file writing on the download_thread_.
432 DownloadFileManager* file_manager_; 389 DownloadFileManager* file_manager_;
433 390
434 // User preferences 391 // User preferences
435 BooleanPrefMember prompt_for_download_; 392 BooleanPrefMember prompt_for_download_;
436 FilePathPrefMember download_path_; 393 FilePathPrefMember download_path_;
437 394
438 // The user's last choice for download directory. This is only used when the 395 // The user's last choice for download directory. This is only used when the
439 // user wants us to prompt for a save location for each download. 396 // user wants us to prompt for a save location for each download.
(...skipping 14 matching lines...) Expand all
454 // after this determination is made. 411 // after this determination is made.
455 // The map is of download_id->remaining size (bytes), both of which are 412 // The map is of download_id->remaining size (bytes), both of which are
456 // required when calling DownloadFinished. 413 // required when calling DownloadFinished.
457 typedef std::map<int32, int64> PendingFinishedMap; 414 typedef std::map<int32, int64> PendingFinishedMap;
458 PendingFinishedMap pending_finished_downloads_; 415 PendingFinishedMap pending_finished_downloads_;
459 416
460 // The "Save As" dialog box used to ask the user where a file should be 417 // The "Save As" dialog box used to ask the user where a file should be
461 // saved. 418 // saved.
462 scoped_refptr<SelectFileDialog> select_file_dialog_; 419 scoped_refptr<SelectFileDialog> select_file_dialog_;
463 420
464 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead.
465 // This is useful for incognito mode or when the history database is offline.
466 // Downloads are expected to have unique handles, so FakeDbHandleGenerator
467 // automatically decrement the handle value on every use.
468 FakeDbHandleGenerator fake_db_handle_;
469
470 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; 421 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_;
471 422
472 DISALLOW_COPY_AND_ASSIGN(DownloadManager); 423 DISALLOW_COPY_AND_ASSIGN(DownloadManager);
473 }; 424 };
474 425
475 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ 426 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/download/download_item.cc ('k') | chrome/browser/download/download_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698