xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| 6 | #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| 7 | |
| 8 | #include "base/macros.h" |
| 9 | #include "base/time/time.h" |
xingliu | 6719c20 | 2017-03-18 03:45:21 | [diff] [blame] | 10 | #include "content/browser/byte_stream.h" |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 11 | #include "content/common/content_export.h" |
| 12 | #include "content/public/browser/download_danger_type.h" |
| 13 | #include "content/public/browser/download_interrupt_reasons.h" |
| 14 | |
| 15 | namespace content { |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 16 | |
| 17 | class DownloadItemImpl; |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 18 | class WebContents; |
| 19 | |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 20 | // DownloadJob lives on UI thread and subclasses implement actual download logic |
| 21 | // and interact with DownloadItemImpl. |
| 22 | // The base class is a friend class of DownloadItemImpl. |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 23 | class CONTENT_EXPORT DownloadJob { |
| 24 | public: |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 25 | explicit DownloadJob(DownloadItemImpl* download_item); |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 26 | virtual ~DownloadJob(); |
| 27 | |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 28 | // Download operations. |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 29 | virtual void Start() = 0; |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 30 | virtual void Cancel(bool user_cancel) = 0; |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 31 | virtual void Pause(); |
| 32 | virtual void Resume(bool resume_request); |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 33 | |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 34 | bool is_paused() const { return is_paused_; } |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 35 | |
| 36 | // 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 |
| 38 | // user. |
| 39 | // Or return nullptr if the download is not associated with an active |
| 40 | // WebContents. |
| 41 | virtual WebContents* GetWebContents() const = 0; |
| 42 | |
qinmin | 1c18f0d | 2017-03-23 02:01:07 | [diff] [blame] | 43 | // Returns whether the download uses parallel requests. |
| 44 | virtual bool UsesParallelRequests() const; |
| 45 | |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 46 | protected: |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 47 | void StartDownload() const; |
xingliu | 391c49d74 | 2017-03-21 19:47:37 | [diff] [blame] | 48 | void Interrupt(DownloadInterruptReason reason); |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 49 | |
xingliu | cca0315b | 2017-03-27 20:54:23 | [diff] [blame] | 50 | // Add a byte stream to the download sink. Return false if we start to |
| 51 | // destroy download file. |
| 52 | bool AddByteStream(std::unique_ptr<ByteStreamReader> stream_reader, |
xingliu | 6719c20 | 2017-03-18 03:45:21 | [diff] [blame] | 53 | int64_t offset, |
| 54 | int64_t length); |
| 55 | |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 56 | DownloadItemImpl* download_item_; |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 57 | |
| 58 | private: |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 59 | // If the download progress is paused by the user. |
| 60 | bool is_paused_; |
| 61 | |
xingliu | f61d39f | 2017-01-27 22:46:46 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(DownloadJob); |
| 63 | }; |
| 64 | |
| 65 | } // namespace content |
| 66 | |
| 67 | #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |