OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_ITEM_IMPL_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/time.h" |
| 16 #include "base/timer.h" |
| 17 #include "content/browser/download/download_item.h" |
| 18 #include "content/browser/download/download_id.h" |
| 19 #include "content/browser/download/download_request_handle.h" |
| 20 #include "content/browser/download/download_state_info.h" |
| 21 #include "content/browser/download/interrupt_reasons.h" |
| 22 #include "content/common/content_export.h" |
| 23 #include "googleurl/src/gurl.h" |
| 24 #include "net/base/net_errors.h" |
| 25 |
| 26 class DownloadFileManager; |
| 27 class DownloadId; |
| 28 class DownloadManager; |
| 29 class TabContents; |
| 30 |
| 31 struct DownloadCreateInfo; |
| 32 struct DownloadPersistentStoreInfo; |
| 33 |
| 34 // See download_item.h for usage. |
| 35 class CONTENT_EXPORT DownloadItemImpl : public DownloadItem { |
| 36 public: |
| 37 // Constructing from persistent store: |
| 38 DownloadItemImpl(DownloadManager* download_manager, |
| 39 const DownloadPersistentStoreInfo& info); |
| 40 |
| 41 // Constructing for a regular download. |
| 42 // Takes ownership of the object pointed to by |request_handle|. |
| 43 DownloadItemImpl(DownloadManager* download_manager, |
| 44 const DownloadCreateInfo& info, |
| 45 DownloadRequestHandleInterface* request_handle, |
| 46 bool is_otr); |
| 47 |
| 48 // Constructing for the "Save Page As..." feature: |
| 49 DownloadItemImpl(DownloadManager* download_manager, |
| 50 const FilePath& path, |
| 51 const GURL& url, |
| 52 bool is_otr, |
| 53 DownloadId download_id); |
| 54 |
| 55 virtual ~DownloadItemImpl(); |
| 56 |
| 57 // Overridden from DownloadItem. |
| 58 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
| 59 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
| 60 virtual void UpdateObservers() OVERRIDE; |
| 61 virtual bool CanShowInFolder() OVERRIDE; |
| 62 virtual bool CanOpenDownload() OVERRIDE; |
| 63 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
| 64 virtual void OpenDownload() OVERRIDE; |
| 65 virtual void ShowDownloadInShell() OVERRIDE; |
| 66 virtual void DangerousDownloadValidated() OVERRIDE; |
| 67 virtual void Update(int64 bytes_so_far) OVERRIDE; |
| 68 virtual void Cancel(bool user_cancel) OVERRIDE; |
| 69 virtual void MarkAsComplete() OVERRIDE; |
| 70 virtual void DelayedDownloadOpened() OVERRIDE; |
| 71 virtual void OnAllDataSaved( |
| 72 int64 size, const std::string& final_hash) OVERRIDE; |
| 73 virtual void OnDownloadedFileRemoved() OVERRIDE; |
| 74 virtual void Interrupted(int64 size, InterruptReason reason) OVERRIDE; |
| 75 virtual void Delete(DeleteReason reason) OVERRIDE; |
| 76 virtual void Remove() OVERRIDE; |
| 77 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; |
| 78 virtual int64 CurrentSpeed() const OVERRIDE; |
| 79 virtual int PercentComplete() const OVERRIDE; |
| 80 virtual void OnPathDetermined(const FilePath& path) OVERRIDE; |
| 81 virtual bool AllDataSaved() const OVERRIDE; |
| 82 virtual void SetFileCheckResults(const DownloadStateInfo& state) OVERRIDE; |
| 83 virtual void Rename(const FilePath& full_path) OVERRIDE; |
| 84 virtual void TogglePause() OVERRIDE; |
| 85 virtual void OnDownloadCompleting(DownloadFileManager* file_manager) OVERRIDE; |
| 86 virtual void OnDownloadRenamedToFinalName(const FilePath& full_path) OVERRIDE; |
| 87 virtual bool MatchesQuery(const string16& query) const OVERRIDE; |
| 88 virtual bool IsPartialDownload() const OVERRIDE; |
| 89 virtual bool IsInProgress() const OVERRIDE; |
| 90 virtual bool IsCancelled() const OVERRIDE; |
| 91 virtual bool IsInterrupted() const OVERRIDE; |
| 92 virtual bool IsComplete() const OVERRIDE; |
| 93 virtual DownloadState GetState() const OVERRIDE; |
| 94 virtual const FilePath& GetFullPath() const OVERRIDE; |
| 95 virtual void SetPathUniquifier(int uniquifier) OVERRIDE; |
| 96 virtual const GURL& GetURL() const OVERRIDE; |
| 97 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; |
| 98 virtual const GURL& GetOriginalUrl() const OVERRIDE; |
| 99 virtual const GURL& GetReferrerUrl() const OVERRIDE; |
| 100 virtual std::string GetSuggestedFilename() const OVERRIDE; |
| 101 virtual std::string GetContentDisposition() const OVERRIDE; |
| 102 virtual std::string GetMimeType() const OVERRIDE; |
| 103 virtual std::string GetOriginalMimeType() const OVERRIDE; |
| 104 virtual std::string GetReferrerCharset() const OVERRIDE; |
| 105 virtual int64 GetTotalBytes() const OVERRIDE; |
| 106 virtual void SetTotalBytes(int64 total_bytes) OVERRIDE; |
| 107 virtual const std::string& GetHash() const OVERRIDE; |
| 108 virtual int64 GetReceivedBytes() const OVERRIDE; |
| 109 virtual int32 GetId() const OVERRIDE; |
| 110 virtual DownloadId GetGlobalId() const OVERRIDE; |
| 111 virtual base::Time GetStartTime() const OVERRIDE; |
| 112 virtual base::Time GetEndTime() const OVERRIDE; |
| 113 virtual void SetDbHandle(int64 handle) OVERRIDE; |
| 114 virtual int64 GetDbHandle() const OVERRIDE; |
| 115 virtual DownloadManager* GetDownloadManager() OVERRIDE; |
| 116 virtual bool IsPaused() const OVERRIDE; |
| 117 virtual bool GetOpenWhenComplete() const OVERRIDE; |
| 118 virtual void SetOpenWhenComplete(bool open) OVERRIDE; |
| 119 virtual bool GetFileExternallyRemoved() const OVERRIDE; |
| 120 virtual SafetyState GetSafetyState() const OVERRIDE; |
| 121 virtual DownloadStateInfo::DangerType GetDangerType() const OVERRIDE; |
| 122 virtual bool IsDangerous() const OVERRIDE; |
| 123 virtual void MarkFileDangerous() OVERRIDE; |
| 124 virtual void MarkUrlDangerous() OVERRIDE; |
| 125 virtual void MarkContentDangerous() OVERRIDE; |
| 126 virtual bool GetAutoOpened() OVERRIDE; |
| 127 virtual const FilePath& GetTargetName() const OVERRIDE; |
| 128 virtual bool PromptUserForSaveLocation() const OVERRIDE; |
| 129 virtual bool IsOtr() const OVERRIDE; |
| 130 virtual const FilePath& GetSuggestedPath() const OVERRIDE; |
| 131 virtual bool IsTemporary() const OVERRIDE; |
| 132 virtual void SetOpened(bool opened) OVERRIDE; |
| 133 virtual bool GetOpened() const OVERRIDE; |
| 134 virtual InterruptReason GetLastReason() const OVERRIDE; |
| 135 virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const OVERRIDE; |
| 136 virtual DownloadStateInfo GetStateInfo() const OVERRIDE; |
| 137 virtual TabContents* GetTabContents() const OVERRIDE; |
| 138 virtual FilePath GetTargetFilePath() const OVERRIDE; |
| 139 virtual FilePath GetFileNameToReportUser() const OVERRIDE; |
| 140 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; |
| 141 virtual bool NeedsRename() const OVERRIDE; |
| 142 virtual void OffThreadCancel(DownloadFileManager* file_manager) OVERRIDE; |
| 143 virtual std::string DebugString(bool verbose) const OVERRIDE; |
| 144 virtual void MockDownloadOpenForTesting() OVERRIDE; |
| 145 |
| 146 private: |
| 147 // Construction common to all constructors. |active| should be true for new |
| 148 // downloads and false for downloads from the history. |
| 149 void Init(bool active); |
| 150 |
| 151 // Internal helper for maintaining consistent received and total sizes. |
| 152 void UpdateSize(int64 size); |
| 153 |
| 154 // Called when the entire download operation (including renaming etc) |
| 155 // is completed. |
| 156 void Completed(); |
| 157 |
| 158 // Call to transition state; all state transitions should go through this. |
| 159 void TransitionTo(DownloadState new_state); |
| 160 |
| 161 // Called when safety_state_ should be recomputed from is_dangerous_file |
| 162 // and is_dangerous_url. |
| 163 void UpdateSafetyState(); |
| 164 |
| 165 // Helper function to recompute |state_info_.target_name| when |
| 166 // it may have changed. (If it's non-null it should be left alone, |
| 167 // otherwise updated from |full_path_|.) |
| 168 void UpdateTarget(); |
| 169 |
| 170 // State information used by the download manager. |
| 171 DownloadStateInfo state_info_; |
| 172 |
| 173 // The handle to the request information. Used for operations outside the |
| 174 // download system. |
| 175 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
| 176 |
| 177 // Download ID assigned by DownloadResourceHandler. |
| 178 DownloadId download_id_; |
| 179 |
| 180 // Full path to the downloaded or downloading file. |
| 181 FilePath full_path_; |
| 182 |
| 183 // A number that should be appended to the path to make it unique, or 0 if the |
| 184 // path should be used as is. |
| 185 int path_uniquifier_; |
| 186 |
| 187 // The chain of redirects that leading up to and including the final URL. |
| 188 std::vector<GURL> url_chain_; |
| 189 |
| 190 // The URL of the page that initiated the download. |
| 191 GURL referrer_url_; |
| 192 |
| 193 // Suggested filename in 'download' attribute of an anchor. Details: |
| 194 // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks |
| 195 std::string suggested_filename_; |
| 196 |
| 197 // Information from the request. |
| 198 // Content-disposition field from the header. |
| 199 std::string content_disposition_; |
| 200 |
| 201 // Mime-type from the header. Subject to change. |
| 202 std::string mime_type_; |
| 203 |
| 204 // The value of the content type header sent with the downloaded item. It |
| 205 // may be different from |mime_type_|, which may be set based on heuristics |
| 206 // which may look at the file extension and first few bytes of the file. |
| 207 std::string original_mime_type_; |
| 208 |
| 209 // The charset of the referring page where the download request comes from. |
| 210 // It's used to construct a suggested filename. |
| 211 std::string referrer_charset_; |
| 212 |
| 213 // Total bytes expected |
| 214 int64 total_bytes_; |
| 215 |
| 216 // Current received bytes |
| 217 int64 received_bytes_; |
| 218 |
| 219 // Sha256 hash of the content. This might be empty either because |
| 220 // the download isn't done yet or because the hash isn't needed |
| 221 // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false). |
| 222 std::string hash_; |
| 223 |
| 224 // Last reason. |
| 225 InterruptReason last_reason_; |
| 226 |
| 227 // Start time for calculating remaining time |
| 228 base::TimeTicks start_tick_; |
| 229 |
| 230 // The current state of this download |
| 231 DownloadState state_; |
| 232 |
| 233 // The views of this item in the download shelf and download tab |
| 234 ObserverList<Observer> observers_; |
| 235 |
| 236 // Time the download was started |
| 237 base::Time start_time_; |
| 238 |
| 239 // Time the download completed |
| 240 base::Time end_time_; |
| 241 |
| 242 // Our persistent store handle |
| 243 int64 db_handle_; |
| 244 |
| 245 // Our owning object |
| 246 DownloadManager* download_manager_; |
| 247 |
| 248 // In progress downloads may be paused by the user, we note it here |
| 249 bool is_paused_; |
| 250 |
| 251 // A flag for indicating if the download should be opened at completion. |
| 252 bool open_when_complete_; |
| 253 |
| 254 // A flag for indicating if the downloaded file is externally removed. |
| 255 bool file_externally_removed_; |
| 256 |
| 257 // Indicates if the download is considered potentially safe or dangerous |
| 258 // (executable files are typically considered dangerous). |
| 259 SafetyState safety_state_; |
| 260 |
| 261 // True if the download was auto-opened. We set this rather than using |
| 262 // an observer as it's frequently possible for the download to be auto opened |
| 263 // before the observer is added. |
| 264 bool auto_opened_; |
| 265 |
| 266 // True if the download was initiated in an incognito window. |
| 267 bool is_otr_; |
| 268 |
| 269 // True if the item was downloaded temporarily. |
| 270 bool is_temporary_; |
| 271 |
| 272 // True if we've saved all the data for the download. |
| 273 bool all_data_saved_; |
| 274 |
| 275 // Did the user open the item either directly or indirectly (such as by |
| 276 // setting always open files of this type)? The shelf also sets this field |
| 277 // when the user closes the shelf before the item has been opened but should |
| 278 // be treated as though the user opened it. |
| 279 bool opened_; |
| 280 |
| 281 // Do we actual open downloads when requested? For testing purposes |
| 282 // only. |
| 283 bool open_enabled_; |
| 284 |
| 285 // Did the delegate delay calling Complete on this download? |
| 286 bool delegate_delayed_complete_; |
| 287 |
| 288 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
| 289 }; |
| 290 |
| 291 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |