OLD | NEW |
---|---|
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 146 |
140 // All remaining public interfaces virtual to allow for DownloadItemImpl | 147 // All remaining public interfaces virtual to allow for DownloadItemImpl |
141 // mocks. | 148 // mocks. |
142 | 149 |
143 // Main entry points for regular downloads, in order ------------------------- | 150 // Main entry points for regular downloads, in order ------------------------- |
144 | 151 |
145 // TODO(rdsmith): Fold the process that uses these fully into | 152 // TODO(rdsmith): Fold the process that uses these fully into |
146 // DownloadItemImpl and pass callbacks to the delegate so that all of | 153 // DownloadItemImpl and pass callbacks to the delegate so that all of |
147 // these can be made private | 154 // these can be made private |
148 | 155 |
156 // Start the download | |
157 virtual void Start(scoped_ptr<content::DownloadFile> download_file); | |
158 | |
149 // Called when the target path has been determined. |target_path| is the | 159 // Called when the target path has been determined. |target_path| is the |
150 // suggested target path. |disposition| indicates how the target path should | 160 // suggested target path. |disposition| indicates how the target path should |
151 // be used (see TargetDisposition). |danger_type| is the danger level of | 161 // be used (see TargetDisposition). |danger_type| is the danger level of |
152 // |target_path| as determined by the caller. |intermediate_path| is the path | 162 // |target_path| as determined by the caller. |intermediate_path| is the path |
153 // to use to store the download until OnDownloadCompleting() is called. | 163 // to use to store the download until OnDownloadCompleting() is called. |
154 virtual void OnDownloadTargetDetermined( | 164 virtual void OnDownloadTargetDetermined( |
155 const FilePath& target_path, | 165 const FilePath& target_path, |
156 TargetDisposition disposition, | 166 TargetDisposition disposition, |
157 content::DownloadDangerType danger_type, | 167 content::DownloadDangerType danger_type, |
158 const FilePath& intermediate_path); | 168 const FilePath& intermediate_path); |
159 | 169 |
160 // Called when the download is ready to complete. | 170 // Called when the download is ready to complete. |
161 // This may perform final rename if necessary and will eventually call | 171 // This may perform final rename if necessary and will eventually call |
162 // DownloadItem::Completed(). | 172 // DownloadItem::Completed(). |
163 virtual void OnDownloadCompleting(); | 173 virtual void OnDownloadCompleting(); |
164 | 174 |
165 // Needed because of interwining with DownloadManagerImpl -------------------- | 175 // Needed because of interwining with DownloadManagerImpl -------------------- |
166 | 176 |
167 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 177 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
168 // removing these from the public interface. | 178 // removing these from the public interface. |
169 | 179 |
170 // Notify observers that this item is being removed by the user. | 180 // Notify observers that this item is being removed by the user. |
171 virtual void NotifyRemoved(); | 181 virtual void NotifyRemoved(); |
172 | 182 |
173 virtual void OnDownloadedFileRemoved(); | 183 virtual void OnDownloadedFileRemoved(); |
174 | 184 |
175 // Cancels the off-thread aspects of the download (such as | |
176 // the corresponding URLRequest). | |
177 // TODO(rdsmith): This should be private and only called from | |
178 // DownloadItem::Cancel/Interrupt; it isn't now because we can't | |
179 // call those functions from | |
180 // DownloadManager::FileSelectionCancelled() without doing some | |
181 // rewrites of the DownloadManager queues. | |
182 virtual void OffThreadCancel(); | |
183 | |
184 // Indicate that an error has occurred on the download. | 185 // Indicate that an error has occurred on the download. |
185 virtual void Interrupt(content::DownloadInterruptReason reason); | 186 virtual void Interrupt(content::DownloadInterruptReason reason); |
186 | 187 |
188 // Provide a weak pointer reference to a DownloadDestinationObserver | |
189 // for use by download destinations. | |
190 base::WeakPtr<content::DownloadDestinationObserver> | |
191 DestinationObserverAsWeakPtr(); | |
192 | |
193 // For dispatching on whether we're dealing with a SavePackage download. | |
194 virtual bool IsSavePackageDownload() const; | |
195 | |
187 // DownloadItemImpl routines only needed by SavePackage ---------------------- | 196 // DownloadItemImpl routines only needed by SavePackage ---------------------- |
188 | 197 |
189 // Called by SavePackage to set the total number of bytes on the item. | 198 // Called by SavePackage to set the total number of bytes on the item. |
190 virtual void SetTotalBytes(int64 total_bytes); | 199 virtual void SetTotalBytes(int64 total_bytes); |
191 | 200 |
192 // Indicate progress in saving data to its destination. | 201 // Indicate progress in saving data to its destination. |
193 // |bytes_so_far| is the number of bytes received so far. | 202 // |bytes_so_far| is the number of bytes received so far. |
194 // |hash_state| is the current hash state. | 203 // |hash_state| is the current hash state. |
195 virtual void UpdateProgress(int64 bytes_so_far, | 204 virtual void UpdateProgress(int64 bytes_so_far, |
196 int64 bytes_per_sec, | 205 int64 bytes_per_sec, |
197 const std::string& hash_state); | 206 const std::string& hash_state); |
198 | 207 |
199 virtual void OnAllDataSaved(int64 size, const std::string& final_hash); | 208 virtual void OnAllDataSaved(const std::string& final_hash); |
200 | 209 |
201 // Called by SavePackage to display progress when the DownloadItem | 210 // Called by SavePackage to display progress when the DownloadItem |
202 // should be considered complete. | 211 // should be considered complete. |
203 virtual void MarkAsComplete(); | 212 virtual void MarkAsComplete(); |
204 | 213 |
205 // Interactions with persistence system -------------------------------------- | 214 // Interactions with persistence system -------------------------------------- |
206 | 215 |
207 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. | 216 // TODO(benjhayden): Remove when DownloadHistory becomes an observer. |
208 virtual void SetIsPersisted(); | 217 virtual void SetIsPersisted(); |
209 virtual void SetDbHandle(int64 handle); | 218 virtual void SetDbHandle(int64 handle); |
210 | 219 |
211 private: | 220 private: |
221 // For weak pointer downcasting. | |
benjhayden
2012/09/12 21:01:01
Can you explain this a bit more? Who downcasts wha
Randy Smith (Not in Mondays)
2012/09/13 20:15:12
Whoops, no longer needed; removed.
It was origina
| |
222 friend class base::WeakPtr<content::DownloadDestinationObserver>; | |
223 | |
224 // DownloadDestinationObserver | |
225 virtual void DestinationUpdate(int64 bytes_so_far, | |
226 int64 bytes_per_sec, | |
227 const std::string& hash_state) OVERRIDE; | |
228 virtual void DestinationError( | |
229 content::DownloadInterruptReason reason) OVERRIDE; | |
230 virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE; | |
231 | |
212 // Normal progression of a download ------------------------------------------ | 232 // Normal progression of a download ------------------------------------------ |
213 | 233 |
214 // These are listed in approximately chronological order. There are also | 234 // These are listed in approximately chronological order. There are also |
215 // public methods involved in normal download progression; see | 235 // public methods involved in normal download progression; see |
216 // the implementation ordering in download_item_impl.cc. | 236 // the implementation ordering in download_item_impl.cc. |
217 | 237 |
218 // Construction common to all constructors. |active| should be true for new | 238 // Construction common to all constructors. |active| should be true for new |
219 // downloads and false for downloads from the history. | 239 // downloads and false for downloads from the history. |
220 // |download_type| indicates to the net log system what kind of download | 240 // |download_type| indicates to the net log system what kind of download |
221 // this is. | 241 // this is. |
222 void Init(bool active, download_net_logs::DownloadType download_type); | 242 void Init(bool active, download_net_logs::DownloadType download_type); |
223 | 243 |
244 // Callback from file thread when we initialize the DownloadFile. | |
245 void OnDownloadFileInitialized( | |
246 content::DownloadInterruptReason result); | |
247 | |
224 void OnDownloadRenamedToIntermediateName( | 248 void OnDownloadRenamedToIntermediateName( |
225 content::DownloadInterruptReason reason, const FilePath& full_path); | 249 content::DownloadInterruptReason reason, const FilePath& full_path); |
226 | 250 |
227 // If all pre-requisites have been met, complete download processing, i.e. do | 251 // If all pre-requisites have been met, complete download processing, i.e. do |
228 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 252 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
229 // downloads still may block on user acceptance after this point.) | 253 // downloads still may block on user acceptance after this point.) |
230 void MaybeCompleteDownload(); | 254 void MaybeCompleteDownload(); |
231 | 255 |
232 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, | 256 void OnDownloadRenamedToFinalName(content::DownloadInterruptReason reason, |
233 const FilePath& full_path); | 257 const FilePath& full_path); |
234 | 258 |
259 void ReleaseDownloadFile(); | |
260 | |
235 void OnDownloadFileReleased(); | 261 void OnDownloadFileReleased(); |
236 | 262 |
237 // Called when the entire download operation (including renaming etc) | 263 // Called when the entire download operation (including renaming etc) |
238 // is completed. | 264 // is completed. |
239 void Completed(); | 265 void Completed(); |
240 | 266 |
241 // Helper routines ----------------------------------------------------------- | 267 // Helper routines ----------------------------------------------------------- |
242 | 268 |
243 // Returns true if the download still needs to be renamed to | 269 // Returns true if the download still needs to be renamed to |
244 // GetTargetFilePath(). | 270 // GetTargetFilePath(). |
245 bool NeedsRename() const; | 271 bool NeedsRename() const; |
246 | 272 |
247 // Internal helper for maintaining consistent received and total sizes, and | |
248 // setting the final hash. | |
249 // Should only be called from |OnAllDataSaved|. | |
250 void ProgressComplete(int64 bytes_so_far, | |
251 const std::string& final_hash); | |
252 | |
253 // Call to transition state; all state transitions should go through this. | 273 // Call to transition state; all state transitions should go through this. |
254 void TransitionTo(DownloadState new_state); | 274 void TransitionTo(DownloadState new_state); |
255 | 275 |
256 // Set the |danger_type_| and invoke obserers if necessary. | 276 // Set the |danger_type_| and invoke obserers if necessary. |
257 void SetDangerType(content::DownloadDangerType danger_type); | 277 void SetDangerType(content::DownloadDangerType danger_type); |
258 | 278 |
259 void SetFullPath(const FilePath& new_path); | 279 void SetFullPath(const FilePath& new_path); |
260 | 280 |
281 // Will be false for save package downloads retrieved from the history. | |
282 // TODO(rdsmith): Replace with a generalized enum for "download source". | |
283 const bool is_save_package_download_; | |
284 | |
261 // The handle to the request information. Used for operations outside the | 285 // The handle to the request information. Used for operations outside the |
262 // download system. | 286 // download system. |
263 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 287 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
264 | 288 |
265 // Download ID assigned by DownloadResourceHandler. | 289 // Download ID assigned by DownloadResourceHandler. |
266 content::DownloadId download_id_; | 290 content::DownloadId download_id_; |
267 | 291 |
268 // Display name for the download. If this is empty, then the display name is | 292 // Display name for the download. If this is empty, then the display name is |
269 // considered to be |target_path_.BaseName()|. | 293 // considered to be |target_path_.BaseName()|. |
270 FilePath display_name_; | 294 FilePath display_name_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 // when the user closes the shelf before the item has been opened but should | 430 // when the user closes the shelf before the item has been opened but should |
407 // be treated as though the user opened it. | 431 // be treated as though the user opened it. |
408 bool opened_; | 432 bool opened_; |
409 | 433 |
410 // Do we actually open downloads when requested? For testing purposes only. | 434 // Do we actually open downloads when requested? For testing purposes only. |
411 bool open_enabled_; | 435 bool open_enabled_; |
412 | 436 |
413 // Did the delegate delay calling Complete on this download? | 437 // Did the delegate delay calling Complete on this download? |
414 bool delegate_delayed_complete_; | 438 bool delegate_delayed_complete_; |
415 | 439 |
440 // DownloadFile associated with this download. Note that this | |
441 // pointer may only be used or destroyed on the FILE thread. | |
442 // This pointer will be non-null only while the DownloadItem is in | |
443 // the IN_PROGRESS state. | |
444 scoped_ptr<content::DownloadFile> download_file_; | |
445 | |
416 // Net log to use for this download. | 446 // Net log to use for this download. |
417 const net::BoundNetLog bound_net_log_; | 447 const net::BoundNetLog bound_net_log_; |
418 | 448 |
419 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 449 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
420 | 450 |
421 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 451 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
422 }; | 452 }; |
423 | 453 |
424 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 454 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |