Revert 99759 - Add two new valgrind suppressions.
[chromium-blink-merge.git] / content / browser / download / download_file_manager.h
blob5b237b11f777c25b7a68e41826f925f237ae08c8
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.
14 // A typical download operation involves multiple threads:
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)
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)
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).
39 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
40 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
41 #pragma once
43 #include <map>
45 #include "base/atomic_sequence_num.h"
46 #include "base/basictypes.h"
47 #include "base/gtest_prod_util.h"
48 #include "base/hash_tables.h"
49 #include "base/memory/ref_counted.h"
50 #include "base/timer.h"
51 #include "content/browser/download/download_request_handle.h"
52 #include "net/base/net_errors.h"
53 #include "ui/gfx/native_widget_types.h"
55 struct DownloadBuffer;
56 struct DownloadCreateInfo;
57 struct DownloadSaveInfo;
58 class DownloadFile;
59 class DownloadManager;
60 class FilePath;
61 class GURL;
62 class ResourceDispatcherHost;
64 namespace net {
65 class URLRequestContextGetter;
68 // Manages all in progress downloads.
69 class DownloadFileManager
70 : public base::RefCountedThreadSafe<DownloadFileManager> {
71 public:
72 explicit DownloadFileManager(ResourceDispatcherHost* rdh);
74 // Called on shutdown on the UI thread.
75 void Shutdown();
77 // Called on the IO or UI threads.
78 int GetNextId();
80 // Called on UI thread to make DownloadFileManager start the download.
81 void StartDownload(DownloadCreateInfo* info);
83 // Handlers for notifications sent from the IO thread and run on the
84 // FILE thread.
85 void UpdateDownload(int id, DownloadBuffer* buffer);
86 // |net_error| is 0 for normal completions, and non-0 for errors.
87 // |security_info| contains SSL information (cert_id, cert_status,
88 // security_bits, ssl_connection_status), which can be used to
89 // fine-tune the error message. It is empty if the transaction
90 // was not performed securely.
91 void OnResponseCompleted(int id,
92 DownloadBuffer* buffer,
93 net::Error net_error,
94 const std::string& security_info);
96 // Handlers for notifications sent from the UI thread and run on the
97 // FILE thread. These are both terminal actions with respect to the
98 // download file, as far as the DownloadFileManager is concerned -- if
99 // anything happens to the download file after they are called, it will
100 // be ignored.
101 void CancelDownload(int id);
102 void CompleteDownload(int id);
104 // Called on FILE thread by DownloadManager at the beginning of its shutdown.
105 void OnDownloadManagerShutdown(DownloadManager* manager);
107 // The DownloadManager in the UI thread has provided an intermediate
108 // .crdownload name for the download specified by |id|.
109 void RenameInProgressDownloadFile(int id, const FilePath& full_path);
111 // The DownloadManager in the UI thread has provided a final name for the
112 // download specified by |id|.
113 // |overwrite_existing_file| prevents uniquification, and is used for SAFE
114 // downloads, as the user may have decided to overwrite the file.
115 // Sent from the UI thread and run on the FILE thread.
116 void RenameCompletingDownloadFile(int id,
117 const FilePath& full_path,
118 bool overwrite_existing_file);
120 // The number of downloads currently active on the DownloadFileManager.
121 // Primarily for testing.
122 int NumberOfActiveDownloads() const {
123 return downloads_.size();
126 private:
127 friend class base::RefCountedThreadSafe<DownloadFileManager>;
128 friend class DownloadManagerTest;
129 FRIEND_TEST_ALL_PREFIXES(DownloadManagerTest, StartDownload);
131 ~DownloadFileManager();
133 // Timer helpers for updating the UI about the current progress of a download.
134 void StartUpdateTimer();
135 void StopUpdateTimer();
136 void UpdateInProgressDownloads();
138 // Clean up helper that runs on the download thread.
139 void OnShutdown();
141 // Creates DownloadFile on FILE thread and continues starting the download
142 // process.
143 void CreateDownloadFile(DownloadCreateInfo* info,
144 DownloadManager* download_manager,
145 bool hash_needed);
147 // Called only on the download thread.
148 DownloadFile* GetDownloadFile(int id);
150 // Called only from RenameInProgressDownloadFile and
151 // RenameCompletingDownloadFile on the FILE thread.
152 // |rename_error| indicates what error caused the cancel.
153 void CancelDownloadOnRename(int id, net::Error rename_error);
155 // Erases the download file with the given the download |id| and removes
156 // it from the maps.
157 void EraseDownload(int id);
159 // Unique ID for each DownloadItem.
160 base::AtomicSequenceNumber next_id_;
162 typedef base::hash_map<int, DownloadFile*> DownloadFileMap;
164 // A map of all in progress downloads. It owns the download files.
165 DownloadFileMap downloads_;
167 // Schedule periodic updates of the download progress. This timer
168 // is controlled from the FILE thread, and posts updates to the UI thread.
169 base::RepeatingTimer<DownloadFileManager> update_timer_;
171 ResourceDispatcherHost* resource_dispatcher_host_;
173 DISALLOW_COPY_AND_ASSIGN(DownloadFileManager);
176 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_