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