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

Side by Side Diff: content/browser/download/download_file_impl.cc

Issue 10912173: Replace the DownloadFileManager with direct ownership of DownloadFileImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR (r162700) Created 8 years, 2 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "content/browser/download/download_file_impl.h" 5 #include "content/browser/download/download_file_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/message_loop_proxy.h" 11 #include "base/message_loop_proxy.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 #include "content/browser/download/byte_stream.h" 13 #include "content/browser/download/byte_stream.h"
14 #include "content/browser/download/download_create_info.h" 14 #include "content/browser/download/download_create_info.h"
15 #include "content/browser/download/download_interrupt_reasons_impl.h" 15 #include "content/browser/download/download_interrupt_reasons_impl.h"
16 #include "content/browser/download/download_net_log_parameters.h" 16 #include "content/browser/download/download_net_log_parameters.h"
17 #include "content/browser/power_save_blocker.h" 17 #include "content/browser/power_save_blocker.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/download_manager.h" 19 #include "content/public/browser/download_destination_observer.h"
20 #include "content/browser/download/download_stats.h" 20 #include "content/browser/download/download_stats.h"
21 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
22 22
23 using content::BrowserThread; 23 using content::BrowserThread;
24 using content::DownloadId; 24 using content::DownloadId;
25 using content::DownloadManager; 25 using content::DownloadManager;
26 26
27 const int kUpdatePeriodMs = 500; 27 const int kUpdatePeriodMs = 500;
28 const int kMaxTimeBlockingFileThreadMs = 1000; 28 const int kMaxTimeBlockingFileThreadMs = 1000;
29 29
30 int content::DownloadFile::number_active_objects_ = 0;
31
30 DownloadFileImpl::DownloadFileImpl( 32 DownloadFileImpl::DownloadFileImpl(
31 scoped_ptr<DownloadCreateInfo> info, 33 scoped_ptr<content::DownloadSaveInfo> save_info,
34 const FilePath& default_download_directory,
35 const GURL& url,
36 const GURL& referrer_url,
37 int64 received_bytes,
38 bool calculate_hash,
32 scoped_ptr<content::ByteStreamReader> stream, 39 scoped_ptr<content::ByteStreamReader> stream,
33 scoped_ptr<DownloadRequestHandleInterface> request_handle, 40 const net::BoundNetLog& bound_net_log,
34 scoped_refptr<DownloadManager> download_manager,
35 bool calculate_hash,
36 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, 41 scoped_ptr<content::PowerSaveBlocker> power_save_blocker,
37 const net::BoundNetLog& bound_net_log) 42 base::WeakPtr<content::DownloadDestinationObserver> observer)
38 : file_(info->save_info->file_path, 43 : file_(save_info->file_path,
39 info->url(), 44 url,
40 info->referrer_url, 45 referrer_url,
41 info->received_bytes, 46 received_bytes,
42 calculate_hash, 47 calculate_hash,
43 info->save_info->hash_state, 48 save_info->hash_state,
44 info->save_info->file_stream.Pass(), 49 save_info->file_stream.Pass(),
45 bound_net_log), 50 bound_net_log),
51 default_download_directory_(default_download_directory),
46 stream_reader_(stream.Pass()), 52 stream_reader_(stream.Pass()),
47 id_(info->download_id),
48 default_download_directory_(info->default_download_directory),
49 request_handle_(request_handle.Pass()),
50 download_manager_(download_manager),
51 bytes_seen_(0), 53 bytes_seen_(0),
52 bound_net_log_(bound_net_log), 54 bound_net_log_(bound_net_log),
55 observer_(observer),
53 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 56 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
54 power_save_blocker_(power_save_blocker.Pass()) { 57 power_save_blocker_(power_save_blocker.Pass()) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
56 DCHECK(download_manager.get());
57 } 58 }
58 59
59 DownloadFileImpl::~DownloadFileImpl() { 60 DownloadFileImpl::~DownloadFileImpl() {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
62 --number_active_objects_;
60 } 63 }
61 64
62 content::DownloadInterruptReason DownloadFileImpl::Initialize() { 65 void DownloadFileImpl::Initialize(const InitializeCallback& callback) {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
64 67
65 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); 68 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>());
66 net::Error net_result = file_.Initialize(default_download_directory_); 69 net::Error net_result = file_.Initialize(default_download_directory_);
67 if (net_result != net::OK) { 70 if (net_result != net::OK) {
68 return content::ConvertNetErrorToInterruptReason( 71 BrowserThread::PostTask(
69 net_result, content::DOWNLOAD_INTERRUPT_FROM_DISK); 72 BrowserThread::UI, FROM_HERE, base::Bind(
73 callback, content::ConvertNetErrorToInterruptReason(
74 net_result, content::DOWNLOAD_INTERRUPT_FROM_DISK)));
75 return;
70 } 76 }
71 77
72 stream_reader_->RegisterCallback( 78 stream_reader_->RegisterCallback(
73 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr())); 79 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr()));
74 80
75 download_start_ = base::TimeTicks::Now(); 81 download_start_ = base::TimeTicks::Now();
76 82
77 // Initial pull from the straw. 83 // Initial pull from the straw.
78 StreamActive(); 84 StreamActive();
79 85
80 return content::DOWNLOAD_INTERRUPT_REASON_NONE; 86 BrowserThread::PostTask(
87 BrowserThread::UI, FROM_HERE, base::Bind(
88 callback, content::DOWNLOAD_INTERRUPT_REASON_NONE));
89
90 ++number_active_objects_;
81 } 91 }
82 92
83 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile( 93 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile(
84 const char* data, size_t data_len) { 94 const char* data, size_t data_len) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
96
85 if (!update_timer_->IsRunning()) { 97 if (!update_timer_->IsRunning()) {
86 update_timer_->Start(FROM_HERE, 98 update_timer_->Start(FROM_HERE,
87 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), 99 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs),
88 this, &DownloadFileImpl::SendUpdate); 100 this, &DownloadFileImpl::SendUpdate);
89 } 101 }
90 return content::ConvertNetErrorToInterruptReason( 102 return content::ConvertNetErrorToInterruptReason(
91 file_.AppendDataToFile(data, data_len), 103 file_.AppendDataToFile(data, data_len),
92 content::DOWNLOAD_INTERRUPT_FROM_DISK); 104 content::DOWNLOAD_INTERRUPT_FROM_DISK);
93 } 105 }
94 106
95 void DownloadFileImpl::Rename(const FilePath& full_path, 107 void DownloadFileImpl::Rename(const FilePath& full_path,
96 bool overwrite_existing_file, 108 bool overwrite_existing_file,
97 const RenameCompletionCallback& callback) { 109 const RenameCompletionCallback& callback) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
111
98 FilePath new_path(full_path); 112 FilePath new_path(full_path);
99 if (!overwrite_existing_file) { 113 if (!overwrite_existing_file) {
100 // Make the file unique if requested. 114 // Make the file unique if requested.
101 int uniquifier = 115 int uniquifier =
102 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); 116 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL(""));
103 if (uniquifier > 0) { 117 if (uniquifier > 0) {
104 new_path = new_path.InsertBeforeExtensionASCII( 118 new_path = new_path.InsertBeforeExtensionASCII(
105 StringPrintf(" (%d)", uniquifier)); 119 StringPrintf(" (%d)", uniquifier));
106 } 120 }
107 } 121 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 } 183 }
170 184
171 bool DownloadFileImpl::GetHash(std::string* hash) { 185 bool DownloadFileImpl::GetHash(std::string* hash) {
172 return file_.GetHash(hash); 186 return file_.GetHash(hash);
173 } 187 }
174 188
175 std::string DownloadFileImpl::GetHashState() { 189 std::string DownloadFileImpl::GetHashState() {
176 return file_.GetHashState(); 190 return file_.GetHashState();
177 } 191 }
178 192
179 // DownloadFileInterface implementation.
180 void DownloadFileImpl::CancelDownloadRequest() {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
182 request_handle_->CancelRequest();
183 }
184
185 int DownloadFileImpl::Id() const {
186 return id_.local();
187 }
188
189 DownloadManager* DownloadFileImpl::GetDownloadManager() {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
191 return download_manager_.get();
192 }
193
194 const DownloadId& DownloadFileImpl::GlobalId() const {
195 return id_;
196 }
197
198 std::string DownloadFileImpl::DebugString() const {
199 return base::StringPrintf("{"
200 " id_ = " "%d"
201 " request_handle = %s"
202 " Base File = %s"
203 " }",
204 id_.local(),
205 request_handle_->DebugString().c_str(),
206 file_.DebugString().c_str());
207 }
208
209 void DownloadFileImpl::StreamActive() { 193 void DownloadFileImpl::StreamActive() {
210 base::TimeTicks start(base::TimeTicks::Now()); 194 base::TimeTicks start(base::TimeTicks::Now());
211 base::TimeTicks now; 195 base::TimeTicks now;
212 scoped_refptr<net::IOBuffer> incoming_data; 196 scoped_refptr<net::IOBuffer> incoming_data;
213 size_t incoming_data_size = 0; 197 size_t incoming_data_size = 0;
214 size_t total_incoming_data_size = 0; 198 size_t total_incoming_data_size = 0;
215 size_t num_buffers = 0; 199 size_t num_buffers = 0;
216 content::ByteStreamReader::StreamState state( 200 content::ByteStreamReader::StreamState state(
217 content::ByteStreamReader::STREAM_EMPTY); 201 content::ByteStreamReader::STREAM_EMPTY);
218 content::DownloadInterruptReason reason = 202 content::DownloadInterruptReason reason =
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 BrowserThread::FILE, FROM_HERE, 251 BrowserThread::FILE, FROM_HERE,
268 base::Bind(&DownloadFileImpl::StreamActive, 252 base::Bind(&DownloadFileImpl::StreamActive,
269 weak_factory_.GetWeakPtr())); 253 weak_factory_.GetWeakPtr()));
270 } 254 }
271 255
272 if (total_incoming_data_size) 256 if (total_incoming_data_size)
273 download_stats::RecordFileThreadReceiveBuffers(num_buffers); 257 download_stats::RecordFileThreadReceiveBuffers(num_buffers);
274 258
275 download_stats::RecordContiguousWriteTime(now - start); 259 download_stats::RecordContiguousWriteTime(now - start);
276 260
277 // Take care of communication with our controller. 261 // Take care of communication with our observer.
278 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { 262 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
279 // Error case for both upstream source and file write. 263 // Error case for both upstream source and file write.
280 // Shut down processing and signal an error to our controller. 264 // Shut down processing and signal an error to our observer.
281 // Our controller will clean us up. 265 // Our observer will clean us up.
282 stream_reader_->RegisterCallback(base::Closure()); 266 stream_reader_->RegisterCallback(base::Closure());
283 weak_factory_.InvalidateWeakPtrs(); 267 weak_factory_.InvalidateWeakPtrs();
284 SendUpdate(); // Make info up to date before error. 268 SendUpdate(); // Make info up to date before error.
285 BrowserThread::PostTask( 269 BrowserThread::PostTask(
286 BrowserThread::UI, FROM_HERE, 270 BrowserThread::UI, FROM_HERE,
287 base::Bind(&DownloadManager::OnDownloadInterrupted, 271 base::Bind(&content::DownloadDestinationObserver::DestinationError,
288 download_manager_, id_.local(), reason)); 272 observer_, reason));
289 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) { 273 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) {
290 // Signal successful completion and shut down processing. 274 // Signal successful completion and shut down processing.
291 stream_reader_->RegisterCallback(base::Closure()); 275 stream_reader_->RegisterCallback(base::Closure());
292 weak_factory_.InvalidateWeakPtrs(); 276 weak_factory_.InvalidateWeakPtrs();
293 std::string hash; 277 std::string hash;
294 if (!GetHash(&hash) || file_.IsEmptyHash(hash)) 278 if (!GetHash(&hash) || file_.IsEmptyHash(hash))
295 hash.clear(); 279 hash.clear();
280 SendUpdate();
296 BrowserThread::PostTask( 281 BrowserThread::PostTask(
297 BrowserThread::UI, FROM_HERE, 282 BrowserThread::UI, FROM_HERE,
298 base::Bind(&DownloadManager::OnResponseCompleted, 283 base::Bind(
299 download_manager_, id_.local(), 284 &content::DownloadDestinationObserver::DestinationCompleted,
300 BytesSoFar(), hash)); 285 observer_, hash));
301 } 286 }
302 if (bound_net_log_.IsLoggingAllEvents()) { 287 if (bound_net_log_.IsLoggingAllEvents()) {
303 bound_net_log_.AddEvent( 288 bound_net_log_.AddEvent(
304 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED, 289 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED,
305 base::Bind(&download_net_logs::FileStreamDrainedCallback, 290 base::Bind(&download_net_logs::FileStreamDrainedCallback,
306 total_incoming_data_size, num_buffers)); 291 total_incoming_data_size, num_buffers));
307 } 292 }
308 } 293 }
309 294
310 void DownloadFileImpl::SendUpdate() { 295 void DownloadFileImpl::SendUpdate() {
311 BrowserThread::PostTask( 296 BrowserThread::PostTask(
312 BrowserThread::UI, FROM_HERE, 297 BrowserThread::UI, FROM_HERE,
313 base::Bind(&DownloadManager::UpdateDownload, 298 base::Bind(&content::DownloadDestinationObserver::DestinationUpdate,
314 download_manager_, id_.local(), 299 observer_, BytesSoFar(), CurrentSpeed(), GetHashState()));
315 BytesSoFar(), CurrentSpeed(), GetHashState()));
316 } 300 }
301
302 // static
303 int content::DownloadFile::GetNumberOfDownloadFiles() {
304 return number_active_objects_;
305 }
306
OLDNEW
« no previous file with comments | « content/browser/download/download_file_impl.h ('k') | content/browser/download/download_file_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698