Chromium Code Reviews| Index: content/browser/download/download_job.h |
| diff --git a/content/browser/download/download_job.h b/content/browser/download/download_job.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11e5c1eee3102b707ba4da80fb7acbe15bef48b2 |
| --- /dev/null |
| +++ b/content/browser/download/download_job.h |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| +#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |
| + |
| +#include "base/macros.h" |
| +#include "base/time/time.h" |
| +#include "content/common/content_export.h" |
| +#include "content/public/browser/download_danger_type.h" |
| +#include "content/public/browser/download_interrupt_reasons.h" |
| + |
| +namespace content { |
| +class WebContents; |
| + |
| +// Unknown download progress. |
| +extern const int kDownloadProgressUnknown; |
| + |
| +// Unknown download speed. |
| +extern const int kDownloadSpeedUnknown; |
| + |
| +// 1. DownloadJob contains internal download logic. Subclasses should implement |
| +// their own state machine. |
| +// 2. The DownloadJob::Manager(e.g DownloadItemImpl) may provide content public |
| +// api to the embedder backed by different implementation of DownloadJob |
| +// internally. |
| +// 3. The DownloadJob::Manager may maintain multiple DownloadJobs. |
| +class CONTENT_EXPORT DownloadJob { |
| + public: |
| + // Types of download jobs. |
| + enum Type { |
| + FILE = 0, // Normal download. |
| + SAVE_PAGE, // Save page download. |
| + }; |
| + |
| + // The owner of DownloadJob(e.g DownloadItemImpl or parallel download job) |
| + // should implement this to be informed with important changes from |
| + // DownloadJob and modify its states accordingly. |
| + class CONTENT_EXPORT Manager { |
| + public: |
| + // Called when |download_job| starts, and pass the ResponseInfo to the |
| + // manager. |
| + // TODO(xingliu): Pass ResponseInfo to the manager. |
| + virtual void StartSaving(DownloadJob* download_job) = 0; |
|
qinmin
2017/01/23 21:46:35
Shouldn't the manager just call download_job_->Sta
xingliu
2017/01/23 22:59:09
These are the callbacks to DownloadItemImpl which
qinmin
2017/01/23 23:03:15
Then shouldn't the function name be OnSavingStarte
xingliu
2017/01/23 23:26:08
Done, changed to OnSavingStarted aligned with othe
|
| + |
| + // Called when |download_job| is interrupted. |
| + virtual void OnDownloadInterrupted(DownloadJob* download_job, |
| + DownloadInterruptReason reason) = 0; |
| + |
| + // Called when |download_job| is completed and inform the manager. |
| + virtual void OnDownloadComplete(DownloadJob* download_job) = 0; |
| + |
| + // Sets the download danger type. |
| + virtual void SetDangerType(DownloadDangerType danger_type) = 0; |
| + }; |
| + |
| + DownloadJob(); |
| + virtual ~DownloadJob(); |
| + |
| + DownloadJob::Manager* manager() { return manager_; } |
| + |
| + Type type() const { return type_; } |
| + |
| + // Called when DownloadJob is attached to DownloadJob::Manager. |
| + virtual void OnAttached(DownloadJob::Manager* manager); |
| + |
| + // Called before DownloadJob is detached from its manager. |
| + virtual void OnBeforeDetach(); |
| + |
| + // Download operations. |
| + virtual void Cancel(bool user_cancel) = 0; |
| + virtual void Pause() = 0; |
| + virtual void Resume() = 0; |
| + |
| + // Return if the download file can be opened. |
| + // See |DownloadItem:: CanOpenDownload|. |
| + virtual bool CanOpen() const = 0; |
| + |
| + // Return if the download job can be resumed. |
| + virtual bool CanResume() const = 0; |
| + |
| + // See |DownloadItem::CanShowInFolder|. |
| + virtual bool CanShowInFolder() const = 0; |
| + |
| + // Return if the download job is actively downloading. |
| + virtual bool IsActive() const = 0; |
| + |
| + // Return if the download job is paused. |
| + virtual bool IsPause() const = 0; |
| + |
| + // Return a number between 0 and 100 to represent the percentage progress of |
| + // the download. Return |kDownloadProgressUnknown| if the progress is |
| + // indeterminate. |
| + virtual int PercentComplete() const = 0; |
| + |
| + // Return the estimated current download speed in bytes per second. |
| + // Or return kDownloadSpeedUnknown if the download speed is not measurable. |
| + virtual int64_t CurrentSpeed() const = 0; |
| + |
| + // Set the estimated remaining time of the download to |remaining| and return |
| + // true if time remaining is available, otherwise return false. |
| + virtual bool TimeRemaining(base::TimeDelta* remaining) const = 0; |
| + |
| + // Return the WebContents associated with the download. Usually used to |
| + // associate a browser window for any UI that needs to be displayed to the |
| + // user. |
| + // Or return nullptr if the download is not associated with an active |
| + // WebContents. |
| + virtual WebContents* GetWebContents() const = 0; |
| + |
| + // Print the debug string including internal states of the download job. |
| + virtual std::string DebugString(bool verbose) const = 0; |
| + |
| + protected: |
| + // Mark the download as being in-progress. |
| + // TODO(xingliu): Pass ResponseInfo to the manager here. |
| + void StartedSavingResponse(); |
| + |
| + // Mark the download as interrupted. |
| + void Interrupt(DownloadInterruptReason reason); |
| + |
| + // Mark the download job as completed. |
| + void Complete(); |
| + |
| + // Owner of this DownloadJob, Only valid between OnAttached() and |
| + // OnBeforeDetach(), inclusive. Otherwise set to nullptr. |
| + DownloadJob::Manager* manager_; |
| + |
| + // The type of the download. |
| + DownloadJob::Type type_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(DownloadJob); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_JOB_H_ |