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

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

Powered by Google App Engine
This is Rietveld 408576698