Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Unified Diff: content/browser/download/download_item_impl.h

Issue 8503018: Split DownloadItem into an ABC, an Impl, and a Mock. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: " Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/download/download_item_impl.h
diff --git a/content/browser/download/download_item_impl.h b/content/browser/download/download_item_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..eb5e90633a32f88f6f8322258dda49088f8aa8d4
--- /dev/null
+++ b/content/browser/download/download_item_impl.h
@@ -0,0 +1,326 @@
+// Copyright (c) 2011 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.
+//
+// Each download is represented by a DownloadItem, and all DownloadItems
+// are owned by the DownloadManager which maintains a global list of all
+// downloads. DownloadItems are created when a user initiates a download,
+// and exist for the duration of the browser life time.
+//
+// Download observers:
+// DownloadItem::Observer:
+// - allows observers to receive notifications about one download from start
+// to completion
+// Use AddObserver() / RemoveObserver() on the appropriate download object to
+// receive state updates.
+
+#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
+#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
+#pragma once
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
+#include "base/time.h"
+#include "base/timer.h"
+#include "content/browser/download/download_item.h"
+#include "content/browser/download/download_id.h"
+#include "content/browser/download/download_request_handle.h"
+#include "content/browser/download/download_state_info.h"
+#include "content/browser/download/interrupt_reasons.h"
+#include "content/common/content_export.h"
+#include "googleurl/src/gurl.h"
+#include "net/base/net_errors.h"
+
+class DownloadFileManager;
+class DownloadId;
+class DownloadManager;
+class TabContents;
+
+struct DownloadCreateInfo;
+struct DownloadPersistentStoreInfo;
+
+// See download_item.h for usage.
+class DownloadItemImpl : public DownloadItem {
+ public:
+ // Constructing from persistent store:
+ DownloadItemImpl(DownloadManager* download_manager,
+ const DownloadPersistentStoreInfo& info);
+
+ // Constructing for a regular download.
+ // Takes ownership of the object pointed to by |request_handle|.
+ DownloadItemImpl(DownloadManager* download_manager,
+ const DownloadCreateInfo& info,
+ DownloadRequestHandleInterface* request_handle,
+ bool is_otr);
+
+ // Constructing for the "Save Page As..." feature:
+ DownloadItemImpl(DownloadManager* download_manager,
+ const FilePath& path,
+ const GURL& url,
+ bool is_otr,
+ DownloadId download_id);
+
+ virtual ~DownloadItemImpl();
+
+ virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE;
Randy Smith (Not in Mondays) 2011/11/09 17:09:57 Note that these are all inherited from DownloadIte
benjhayden 2011/11/11 22:31:12 Done.
+ virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE;
+ virtual void UpdateObservers() OVERRIDE;
+ virtual bool CanShowInFolder() OVERRIDE;
+ virtual bool CanOpenDownload() OVERRIDE;
+ virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE;
+ virtual void OpenDownload() OVERRIDE;
+ virtual void ShowDownloadInShell() OVERRIDE;
+ virtual void DangerousDownloadValidated() OVERRIDE;
+ virtual void Update(int64 bytes_so_far) OVERRIDE;
+ virtual void Cancel(bool user_cancel) OVERRIDE;
+ virtual void MarkAsComplete() OVERRIDE;
+ virtual void DelayedDownloadOpened() OVERRIDE;
+ virtual void OnAllDataSaved(int64 size) OVERRIDE;
+ virtual void OnDownloadedFileRemoved() OVERRIDE;
+ virtual void Interrupted(int64 size, InterruptReason reason) OVERRIDE;
+ virtual void Delete(DeleteReason reason) OVERRIDE;
+ virtual void Remove() OVERRIDE;
+ virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE;
+ virtual int64 CurrentSpeed() const OVERRIDE;
+ virtual int PercentComplete() const OVERRIDE;
+ virtual void OnPathDetermined(const FilePath& path) OVERRIDE;
+ virtual bool all_data_saved() const OVERRIDE { return all_data_saved_; }
+ virtual void SetFileCheckResults(const DownloadStateInfo& state) OVERRIDE;
+ virtual void Rename(const FilePath& full_path) OVERRIDE;
+ virtual void TogglePause() OVERRIDE;
+ virtual void OnDownloadCompleting(DownloadFileManager* file_manager) OVERRIDE;
+ virtual void OnDownloadRenamedToFinalName(const FilePath& full_path) OVERRIDE;
+ virtual bool MatchesQuery(const string16& query) const OVERRIDE;
+ virtual bool IsPartialDownload() const OVERRIDE;
+ virtual bool IsInProgress() const OVERRIDE;
+ virtual bool IsCancelled() const OVERRIDE;
+ virtual bool IsInterrupted() const OVERRIDE;
+ virtual bool IsComplete() const OVERRIDE;
+ virtual DownloadState state() const OVERRIDE { return state_; }
+ virtual const FilePath& full_path() const OVERRIDE { return full_path_; }
+ virtual void set_path_uniquifier(int uniquifier) OVERRIDE {
+ state_info_.path_uniquifier = uniquifier;
+ }
+ virtual const GURL& GetURL() const OVERRIDE;
+ virtual const std::vector<GURL>& url_chain() const OVERRIDE {
+ return url_chain_;
+ }
+ virtual const GURL& original_url() const OVERRIDE {
+ return url_chain_.front();
+ }
+ virtual const GURL& referrer_url() const OVERRIDE { return referrer_url_; }
+ virtual std::string suggested_filename() const OVERRIDE {
+ return suggested_filename_;
+ }
+ virtual std::string content_disposition() const OVERRIDE {
+ return content_disposition_;
+ }
+ virtual std::string mime_type() const OVERRIDE { return mime_type_; }
+ virtual std::string original_mime_type() const OVERRIDE {
+ return original_mime_type_;
+ }
+ virtual std::string referrer_charset() const OVERRIDE {
+ return referrer_charset_;
+ }
+ virtual int64 total_bytes() const OVERRIDE { return total_bytes_; }
+ virtual void set_total_bytes(int64 total_bytes) OVERRIDE {
+ total_bytes_ = total_bytes;
+ }
+ virtual int64 received_bytes() const OVERRIDE { return received_bytes_; }
+ virtual int32 id() const OVERRIDE { return download_id_.local(); }
+ virtual DownloadId global_id() const OVERRIDE { return download_id_; }
+ virtual base::Time start_time() const OVERRIDE { return start_time_; }
+ virtual base::Time end_time() const OVERRIDE { return end_time_; }
+ virtual void set_db_handle(int64 handle) OVERRIDE { db_handle_ = handle; }
+ virtual int64 db_handle() const OVERRIDE { return db_handle_; }
+ virtual DownloadManager* download_manager() OVERRIDE {
+ return download_manager_;
+ }
+ virtual bool is_paused() const OVERRIDE { return is_paused_; }
+ virtual bool open_when_complete() const OVERRIDE {
+ return open_when_complete_;
+ }
+ virtual void set_open_when_complete(bool open) OVERRIDE {
+ open_when_complete_ = open;
+ }
+ virtual bool file_externally_removed() const OVERRIDE {
+ return file_externally_removed_;
+ }
+ virtual SafetyState safety_state() const OVERRIDE { return safety_state_; }
+ virtual DangerType GetDangerType() const;
+ virtual bool IsDangerous() const;
+ virtual void MarkFileDangerous();
+ virtual void MarkUrlDangerous();
+ virtual bool auto_opened() { return auto_opened_; }
+ virtual const FilePath& target_name() const {
+ return state_info_.target_name;
+ }
+ virtual bool prompt_user_for_save_location() const {
+ return state_info_.prompt_user_for_save_location;
+ }
+ virtual bool is_otr() const { return is_otr_; }
+ virtual const FilePath& suggested_path() const {
+ return state_info_.suggested_path;
+ }
+ virtual bool is_temporary() const { return is_temporary_; }
+ virtual void set_opened(bool opened) { opened_ = opened; }
+ virtual bool opened() const { return opened_; }
+ virtual InterruptReason last_reason() const { return last_reason_; }
+ virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const;
+ virtual DownloadStateInfo state_info() const { return state_info_; }
+ virtual TabContents* GetTabContents() const;
+ virtual FilePath GetTargetFilePath() const;
+ virtual FilePath GetFileNameToReportUser() const;
+ virtual FilePath GetUserVerifiedFilePath() const;
+ virtual bool NeedsRename() const {
+ return state_info_.target_name != full_path_.BaseName();
+ }
+ virtual void OffThreadCancel(DownloadFileManager* file_manager);
+ virtual std::string DebugString(bool verbose) const;
+ virtual void MockDownloadOpenForTesting() { open_enabled_ = false; }
+
+ private:
+ // Construction common to all constructors. |active| should be true for new
+ // downloads and false for downloads from the history.
+ void Init(bool active);
+
+ // Internal helper for maintaining consistent received and total sizes.
+ void UpdateSize(int64 size);
+
+ // Called when the entire download operation (including renaming etc)
+ // is completed.
+ void Completed();
+
+ // Call to transition state; all state transitions should go through this.
+ void TransitionTo(DownloadState new_state);
+
+ // Called when safety_state_ should be recomputed from is_dangerous_file
+ // and is_dangerous_url.
+ void UpdateSafetyState();
+
+ // Helper function to recompute |state_info_.target_name| when
+ // it may have changed. (If it's non-null it should be left alone,
+ // otherwise updated from |full_path_|.)
+ void UpdateTarget();
+
+ // State information used by the download manager.
+ DownloadStateInfo state_info_;
+
+ // The handle to the request information. Used for operations outside the
+ // download system.
+ scoped_ptr<DownloadRequestHandleInterface> request_handle_;
+
+ // Download ID assigned by DownloadResourceHandler.
+ DownloadId download_id_;
+
+ // Full path to the downloaded or downloading file.
+ FilePath full_path_;
+
+ // A number that should be appended to the path to make it unique, or 0 if the
+ // path should be used as is.
+ int path_uniquifier_;
+
+ // The chain of redirects that leading up to and including the final URL.
+ std::vector<GURL> url_chain_;
+
+ // The URL of the page that initiated the download.
+ GURL referrer_url_;
+
+ // Suggested filename in 'download' attribute of an anchor. Details:
+ // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks
+ std::string suggested_filename_;
+
+ // Information from the request.
+ // Content-disposition field from the header.
+ std::string content_disposition_;
+
+ // Mime-type from the header. Subject to change.
+ std::string mime_type_;
+
+ // The value of the content type header sent with the downloaded item. It
+ // may be different from |mime_type_|, which may be set based on heuristics
+ // which may look at the file extension and first few bytes of the file.
+ std::string original_mime_type_;
+
+ // The charset of the referring page where the download request comes from.
+ // It's used to construct a suggested filename.
+ std::string referrer_charset_;
+
+ // Total bytes expected
+ int64 total_bytes_;
+
+ // Current received bytes
+ int64 received_bytes_;
+
+ // Last reason.
+ InterruptReason last_reason_;
+
+ // Start time for calculating remaining time
+ base::TimeTicks start_tick_;
+
+ // The current state of this download
+ DownloadState state_;
+
+ // The views of this item in the download shelf and download tab
+ ObserverList<Observer> observers_;
+
+ // Time the download was started
+ base::Time start_time_;
+
+ // Time the download completed
+ base::Time end_time_;
+
+ // Our persistent store handle
+ int64 db_handle_;
+
+ // Our owning object
+ DownloadManager* download_manager_;
+
+ // In progress downloads may be paused by the user, we note it here
+ bool is_paused_;
+
+ // A flag for indicating if the download should be opened at completion.
+ bool open_when_complete_;
+
+ // A flag for indicating if the downloaded file is externally removed.
+ bool file_externally_removed_;
+
+ // Indicates if the download is considered potentially safe or dangerous
+ // (executable files are typically considered dangerous).
+ SafetyState safety_state_;
+
+ // True if the download was auto-opened. We set this rather than using
+ // an observer as it's frequently possible for the download to be auto opened
+ // before the observer is added.
+ bool auto_opened_;
+
+ // True if the download was initiated in an incognito window.
+ bool is_otr_;
+
+ // True if the item was downloaded temporarily.
+ bool is_temporary_;
+
+ // True if we've saved all the data for the download.
+ bool all_data_saved_;
+
+ // Did the user open the item either directly or indirectly (such as by
+ // setting always open files of this type)? The shelf also sets this field
+ // when the user closes the shelf before the item has been opened but should
+ // be treated as though the user opened it.
+ bool opened_;
+
+ // Do we actual open downloads when requested? For testing purposes
+ // only.
+ bool open_enabled_;
+
+ // Did the delegate delay calling Complete on this download?
+ bool delegate_delayed_complete_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl);
+};
+
+#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_

Powered by Google App Engine
This is Rietveld 408576698