OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_ITEM_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "content/browser/download/download_item.h" | 17 #include "content/browser/download/download_item.h" |
18 #include "content/browser/download/download_id.h" | 18 #include "content/browser/download/download_id.h" |
19 #include "content/browser/download/download_request_handle.h" | 19 #include "content/browser/download/download_request_handle.h" |
20 #include "content/browser/download/download_state_info.h" | 20 #include "content/browser/download/download_state_info.h" |
21 #include "content/browser/download/interrupt_reasons.h" | 21 #include "content/browser/download/interrupt_reasons.h" |
22 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 | 25 |
26 class DownloadFileManager; | 26 class DownloadFileManager; |
27 class DownloadId; | |
28 class DownloadManager; | |
29 class TabContents; | 27 class TabContents; |
30 | 28 |
31 struct DownloadCreateInfo; | 29 struct DownloadCreateInfo; |
32 struct DownloadPersistentStoreInfo; | 30 struct DownloadPersistentStoreInfo; |
33 | 31 |
| 32 namespace content { |
| 33 class BrowserContext; |
| 34 } |
| 35 |
34 // See download_item.h for usage. | 36 // See download_item.h for usage. |
35 class CONTENT_EXPORT DownloadItemImpl : public DownloadItem { | 37 class CONTENT_EXPORT DownloadItemImpl : public DownloadItem { |
36 public: | 38 public: |
| 39 // Delegate is defined in DownloadItemImpl (rather than DownloadItem) |
| 40 // as it's relevant to the class implementation (class methods need to |
| 41 // call into it) and doesn't have anything to do with its interface. |
| 42 // Despite this, the delegate methods take DownloadItems as arguments |
| 43 // (rather than DownloadItemImpls) so that classes that inherit from it |
| 44 // can be used with DownloadItem mocks rather than being tied to |
| 45 // DownloadItemImpls. |
| 46 class CONTENT_EXPORT Delegate { |
| 47 public: |
| 48 Delegate(); |
| 49 virtual ~Delegate(); |
| 50 |
| 51 // Used for catching use-after-free errors. |
| 52 void Attach(); |
| 53 void Detach(); |
| 54 |
| 55 // Tests if a file type should be opened automatically. |
| 56 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) = 0; |
| 57 |
| 58 // Allows the delegate to override the opening of a download. If it returns |
| 59 // true then it's reponsible for opening the item. |
| 60 virtual bool ShouldOpenDownload(DownloadItem* download) = 0; |
| 61 |
| 62 // Checks whether a downloaded file still exists and updates the |
| 63 // file's state if the file is already removed. |
| 64 // The check may or may not result in a later asynchronous call |
| 65 // to OnDownloadedFileRemoved(). |
| 66 virtual void CheckForFileRemoval(DownloadItem* download_item) = 0; |
| 67 |
| 68 // If all pre-requisites have been met, complete download processing. |
| 69 // TODO(rdsmith): Move into DownloadItem. |
| 70 virtual void MaybeCompleteDownload(DownloadItem* download) = 0; |
| 71 |
| 72 // For contextual issues like language and prefs. |
| 73 virtual content::BrowserContext* BrowserContext() const = 0; |
| 74 |
| 75 // Handle any delegate portions of a state change operation on the |
| 76 // DownloadItem. |
| 77 virtual void DownloadCancelled(DownloadItem* download) = 0; |
| 78 virtual void DownloadCompleted(DownloadItem* download) = 0; |
| 79 virtual void DownloadOpened(DownloadItem* download) = 0; |
| 80 virtual void DownloadRemoved(DownloadItem* download) = 0; |
| 81 |
| 82 // Assert consistent state for delgate object at various transitions. |
| 83 virtual void AssertStateConsistent(DownloadItem* download) const = 0; |
| 84 |
| 85 private: |
| 86 // For "Outlives attached DownloadItemImpl" invariant assertion. |
| 87 int count_; |
| 88 }; |
| 89 |
| 90 // Note that it is the responsibility of the caller to ensure that a |
| 91 // DownloadItemImpl::Delegate passed to a DownloadItemImpl constructor |
| 92 // outlives the DownloadItemImpl. |
| 93 |
37 // Constructing from persistent store: | 94 // Constructing from persistent store: |
38 DownloadItemImpl(DownloadManager* download_manager, | 95 DownloadItemImpl(Delegate* delegate, |
| 96 DownloadId download_id, |
39 const DownloadPersistentStoreInfo& info); | 97 const DownloadPersistentStoreInfo& info); |
40 | 98 |
41 // Constructing for a regular download. | 99 // Constructing for a regular download. |
42 // Takes ownership of the object pointed to by |request_handle|. | 100 // Takes ownership of the object pointed to by |request_handle|. |
43 DownloadItemImpl(DownloadManager* download_manager, | 101 DownloadItemImpl(Delegate* delegate, |
44 const DownloadCreateInfo& info, | 102 const DownloadCreateInfo& info, |
45 DownloadRequestHandleInterface* request_handle, | 103 DownloadRequestHandleInterface* request_handle, |
46 bool is_otr); | 104 bool is_otr); |
47 | 105 |
48 // Constructing for the "Save Page As..." feature: | 106 // Constructing for the "Save Page As..." feature: |
49 DownloadItemImpl(DownloadManager* download_manager, | 107 DownloadItemImpl(Delegate* delegate, |
50 const FilePath& path, | 108 const FilePath& path, |
51 const GURL& url, | 109 const GURL& url, |
52 bool is_otr, | 110 bool is_otr, |
53 DownloadId download_id); | 111 DownloadId download_id); |
54 | 112 |
55 virtual ~DownloadItemImpl(); | 113 virtual ~DownloadItemImpl(); |
56 | 114 |
57 // Overridden from DownloadItem. | 115 // Overridden from DownloadItem. |
58 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 116 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
59 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 117 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
60 virtual void UpdateObservers() OVERRIDE; | 118 virtual void UpdateObservers() OVERRIDE; |
61 virtual bool CanShowInFolder() OVERRIDE; | 119 virtual bool CanShowInFolder() OVERRIDE; |
62 virtual bool CanOpenDownload() OVERRIDE; | 120 virtual bool CanOpenDownload() OVERRIDE; |
63 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | 121 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
64 virtual void OpenDownload() OVERRIDE; | 122 virtual void OpenDownload() OVERRIDE; |
65 virtual void ShowDownloadInShell() OVERRIDE; | 123 virtual void ShowDownloadInShell() OVERRIDE; |
66 virtual void DangerousDownloadValidated() OVERRIDE; | 124 virtual void DangerousDownloadValidated() OVERRIDE; |
67 virtual void UpdateProgress(int64 bytes_so_far, int64 bytes_per_sec) OVERRIDE; | 125 virtual void UpdateProgress(int64 bytes_so_far, int64 bytes_per_sec) OVERRIDE; |
68 virtual void Cancel(bool user_cancel) OVERRIDE; | 126 virtual void Cancel(bool user_cancel) OVERRIDE; |
69 virtual void MarkAsComplete() OVERRIDE; | 127 virtual void MarkAsComplete() OVERRIDE; |
70 virtual void DelayedDownloadOpened() OVERRIDE; | 128 virtual void DelayedDownloadOpened() OVERRIDE; |
71 virtual void OnAllDataSaved( | 129 virtual void OnAllDataSaved( |
72 int64 size, const std::string& final_hash) OVERRIDE; | 130 int64 size, const std::string& final_hash) OVERRIDE; |
73 virtual void OnDownloadedFileRemoved() OVERRIDE; | 131 virtual void OnDownloadedFileRemoved() OVERRIDE; |
| 132 virtual void MaybeCompleteDownload() OVERRIDE; |
74 virtual void Interrupted(int64 size, InterruptReason reason) OVERRIDE; | 133 virtual void Interrupted(int64 size, InterruptReason reason) OVERRIDE; |
75 virtual void Delete(DeleteReason reason) OVERRIDE; | 134 virtual void Delete(DeleteReason reason) OVERRIDE; |
76 virtual void Remove() OVERRIDE; | 135 virtual void Remove() OVERRIDE; |
77 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; | 136 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; |
78 virtual int64 CurrentSpeed() const OVERRIDE; | 137 virtual int64 CurrentSpeed() const OVERRIDE; |
79 virtual int PercentComplete() const OVERRIDE; | 138 virtual int PercentComplete() const OVERRIDE; |
80 virtual void OnPathDetermined(const FilePath& path) OVERRIDE; | 139 virtual void OnPathDetermined(const FilePath& path) OVERRIDE; |
81 virtual bool AllDataSaved() const OVERRIDE; | 140 virtual bool AllDataSaved() const OVERRIDE; |
82 virtual void SetFileCheckResults(const DownloadStateInfo& state) OVERRIDE; | 141 virtual void SetFileCheckResults(const DownloadStateInfo& state) OVERRIDE; |
83 virtual void Rename(const FilePath& full_path) OVERRIDE; | 142 virtual void Rename(const FilePath& full_path) OVERRIDE; |
(...skipping 22 matching lines...) Expand all Loading... |
106 virtual int64 GetTotalBytes() const OVERRIDE; | 165 virtual int64 GetTotalBytes() const OVERRIDE; |
107 virtual void SetTotalBytes(int64 total_bytes) OVERRIDE; | 166 virtual void SetTotalBytes(int64 total_bytes) OVERRIDE; |
108 virtual const std::string& GetHash() const OVERRIDE; | 167 virtual const std::string& GetHash() const OVERRIDE; |
109 virtual int64 GetReceivedBytes() const OVERRIDE; | 168 virtual int64 GetReceivedBytes() const OVERRIDE; |
110 virtual int32 GetId() const OVERRIDE; | 169 virtual int32 GetId() const OVERRIDE; |
111 virtual DownloadId GetGlobalId() const OVERRIDE; | 170 virtual DownloadId GetGlobalId() const OVERRIDE; |
112 virtual base::Time GetStartTime() const OVERRIDE; | 171 virtual base::Time GetStartTime() const OVERRIDE; |
113 virtual base::Time GetEndTime() const OVERRIDE; | 172 virtual base::Time GetEndTime() const OVERRIDE; |
114 virtual void SetDbHandle(int64 handle) OVERRIDE; | 173 virtual void SetDbHandle(int64 handle) OVERRIDE; |
115 virtual int64 GetDbHandle() const OVERRIDE; | 174 virtual int64 GetDbHandle() const OVERRIDE; |
116 virtual DownloadManager* GetDownloadManager() OVERRIDE; | |
117 virtual bool IsPaused() const OVERRIDE; | 175 virtual bool IsPaused() const OVERRIDE; |
118 virtual bool GetOpenWhenComplete() const OVERRIDE; | 176 virtual bool GetOpenWhenComplete() const OVERRIDE; |
119 virtual void SetOpenWhenComplete(bool open) OVERRIDE; | 177 virtual void SetOpenWhenComplete(bool open) OVERRIDE; |
120 virtual bool GetFileExternallyRemoved() const OVERRIDE; | 178 virtual bool GetFileExternallyRemoved() const OVERRIDE; |
121 virtual SafetyState GetSafetyState() const OVERRIDE; | 179 virtual SafetyState GetSafetyState() const OVERRIDE; |
122 virtual DownloadStateInfo::DangerType GetDangerType() const OVERRIDE; | 180 virtual DownloadStateInfo::DangerType GetDangerType() const OVERRIDE; |
123 virtual bool IsDangerous() const OVERRIDE; | 181 virtual bool IsDangerous() const OVERRIDE; |
124 virtual void MarkFileDangerous() OVERRIDE; | 182 virtual void MarkFileDangerous() OVERRIDE; |
125 virtual void MarkUrlDangerous() OVERRIDE; | 183 virtual void MarkUrlDangerous() OVERRIDE; |
126 virtual void MarkContentDangerous() OVERRIDE; | 184 virtual void MarkContentDangerous() OVERRIDE; |
127 virtual bool GetAutoOpened() OVERRIDE; | 185 virtual bool GetAutoOpened() OVERRIDE; |
128 virtual const FilePath& GetTargetName() const OVERRIDE; | 186 virtual const FilePath& GetTargetName() const OVERRIDE; |
129 virtual bool PromptUserForSaveLocation() const OVERRIDE; | 187 virtual bool PromptUserForSaveLocation() const OVERRIDE; |
130 virtual bool IsOtr() const OVERRIDE; | 188 virtual bool IsOtr() const OVERRIDE; |
131 virtual const FilePath& GetSuggestedPath() const OVERRIDE; | 189 virtual const FilePath& GetSuggestedPath() const OVERRIDE; |
132 virtual bool IsTemporary() const OVERRIDE; | 190 virtual bool IsTemporary() const OVERRIDE; |
133 virtual void SetOpened(bool opened) OVERRIDE; | 191 virtual void SetOpened(bool opened) OVERRIDE; |
134 virtual bool GetOpened() const OVERRIDE; | 192 virtual bool GetOpened() const OVERRIDE; |
135 virtual InterruptReason GetLastReason() const OVERRIDE; | 193 virtual InterruptReason GetLastReason() const OVERRIDE; |
136 virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const OVERRIDE; | 194 virtual DownloadPersistentStoreInfo GetPersistentStoreInfo() const OVERRIDE; |
137 virtual DownloadStateInfo GetStateInfo() const OVERRIDE; | 195 virtual DownloadStateInfo GetStateInfo() const OVERRIDE; |
| 196 virtual content::BrowserContext* BrowserContext() const OVERRIDE; |
138 virtual TabContents* GetTabContents() const OVERRIDE; | 197 virtual TabContents* GetTabContents() const OVERRIDE; |
139 virtual FilePath GetTargetFilePath() const OVERRIDE; | 198 virtual FilePath GetTargetFilePath() const OVERRIDE; |
140 virtual FilePath GetFileNameToReportUser() const OVERRIDE; | 199 virtual FilePath GetFileNameToReportUser() const OVERRIDE; |
141 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; | 200 virtual FilePath GetUserVerifiedFilePath() const OVERRIDE; |
142 virtual bool NeedsRename() const OVERRIDE; | 201 virtual bool NeedsRename() const OVERRIDE; |
143 virtual void OffThreadCancel(DownloadFileManager* file_manager) OVERRIDE; | 202 virtual void OffThreadCancel(DownloadFileManager* file_manager) OVERRIDE; |
144 virtual std::string DebugString(bool verbose) const OVERRIDE; | 203 virtual std::string DebugString(bool verbose) const OVERRIDE; |
145 virtual void MockDownloadOpenForTesting() OVERRIDE; | 204 virtual void MockDownloadOpenForTesting() OVERRIDE; |
146 | 205 |
147 private: | 206 private: |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 | 302 |
244 // Time the download was started | 303 // Time the download was started |
245 base::Time start_time_; | 304 base::Time start_time_; |
246 | 305 |
247 // Time the download completed | 306 // Time the download completed |
248 base::Time end_time_; | 307 base::Time end_time_; |
249 | 308 |
250 // Our persistent store handle | 309 // Our persistent store handle |
251 int64 db_handle_; | 310 int64 db_handle_; |
252 | 311 |
253 // Our owning object | 312 // Our delegate. |
254 DownloadManager* download_manager_; | 313 Delegate* delegate_; |
255 | 314 |
256 // In progress downloads may be paused by the user, we note it here | 315 // In progress downloads may be paused by the user, we note it here |
257 bool is_paused_; | 316 bool is_paused_; |
258 | 317 |
259 // A flag for indicating if the download should be opened at completion. | 318 // A flag for indicating if the download should be opened at completion. |
260 bool open_when_complete_; | 319 bool open_when_complete_; |
261 | 320 |
262 // A flag for indicating if the downloaded file is externally removed. | 321 // A flag for indicating if the downloaded file is externally removed. |
263 bool file_externally_removed_; | 322 bool file_externally_removed_; |
264 | 323 |
(...skipping 25 matching lines...) Expand all Loading... |
290 // only. | 349 // only. |
291 bool open_enabled_; | 350 bool open_enabled_; |
292 | 351 |
293 // Did the delegate delay calling Complete on this download? | 352 // Did the delegate delay calling Complete on this download? |
294 bool delegate_delayed_complete_; | 353 bool delegate_delayed_complete_; |
295 | 354 |
296 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 355 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
297 }; | 356 }; |
298 | 357 |
299 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 358 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |