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

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

Issue 7618048: Move the core download files to content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4 //
5 // Each download is represented by a DownloadItem, and all DownloadItems
6 // are owned by the DownloadManager which maintains a global list of all
7 // downloads. DownloadItems are created when a user initiates a download,
8 // and exist for the duration of the browser life time.
9 //
10 // Download observers:
11 // DownloadItem::Observer:
12 // - allows observers to receive notifications about one download from start
13 // to completion
14 // Use AddObserver() / RemoveObserver() on the appropriate download object to
15 // receive state updates.
16
17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
19 #pragma once
20
21 #include <string>
22
23 #include "base/basictypes.h"
24 #include "base/file_path.h"
25 #include "base/observer_list.h"
26 #include "base/time.h"
27 #include "base/timer.h"
28 #include "chrome/browser/download/download_request_handle.h"
29 #include "chrome/browser/download/download_state_info.h"
30 #include "content/common/notification_observer.h"
31 #include "content/common/notification_registrar.h"
32 #include "googleurl/src/gurl.h"
33
34 class CrxInstaller;
35 class DownloadFileManager;
36 class DownloadManager;
37 struct DownloadCreateInfo;
38 struct DownloadHistoryInfo;
39
40 // One DownloadItem per download. This is the model class that stores all the
41 // state for a download. Multiple views, such as a tab's download shelf and the
42 // Destination tab's download view, may refer to a given DownloadItem.
43 //
44 // This is intended to be used only on the UI thread.
45 class DownloadItem : public NotificationObserver {
46 public:
47 enum DownloadState {
48 // Download is actively progressing.
49 IN_PROGRESS = 0,
50
51 // Download is completely finished.
52 COMPLETE,
53
54 // Download has been cancelled.
55 CANCELLED,
56
57 // This state indicates that the download item is about to be destroyed,
58 // and observers seeing this state should release all references.
59 REMOVING,
60
61 // This state indicates that the download has been interrupted.
62 INTERRUPTED,
63
64 // Maximum value.
65 MAX_DOWNLOAD_STATE
66 };
67
68 enum SafetyState {
69 SAFE = 0,
70 DANGEROUS,
71 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download.
72 };
73
74 // This enum is used by histograms. Do not change the ordering or remove
75 // items.
76 enum DangerType {
77 NOT_DANGEROUS = 0,
78
79 // A dangerous file to the system (e.g.: an executable or extension from
80 // places other than gallery).
81 DANGEROUS_FILE,
82
83 // Safebrowsing service shows this URL leads to malicious file download.
84 DANGEROUS_URL,
85
86 // Memory space for histograms is determined by the max.
87 // ALWAYS ADD NEW VALUES BEFORE THIS ONE.
88 DANGEROUS_TYPE_MAX
89 };
90
91 // Reason for deleting the download. Passed to Delete().
92 enum DeleteReason {
93 DELETE_DUE_TO_BROWSER_SHUTDOWN = 0,
94 DELETE_DUE_TO_USER_DISCARD
95 };
96
97 // Interface that observers of a particular download must implement in order
98 // to receive updates to the download's status.
99 class Observer {
100 public:
101 virtual void OnDownloadUpdated(DownloadItem* download) = 0;
102
103 // Called when a downloaded file has been opened.
104 virtual void OnDownloadOpened(DownloadItem* download) = 0;
105
106 protected:
107 virtual ~Observer() {}
108 };
109
110 // Constructing from persistent store:
111 DownloadItem(DownloadManager* download_manager,
112 const DownloadHistoryInfo& info);
113
114 // Constructing for a regular download:
115 DownloadItem(DownloadManager* download_manager,
116 const DownloadCreateInfo& info,
117 bool is_otr);
118
119 // Constructing for the "Save Page As..." feature:
120 DownloadItem(DownloadManager* download_manager,
121 const FilePath& path,
122 const GURL& url,
123 bool is_otr);
124
125 virtual ~DownloadItem();
126
127 void AddObserver(Observer* observer);
128 void RemoveObserver(Observer* observer);
129
130 // Notifies our observers periodically.
131 void UpdateObservers();
132
133 // NotificationObserver implementation.
134 virtual void Observe(int type,
135 const NotificationSource& source,
136 const NotificationDetails& details);
137
138 // Returns true if it is OK to open a folder which this file is inside.
139 bool CanShowInFolder();
140
141 // Returns true if it is OK to register the type of this file so that
142 // it opens automatically.
143 bool CanOpenDownload();
144
145 // Tests if a file type should be opened automatically.
146 bool ShouldOpenFileBasedOnExtension();
147
148 // Registers this file extension for automatic opening upon download
149 // completion if 'open' is true, or prevents the extension from automatic
150 // opening if 'open' is false.
151 void OpenFilesBasedOnExtension(bool open);
152
153 // Open the file associated with this download (wait for the download to
154 // complete if it is in progress).
155 void OpenDownload();
156
157 // Show the download via the OS shell.
158 void ShowDownloadInShell();
159
160 // Called when the user has validated the download of a dangerous file.
161 void DangerousDownloadValidated();
162
163 // Received a new chunk of data
164 void Update(int64 bytes_so_far);
165
166 // Cancel the download operation. We need to distinguish between cancels at
167 // exit (DownloadManager destructor) from user interface initiated cancels
168 // because at exit, the history system may not exist, and any updates to it
169 // require AddRef'ing the DownloadManager in the destructor which results in
170 // a DCHECK failure. Set 'update_history' to false when canceling from at
171 // exit to prevent this crash. This may result in a difference between the
172 // downloaded file's size on disk, and what the history system's last record
173 // of it is. At worst, we'll end up re-downloading a small portion of the file
174 // when resuming a download (assuming the server supports byte ranges).
175 void Cancel(bool update_history);
176
177 // Called by external code (SavePackage) using the DownloadItem interface
178 // to display progress when the DownloadItem should be considered complete.
179 void MarkAsComplete();
180
181 // Called when all data has been saved. Only has display effects.
182 void OnAllDataSaved(int64 size);
183
184 // Called when the downloaded file is removed.
185 void OnDownloadedFileRemoved();
186
187 // Download operation had an error.
188 // |size| is the amount of data received so far, and |os_error| is the error
189 // code that the operation received.
190 void Interrupted(int64 size, int os_error);
191
192 // Deletes the file from disk and removes the download from the views and
193 // history. |user| should be true if this is the result of the user clicking
194 // the discard button, and false if it is being deleted for other reasons like
195 // browser shutdown.
196 void Delete(DeleteReason reason);
197
198 // Removes the download from the views and history.
199 void Remove();
200
201 // Simple calculation of the amount of time remaining to completion. Fills
202 // |*remaining| with the amount of time remaining if successful. Fails and
203 // returns false if we do not have the number of bytes or the speed so can
204 // not estimate.
205 bool TimeRemaining(base::TimeDelta* remaining) const;
206
207 // Simple speed estimate in bytes/s
208 int64 CurrentSpeed() const;
209
210 // Rough percent complete, -1 means we don't know (since we didn't receive a
211 // total size).
212 int PercentComplete() const;
213
214 // Called when the final path has been determined.
215 void OnPathDetermined(const FilePath& path);
216
217 // Returns true if this download has saved all of its data.
218 bool all_data_saved() const { return all_data_saved_; }
219
220 // Update the fields that may have changed in DownloadStateInfo as a
221 // result of analyzing the file and figuring out its type, location, etc.
222 // May only be called once.
223 void SetFileCheckResults(const DownloadStateInfo& state);
224
225 // Update the download's path, the actual file is renamed on the download
226 // thread.
227 void Rename(const FilePath& full_path);
228
229 // Allow the user to temporarily pause a download or resume a paused download.
230 void TogglePause();
231
232 // Called when the download is ready to complete.
233 // This may perform final rename if necessary and will eventually call
234 // DownloadItem::Completed().
235 void OnDownloadCompleting(DownloadFileManager* file_manager);
236
237 // Called when the file name for the download is renamed to its final name.
238 void OnDownloadRenamedToFinalName(const FilePath& full_path);
239
240 // Returns true if this item matches |query|. |query| must be lower-cased.
241 bool MatchesQuery(const string16& query) const;
242
243 // Returns true if the download needs more data.
244 bool IsPartialDownload() const;
245
246 // Returns true if the download is still receiving data.
247 bool IsInProgress() const;
248
249 // Returns true if the download has been cancelled or was interrupted.
250 bool IsCancelled() const;
251
252 // Returns true if the download was interrupted.
253 bool IsInterrupted() const;
254
255 // Returns true if we have all the data and know the final file name.
256 bool IsComplete() const;
257
258 // Accessors
259 DownloadState state() const { return state_; }
260 const FilePath& full_path() const { return full_path_; }
261 void set_path_uniquifier(int uniquifier) {
262 state_info_.path_uniquifier = uniquifier;
263 }
264 const GURL& GetURL() const;
265
266 const std::vector<GURL>& url_chain() const { return url_chain_; }
267 const GURL& original_url() const { return url_chain_.front(); }
268 const GURL& referrer_url() const { return referrer_url_; }
269 std::string suggested_filename() const { return suggested_filename_; }
270 std::string content_disposition() const { return content_disposition_; }
271 std::string mime_type() const { return mime_type_; }
272 std::string original_mime_type() const { return original_mime_type_; }
273 std::string referrer_charset() const { return referrer_charset_; }
274 int64 total_bytes() const { return total_bytes_; }
275 void set_total_bytes(int64 total_bytes) {
276 total_bytes_ = total_bytes;
277 }
278 int64 received_bytes() const { return received_bytes_; }
279 int32 id() const { return download_id_; }
280 base::Time start_time() const { return start_time_; }
281 void set_db_handle(int64 handle) { db_handle_ = handle; }
282 int64 db_handle() const { return db_handle_; }
283 bool is_paused() const { return is_paused_; }
284 bool open_when_complete() const { return open_when_complete_; }
285 void set_open_when_complete(bool open) { open_when_complete_ = open; }
286 bool file_externally_removed() const { return file_externally_removed_; }
287 SafetyState safety_state() const { return safety_state_; }
288 // Why |safety_state_| is not SAFE.
289 DangerType GetDangerType() const;
290 bool IsDangerous() const;
291 void MarkFileDangerous();
292 void MarkUrlDangerous();
293
294 bool auto_opened() { return auto_opened_; }
295 const FilePath& target_name() const { return state_info_.target_name; }
296 bool prompt_user_for_save_location() const {
297 return state_info_.prompt_user_for_save_location;
298 }
299 bool is_otr() const { return is_otr_; }
300 bool is_extension_install() const {
301 return state_info_.is_extension_install;
302 }
303 const FilePath& suggested_path() const { return state_info_.suggested_path; }
304 bool is_temporary() const { return is_temporary_; }
305 void set_opened(bool opened) { opened_ = opened; }
306 bool opened() const { return opened_; }
307
308 DownloadHistoryInfo GetHistoryInfo() const;
309 DownloadStateInfo state_info() const { return state_info_; }
310 const DownloadRequestHandle& request_handle() const {
311 return request_handle_;
312 }
313
314 // Returns the final target file path for the download.
315 FilePath GetTargetFilePath() const;
316
317 // Returns the file-name that should be reported to the user, which is
318 // target_name possibly with the uniquifier number.
319 FilePath GetFileNameToReportUser() const;
320
321 // Returns the user-verified target file path for the download.
322 // This returns the same path as GetTargetFilePath() for safe downloads
323 // but does not for dangerous downloads until the name is verified.
324 FilePath GetUserVerifiedFilePath() const;
325
326 // Returns true if the current file name is not the final target name yet.
327 bool NeedsRename() const {
328 return state_info_.target_name != full_path_.BaseName();
329 }
330
331 // Is a CRX installer running on this download?
332 bool IsCrxInstallRuning() const {
333 return (is_extension_install() &&
334 all_data_saved() &&
335 state_ == IN_PROGRESS);
336 }
337
338 std::string DebugString(bool verbose) const;
339
340 #ifdef UNIT_TEST
341 // Mock opening downloads (for testing only).
342 void TestMockDownloadOpen() { open_enabled_ = false; }
343 #endif
344
345 private:
346 // Construction common to all constructors. |active| should be true for new
347 // downloads and false for downloads from the history.
348 void Init(bool active);
349
350 // Internal helper for maintaining consistent received and total sizes.
351 void UpdateSize(int64 size);
352
353 // Called when the entire download operation (including renaming etc)
354 // is completed.
355 void Completed();
356
357 // Start/stop sending periodic updates to our observers
358 void StartProgressTimer();
359 void StopProgressTimer();
360
361 // Call to install this item as a CRX. Should only be called on
362 // items which are CRXes. Use is_extension_install() to check.
363 void StartCrxInstall();
364
365 // Call to transition state; all state transitions should go through this.
366 void TransitionTo(DownloadState new_state);
367
368 // Called when safety_state_ should be recomputed from is_dangerous_file
369 // and is_dangerous_url.
370 void UpdateSafetyState();
371
372 // Helper function to recompute |state_info_.target_name| when
373 // it may have changed. (If it's non-null it should be left alone,
374 // otherwise updated from |full_path_|.)
375 void UpdateTarget();
376
377 // State information used by the download manager.
378 DownloadStateInfo state_info_;
379
380 // The handle to the request information. Used for operations outside the
381 // download system.
382 DownloadRequestHandle request_handle_;
383
384 // Download ID assigned by DownloadResourceHandler.
385 int32 download_id_;
386
387 // Full path to the downloaded or downloading file.
388 FilePath full_path_;
389
390 // A number that should be appended to the path to make it unique, or 0 if the
391 // path should be used as is.
392 int path_uniquifier_;
393
394 // The chain of redirects that leading up to and including the final URL.
395 std::vector<GURL> url_chain_;
396
397 // The URL of the page that initiated the download.
398 GURL referrer_url_;
399
400 // Suggested filename in 'download' attribute of an anchor. Details:
401 // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks
402 std::string suggested_filename_;
403
404 // Information from the request.
405 // Content-disposition field from the header.
406 std::string content_disposition_;
407
408 // Mime-type from the header. Subject to change.
409 std::string mime_type_;
410
411 // The value of the content type header sent with the downloaded item. It
412 // may be different from |mime_type_|, which may be set based on heuristics
413 // which may look at the file extension and first few bytes of the file.
414 std::string original_mime_type_;
415
416 // The charset of the referring page where the download request comes from.
417 // It's used to construct a suggested filename.
418 std::string referrer_charset_;
419
420 // Total bytes expected
421 int64 total_bytes_;
422
423 // Current received bytes
424 int64 received_bytes_;
425
426 // Last error.
427 int last_os_error_;
428
429 // Start time for calculating remaining time
430 base::TimeTicks start_tick_;
431
432 // The current state of this download
433 DownloadState state_;
434
435 // The views of this item in the download shelf and download tab
436 ObserverList<Observer> observers_;
437
438 // Time the download was started
439 base::Time start_time_;
440
441 // Our persistent store handle
442 int64 db_handle_;
443
444 // Timer for regularly updating our observers
445 base::RepeatingTimer<DownloadItem> update_timer_;
446
447 // Our owning object
448 DownloadManager* download_manager_;
449
450 // In progress downloads may be paused by the user, we note it here
451 bool is_paused_;
452
453 // A flag for indicating if the download should be opened at completion.
454 bool open_when_complete_;
455
456 // A flag for indicating if the downloaded file is externally removed.
457 bool file_externally_removed_;
458
459 // Indicates if the download is considered potentially safe or dangerous
460 // (executable files are typically considered dangerous).
461 SafetyState safety_state_;
462
463 // True if the download was auto-opened. We set this rather than using
464 // an observer as it's frequently possible for the download to be auto opened
465 // before the observer is added.
466 bool auto_opened_;
467
468 // True if the download was initiated in an incognito window.
469 bool is_otr_;
470
471 // True if the item was downloaded temporarily.
472 bool is_temporary_;
473
474 // True if we've saved all the data for the download.
475 bool all_data_saved_;
476
477 // Did the user open the item either directly or indirectly (such as by
478 // setting always open files of this type)? The shelf also sets this field
479 // when the user closes the shelf before the item has been opened but should
480 // be treated as though the user opened it.
481 bool opened_;
482
483 // Do we actual open downloads when requested? For testing purposes
484 // only.
485 bool open_enabled_;
486
487 // DownloadItem observes CRX installs it initiates.
488 NotificationRegistrar registrar_;
489
490 DISALLOW_COPY_AND_ASSIGN(DownloadItem);
491 };
492
493 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698