Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1071)

Side by Side Diff: content/browser/download/download_file_manager.h

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to LKGR. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 // ResourceDispatcherHostImpl.
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 // contents (profile) where the download was initiated. In the event of a
35 // contents closure during a download, the DownloadFileManager will continue to
36 // route data to the appropriate DownloadManager. In progress downloads are
37 // cancelled for a DownloadManager that exits (such as when closing a profile).
38
39 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
40 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
41
42 #include <map>
43
44 #include "base/atomic_sequence_num.h"
45 #include "base/basictypes.h"
46 #include "base/callback_forward.h"
47 #include "base/gtest_prod_util.h"
48 #include "base/hash_tables.h"
49 #include "base/memory/ref_counted.h"
50 #include "base/memory/scoped_ptr.h"
51 #include "base/timer.h"
52 #include "content/browser/download/download_file.h"
53 #include "content/common/content_export.h"
54 #include "content/public/browser/download_id.h"
55 #include "content/public/browser/download_interrupt_reasons.h"
56 #include "net/base/net_errors.h"
57 #include "ui/gfx/native_widget_types.h"
58
59 struct DownloadCreateInfo;
60 class DownloadRequestHandle;
61 class FilePath;
62
63 namespace content {
64 class ByteStreamReader;
65 class DownloadId;
66 class DownloadManager;
67 }
68
69 namespace net {
70 class BoundNetLog;
71 }
72
73 // Manages all in progress downloads.
74 // Methods are virtual to allow mocks--this class is not intended
75 // to be a base class.
76 class CONTENT_EXPORT DownloadFileManager
77 : public base::RefCountedThreadSafe<DownloadFileManager> {
78 public:
79 // Callback used with CreateDownloadFile(). |reason| will be
80 // DOWNLOAD_INTERRUPT_REASON_NONE on a successful creation.
81 typedef base::Callback<void(content::DownloadInterruptReason reason)>
82 CreateDownloadFileCallback;
83
84 // Callback used with RenameDownloadFile().
85 typedef content::DownloadFile::RenameCompletionCallback
86 RenameCompletionCallback;
87
88 class DownloadFileFactory {
89 public:
90 virtual ~DownloadFileFactory() {}
91
92 virtual content::DownloadFile* CreateFile(
93 DownloadCreateInfo* info,
94 scoped_ptr<content::ByteStreamReader> stream,
95 content::DownloadManager* download_manager,
96 bool calculate_hash,
97 const net::BoundNetLog& bound_net_log) = 0;
98 };
99
100 // Takes ownership of the factory.
101 // Passing in a NULL for |factory| will cause a default
102 // |DownloadFileFactory| to be used.
103 explicit DownloadFileManager(DownloadFileFactory* factory);
104
105 // Create a download file and record it in the download file manager.
106 virtual void CreateDownloadFile(
107 scoped_ptr<DownloadCreateInfo> info,
108 scoped_ptr<content::ByteStreamReader> stream,
109 scoped_refptr<content::DownloadManager> download_manager,
110 bool hash_needed,
111 const net::BoundNetLog& bound_net_log,
112 const CreateDownloadFileCallback& callback);
113
114 // Called on shutdown on the UI thread.
115 virtual void Shutdown();
116
117 // Handlers for notifications sent from the UI thread and run on the
118 // FILE thread. These are both terminal actions with respect to the
119 // download file, as far as the DownloadFileManager is concerned -- if
120 // anything happens to the download file after they are called, it will
121 // be ignored.
122 // We call back to the UI thread in the case of CompleteDownload so that
123 // we know when we can hand the file off to other consumers.
124 virtual void CancelDownload(content::DownloadId id);
125 virtual void CompleteDownload(content::DownloadId id,
126 const base::Closure& callback);
127
128 // Called on FILE thread by DownloadManager at the beginning of its shutdown.
129 virtual void OnDownloadManagerShutdown(content::DownloadManager* manager);
130
131 // Rename the download file, uniquifying if overwrite was not requested.
132 virtual void RenameDownloadFile(
133 content::DownloadId id,
134 const FilePath& full_path,
135 bool overwrite_existing_file,
136 const RenameCompletionCallback& callback);
137
138 // The number of downloads currently active on the DownloadFileManager.
139 // Primarily for testing.
140 virtual int NumberOfActiveDownloads() const;
141
142 void SetFileFactoryForTesting(scoped_ptr<DownloadFileFactory> file_factory) {
143 download_file_factory_.reset(file_factory.release());
144 }
145
146 DownloadFileFactory* GetFileFactoryForTesting() const {
147 return download_file_factory_.get(); // Explicitly NOT a scoped_ptr.
148 }
149
150 protected:
151 virtual ~DownloadFileManager();
152
153 private:
154 friend class base::RefCountedThreadSafe<DownloadFileManager>;
155 friend class DownloadFileManagerTest;
156 friend class DownloadManagerTest;
157 FRIEND_TEST_ALL_PREFIXES(DownloadManagerTest, StartDownload);
158
159 // Clean up helper that runs on the download thread.
160 void OnShutdown();
161
162 // Called only on the download thread.
163 content::DownloadFile* GetDownloadFile(content::DownloadId global_id);
164
165 // Erases the download file with the given the download |id| and removes
166 // it from the maps.
167 void EraseDownload(content::DownloadId global_id);
168
169 typedef base::hash_map<content::DownloadId, content::DownloadFile*>
170 DownloadFileMap;
171
172 // A map of all in progress downloads. It owns the download files.
173 DownloadFileMap downloads_;
174
175 scoped_ptr<DownloadFileFactory> download_file_factory_;
176
177 DISALLOW_COPY_AND_ASSIGN(DownloadFileManager);
178 };
179
180 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
OLDNEW
« no previous file with comments | « content/browser/download/download_file_impl.cc ('k') | content/browser/download/download_file_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698