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 // The DownloadFileManager owns a set of DownloadFile objects, each of which | 5 // The DownloadFileManager owns a set of DownloadFile objects, each of which |
6 // represent one in progress download and performs the disk IO for that | 6 // represent one in progress download and performs the disk IO for that |
7 // download. The DownloadFileManager itself is a singleton object owned by the | 7 // download. The DownloadFileManager itself is a singleton object owned by the |
8 // ResourceDispatcherHost. | 8 // ResourceDispatcherHost. |
9 // | 9 // |
10 // The DownloadFileManager uses the file_thread for performing file write | 10 // The DownloadFileManager uses the file_thread for performing file write |
(...skipping 30 matching lines...) Expand all Loading... |
41 #pragma once | 41 #pragma once |
42 | 42 |
43 #include <map> | 43 #include <map> |
44 | 44 |
45 #include "base/atomic_sequence_num.h" | 45 #include "base/atomic_sequence_num.h" |
46 #include "base/basictypes.h" | 46 #include "base/basictypes.h" |
47 #include "base/gtest_prod_util.h" | 47 #include "base/gtest_prod_util.h" |
48 #include "base/hash_tables.h" | 48 #include "base/hash_tables.h" |
49 #include "base/memory/ref_counted.h" | 49 #include "base/memory/ref_counted.h" |
50 #include "base/timer.h" | 50 #include "base/timer.h" |
| 51 #include "content/browser/download/download_id.h" |
51 #include "content/browser/download/download_request_handle.h" | 52 #include "content/browser/download/download_request_handle.h" |
52 #include "content/common/content_export.h" | 53 #include "content/common/content_export.h" |
53 #include "net/base/net_errors.h" | 54 #include "net/base/net_errors.h" |
54 #include "ui/gfx/native_widget_types.h" | 55 #include "ui/gfx/native_widget_types.h" |
55 | 56 |
56 struct DownloadBuffer; | 57 struct DownloadBuffer; |
57 struct DownloadCreateInfo; | 58 struct DownloadCreateInfo; |
58 struct DownloadSaveInfo; | 59 struct DownloadSaveInfo; |
59 class DownloadFile; | 60 class DownloadFile; |
60 class DownloadManager; | 61 class DownloadManager; |
61 class FilePath; | 62 class FilePath; |
62 class GURL; | 63 class GURL; |
63 class ResourceDispatcherHost; | 64 class ResourceDispatcherHost; |
64 | 65 |
65 namespace net { | 66 namespace net { |
66 class URLRequestContextGetter; | 67 class URLRequestContextGetter; |
67 } | 68 } |
68 | 69 |
69 // Manages all in progress downloads. | 70 // Manages all in progress downloads. |
70 class DownloadFileManager | 71 class DownloadFileManager |
71 : public base::RefCountedThreadSafe<DownloadFileManager> { | 72 : public base::RefCountedThreadSafe<DownloadFileManager> { |
72 public: | 73 public: |
73 explicit DownloadFileManager(ResourceDispatcherHost* rdh); | 74 explicit DownloadFileManager(ResourceDispatcherHost* rdh); |
74 | 75 |
75 // Called on shutdown on the UI thread. | 76 // Called on shutdown on the UI thread. |
76 CONTENT_EXPORT void Shutdown(); | 77 CONTENT_EXPORT void Shutdown(); |
77 | 78 |
78 // Called on the IO or UI threads. | |
79 int GetNextId(); | |
80 | |
81 // Called on UI thread to make DownloadFileManager start the download. | 79 // Called on UI thread to make DownloadFileManager start the download. |
82 void StartDownload(DownloadCreateInfo* info); | 80 void StartDownload(DownloadCreateInfo* info); |
83 | 81 |
84 // Handlers for notifications sent from the IO thread and run on the | 82 // Handlers for notifications sent from the IO thread and run on the |
85 // FILE thread. | 83 // FILE thread. |
86 void UpdateDownload(int id, DownloadBuffer* buffer); | 84 void UpdateDownload(DownloadId global_id, DownloadBuffer* buffer); |
87 // |net_error| is 0 for normal completions, and non-0 for errors. | 85 // |net_error| is 0 for normal completions, and non-0 for errors. |
88 // |security_info| contains SSL information (cert_id, cert_status, | 86 // |security_info| contains SSL information (cert_id, cert_status, |
89 // security_bits, ssl_connection_status), which can be used to | 87 // security_bits, ssl_connection_status), which can be used to |
90 // fine-tune the error message. It is empty if the transaction | 88 // fine-tune the error message. It is empty if the transaction |
91 // was not performed securely. | 89 // was not performed securely. |
92 void OnResponseCompleted(int id, | 90 void OnResponseCompleted(DownloadId global_id, |
93 DownloadBuffer* buffer, | 91 DownloadBuffer* buffer, |
94 net::Error net_error, | 92 net::Error net_error, |
95 const std::string& security_info); | 93 const std::string& security_info); |
96 | 94 |
97 // Handlers for notifications sent from the UI thread and run on the | 95 // Handlers for notifications sent from the UI thread and run on the |
98 // FILE thread. These are both terminal actions with respect to the | 96 // FILE thread. These are both terminal actions with respect to the |
99 // download file, as far as the DownloadFileManager is concerned -- if | 97 // download file, as far as the DownloadFileManager is concerned -- if |
100 // anything happens to the download file after they are called, it will | 98 // anything happens to the download file after they are called, it will |
101 // be ignored. | 99 // be ignored. |
102 void CancelDownload(int id); | 100 void CancelDownload(DownloadId id); |
103 void CompleteDownload(int id); | 101 void CompleteDownload(DownloadId id); |
104 | 102 |
105 // Called on FILE thread by DownloadManager at the beginning of its shutdown. | 103 // Called on FILE thread by DownloadManager at the beginning of its shutdown. |
106 void OnDownloadManagerShutdown(DownloadManager* manager); | 104 void OnDownloadManagerShutdown(DownloadManager* manager); |
107 | 105 |
108 // The DownloadManager in the UI thread has provided an intermediate | 106 // The DownloadManager in the UI thread has provided an intermediate |
109 // .crdownload name for the download specified by |id|. | 107 // .crdownload name for the download specified by |id|. |
110 void RenameInProgressDownloadFile(int id, const FilePath& full_path); | 108 void RenameInProgressDownloadFile(DownloadId id, const FilePath& full_path); |
111 | 109 |
112 // The DownloadManager in the UI thread has provided a final name for the | 110 // The DownloadManager in the UI thread has provided a final name for the |
113 // download specified by |id|. | 111 // download specified by |id|. |
114 // |overwrite_existing_file| prevents uniquification, and is used for SAFE | 112 // |overwrite_existing_file| prevents uniquification, and is used for SAFE |
115 // downloads, as the user may have decided to overwrite the file. | 113 // downloads, as the user may have decided to overwrite the file. |
116 // Sent from the UI thread and run on the FILE thread. | 114 // Sent from the UI thread and run on the FILE thread. |
117 void RenameCompletingDownloadFile(int id, | 115 void RenameCompletingDownloadFile(DownloadId id, |
118 const FilePath& full_path, | 116 const FilePath& full_path, |
119 bool overwrite_existing_file); | 117 bool overwrite_existing_file); |
120 | 118 |
121 // The number of downloads currently active on the DownloadFileManager. | 119 // The number of downloads currently active on the DownloadFileManager. |
122 // Primarily for testing. | 120 // Primarily for testing. |
123 int NumberOfActiveDownloads() const { | 121 int NumberOfActiveDownloads() const { |
124 return downloads_.size(); | 122 return downloads_.size(); |
125 } | 123 } |
126 | 124 |
127 private: | 125 private: |
(...skipping 11 matching lines...) Expand all Loading... |
139 // Clean up helper that runs on the download thread. | 137 // Clean up helper that runs on the download thread. |
140 void OnShutdown(); | 138 void OnShutdown(); |
141 | 139 |
142 // Creates DownloadFile on FILE thread and continues starting the download | 140 // Creates DownloadFile on FILE thread and continues starting the download |
143 // process. | 141 // process. |
144 void CreateDownloadFile(DownloadCreateInfo* info, | 142 void CreateDownloadFile(DownloadCreateInfo* info, |
145 DownloadManager* download_manager, | 143 DownloadManager* download_manager, |
146 bool hash_needed); | 144 bool hash_needed); |
147 | 145 |
148 // Called only on the download thread. | 146 // Called only on the download thread. |
149 DownloadFile* GetDownloadFile(int id); | 147 DownloadFile* GetDownloadFile(DownloadId global_id); |
150 | 148 |
151 // Called only from RenameInProgressDownloadFile and | 149 // Called only from RenameInProgressDownloadFile and |
152 // RenameCompletingDownloadFile on the FILE thread. | 150 // RenameCompletingDownloadFile on the FILE thread. |
153 // |rename_error| indicates what error caused the cancel. | 151 // |rename_error| indicates what error caused the cancel. |
154 void CancelDownloadOnRename(int id, net::Error rename_error); | 152 void CancelDownloadOnRename(DownloadId global_id, net::Error rename_error); |
155 | 153 |
156 // Erases the download file with the given the download |id| and removes | 154 // Erases the download file with the given the download |id| and removes |
157 // it from the maps. | 155 // it from the maps. |
158 void EraseDownload(int id); | 156 void EraseDownload(DownloadId global_id); |
159 | 157 |
160 // Unique ID for each DownloadItem. | 158 typedef base::hash_map<DownloadId, DownloadFile*> DownloadFileMap; |
161 base::AtomicSequenceNumber next_id_; | |
162 | |
163 typedef base::hash_map<int, DownloadFile*> DownloadFileMap; | |
164 | 159 |
165 // A map of all in progress downloads. It owns the download files. | 160 // A map of all in progress downloads. It owns the download files. |
166 DownloadFileMap downloads_; | 161 DownloadFileMap downloads_; |
167 | 162 |
168 // Schedule periodic updates of the download progress. This timer | 163 // Schedule periodic updates of the download progress. This timer |
169 // is controlled from the FILE thread, and posts updates to the UI thread. | 164 // is controlled from the FILE thread, and posts updates to the UI thread. |
170 base::RepeatingTimer<DownloadFileManager> update_timer_; | 165 base::RepeatingTimer<DownloadFileManager> update_timer_; |
171 | 166 |
172 ResourceDispatcherHost* resource_dispatcher_host_; | 167 ResourceDispatcherHost* resource_dispatcher_host_; |
173 | 168 |
174 DISALLOW_COPY_AND_ASSIGN(DownloadFileManager); | 169 DISALLOW_COPY_AND_ASSIGN(DownloadFileManager); |
175 }; | 170 }; |
176 | 171 |
177 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_ | 172 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_ |
OLD | NEW |