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

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

Issue 6043: Added dangerous download prompting. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 2 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 // Each download is represented by a DownloadItem, and all DownloadItems 10 // Each download is represented by a DownloadItem, and all DownloadItems
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // Destination tab's download view, may refer to a given DownloadItem. 73 // Destination tab's download view, may refer to a given DownloadItem.
74 class DownloadItem { 74 class DownloadItem {
75 public: 75 public:
76 enum DownloadState { 76 enum DownloadState {
77 IN_PROGRESS, 77 IN_PROGRESS,
78 COMPLETE, 78 COMPLETE,
79 CANCELLED, 79 CANCELLED,
80 REMOVING 80 REMOVING
81 }; 81 };
82 82
83 enum SafetyState {
84 SAFE = 0,
85 DANGEROUS,
86 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download.
87 };
88
83 // Interface that observers of a particular download must implement in order 89 // Interface that observers of a particular download must implement in order
84 // to receive updates to the download's status. 90 // to receive updates to the download's status.
85 class Observer { 91 class Observer {
86 public: 92 public:
87 virtual void OnDownloadUpdated(DownloadItem* download) = 0; 93 virtual void OnDownloadUpdated(DownloadItem* download) = 0;
88 }; 94 };
89 95
90 // Constructing from persistent store: 96 // Constructing from persistent store:
91 DownloadItem(const DownloadCreateInfo& info); 97 DownloadItem(const DownloadCreateInfo& info);
92 98
93 // Constructing from user action: 99 // Constructing from user action:
94 DownloadItem(int32 download_id, 100 DownloadItem(int32 download_id,
95 const std::wstring& path, 101 const std::wstring& path,
96 const std::wstring& url, 102 const std::wstring& url,
103 const std::wstring& original_name,
97 const Time start_time, 104 const Time start_time,
98 int64 download_size, 105 int64 download_size,
99 int render_process_id, 106 int render_process_id,
100 int request_id); 107 int request_id,
108 bool is_dangerous);
101 109
102 ~DownloadItem(); 110 ~DownloadItem();
103 111
104 void Init(bool start_timer); 112 void Init(bool start_timer);
105 113
106 // Public API 114 // Public API
107 115
108 void AddObserver(Observer* observer); 116 void AddObserver(Observer* observer);
109 void RemoveObserver(Observer* observer); 117 void RemoveObserver(Observer* observer);
110 118
111 // Notify our observers periodically 119 // Notify our observers periodically
112 void UpdateObservers(); 120 void UpdateObservers();
113 121
114 // Received a new chunk of data 122 // Received a new chunk of data
115 void Update(int64 bytes_so_far); 123 void Update(int64 bytes_so_far);
116 124
117 // Cancel the download operation. We need to distinguish between cancels at 125 // Cancel the download operation. We need to distinguish between cancels at
118 // exit (DownloadManager destructor) from user interface initiated cancels 126 // exit (DownloadManager destructor) from user interface initiated cancels
119 // because at exit, the history system may not exist, and any updates to it 127 // because at exit, the history system may not exist, and any updates to it
120 // require AddRef'ing the DownloadManager in the destructor which results in 128 // require AddRef'ing the DownloadManager in the destructor which results in
121 // a DCHECK failure. Set 'update_history' to false when canceling from at 129 // a DCHECK failure. Set 'update_history' to false when canceling from at
122 // exit to prevent this crash. This may result in a difference between the 130 // exit to prevent this crash. This may result in a difference between the
123 // downloaded file's size on disk, and what the history system's last record 131 // downloaded file's size on disk, and what the history system's last record
124 // of it is. At worst, we'll end up re-downloading a small portion of the file 132 // of it is. At worst, we'll end up re-downloading a small portion of the file
125 // when resuming a download (assuming the server supports byte ranges). 133 // when resuming a download (assuming the server supports byte ranges).
126 void Cancel(bool update_history); 134 void Cancel(bool update_history);
127 135
128 // Download operation completed 136 // Download operation completed.
129 void Finished(int64 size); 137 void Finished(int64 size);
130 138
131 // The user wants to remove the download from the views and history. This 139 // The user wants to remove the download from the views and history. If
132 // operation does not delete the file on the disk. 140 // |delete_file| is true, the file is deleted on the disk.
133 void Remove(); 141 void Remove(bool delete_file);
134 142
135 // Start/stop sending periodic updates to our observers 143 // Start/stop sending periodic updates to our observers
136 void StartProgressTimer(); 144 void StartProgressTimer();
137 void StopProgressTimer(); 145 void StopProgressTimer();
138 146
139 // Simple calculation of the amount of time remaining to completion. Fills 147 // Simple calculation of the amount of time remaining to completion. Fills
140 // |*remaining| with the amount of time remaining if successful. Fails and 148 // |*remaining| with the amount of time remaining if successful. Fails and
141 // returns false if we do not have the number of bytes or the speed so can 149 // returns false if we do not have the number of bytes or the speed so can
142 // not estimate. 150 // not estimate.
143 bool TimeRemaining(TimeDelta* remaining) const; 151 bool TimeRemaining(TimeDelta* remaining) const;
144 152
145 // Simple speed estimate in bytes/s 153 // Simple speed estimate in bytes/s
146 int64 CurrentSpeed() const; 154 int64 CurrentSpeed() const;
147 155
148 // Rough percent complete, -1 means we don't know (since we didn't receive a 156 // Rough percent complete, -1 means we don't know (since we didn't receive a
149 // total size). 157 // total size).
150 int PercentComplete() const; 158 int PercentComplete() const;
151 159
152 // Update the download's path, the actual file is renamed on the download 160 // Update the download's path, the actual file is renamed on the download
153 // thread. 161 // thread.
154 void Rename(const std::wstring& full_path); 162 void Rename(const std::wstring& full_path);
155 163
156 // Allow the user to temporarily pause a download or resume a paused download. 164 // Allow the user to temporarily pause a download or resume a paused download.
157 void TogglePause(); 165 void TogglePause();
158 166
159 // Accessors 167 // Accessors
160 DownloadState state() const { return state_; } 168 DownloadState state() const { return state_; }
169 std::wstring file_name() const { return file_name_; }
170 void set_file_name(const std::wstring& name) { file_name_ = name; }
161 std::wstring full_path() const { return full_path_; } 171 std::wstring full_path() const { return full_path_; }
162 std::wstring file_name() const { return file_name_; } 172 void set_full_path(const std::wstring& path) { full_path_ = path; }
163 std::wstring url() const { return url_; } 173 std::wstring url() const { return url_; }
164 int64 total_bytes() const { return total_bytes_; } 174 int64 total_bytes() const { return total_bytes_; }
165 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } 175 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; }
166 int64 received_bytes() const { return received_bytes_; } 176 int64 received_bytes() const { return received_bytes_; }
167 int32 id() const { return id_; } 177 int32 id() const { return id_; }
168 Time start_time() const { return start_time_; } 178 Time start_time() const { return start_time_; }
169 void set_db_handle(int64 handle) { db_handle_ = handle; } 179 void set_db_handle(int64 handle) { db_handle_ = handle; }
170 int64 db_handle() const { return db_handle_; } 180 int64 db_handle() const { return db_handle_; }
171 DownloadManager* manager() const { return manager_; } 181 DownloadManager* manager() const { return manager_; }
172 void set_manager(DownloadManager* manager) { manager_ = manager; } 182 void set_manager(DownloadManager* manager) { manager_ = manager; }
173 bool is_paused() const { return is_paused_; } 183 bool is_paused() const { return is_paused_; }
174 void set_is_paused(bool pause) { is_paused_ = pause; } 184 void set_is_paused(bool pause) { is_paused_ = pause; }
175 bool open_when_complete() const { return open_when_complete_; } 185 bool open_when_complete() const { return open_when_complete_; }
176 void set_open_when_complete(bool open) { open_when_complete_ = open; } 186 void set_open_when_complete(bool open) { open_when_complete_ = open; }
177 int render_process_id() const { return render_process_id_; } 187 int render_process_id() const { return render_process_id_; }
178 int request_id() const { return request_id_; } 188 int request_id() const { return request_id_; }
189 SafetyState safety_state() const { return safety_state_; }
190 void set_safety_state(SafetyState safety_state) {
191 safety_state_ = safety_state;
192 }
193 std::wstring original_name() const { return original_name_; }
194 void set_original_name(const std::wstring& name) { original_name_ = name; }
195
196 // Returns the file-name that should be reported to the user, which is
197 // file_name_ for safe downloads and original_name_ for dangerous ones.
198 std::wstring GetFileName() const;
179 199
180 private: 200 private:
181 // Internal helper for maintaining consistent received and total sizes. 201 // Internal helper for maintaining consistent received and total sizes.
182 void UpdateSize(int64 size); 202 void UpdateSize(int64 size);
183 203
184 // Request ID assigned by the ResourceDispatcherHost. 204 // Request ID assigned by the ResourceDispatcherHost.
185 int32 id_; 205 int32 id_;
186 206
187 // Full path to the downloaded file 207 // Full path to the downloaded file
188 std::wstring full_path_; 208 std::wstring full_path_;
(...skipping 30 matching lines...) Expand all
219 239
220 // Our owning object 240 // Our owning object
221 DownloadManager* manager_; 241 DownloadManager* manager_;
222 242
223 // In progress downloads may be paused by the user, we note it here 243 // In progress downloads may be paused by the user, we note it here
224 bool is_paused_; 244 bool is_paused_;
225 245
226 // A flag for indicating if the download should be opened at completion. 246 // A flag for indicating if the download should be opened at completion.
227 bool open_when_complete_; 247 bool open_when_complete_;
228 248
249 // Whether the download is considered potentially safe or dangerous
250 // (executable files are typically considered dangerous).
251 SafetyState safety_state_;
252
253 // Dangerous download are given temporary names until the user approves them.
254 // This stores their original name.
255 std::wstring original_name_;
256
229 // For canceling or pausing requests. 257 // For canceling or pausing requests.
230 int render_process_id_; 258 int render_process_id_;
231 int request_id_; 259 int request_id_;
232 260
233 DISALLOW_EVIL_CONSTRUCTORS(DownloadItem); 261 DISALLOW_EVIL_CONSTRUCTORS(DownloadItem);
234 }; 262 };
235 263
236 264
237 // DownloadManager ------------------------------------------------------------- 265 // DownloadManager -------------------------------------------------------------
238 266
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 void ResetAutoOpenFiles(); 375 void ResetAutoOpenFiles();
348 376
349 // Returns true if there are automatic handlers registered for any file 377 // Returns true if there are automatic handlers registered for any file
350 // types. 378 // types.
351 bool HasAutoOpenFileTypesRegistered() const; 379 bool HasAutoOpenFileTypesRegistered() const;
352 380
353 // Overridden from SelectFileDialog::Listener: 381 // Overridden from SelectFileDialog::Listener:
354 virtual void FileSelected(const std::wstring& path, void* params); 382 virtual void FileSelected(const std::wstring& path, void* params);
355 virtual void FileSelectionCanceled(void* params); 383 virtual void FileSelectionCanceled(void* params);
356 384
385 // Deletes the specified path on the file thread.
386 void DeleteDownload(const std::wstring& path);
387
388 // Called when the user has validated the donwload of a dangerous file.
389 void DangerousDownloadValidated(DownloadItem* download);
390
357 private: 391 private:
358 // Shutdown the download manager. This call is needed only after Init. 392 // Shutdown the download manager. This call is needed only after Init.
359 void Shutdown(); 393 void Shutdown();
360 394
361 // Called on the download thread to check whether the suggested file path 395 // Called on the download thread to check whether the suggested file path
362 // exists. We don't check if the file exists on the UI thread to avoid UI 396 // exists. We don't check if the file exists on the UI thread to avoid UI
363 // stalls from interacting with the file system. 397 // stalls from interacting with the file system.
364 void CheckIfSuggestedPathExists(DownloadCreateInfo* info); 398 void CheckIfSuggestedPathExists(DownloadCreateInfo* info);
365 399
366 // Called on the UI thread once the DownloadManager has determined whether the 400 // Called on the UI thread once the DownloadManager has determined whether the
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 static void OnCancelDownloadRequest(ResourceDispatcherHost* rdh, 432 static void OnCancelDownloadRequest(ResourceDispatcherHost* rdh,
399 int render_process_id, 433 int render_process_id,
400 int request_id); 434 int request_id);
401 435
402 // Runs the pause on the IO thread. 436 // Runs the pause on the IO thread.
403 static void OnPauseDownloadRequest(ResourceDispatcherHost* rdh, 437 static void OnPauseDownloadRequest(ResourceDispatcherHost* rdh,
404 int render_process_id, 438 int render_process_id,
405 int request_id, 439 int request_id,
406 bool pause); 440 bool pause);
407 441
442 // Performs the last steps required when a download has been completed.
443 // It is necessary to break down the flow when a download is finished as
444 // dangerous downloads are downloaded to temporary files that need to be
445 // renamed on the file thread first.
446 // Invoked on the UI thread.
447 void ContinueDownloadFinished(DownloadItem* download);
448
449 // Renames a finished dangerous download from its temporary file name to its
450 // real file name.
451 // Invoked on the file thread.
452 void ProceedWithFinishedDangerousDownload(int64 download_handle,
453 const std::wstring& path,
454 const std::wstring& original_name);
455
456 // Invoked on the UI thread when a dangerous downloaded file has been renamed.
457 void DangerousDownloadRenamed(int64 download_handle,
458 bool success,
459 const std::wstring& new_path);
460
461 // Checks whether a file represents a risk if downloaded.
462 bool IsDangerous(const std::wstring& file_name);
463
464 // Changes the paths and file name of the specified |download|, propagating
465 // the change to the history system.
466 void RenameDownload(DownloadItem* download, const std::wstring& new_path);
467
408 // 'downloads_' is map of all downloads in this profile. The key is the handle 468 // 'downloads_' is map of all downloads in this profile. The key is the handle
409 // returned by the history system, which is unique across sessions. This map 469 // returned by the history system, which is unique across sessions. This map
410 // owns all the DownloadItems once they have been created in the history 470 // owns all the DownloadItems once they have been created in the history
411 // system. 471 // system.
412 // 472 //
413 // 'in_progress_' is a map of all downloads that are in progress and that have 473 // 'in_progress_' is a map of all downloads that are in progress and that have
414 // not yet received a valid history handle. The key is the ID assigned by the 474 // not yet received a valid history handle. The key is the ID assigned by the
415 // ResourceDispatcherHost, which is unique for the current session. This map 475 // ResourceDispatcherHost, which is unique for the current session. This map
416 // does not own the DownloadItems. 476 // does not own the DownloadItems.
417 // 477 //
478 // 'dangerous_finished_' is a map of dangerous download that have finished
479 // but were not yet approved by the user. Similarly to in_progress_, the key
480 // is the ID assigned by the ResourceDispatcherHost and the map does not own
481 // the DownloadItems. It is used on shutdown to delete completed downloads
482 // that have not been approved.
483 //
418 // When a download is created through a user action, the corresponding 484 // When a download is created through a user action, the corresponding
419 // DownloadItem* is placed in 'in_progress_' and remains there until it has 485 // DownloadItem* is placed in 'in_progress_' and remains there until it has
420 // received a valid handle from the history system. Once it has a valid 486 // received a valid handle from the history system. Once it has a valid
421 // handle, the DownloadItem* is placed in the 'downloads_' map. When the 487 // handle, the DownloadItem* is placed in the 'downloads_' map. When the
422 // download is complete, it is removed from 'in_progress_'. Downloads from 488 // download is complete, it is removed from 'in_progress_'. Downloads from
423 // past sessions read from a persisted state from the history system are 489 // past sessions read from a persisted state from the history system are
424 // placed directly into 'downloads_' since they have valid handles in the 490 // placed directly into 'downloads_' since they have valid handles in the
425 // history system. 491 // history system.
426 typedef base::hash_map<int64, DownloadItem*> DownloadMap; 492 typedef base::hash_map<int64, DownloadItem*> DownloadMap;
427 DownloadMap downloads_; 493 DownloadMap downloads_;
428 DownloadMap in_progress_; 494 DownloadMap in_progress_;
495 DownloadMap dangerous_finished_;
429 496
430 // True if the download manager has been initialized and requires a shutdown. 497 // True if the download manager has been initialized and requires a shutdown.
431 bool shutdown_needed_; 498 bool shutdown_needed_;
432 499
433 // Observers that want to be notified of changes to the set of downloads. 500 // Observers that want to be notified of changes to the set of downloads.
434 ObserverList<Observer> observers_; 501 ObserverList<Observer> observers_;
435 502
436 // The current active profile. 503 // The current active profile.
437 Profile* profile_; 504 Profile* profile_;
438 scoped_refptr<URLRequestContext> request_context_; 505 scoped_refptr<URLRequestContext> request_context_;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 541
475 // The "Save As" dialog box used to ask the user where a file should be 542 // The "Save As" dialog box used to ask the user where a file should be
476 // saved. 543 // saved.
477 scoped_refptr<SelectFileDialog> select_file_dialog_; 544 scoped_refptr<SelectFileDialog> select_file_dialog_;
478 545
479 DISALLOW_EVIL_CONSTRUCTORS(DownloadManager); 546 DISALLOW_EVIL_CONSTRUCTORS(DownloadManager);
480 }; 547 };
481 548
482 549
483 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H__ 550 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698