OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_FILE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
7 | 7 |
8 #include "content/browser/download/download_file.h" | 8 #include "content/browser/download/download_file.h" |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "content/browser/download/base_file.h" | 15 #include "content/browser/download/base_file.h" |
16 #include "content/browser/download/byte_stream.h" | 16 #include "content/browser/download/byte_stream.h" |
17 #include "content/public/browser/download_save_info.h" | 17 #include "content/browser/download/download_request_handle.h" |
18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
19 | 19 |
20 struct DownloadCreateInfo; | 20 struct DownloadCreateInfo; |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 class ByteStreamReader; | 23 class ByteStreamReader; |
24 class DownloadDestinationObserver; | |
25 class DownloadManager; | 24 class DownloadManager; |
26 class PowerSaveBlocker; | 25 class PowerSaveBlocker; |
27 } | 26 } |
28 | 27 |
29 class CONTENT_EXPORT DownloadFileImpl : virtual public content::DownloadFile { | 28 class CONTENT_EXPORT DownloadFileImpl : virtual public content::DownloadFile { |
30 public: | 29 public: |
31 // Takes ownership of the object pointed to by |request_handle|. | 30 // Takes ownership of the object pointed to by |request_handle|. |
32 // |bound_net_log| will be used for logging the download file's events. | 31 // |bound_net_log| will be used for logging the download file's events. |
33 // May be constructed on any thread. All methods besides the constructor | 32 DownloadFileImpl(const DownloadCreateInfo* info, |
34 // (including destruction) must occur on the FILE thread. | 33 scoped_ptr<content::ByteStreamReader> stream, |
35 // | 34 DownloadRequestHandleInterface* request_handle, |
36 // Note that the DownloadFileImpl automatically reads from the passed in | 35 scoped_refptr<content::DownloadManager> download_manager, |
37 // stream, and sends updates and status of those reads to the | 36 bool calculate_hash, |
38 // DownloadDestinationObserver. | 37 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
39 DownloadFileImpl( | 38 const net::BoundNetLog& bound_net_log); |
40 const content::DownloadSaveInfo& save_info, | |
41 const GURL& url, | |
42 const GURL& referrer_url, | |
43 int64 received_bytes, | |
44 bool calculate_hash, | |
45 scoped_ptr<content::ByteStreamReader> stream, | |
46 const net::BoundNetLog& bound_net_log, | |
47 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | |
48 base::WeakPtr<content::DownloadDestinationObserver> observer); | |
49 | |
50 virtual ~DownloadFileImpl(); | 39 virtual ~DownloadFileImpl(); |
51 | 40 |
52 // DownloadFile functions. | 41 // DownloadFile functions. |
53 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; | 42 virtual content::DownloadInterruptReason Initialize() OVERRIDE; |
54 virtual void Rename(const FilePath& full_path, | 43 virtual void Rename(const FilePath& full_path, |
55 bool overwrite_existing_file, | 44 bool overwrite_existing_file, |
56 const RenameCompletionCallback& callback) OVERRIDE; | 45 const RenameCompletionCallback& callback) OVERRIDE; |
57 virtual void Detach() OVERRIDE; | 46 virtual void Detach() OVERRIDE; |
58 virtual void Cancel() OVERRIDE; | 47 virtual void Cancel() OVERRIDE; |
59 virtual void AnnotateWithSourceInformation() OVERRIDE; | 48 virtual void AnnotateWithSourceInformation() OVERRIDE; |
60 virtual FilePath FullPath() const OVERRIDE; | 49 virtual FilePath FullPath() const OVERRIDE; |
61 virtual bool InProgress() const OVERRIDE; | 50 virtual bool InProgress() const OVERRIDE; |
62 virtual int64 BytesSoFar() const OVERRIDE; | 51 virtual int64 BytesSoFar() const OVERRIDE; |
63 virtual int64 CurrentSpeed() const OVERRIDE; | 52 virtual int64 CurrentSpeed() const OVERRIDE; |
64 virtual bool GetHash(std::string* hash) OVERRIDE; | 53 virtual bool GetHash(std::string* hash) OVERRIDE; |
65 virtual std::string GetHashState() OVERRIDE; | 54 virtual std::string GetHashState() OVERRIDE; |
| 55 virtual void CancelDownloadRequest() OVERRIDE; |
| 56 virtual int Id() const OVERRIDE; |
| 57 virtual content::DownloadManager* GetDownloadManager() OVERRIDE; |
| 58 virtual const content::DownloadId& GlobalId() const OVERRIDE; |
| 59 virtual std::string DebugString() const OVERRIDE; |
66 | 60 |
67 protected: | 61 protected: |
68 // For test class overrides. | 62 // For test class overrides. |
69 virtual content::DownloadInterruptReason AppendDataToFile( | 63 virtual content::DownloadInterruptReason AppendDataToFile( |
70 const char* data, size_t data_len); | 64 const char* data, size_t data_len); |
71 | 65 |
72 private: | 66 private: |
73 // Send an update on our progress. | 67 // Send an update on our progress. |
74 void SendUpdate(); | 68 void SendUpdate(); |
75 | 69 |
76 // Called when there's some activity on stream_reader_ that needs to be | 70 // Called when there's some activity on stream_reader_ that needs to be |
77 // handled. | 71 // handled. |
78 void StreamActive(); | 72 void StreamActive(); |
79 | 73 |
80 // The base file instance. | 74 // The base file instance. |
81 BaseFile file_; | 75 BaseFile file_; |
82 | 76 |
83 // The stream through which data comes. | 77 // The stream through which data comes. |
84 // TODO(rdsmith): Move this into BaseFile; requires using the same | 78 // TODO(rdsmith): Move this into BaseFile; requires using the same |
85 // stream semantics in SavePackage. Alternatively, replace SaveFile | 79 // stream semantics in SavePackage. Alternatively, replace SaveFile |
86 // with DownloadFile and get rid of BaseFile. | 80 // with DownloadFile and get rid of BaseFile. |
87 scoped_ptr<content::ByteStreamReader> stream_reader_; | 81 scoped_ptr<content::ByteStreamReader> stream_reader_; |
88 | 82 |
| 83 // The unique identifier for this download, assigned at creation by |
| 84 // the DownloadFileManager for its internal record keeping. |
| 85 content::DownloadId id_; |
| 86 |
89 // Used to trigger progress updates. | 87 // Used to trigger progress updates. |
90 scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_; | 88 scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_; |
91 | 89 |
| 90 // The handle to the request information. Used for operations outside the |
| 91 // download system, specifically canceling a download. |
| 92 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
| 93 |
| 94 // DownloadManager this download belongs to. |
| 95 scoped_refptr<content::DownloadManager> download_manager_; |
| 96 |
92 // Statistics | 97 // Statistics |
93 size_t bytes_seen_; | 98 size_t bytes_seen_; |
94 base::TimeDelta disk_writes_time_; | 99 base::TimeDelta disk_writes_time_; |
95 base::TimeTicks download_start_; | 100 base::TimeTicks download_start_; |
96 | 101 |
97 net::BoundNetLog bound_net_log_; | 102 net::BoundNetLog bound_net_log_; |
98 | 103 |
99 base::WeakPtr<content::DownloadDestinationObserver> observer_; | |
100 | |
101 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; | 104 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; |
102 | 105 |
103 // RAII handle to keep the system from sleeping while we're downloading. | 106 // RAII handle to keep the system from sleeping while we're downloading. |
104 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_; | 107 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_; |
105 | 108 |
106 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); | 109 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); |
107 }; | 110 }; |
108 | 111 |
109 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ | 112 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ |
OLD | NEW |