| 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/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| 11 #include "content/public/browser/download_danger_type.h" | 11 #include "content/public/browser/download_danger_type.h" |
| 12 #include "content/public/browser/download_interrupt_reasons.h" | 12 #include "content/public/browser/download_interrupt_reasons.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 |
| 16 class DownloadItemImpl; |
| 15 class WebContents; | 17 class WebContents; |
| 16 | 18 |
| 17 // Unknown download progress. | 19 // DownloadJob lives on UI thread and subclasses implement actual download logic |
| 18 extern const int kDownloadProgressUnknown; | 20 // and interact with DownloadItemImpl. |
| 19 | 21 // The base class is a friend class of DownloadItemImpl. |
| 20 // Unknown download speed. | |
| 21 extern const int kDownloadSpeedUnknown; | |
| 22 | |
| 23 // DownloadJob contains internal download logic. | |
| 24 // The owner of DownloadJob(e.g DownloadItemImpl) should implement | |
| 25 // DownloadJob::Manager to be informed with important changes from | |
| 26 // DownloadJob and modify its states accordingly. | |
| 27 class CONTENT_EXPORT DownloadJob { | 22 class CONTENT_EXPORT DownloadJob { |
| 28 public: | 23 public: |
| 29 // The interface used to interact with the owner of the download job. | |
| 30 class CONTENT_EXPORT Manager { | |
| 31 public: | |
| 32 // Called when |download_job| becomes actively download data. | |
| 33 virtual void OnSavingStarted(DownloadJob* download_job) = 0; | |
| 34 | |
| 35 // Called when |download_job| is interrupted. | |
| 36 virtual void OnDownloadInterrupted(DownloadJob* download_job, | |
| 37 DownloadInterruptReason reason) = 0; | |
| 38 | |
| 39 // Called when |download_job| is completed and inform the manager. | |
| 40 virtual void OnDownloadComplete(DownloadJob* download_job) = 0; | |
| 41 | |
| 42 // Sets the download danger type. | |
| 43 virtual void SetDangerType(DownloadDangerType danger_type) = 0; | |
| 44 }; | |
| 45 | |
| 46 DownloadJob(); | 24 DownloadJob(); |
| 47 virtual ~DownloadJob(); | 25 virtual ~DownloadJob(); |
| 48 | 26 |
| 49 DownloadJob::Manager* manager() { return manager_; } | 27 // Attach the download item to this job. |
| 50 | 28 void Attach(DownloadItemImpl* download_item); |
| 51 // Called when DownloadJob is attached to DownloadJob::Manager. | |
| 52 // |manager| can be nullptr. | |
| 53 virtual void OnAttached(DownloadJob::Manager* manager); | |
| 54 | |
| 55 // Called before DownloadJob is detached from its manager. | |
| 56 virtual void OnBeforeDetach(); | |
| 57 | 29 |
| 58 // Download operations. | 30 // Download operations. |
| 31 virtual void Start() = 0; |
| 59 virtual void Cancel(bool user_cancel) = 0; | 32 virtual void Cancel(bool user_cancel) = 0; |
| 60 virtual void Pause() = 0; | 33 virtual void Pause() = 0; |
| 61 virtual void Resume() = 0; | 34 virtual void Resume() = 0; |
| 62 | 35 |
| 63 // Return if the download file can be opened. | |
| 64 // See |DownloadItem::CanOpenDownload|. | |
| 65 virtual bool CanOpen() const = 0; | |
| 66 | |
| 67 // Return if the download job can be resumed. | |
| 68 virtual bool CanResume() const = 0; | |
| 69 | |
| 70 // See |DownloadItem::CanShowInFolder|. | |
| 71 virtual bool CanShowInFolder() const = 0; | |
| 72 | |
| 73 // Return if the download job is actively downloading. | |
| 74 virtual bool IsActive() const = 0; | |
| 75 | |
| 76 // Return if the download job is paused. | |
| 77 virtual bool IsPaused() const = 0; | |
| 78 | |
| 79 // Return a number between 0 and 100 to represent the percentage progress of | |
| 80 // the download. Return |kDownloadProgressUnknown| if the progress is | |
| 81 // indeterminate. | |
| 82 virtual int PercentComplete() const = 0; | |
| 83 | |
| 84 // Return the estimated current download speed in bytes per second. | |
| 85 // Or return kDownloadSpeedUnknown if the download speed is not measurable. | |
| 86 virtual int64_t CurrentSpeed() const = 0; | |
| 87 | |
| 88 // Set the estimated remaining time of the download to |remaining| and return | |
| 89 // true if time remaining is available, otherwise return false. | |
| 90 virtual bool TimeRemaining(base::TimeDelta* remaining) const = 0; | |
| 91 | |
| 92 // Return the WebContents associated with the download. Usually used to | 36 // Return the WebContents associated with the download. Usually used to |
| 93 // associate a browser window for any UI that needs to be displayed to the | 37 // associate a browser window for any UI that needs to be displayed to the |
| 94 // user. | 38 // user. |
| 95 // Or return nullptr if the download is not associated with an active | 39 // Or return nullptr if the download is not associated with an active |
| 96 // WebContents. | 40 // WebContents. |
| 97 virtual WebContents* GetWebContents() const = 0; | 41 virtual WebContents* GetWebContents() const = 0; |
| 98 | 42 |
| 99 // Print the debug string including internal states of the download job. | 43 protected: |
| 100 virtual std::string DebugString(bool verbose) const = 0; | 44 void InitializeDownloadFile() const; |
| 101 | 45 |
| 102 protected: | 46 DownloadItemImpl* download_item_; |
| 103 // Mark the download as being active. | |
| 104 void StartedSavingResponse(); | |
| 105 | |
| 106 // Mark the download as interrupted. | |
| 107 void Interrupt(DownloadInterruptReason reason); | |
| 108 | |
| 109 // Mark the download job as completed. | |
| 110 void Complete(); | |
| 111 | |
| 112 // Owner of this DownloadJob, Only valid between OnAttached() and | |
| 113 // OnBeforeDetach(), inclusive. Otherwise set to nullptr. | |
| 114 DownloadJob::Manager* manager_; | |
| 115 | 47 |
| 116 private: | 48 private: |
| 117 DISALLOW_COPY_AND_ASSIGN(DownloadJob); | 49 DISALLOW_COPY_AND_ASSIGN(DownloadJob); |
| 118 }; | 50 }; |
| 119 | 51 |
| 120 } // namespace content | 52 } // namespace content |
| 121 | 53 |
| 122 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ | 54 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| OLD | NEW |