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