| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 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 | |
| 7 // download. The DownloadFileManager itself is a singleton object owned by the | |
| 8 // ResourceDispatcherHost. | |
| 9 // | |
| 10 // The DownloadFileManager uses the file_thread for performing file write | |
| 11 // operations, in order to avoid disk activity on either the IO (network) thread | |
| 12 // and the UI thread. It coordinates the notifications from the network and UI. | |
| 13 // | |
| 14 // A typical download operation involves multiple threads: | |
| 15 // | |
| 16 // Updating an in progress download | |
| 17 // io_thread | |
| 18 // |----> data ---->| | |
| 19 // file_thread (writes to disk) | |
| 20 // |----> stats ---->| | |
| 21 // ui_thread (feedback for user and | |
| 22 // updates to history) | |
| 23 // | |
| 24 // Cancel operations perform the inverse order when triggered by a user action: | |
| 25 // ui_thread (user click) | |
| 26 // |----> cancel command ---->| | |
| 27 // file_thread (close file) | |
| 28 // |----> cancel command ---->| | |
| 29 // io_thread (stops net IO | |
| 30 // for download) | |
| 31 // | |
| 32 // The DownloadFileManager tracks download requests, mapping from a download | |
| 33 // ID (unique integer created in the IO thread) to the DownloadManager for the | |
| 34 // tab (profile) where the download was initiated. In the event of a tab closure | |
| 35 // during a download, the DownloadFileManager will continue to route data to the | |
| 36 // appropriate DownloadManager. In progress downloads are cancelled for a | |
| 37 // DownloadManager that exits (such as when closing a profile). | |
| 38 | |
| 39 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_ | |
| 40 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_ | |
| 41 #pragma once | |
| 42 | |
| 43 #include <map> | |
| 44 | |
| 45 #include "base/basictypes.h" | |
| 46 #include "base/gtest_prod_util.h" | |
| 47 #include "base/hash_tables.h" | |
| 48 #include "base/memory/ref_counted.h" | |
| 49 #include "base/timer.h" | |
| 50 #include "chrome/browser/download/download_request_handle.h" | |
| 51 #include "ui/gfx/native_widget_types.h" | |
| 52 | |
| 53 struct DownloadBuffer; | |
| 54 struct DownloadCreateInfo; | |
| 55 struct DownloadSaveInfo; | |
| 56 class DownloadFile; | |
| 57 class DownloadManager; | |
| 58 class FilePath; | |
| 59 class GURL; | |
| 60 class ResourceDispatcherHost; | |
| 61 | |
| 62 namespace net { | |
| 63 class URLRequestContextGetter; | |
| 64 } | |
| 65 | |
| 66 // Manages all in progress downloads. | |
| 67 class DownloadFileManager | |
| 68 : public base::RefCountedThreadSafe<DownloadFileManager> { | |
| 69 public: | |
| 70 explicit DownloadFileManager(ResourceDispatcherHost* rdh); | |
| 71 | |
| 72 // Called on shutdown on the UI thread. | |
| 73 void Shutdown(); | |
| 74 | |
| 75 // Called on the IO thread | |
| 76 int GetNextId(); | |
| 77 | |
| 78 // Called on UI thread to make DownloadFileManager start the download. | |
| 79 void StartDownload(DownloadCreateInfo* info); | |
| 80 | |
| 81 // Handlers for notifications sent from the IO thread and run on the | |
| 82 // FILE thread. | |
| 83 void UpdateDownload(int id, DownloadBuffer* buffer); | |
| 84 // |os_error| is 0 for normal completions, and non-0 for errors. | |
| 85 // |security_info| contains SSL information (cert_id, cert_status, | |
| 86 // security_bits, ssl_connection_status), which can be used to | |
| 87 // fine-tune the error message. It is empty if the transaction | |
| 88 // was not performed securely. | |
| 89 void OnResponseCompleted(int id, | |
| 90 DownloadBuffer* buffer, | |
| 91 int os_error, | |
| 92 const std::string& security_info); | |
| 93 | |
| 94 // Handlers for notifications sent from the UI thread and run on the | |
| 95 // FILE thread. These are both terminal actions with respect to the | |
| 96 // download file, as far as the DownloadFileManager is concerned -- if | |
| 97 // anything happens to the download file after they are called, it will | |
| 98 // be ignored. | |
| 99 void CancelDownload(int id); | |
| 100 void CompleteDownload(int id); | |
| 101 | |
| 102 // Called on FILE thread by DownloadManager at the beginning of its shutdown. | |
| 103 void OnDownloadManagerShutdown(DownloadManager* manager); | |
| 104 | |
| 105 // The DownloadManager in the UI thread has provided an intermediate | |
| 106 // .crdownload name for the download specified by |id|. | |
| 107 void RenameInProgressDownloadFile(int id, const FilePath& full_path); | |
| 108 | |
| 109 // The DownloadManager in the UI thread has provided a final name for the | |
| 110 // download specified by |id|. | |
| 111 // |overwrite_existing_file| prevents uniquification, and is used for SAFE | |
| 112 // downloads, as the user may have decided to overwrite the file. | |
| 113 // Sent from the UI thread and run on the FILE thread. | |
| 114 void RenameCompletingDownloadFile(int id, | |
| 115 const FilePath& full_path, | |
| 116 bool overwrite_existing_file); | |
| 117 | |
| 118 // The number of downloads currently active on the DownloadFileManager. | |
| 119 // Primarily for testing. | |
| 120 int NumberOfActiveDownloads() const { | |
| 121 return downloads_.size(); | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 friend class base::RefCountedThreadSafe<DownloadFileManager>; | |
| 126 friend class DownloadManagerTest; | |
| 127 FRIEND_TEST_ALL_PREFIXES(DownloadManagerTest, StartDownload); | |
| 128 | |
| 129 ~DownloadFileManager(); | |
| 130 | |
| 131 // Timer helpers for updating the UI about the current progress of a download. | |
| 132 void StartUpdateTimer(); | |
| 133 void StopUpdateTimer(); | |
| 134 void UpdateInProgressDownloads(); | |
| 135 | |
| 136 // Clean up helper that runs on the download thread. | |
| 137 void OnShutdown(); | |
| 138 | |
| 139 // Creates DownloadFile on FILE thread and continues starting the download | |
| 140 // process. | |
| 141 void CreateDownloadFile(DownloadCreateInfo* info, | |
| 142 DownloadManager* download_manager, | |
| 143 bool hash_needed); | |
| 144 | |
| 145 // Called only on the download thread. | |
| 146 DownloadFile* GetDownloadFile(int id); | |
| 147 | |
| 148 // Called only from RenameInProgressDownloadFile and | |
| 149 // RenameCompletingDownloadFile on the FILE thread. | |
| 150 void CancelDownloadOnRename(int id); | |
| 151 | |
| 152 // Erases the download file with the given the download |id| and removes | |
| 153 // it from the maps. | |
| 154 void EraseDownload(int id); | |
| 155 | |
| 156 // Unique ID for each DownloadFile. | |
| 157 int next_id_; | |
| 158 | |
| 159 typedef base::hash_map<int, DownloadFile*> DownloadFileMap; | |
| 160 | |
| 161 // A map of all in progress downloads. It owns the download files. | |
| 162 DownloadFileMap downloads_; | |
| 163 | |
| 164 // Schedule periodic updates of the download progress. This timer | |
| 165 // is controlled from the FILE thread, and posts updates to the UI thread. | |
| 166 base::RepeatingTimer<DownloadFileManager> update_timer_; | |
| 167 | |
| 168 ResourceDispatcherHost* resource_dispatcher_host_; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(DownloadFileManager); | |
| 171 }; | |
| 172 | |
| 173 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_ | |
| OLD | NEW |