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

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

Issue 6969009: Reduced the lifetime of DownloadCreateInfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed structure accessors from DownloadItem, per request. Created 9 years, 7 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // Each download is represented by a DownloadItem, and all DownloadItems 5 // Each download is represented by a DownloadItem, and all DownloadItems
6 // are owned by the DownloadManager which maintains a global list of all 6 // are owned by the DownloadManager which maintains a global list of all
7 // downloads. DownloadItems are created when a user initiates a download, 7 // downloads. DownloadItems are created when a user initiates a download,
8 // and exist for the duration of the browser life time. 8 // and exist for the duration of the browser life time.
9 // 9 //
10 // Download observers: 10 // Download observers:
11 // DownloadItem::Observer: 11 // DownloadItem::Observer:
12 // - allows observers to receive notifications about one download from start 12 // - allows observers to receive notifications about one download from start
13 // to completion 13 // to completion
14 // Use AddObserver() / RemoveObserver() on the appropriate download object to 14 // Use AddObserver() / RemoveObserver() on the appropriate download object to
15 // receive state updates. 15 // receive state updates.
16 16
17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ 17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ 18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
19 #pragma once 19 #pragma once
20 20
21 #include <string> 21 #include <string>
22 22
23 #include "base/basictypes.h" 23 #include "base/basictypes.h"
24 #include "base/file_path.h" 24 #include "base/file_path.h"
25 #include "base/observer_list.h" 25 #include "base/observer_list.h"
26 #include "base/time.h" 26 #include "base/time.h"
27 #include "base/timer.h" 27 #include "base/timer.h"
28 #include "chrome/browser/download/download_process_handle.h" 28 #include "chrome/browser/download/download_process_handle.h"
29 #include "chrome/browser/history/download_create_info.h"
29 #include "googleurl/src/gurl.h" 30 #include "googleurl/src/gurl.h"
30 31
31 class DownloadFileManager; 32 class DownloadFileManager;
32 class DownloadManager; 33 class DownloadManager;
33 struct DownloadCreateInfo;
34 34
35 // One DownloadItem per download. This is the model class that stores all the 35 // One DownloadItem per download. This is the model class that stores all the
36 // state for a download. Multiple views, such as a tab's download shelf and the 36 // state for a download. Multiple views, such as a tab's download shelf and the
37 // Destination tab's download view, may refer to a given DownloadItem. 37 // Destination tab's download view, may refer to a given DownloadItem.
38 // 38 //
39 // This is intended to be used only on the UI thread. 39 // This is intended to be used only on the UI thread.
40 class DownloadItem { 40 class DownloadItem {
41 public: 41 public:
42 enum DownloadState { 42 enum DownloadState {
43 // Download is actively progressing. 43 // Download is actively progressing.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 // ALWAYS ADD NEW VALUES BEFORE THIS ONE. 79 // ALWAYS ADD NEW VALUES BEFORE THIS ONE.
80 DANGEROUS_TYPE_MAX 80 DANGEROUS_TYPE_MAX
81 }; 81 };
82 82
83 // Reason for deleting the download. Passed to Delete(). 83 // Reason for deleting the download. Passed to Delete().
84 enum DeleteReason { 84 enum DeleteReason {
85 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0, 85 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0,
86 DELETE_DUE_TO_USER_DISCARD 86 DELETE_DUE_TO_USER_DISCARD
87 }; 87 };
88 88
89 // Contains information relating to the process of determining what to do with
90 // the download.
91 struct DownloadStateInfo {
92 DownloadStateInfo();
93 explicit DownloadStateInfo(const DownloadItem& item);
94 DownloadStateInfo(bool has_user_gesture,
95 bool prompt_user_for_save_location);
96 DownloadStateInfo(const FilePath& target,
97 const FilePath& forced_name,
98 bool has_user_gesture,
99 bool prompt_user_for_save_location,
100 int uniquifier,
101 bool dangerous_file,
102 bool dangerous_url,
103 bool extension_install);
104
105 // Indicates if the download is dangerous.
106 bool IsDangerous() const;
107
108 // The original name for a dangerous download, specified by the request.
109 FilePath target_name;
110
111 FilePath suggested_path;
112
113 // A number that should be added to the suggested path to make it unique.
114 // 0 means no number should be appended. It is eventually incorporated
115 // into the final file name.
116 int path_uniquifier;
117
118 bool has_user_gesture;
119
120 // True if we should display the 'save as...' UI and prompt the user
121 // for the download location.
122 // False if the UI should be suppressed and the download performed to the
123 // default location.
124 bool prompt_user_for_save_location;
125
126 // Whether this download file is potentially dangerous (ex: exe, dll, ...).
127 bool is_dangerous_file;
128
129 // If safebrowsing believes this URL leads to malware.
130 bool is_dangerous_url;
131
132 // Whether this download is for extension install or not.
133 bool is_extension_install;
134
135 // Whether this download's file name was specified initially.
136 FilePath force_file_name;
137 };
138
89 // Interface that observers of a particular download must implement in order 139 // Interface that observers of a particular download must implement in order
90 // to receive updates to the download's status. 140 // to receive updates to the download's status.
91 class Observer { 141 class Observer {
92 public: 142 public:
93 virtual void OnDownloadUpdated(DownloadItem* download) = 0; 143 virtual void OnDownloadUpdated(DownloadItem* download) = 0;
94 144
95 // Called when a downloaded file has been opened. 145 // Called when a downloaded file has been opened.
96 virtual void OnDownloadOpened(DownloadItem* download) = 0; 146 virtual void OnDownloadOpened(DownloadItem* download) = 0;
97 147
98 protected: 148 protected:
99 virtual ~Observer() {} 149 virtual ~Observer() {}
100 }; 150 };
101 151
102 // Constructing from persistent store: 152 // Constructing from persistent store:
103 DownloadItem(DownloadManager* download_manager, 153 DownloadItem(DownloadManager* download_manager,
104 const DownloadCreateInfo& info); 154 const DownloadHistoryInfo& info);
105 155
106 // Constructing for a regular download: 156 // Constructing for a regular download:
107 DownloadItem(DownloadManager* download_manager, 157 DownloadItem(DownloadManager* download_manager,
108 const DownloadCreateInfo& info, 158 const DownloadCreateInfo& info,
109 bool is_otr); 159 bool is_otr);
110 160
111 // Constructing for the "Save Page As..." feature: 161 // Constructing for the "Save Page As..." feature:
112 DownloadItem(DownloadManager* download_manager, 162 DownloadItem(DownloadManager* download_manager,
113 const FilePath& path, 163 const FilePath& path,
114 const GURL& url, 164 const GURL& url,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 // Simple speed estimate in bytes/s 237 // Simple speed estimate in bytes/s
188 int64 CurrentSpeed() const; 238 int64 CurrentSpeed() const;
189 239
190 // Rough percent complete, -1 means we don't know (since we didn't receive a 240 // Rough percent complete, -1 means we don't know (since we didn't receive a
191 // total size). 241 // total size).
192 int PercentComplete() const; 242 int PercentComplete() const;
193 243
194 // Whether or not this download has saved all of its data. 244 // Whether or not this download has saved all of its data.
195 bool all_data_saved() const { return all_data_saved_; } 245 bool all_data_saved() const { return all_data_saved_; }
196 246
197 // Update the fields that may have changed in DownloadCreateInfo as a 247 // Update the fields that may have changed in DownloadStateInfo as a
198 // result of analyzing the file and figuring out its type, location, etc. 248 // result of analyzing the file and figuring out its type, location, etc.
199 // May only be called once. 249 // May only be called once.
200 void SetFileCheckResults(const FilePath& path, 250 void SetFileCheckResults(const DownloadStateInfo& state);
201 bool is_dangerous_file, 251
202 bool is_dangerous_url, 252 // Updates the target file.
203 int path_uniquifier, 253 void UpdateTarget();
204 bool prompt,
205 bool is_extension_install,
206 const FilePath& original_name);
207 254
208 // Update the download's path, the actual file is renamed on the download 255 // Update the download's path, the actual file is renamed on the download
209 // thread. 256 // thread.
210 void Rename(const FilePath& full_path); 257 void Rename(const FilePath& full_path);
211 258
212 // Allow the user to temporarily pause a download or resume a paused download. 259 // Allow the user to temporarily pause a download or resume a paused download.
213 void TogglePause(); 260 void TogglePause();
214 261
215 // Called when the download is ready to complete. 262 // Called when the download is ready to complete.
216 // This may perform final rename if necessary and will eventually call 263 // This may perform final rename if necessary and will eventually call
(...skipping 15 matching lines...) Expand all
232 // Returns true if the download has been cancelled or was interrupted. 279 // Returns true if the download has been cancelled or was interrupted.
233 bool IsCancelled() const; 280 bool IsCancelled() const;
234 281
235 // Returns true if the download was interrupted. 282 // Returns true if the download was interrupted.
236 bool IsInterrupted() const; 283 bool IsInterrupted() const;
237 284
238 // Returns true if we have all the data and know the final file name. 285 // Returns true if we have all the data and know the final file name.
239 bool IsComplete() const; 286 bool IsComplete() const;
240 287
241 // Accessors 288 // Accessors
242 DownloadState state() const { return state_; } 289 DownloadState state() const;
243 FilePath full_path() const { return full_path_; } 290 FilePath full_path() const { return history_info_.path; }
244 void set_path_uniquifier(int uniquifier) { path_uniquifier_ = uniquifier; } 291 void set_path_uniquifier(int uniquifier) {
245 const GURL& url() const { return url_chain_.back(); } 292 manager_state_.path_uniquifier = uniquifier;
246 const std::vector<GURL>& url_chain() const { return url_chain_; } 293 }
247 const GURL& original_url() const { return url_chain_.front(); } 294 const GURL& url() const;
248 const GURL& referrer_url() const { return referrer_url_; } 295 const std::vector<GURL>& url_chain() const { return history_info_.url_chain; }
296 const GURL& original_url() const { return history_info_.url_chain.front(); }
297 const GURL& referrer_url() const { return history_info_.referrer_url; }
298 std::string content_disposition() const { return content_disposition_; }
249 std::string mime_type() const { return mime_type_; } 299 std::string mime_type() const { return mime_type_; }
250 std::string original_mime_type() const { return original_mime_type_; } 300 std::string original_mime_type() const { return original_mime_type_; }
251 int64 total_bytes() const { return total_bytes_; } 301 std::string referrer_charset() const { return referrer_charset_; }
252 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } 302 int64 total_bytes() const { return history_info_.total_bytes; }
253 int64 received_bytes() const { return received_bytes_; } 303 void set_total_bytes(int64 total_bytes) {
254 int32 id() const { return id_; } 304 history_info_.total_bytes = total_bytes;
255 base::Time start_time() const { return start_time_; } 305 }
256 void set_db_handle(int64 handle) { db_handle_ = handle; } 306 int64 received_bytes() const { return history_info_.received_bytes; }
257 int64 db_handle() const { return db_handle_; } 307 int32 id() const { return history_info_.download_id; }
308 base::Time start_time() const { return history_info_.start_time; }
309 void set_db_handle(int64 handle) { history_info_.db_handle = handle; }
310 int64 db_handle() const { return history_info_.db_handle; }
258 bool is_paused() const { return is_paused_; } 311 bool is_paused() const { return is_paused_; }
259 bool open_when_complete() const { return open_when_complete_; } 312 bool open_when_complete() const { return open_when_complete_; }
260 void set_open_when_complete(bool open) { open_when_complete_ = open; } 313 void set_open_when_complete(bool open) { open_when_complete_ = open; }
261 SafetyState safety_state() const { return safety_state_; } 314 SafetyState safety_state() const { return safety_state_; }
262 void set_safety_state(SafetyState safety_state) { 315 void set_safety_state(SafetyState safety_state) {
263 safety_state_ = safety_state; 316 safety_state_ = safety_state;
264 } 317 }
265 DangerType danger_type() { return danger_type_;} 318 DangerType danger_type() { return danger_type_;}
266 bool auto_opened() { return auto_opened_; } 319 bool auto_opened() { return auto_opened_; }
267 FilePath target_name() const { return target_name_; } 320 FilePath target_name() const { return manager_state_.target_name; }
268 bool save_as() const { return save_as_; } 321 bool save_as() const { return manager_state_.prompt_user_for_save_location; }
269 bool is_otr() const { return is_otr_; } 322 bool is_otr() const { return is_otr_; }
270 bool is_extension_install() const { return is_extension_install_; } 323 bool is_extension_install() const {
324 return manager_state_.is_extension_install;
325 }
271 bool is_temporary() const { return is_temporary_; } 326 bool is_temporary() const { return is_temporary_; }
272 void set_opened(bool opened) { opened_ = opened; } 327 void set_opened(bool opened) { opened_ = opened; }
273 bool opened() const { return opened_; } 328 bool opened() const { return opened_; }
274 329
330 // History state mutators.
331 void set_path(const FilePath& path) { history_info_.path = path; }
332
333 // Manager state accessors.
334 bool IsDangerous() const { return manager_state_.IsDangerous(); }
335
336 // Manager state mutators.
337 void set_dangerous_url(bool is_dangerous_url) {
338 manager_state_.is_dangerous_url = is_dangerous_url;
339 }
340
275 const DownloadProcessHandle& process_handle() const { 341 const DownloadProcessHandle& process_handle() const {
276 return process_handle_; 342 return process_handle_;
277 } 343 }
278 344
279 // Returns the final target file path for the download. 345 // Returns the final target file path for the download.
280 FilePath GetTargetFilePath() const; 346 FilePath GetTargetFilePath() const;
281 347
282 // Returns the file-name that should be reported to the user, which is 348 // Returns the file-name that should be reported to the user, which is
283 // target_name_ possibly with the uniquifier number. 349 // target_name possibly with the uniquifier number.
284 FilePath GetFileNameToReportUser() const; 350 FilePath GetFileNameToReportUser() const;
285 351
286 // Returns the user-verified target file path for the download. 352 // Returns the user-verified target file path for the download.
287 // This returns the same path as GetTargetFilePath() for safe downloads 353 // This returns the same path as GetTargetFilePath() for safe downloads
288 // but does not for dangerous downloads until the name is verified. 354 // but does not for dangerous downloads until the name is verified.
289 FilePath GetUserVerifiedFilePath() const; 355 FilePath GetUserVerifiedFilePath() const;
290 356
291 // Returns true if the current file name is not the final target name yet. 357 // Returns true if the current file name is not the final target name yet.
292 bool NeedsRename() const { 358 bool NeedsRename() const {
293 return target_name_ != full_path_.BaseName(); 359 return manager_state_.target_name != history_info_.path.BaseName();
294 } 360 }
295 361
296 std::string DebugString(bool verbose) const; 362 std::string DebugString(bool verbose) const;
297 363
298 private: 364 private:
299 void Init(bool start_timer); 365 void Init(bool start_timer);
300 366
301 // Internal helper for maintaining consistent received and total sizes. 367 // Internal helper for maintaining consistent received and total sizes.
302 void UpdateSize(int64 size); 368 void UpdateSize(int64 size);
303 369
304 // Called when the entire download operation (including renaming etc) 370 // Called when the entire download operation (including renaming etc)
305 // is completed. 371 // is completed.
306 void Completed(); 372 void Completed();
307 373
308 // Start/stop sending periodic updates to our observers 374 // Start/stop sending periodic updates to our observers
309 void StartProgressTimer(); 375 void StartProgressTimer();
310 void StopProgressTimer(); 376 void StopProgressTimer();
311 377
312 // Request ID assigned by the ResourceDispatcherHost. 378 // Information that is saved to the history DB.
313 int32 id_; 379 DownloadHistoryInfo history_info_;
314 380
315 // Full path to the downloaded or downloading file. 381 // State information used by the download manager.
316 FilePath full_path_; 382 DownloadStateInfo manager_state_;
317 383
318 // A number that should be appended to the path to make it unique, or 0 if the 384 // The handle to the process information. Used for operations outside the
319 // path should be used as is. 385 // download system.
320 int path_uniquifier_; 386 DownloadProcessHandle process_handle_;
321 387
322 // The chain of redirects that leading up to and including the final URL. 388 // Information from the request.
323 std::vector<GURL> url_chain_; 389 // Content-disposition field from the header.
390 std::string content_disposition_;
324 391
325 // The URL of the page that initiated the download. 392 // Mime-type from the header. Subject to change.
326 GURL referrer_url_;
327
328 // The mimetype of the download
329 std::string mime_type_; 393 std::string mime_type_;
330 394
331 // The value of the content type header received when downloading 395 // The value of the content type header sent with the downloaded item. It
332 // this item. |mime_type_| may be different because of type sniffing. 396 // may be different from |mime_type_|, which may be set based on heuristics
397 // which may look at the file extension and first few bytes of the file.
333 std::string original_mime_type_; 398 std::string original_mime_type_;
334 399
335 // Total bytes expected 400 // The charset of the referring page where the download request comes from.
336 int64 total_bytes_; 401 // It's used to construct a suggested filename.
337 402 std::string referrer_charset_;
338 // Current received bytes
339 int64 received_bytes_;
340 403
341 // Last error. 404 // Last error.
342 int last_os_error_; 405 int last_os_error_;
343 406
344 // Start time for calculating remaining time 407 // Start time for calculating remaining time
345 base::TimeTicks start_tick_; 408 base::TimeTicks start_tick_;
346 409
347 // The current state of this download
348 DownloadState state_;
349
350 // The views of this item in the download shelf and download tab 410 // The views of this item in the download shelf and download tab
351 ObserverList<Observer> observers_; 411 ObserverList<Observer> observers_;
352 412
353 // Time the download was started
354 base::Time start_time_;
355
356 // Our persistent store handle
357 int64 db_handle_;
358
359 // Timer for regularly updating our observers 413 // Timer for regularly updating our observers
360 base::RepeatingTimer<DownloadItem> update_timer_; 414 base::RepeatingTimer<DownloadItem> update_timer_;
361 415
362 // Our owning object 416 // Our owning object
363 DownloadManager* download_manager_; 417 DownloadManager* download_manager_;
364 418
365 // In progress downloads may be paused by the user, we note it here 419 // In progress downloads may be paused by the user, we note it here
366 bool is_paused_; 420 bool is_paused_;
367 421
368 // A flag for indicating if the download should be opened at completion. 422 // A flag for indicating if the download should be opened at completion.
369 bool open_when_complete_; 423 bool open_when_complete_;
370 424
371 // Whether the download is considered potentially safe or dangerous 425 // Whether the download is considered potentially safe or dangerous
372 // (executable files are typically considered dangerous). 426 // (executable files are typically considered dangerous).
373 SafetyState safety_state_; 427 SafetyState safety_state_;
374 428
375 // Why |safety_state_| is not SAFE. 429 // Why |safety_state_| is not SAFE.
376 DangerType danger_type_; 430 DangerType danger_type_;
377 431
378 // Whether the download was auto-opened. We set this rather than using 432 // Whether the download was auto-opened. We set this rather than using
379 // an observer as it's frequently possible for the download to be auto opened 433 // an observer as it's frequently possible for the download to be auto opened
380 // before the observer is added. 434 // before the observer is added.
381 bool auto_opened_; 435 bool auto_opened_;
382 436
383 // Dangerous downloads or ongoing downloads are given temporary names until
384 // the user approves them or the downloads finish.
385 // This stores their final target name.
386 FilePath target_name_;
387
388 // The handle to the process information. Used for operations outside the
389 // download system.
390 DownloadProcessHandle process_handle_;
391
392 // True if the item was downloaded as a result of 'save as...'
393 bool save_as_;
394
395 // True if the download was initiated in an incognito window. 437 // True if the download was initiated in an incognito window.
396 bool is_otr_; 438 bool is_otr_;
397 439
398 // True if the item was downloaded for an extension installation.
399 bool is_extension_install_;
400
401 // True if the item was downloaded temporarily. 440 // True if the item was downloaded temporarily.
402 bool is_temporary_; 441 bool is_temporary_;
403 442
404 // True if we've saved all the data for the download. 443 // True if we've saved all the data for the download.
405 bool all_data_saved_; 444 bool all_data_saved_;
406 445
407 // Did the user open the item either directly or indirectly (such as by 446 // Did the user open the item either directly or indirectly (such as by
408 // setting always open files of this type)? The shelf also sets this field 447 // setting always open files of this type)? The shelf also sets this field
409 // when the user closes the shelf before the item has been opened but should 448 // when the user closes the shelf before the item has been opened but should
410 // be treated as though the user opened it. 449 // be treated as though the user opened it.
411 bool opened_; 450 bool opened_;
412 451
413 DISALLOW_COPY_AND_ASSIGN(DownloadItem); 452 DISALLOW_COPY_AND_ASSIGN(DownloadItem);
414 }; 453 };
415 454
416 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ 455 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698