| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_JOB_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "content/browser/byte_stream.h" | 11 #include "content/browser/byte_stream.h" |
| 12 #include "content/browser/download/download_file.h" |
| 11 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 12 #include "content/public/browser/download_danger_type.h" | 14 #include "content/public/browser/download_danger_type.h" |
| 13 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/public/browser/download_interrupt_reasons.h" |
| 14 | 16 |
| 15 namespace content { | 17 namespace content { |
| 16 | 18 |
| 17 class DownloadItemImpl; | 19 class DownloadItemImpl; |
| 18 class WebContents; | 20 class WebContents; |
| 19 | 21 |
| 20 // DownloadJob lives on UI thread and subclasses implement actual download logic | 22 // DownloadJob lives on UI thread and subclasses implement actual download logic |
| 21 // and interact with DownloadItemImpl. | 23 // and interact with DownloadItemImpl. |
| 22 // The base class is a friend class of DownloadItemImpl. | 24 // The base class is a friend class of DownloadItemImpl. |
| 23 class CONTENT_EXPORT DownloadJob { | 25 class CONTENT_EXPORT DownloadJob { |
| 24 public: | 26 public: |
| 25 explicit DownloadJob(DownloadItemImpl* download_item); | 27 explicit DownloadJob(DownloadItemImpl* download_item); |
| 26 virtual ~DownloadJob(); | 28 virtual ~DownloadJob(); |
| 27 | 29 |
| 28 // Download operations. | 30 // Download operations. |
| 29 virtual void Start() = 0; | 31 // TODO(qinmin): Remove the |callback| and |download_file_| parameter if |
| 32 // DownloadJob owns download file. |
| 33 void Start(DownloadFile* download_file_, |
| 34 const DownloadFile::InitializeCallback& callback, |
| 35 const DownloadItem::ReceivedSlices& received_slices); |
| 30 virtual void Cancel(bool user_cancel) = 0; | 36 virtual void Cancel(bool user_cancel) = 0; |
| 31 virtual void Pause(); | 37 virtual void Pause(); |
| 32 virtual void Resume(bool resume_request); | 38 virtual void Resume(bool resume_request); |
| 33 | 39 |
| 34 bool is_paused() const { return is_paused_; } | 40 bool is_paused() const { return is_paused_; } |
| 35 | 41 |
| 36 // Return the WebContents associated with the download. Usually used to | 42 // Return the WebContents associated with the download. Usually used to |
| 37 // associate a browser window for any UI that needs to be displayed to the | 43 // associate a browser window for any UI that needs to be displayed to the |
| 38 // user. | 44 // user. |
| 39 // Or return nullptr if the download is not associated with an active | 45 // Or return nullptr if the download is not associated with an active |
| 40 // WebContents. | 46 // WebContents. |
| 41 virtual WebContents* GetWebContents() const = 0; | 47 virtual WebContents* GetWebContents() const = 0; |
| 42 | 48 |
| 43 // Returns whether the download is parallelizable. The download may not send | 49 // Returns whether the download is parallelizable. The download may not send |
| 44 // parallel requests as it can be disabled through flags. | 50 // parallel requests as it can be disabled through flags. |
| 45 virtual bool IsParallelizable() const; | 51 virtual bool IsParallelizable() const; |
| 46 | 52 |
| 47 // Cancel a particular request starts from |offset|, while the download is not | 53 // Cancel a particular request starts from |offset|, while the download is not |
| 48 // canceled. Used in parallel download. | 54 // canceled. Used in parallel download. |
| 49 // TODO(xingliu): Remove this function if download job owns download file. | 55 // TODO(xingliu): Remove this function if download job owns download file. |
| 50 virtual void CancelRequestWithOffset(int64_t offset); | 56 virtual void CancelRequestWithOffset(int64_t offset); |
| 51 | 57 |
| 52 protected: | 58 protected: |
| 53 void StartDownload() const; | 59 // Callback from file thread when we initialize the DownloadFile. |
| 60 virtual void OnDownloadFileInitialized( |
| 61 const DownloadFile::InitializeCallback& callback, |
| 62 DownloadInterruptReason result); |
| 54 | 63 |
| 55 // Add a byte stream to the download sink. Return false if we start to | 64 // Add a byte stream to the download sink. Return false if we start to |
| 56 // destroy download file. | 65 // destroy download file. |
| 57 bool AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, | 66 bool AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, |
| 58 int64_t offset, | 67 int64_t offset, |
| 59 int64_t length); | 68 int64_t length); |
| 60 | 69 |
| 61 DownloadItemImpl* download_item_; | 70 DownloadItemImpl* download_item_; |
| 62 | 71 |
| 63 private: | 72 private: |
| 64 // If the download progress is paused by the user. | 73 // If the download progress is paused by the user. |
| 65 bool is_paused_; | 74 bool is_paused_; |
| 66 | 75 |
| 76 base::WeakPtrFactory<DownloadJob> weak_ptr_factory_; |
| 77 |
| 67 DISALLOW_COPY_AND_ASSIGN(DownloadJob); | 78 DISALLOW_COPY_AND_ASSIGN(DownloadJob); |
| 68 }; | 79 }; |
| 69 | 80 |
| 70 } // namespace content | 81 } // namespace content |
| 71 | 82 |
| 72 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ | 83 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| OLD | NEW |