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

Side by Side Diff: content/browser/download/download_item_impl.h

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to LKGR. Created 8 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h" 14 #include "base/observer_list.h"
15 #include "base/time.h" 15 #include "base/time.h"
16 #include "base/timer.h" 16 #include "base/timer.h"
17 #include "content/browser/download/download_net_log_parameters.h" 17 #include "content/browser/download/download_net_log_parameters.h"
18 #include "content/browser/download/download_request_handle.h" 18 #include "content/browser/download/download_request_handle.h"
19 #include "content/common/content_export.h" 19 #include "content/common/content_export.h"
20 #include "content/public/browser/download_destination_observer.h"
20 #include "content/public/browser/download_id.h" 21 #include "content/public/browser/download_id.h"
21 #include "content/public/browser/download_item.h" 22 #include "content/public/browser/download_item.h"
22 #include "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
23 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
24 #include "net/base/net_log.h" 25 #include "net/base/net_log.h"
25 26
26 class DownloadItemImplDelegate; 27 class DownloadItemImplDelegate;
27 28
29 namespace content {
30 class DownloadFile;
31 }
32
28 // See download_item.h for usage. 33 // See download_item.h for usage.
29 class CONTENT_EXPORT DownloadItemImpl : public content::DownloadItem { 34 class CONTENT_EXPORT DownloadItemImpl
35 : public content::DownloadItem,
36 public content::DownloadDestinationObserver {
30 public: 37 public:
31 // Note that it is the responsibility of the caller to ensure that a 38 // Note that it is the responsibility of the caller to ensure that a
32 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor 39 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor
33 // outlives the DownloadItemImpl. 40 // outlives the DownloadItemImpl.
34 41
35 // Constructing from persistent store: 42 // Constructing from persistent store:
36 // |bound_net_log| is constructed externally for our use. 43 // |bound_net_log| is constructed externally for our use.
37 DownloadItemImpl(DownloadItemImplDelegate* delegate, 44 DownloadItemImpl(DownloadItemImplDelegate* delegate,
38 content::DownloadId download_id, 45 content::DownloadId download_id,
39 const content::DownloadPersistentStoreInfo& info, 46 const content::DownloadPersistentStoreInfo& info,
(...skipping 14 matching lines...) Expand all
54 const GURL& url, 61 const GURL& url,
55 bool is_otr, 62 bool is_otr,
56 content::DownloadId download_id, 63 content::DownloadId download_id,
57 const std::string& mime_type, 64 const std::string& mime_type,
58 const net::BoundNetLog& bound_net_log); 65 const net::BoundNetLog& bound_net_log);
59 66
60 virtual ~DownloadItemImpl(); 67 virtual ~DownloadItemImpl();
61 68
62 // Implementation functions (not part of the DownloadItem interface). 69 // Implementation functions (not part of the DownloadItem interface).
63 70
71 // Start the download
72 virtual void Start(scoped_ptr<content::DownloadFile> download_file);
73
64 // Called when the target path has been determined. |target_path| is the 74 // Called when the target path has been determined. |target_path| is the
65 // suggested target path. |disposition| indicates how the target path should 75 // suggested target path. |disposition| indicates how the target path should
66 // be used (see TargetDisposition). |danger_type| is the danger level of 76 // be used (see TargetDisposition). |danger_type| is the danger level of
67 // |target_path| as determined by the caller. |intermediate_path| is the path 77 // |target_path| as determined by the caller. |intermediate_path| is the path
68 // to use to store the download until OnDownloadCompleting() is called. 78 // to use to store the download until OnDownloadCompleting() is called.
69 virtual void OnDownloadTargetDetermined( 79 virtual void OnDownloadTargetDetermined(
70 const FilePath& target_path, 80 const FilePath& target_path,
71 TargetDisposition disposition, 81 TargetDisposition disposition,
72 content::DownloadDangerType danger_type, 82 content::DownloadDangerType danger_type,
73 const FilePath& intermediate_path); 83 const FilePath& intermediate_path);
74 84
75 // Indicate that an error has occurred on the download. 85 // Indicate that an error has occurred on the download.
76 virtual void Interrupt(content::DownloadInterruptReason reason); 86 virtual void Interrupt(content::DownloadInterruptReason reason);
77 87
78 // Mark the item as having been persisted. 88 // Mark the item as having been persisted.
79 virtual void SetIsPersisted(); 89 virtual void SetIsPersisted();
80 90
81 // Set the item's DB handle. 91 // Set the item's DB handle.
82 virtual void SetDbHandle(int64 handle); 92 virtual void SetDbHandle(int64 handle);
83 93
84 // Cancels the off-thread aspects of the download.
85 // TODO(rdsmith): This should be private and only called from
86 // DownloadItem::Cancel/Interrupt; it isn't now because we can't
87 // call those functions from
88 // DownloadManager::FileSelectionCancelled() without doing some
89 // rewrites of the DownloadManager queues.
90 virtual void OffThreadCancel();
91
92 // Called when the downloaded file is removed. 94 // Called when the downloaded file is removed.
93 virtual void OnDownloadedFileRemoved(); 95 virtual void OnDownloadedFileRemoved();
94 96
95 // Called when the download is ready to complete. 97 // Called when the download is ready to complete.
96 // This may perform final rename if necessary and will eventually call 98 // This may perform final rename if necessary and will eventually call
97 // DownloadItem::Completed(). 99 // DownloadItem::Completed().
98 virtual void OnDownloadCompleting(); 100 virtual void OnDownloadCompleting();
99 101
100 // Called periodically from the download thread, or from the UI thread 102 // Called periodically from the download thread, or from the UI thread
101 // for saving packages. 103 // for saving packages.
102 // |bytes_so_far| is the number of bytes received so far. 104 // |bytes_so_far| is the number of bytes received so far.
103 // |hash_state| is the current hash state. 105 // |hash_state| is the current hash state.
104 virtual void UpdateProgress(int64 bytes_so_far, 106 virtual void UpdateProgress(int64 bytes_so_far,
105 int64 bytes_per_sec, 107 int64 bytes_per_sec,
106 const std::string& hash_state); 108 const std::string& hash_state);
107 109
108 // Called by SavePackage to display progress when the DownloadItem 110 // Called by SavePackage to display progress when the DownloadItem
109 // should be considered complete. 111 // should be considered complete.
110 virtual void MarkAsComplete(); 112 virtual void MarkAsComplete();
111 113
112 // Called when all data has been saved. Only has display effects. 114 // Called when all data has been saved. Only has display effects.
113 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); 115 virtual void OnAllDataSaved(const std::string& final_hash);
114 116
115 // Called by SavePackage to set the total number of bytes on the item. 117 // Called by SavePackage to set the total number of bytes on the item.
116 virtual void SetTotalBytes(int64 total_bytes); 118 virtual void SetTotalBytes(int64 total_bytes);
117 119
120 // Provide a weak pointer reference to a DownloadDestinationObserver
121 // for use by download destinations.
122 base::WeakPtr<content::DownloadDestinationObserver>
123 DestinationObserverAsWeakPtr();
124
118 // Notify observers that this item is being removed by the user. 125 // Notify observers that this item is being removed by the user.
119 virtual void NotifyRemoved(); 126 virtual void NotifyRemoved();
120 127
121 // Overridden from DownloadItem. 128 // Overridden from DownloadItem.
122 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; 129 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE;
123 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; 130 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE;
124 virtual void UpdateObservers() OVERRIDE; 131 virtual void UpdateObservers() OVERRIDE;
125 virtual bool CanShowInFolder() OVERRIDE; 132 virtual bool CanShowInFolder() OVERRIDE;
126 virtual bool CanOpenDownload() OVERRIDE; 133 virtual bool CanOpenDownload() OVERRIDE;
127 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; 134 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 GetPersistentStoreInfo() const OVERRIDE; 200 GetPersistentStoreInfo() const OVERRIDE;
194 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; 201 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE;
195 virtual content::WebContents* GetWebContents() const OVERRIDE; 202 virtual content::WebContents* GetWebContents() const OVERRIDE;
196 virtual FilePath GetFileNameToReportUser() const OVERRIDE; 203 virtual FilePath GetFileNameToReportUser() const OVERRIDE;
197 virtual void SetDisplayName(const FilePath& name) OVERRIDE; 204 virtual void SetDisplayName(const FilePath& name) OVERRIDE;
198 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; 205 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE;
199 virtual std::string DebugString(bool verbose) const OVERRIDE; 206 virtual std::string DebugString(bool verbose) const OVERRIDE;
200 virtual void MockDownloadOpenForTesting() OVERRIDE; 207 virtual void MockDownloadOpenForTesting() OVERRIDE;
201 208
202 private: 209 private:
210 // DownloadDestinationObserver
211 virtual void DestinationUpdate(int64 bytes_so_far,
212 int64 bytes_per_sec,
213 const std::string& hash_state) OVERRIDE;
214 virtual void DestinationError(
215 content::DownloadInterruptReason reason) OVERRIDE;
216 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE;
217
218 // For weak pointer downcasting.
219 friend class base::WeakPtr<content::DownloadDestinationObserver>;
220
203 // Construction common to all constructors. |active| should be true for new 221 // Construction common to all constructors. |active| should be true for new
204 // downloads and false for downloads from the history. 222 // downloads and false for downloads from the history.
205 // |download_type| indicates to the net log system what kind of download 223 // |download_type| indicates to the net log system what kind of download
206 // this is. 224 // this is.
207 void Init(bool active, download_net_logs::DownloadType download_type); 225 void Init(bool active, download_net_logs::DownloadType download_type);
208 226
209 // Returns true if the download still needs to be renamed to 227 // Returns true if the download still needs to be renamed to
210 // GetTargetFilePath(). 228 // GetTargetFilePath().
211 bool NeedsRename() const; 229 bool NeedsRename() const;
212 230
213 // If all pre-requisites have been met, complete download processing, i.e. do 231 // If all pre-requisites have been met, complete download processing, i.e. do
214 // internal cleanup, file rename, and potentially auto-open. (Dangerous 232 // internal cleanup, file rename, and potentially auto-open. (Dangerous
215 // downloads still may block on user acceptance after this point.) 233 // downloads still may block on user acceptance after this point.)
216 void MaybeCompleteDownload(); 234 void MaybeCompleteDownload();
217 235
218 // Internal helper for maintaining consistent received and total sizes, and
219 // setting the final hash.
220 // Should only be called from |OnAllDataSaved|.
221 void ProgressComplete(int64 bytes_so_far,
222 const std::string& final_hash);
223
224 // Called when the entire download operation (including renaming etc) 236 // Called when the entire download operation (including renaming etc)
225 // is completed. 237 // is completed.
226 void Completed(); 238 void Completed();
227 239
228 // Call to transition state; all state transitions should go through this. 240 // Call to transition state; all state transitions should go through this.
229 void TransitionTo(DownloadState new_state); 241 void TransitionTo(DownloadState new_state);
230 242
231 // Set the |danger_type_| and invoke obserers if necessary. 243 // Set the |danger_type_| and invoke obserers if necessary.
232 void SetDangerType(content::DownloadDangerType danger_type); 244 void SetDangerType(content::DownloadDangerType danger_type);
233 245
234 // Set the |current_path_| to |new_path|. 246 // Set the |current_path_| to |new_path|.
235 void SetFullPath(const FilePath& new_path); 247 void SetFullPath(const FilePath& new_path);
236 248
237 // Callback invoked when the download has been renamed to its final name. 249 // Callback invoked when the download has been renamed to its final name.
238 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, 250 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason,
239 const FilePath& full_path); 251 const FilePath& full_path);
240 252
241 // Callback invoked when the download has been renamed to its intermediate 253 // Callback invoked when the download has been renamed to its intermediate
242 // name. 254 // name.
243 void OnDownloadRenamedToIntermediateName( 255 void OnDownloadRenamedToIntermediateName(
244 content::DownloadInterruptReason reason, const FilePath& full_path); 256 content::DownloadInterruptReason reason, const FilePath& full_path);
245 257
258 // Callback from file thread when we initialize the DownloadFile.
259 void OnDownloadFileInitialized(
260 content::DownloadInterruptReason result);
261
246 // Callback from file thread when we release the DownloadFile. 262 // Callback from file thread when we release the DownloadFile.
247 void OnDownloadFileReleased(); 263 void OnDownloadFileReleased();
248 264
249 // The handle to the request information. Used for operations outside the 265 // The handle to the request information. Used for operations outside the
250 // download system. 266 // download system.
251 scoped_ptr<DownloadRequestHandleInterface> request_handle_; 267 scoped_ptr<DownloadRequestHandleInterface> request_handle_;
252 268
253 // Download ID assigned by DownloadResourceHandler. 269 // Download ID assigned by DownloadResourceHandler.
254 content::DownloadId download_id_; 270 content::DownloadId download_id_;
255 271
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 // when the user closes the shelf before the item has been opened but should 413 // when the user closes the shelf before the item has been opened but should
398 // be treated as though the user opened it. 414 // be treated as though the user opened it.
399 bool opened_; 415 bool opened_;
400 416
401 // Do we actually open downloads when requested? For testing purposes only. 417 // Do we actually open downloads when requested? For testing purposes only.
402 bool open_enabled_; 418 bool open_enabled_;
403 419
404 // Did the delegate delay calling Complete on this download? 420 // Did the delegate delay calling Complete on this download?
405 bool delegate_delayed_complete_; 421 bool delegate_delayed_complete_;
406 422
423 // DownloadFile associated with this download. Note that this
424 // pointer may only be used or destroyed on the FILE thread.
425 // This pointer will be non-null only while the DownloadItem is in
426 // the IN_PROGRESS state.
427 scoped_ptr<content::DownloadFile> download_file_;
428
407 // Net log to use for this download. 429 // Net log to use for this download.
408 const net::BoundNetLog bound_net_log_; 430 const net::BoundNetLog bound_net_log_;
409 431
410 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; 432 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_;
411 433
412 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); 434 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl);
413 }; 435 };
414 436
415 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 437 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
OLDNEW
« no previous file with comments | « content/browser/download/download_file_unittest.cc ('k') | content/browser/download/download_item_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698