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