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