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

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

Issue 10861002: Revert 152213 - Replace the DownloadFileManager with direct ownership of DownloadFile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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
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_destination_observer.h" 19 #include "content/public/browser/download_manager.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
32 DownloadFileImpl::DownloadFileImpl( 30 DownloadFileImpl::DownloadFileImpl(
33 const content::DownloadSaveInfo& save_info, 31 const DownloadCreateInfo* info,
34 const GURL& url, 32 scoped_ptr<content::ByteStreamReader> stream,
35 const GURL& referrer_url, 33 DownloadRequestHandleInterface* request_handle,
36 int64 received_bytes, 34 scoped_refptr<DownloadManager> download_manager,
37 bool calculate_hash, 35 bool calculate_hash,
38 scoped_ptr<content::ByteStreamReader> stream,
39 const net::BoundNetLog& bound_net_log,
40 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, 36 scoped_ptr<content::PowerSaveBlocker> power_save_blocker,
41 base::WeakPtr<content::DownloadDestinationObserver> observer) 37 const net::BoundNetLog& bound_net_log)
42 : file_(save_info.file_path, 38 : file_(info->save_info.file_path,
43 url, 39 info->url(),
44 referrer_url, 40 info->referrer_url,
45 received_bytes, 41 info->received_bytes,
46 calculate_hash, 42 calculate_hash,
47 save_info.hash_state, 43 info->save_info.hash_state,
48 save_info.file_stream, 44 info->save_info.file_stream,
49 bound_net_log), 45 bound_net_log),
50 stream_reader_(stream.Pass()), 46 stream_reader_(stream.Pass()),
47 id_(info->download_id),
48 request_handle_(request_handle),
49 download_manager_(download_manager),
51 bytes_seen_(0), 50 bytes_seen_(0),
52 bound_net_log_(bound_net_log), 51 bound_net_log_(bound_net_log),
53 observer_(observer),
54 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 52 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
55 power_save_blocker_(power_save_blocker.Pass()) { 53 power_save_blocker_(power_save_blocker.Pass()) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
55 DCHECK(download_manager.get());
56 } 56 }
57 57
58 DownloadFileImpl::~DownloadFileImpl() { 58 DownloadFileImpl::~DownloadFileImpl() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
60 --number_active_objects_;
61 } 59 }
62 60
63 void DownloadFileImpl::Initialize(const InitializeCallback& callback) { 61 content::DownloadInterruptReason DownloadFileImpl::Initialize() {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
65
66 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); 62 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>());
67 63 net::Error result = file_.Initialize();
68 net::Error net_result = file_.Initialize(); 64 if (result != net::OK) {
69 if (net_result != net::OK) { 65 return content::ConvertNetErrorToInterruptReason(
70 BrowserThread::PostTask( 66 result, content::DOWNLOAD_INTERRUPT_FROM_DISK);
71 BrowserThread::UI, FROM_HERE, base::Bind(
72 callback, content::ConvertNetErrorToInterruptReason(
73 net_result, content::DOWNLOAD_INTERRUPT_FROM_DISK)));
74 return;
75 } 67 }
76 68
77 stream_reader_->RegisterCallback( 69 stream_reader_->RegisterCallback(
78 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr())); 70 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr()));
79 71
80 download_start_ = base::TimeTicks::Now(); 72 download_start_ = base::TimeTicks::Now();
81
82 // Initial pull from the straw. 73 // Initial pull from the straw.
83 StreamActive(); 74 StreamActive();
84 75
85 BrowserThread::PostTask( 76 return content::DOWNLOAD_INTERRUPT_REASON_NONE;
86 BrowserThread::UI, FROM_HERE, base::Bind(
87 callback, content::DOWNLOAD_INTERRUPT_REASON_NONE));
88
89 ++number_active_objects_;
90 } 77 }
91 78
92 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile( 79 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile(
93 const char* data, size_t data_len) { 80 const char* data, size_t data_len) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
95
96 if (!update_timer_->IsRunning()) { 81 if (!update_timer_->IsRunning()) {
97 update_timer_->Start(FROM_HERE, 82 update_timer_->Start(FROM_HERE,
98 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), 83 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs),
99 this, &DownloadFileImpl::SendUpdate); 84 this, &DownloadFileImpl::SendUpdate);
100 } 85 }
101 return content::ConvertNetErrorToInterruptReason( 86 return content::ConvertNetErrorToInterruptReason(
102 file_.AppendDataToFile(data, data_len), 87 file_.AppendDataToFile(data, data_len),
103 content::DOWNLOAD_INTERRUPT_FROM_DISK); 88 content::DOWNLOAD_INTERRUPT_FROM_DISK);
104 } 89 }
105 90
106 void DownloadFileImpl::Rename(const FilePath& full_path, 91 void DownloadFileImpl::Rename(const FilePath& full_path,
107 bool overwrite_existing_file, 92 bool overwrite_existing_file,
108 const RenameCompletionCallback& callback) { 93 const RenameCompletionCallback& callback) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
110
111 FilePath new_path(full_path); 94 FilePath new_path(full_path);
112 if (!overwrite_existing_file) { 95 if (!overwrite_existing_file) {
113 // Make the file unique if requested. 96 // Make the file unique if requested.
114 int uniquifier = 97 int uniquifier =
115 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); 98 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL(""));
116 if (uniquifier > 0) { 99 if (uniquifier > 0) {
117 new_path = new_path.InsertBeforeExtensionASCII( 100 new_path = new_path.InsertBeforeExtensionASCII(
118 StringPrintf(" (%d)", uniquifier)); 101 StringPrintf(" (%d)", uniquifier));
119 } 102 }
120 } 103 }
(...skipping 15 matching lines...) Expand all
136 content::DOWNLOAD_INTERRUPT_FROM_DISK); 119 content::DOWNLOAD_INTERRUPT_FROM_DISK);
137 new_path.clear(); 120 new_path.clear();
138 } 121 }
139 122
140 BrowserThread::PostTask( 123 BrowserThread::PostTask(
141 BrowserThread::UI, FROM_HERE, 124 BrowserThread::UI, FROM_HERE,
142 base::Bind(callback, reason, new_path)); 125 base::Bind(callback, reason, new_path));
143 } 126 }
144 127
145 void DownloadFileImpl::Detach() { 128 void DownloadFileImpl::Detach() {
146 // Done here on Windows so that anti-virus scanners invoked by
147 // the attachment service actually see the data; see
148 // http://crbug.com/127999.
149 // Done here for mac because we only want to do this once; see
150 // http://crbug.com/13120 for details.
151 // Other platforms don't currently do source annotation.
152 AnnotateWithSourceInformation();
153
154 file_.Detach(); 129 file_.Detach();
155 } 130 }
156 131
157 void DownloadFileImpl::Cancel() { 132 void DownloadFileImpl::Cancel() {
158 file_.Cancel(); 133 file_.Cancel();
159 } 134 }
160 135
161 void DownloadFileImpl::AnnotateWithSourceInformation() { 136 void DownloadFileImpl::AnnotateWithSourceInformation() {
162 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED); 137 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED);
163 file_.AnnotateWithSourceInformation(); 138 file_.AnnotateWithSourceInformation();
(...skipping 17 matching lines...) Expand all
181 } 156 }
182 157
183 bool DownloadFileImpl::GetHash(std::string* hash) { 158 bool DownloadFileImpl::GetHash(std::string* hash) {
184 return file_.GetHash(hash); 159 return file_.GetHash(hash);
185 } 160 }
186 161
187 std::string DownloadFileImpl::GetHashState() { 162 std::string DownloadFileImpl::GetHashState() {
188 return file_.GetHashState(); 163 return file_.GetHashState();
189 } 164 }
190 165
166 // DownloadFileInterface implementation.
167 void DownloadFileImpl::CancelDownloadRequest() {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
169 request_handle_->CancelRequest();
170 }
171
172 int DownloadFileImpl::Id() const {
173 return id_.local();
174 }
175
176 DownloadManager* DownloadFileImpl::GetDownloadManager() {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
178 return download_manager_.get();
179 }
180
181 const DownloadId& DownloadFileImpl::GlobalId() const {
182 return id_;
183 }
184
185 std::string DownloadFileImpl::DebugString() const {
186 return base::StringPrintf("{"
187 " id_ = " "%d"
188 " request_handle = %s"
189 " Base File = %s"
190 " }",
191 id_.local(),
192 request_handle_->DebugString().c_str(),
193 file_.DebugString().c_str());
194 }
195
191 void DownloadFileImpl::StreamActive() { 196 void DownloadFileImpl::StreamActive() {
192 base::TimeTicks start(base::TimeTicks::Now()); 197 base::TimeTicks start(base::TimeTicks::Now());
193 base::TimeTicks now; 198 base::TimeTicks now;
194 scoped_refptr<net::IOBuffer> incoming_data; 199 scoped_refptr<net::IOBuffer> incoming_data;
195 size_t incoming_data_size = 0; 200 size_t incoming_data_size = 0;
196 size_t total_incoming_data_size = 0; 201 size_t total_incoming_data_size = 0;
197 size_t num_buffers = 0; 202 size_t num_buffers = 0;
198 content::ByteStreamReader::StreamState state( 203 content::ByteStreamReader::StreamState state(
199 content::ByteStreamReader::STREAM_EMPTY); 204 content::ByteStreamReader::STREAM_EMPTY);
200 content::DownloadInterruptReason reason = 205 content::DownloadInterruptReason reason =
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 BrowserThread::FILE, FROM_HERE, 254 BrowserThread::FILE, FROM_HERE,
250 base::Bind(&DownloadFileImpl::StreamActive, 255 base::Bind(&DownloadFileImpl::StreamActive,
251 weak_factory_.GetWeakPtr())); 256 weak_factory_.GetWeakPtr()));
252 } 257 }
253 258
254 if (total_incoming_data_size) 259 if (total_incoming_data_size)
255 download_stats::RecordFileThreadReceiveBuffers(num_buffers); 260 download_stats::RecordFileThreadReceiveBuffers(num_buffers);
256 261
257 download_stats::RecordContiguousWriteTime(now - start); 262 download_stats::RecordContiguousWriteTime(now - start);
258 263
259 // Take care of communication with our observer. 264 // Take care of communication with our controller.
260 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { 265 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
261 // Error case for both upstream source and file write. 266 // Error case for both upstream source and file write.
262 // Shut down processing and signal an error to our observer. 267 // Shut down processing and signal an error to our controller.
263 // Our observer will clean us up. 268 // Our controller will clean us up.
264 stream_reader_->RegisterCallback(base::Closure()); 269 stream_reader_->RegisterCallback(base::Closure());
265 weak_factory_.InvalidateWeakPtrs(); 270 weak_factory_.InvalidateWeakPtrs();
266 SendUpdate(); // Make info up to date before error. 271 SendUpdate(); // Make info up to date before error.
267 BrowserThread::PostTask( 272 BrowserThread::PostTask(
268 BrowserThread::UI, FROM_HERE, 273 BrowserThread::UI, FROM_HERE,
269 base::Bind(&content::DownloadDestinationObserver::DestinationError, 274 base::Bind(&DownloadManager::OnDownloadInterrupted,
270 observer_, reason)); 275 download_manager_, id_.local(), reason));
271 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) { 276 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) {
272 // Signal successful completion and shut down processing. 277 // Signal successful completion and shut down processing.
273 stream_reader_->RegisterCallback(base::Closure()); 278 stream_reader_->RegisterCallback(base::Closure());
274 weak_factory_.InvalidateWeakPtrs(); 279 weak_factory_.InvalidateWeakPtrs();
275 std::string hash; 280 std::string hash;
276 if (!GetHash(&hash) || file_.IsEmptyHash(hash)) 281 if (!GetHash(&hash) || file_.IsEmptyHash(hash))
277 hash.clear(); 282 hash.clear();
278 SendUpdate();
279 BrowserThread::PostTask( 283 BrowserThread::PostTask(
280 BrowserThread::UI, FROM_HERE, 284 BrowserThread::UI, FROM_HERE,
281 base::Bind( 285 base::Bind(&DownloadManager::OnResponseCompleted,
282 &content::DownloadDestinationObserver::DestinationCompleted, 286 download_manager_, id_.local(),
283 observer_, hash)); 287 BytesSoFar(), hash));
284 } 288 }
285 if (bound_net_log_.IsLoggingAllEvents()) { 289 if (bound_net_log_.IsLoggingAllEvents()) {
286 bound_net_log_.AddEvent( 290 bound_net_log_.AddEvent(
287 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED, 291 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED,
288 base::Bind(&download_net_logs::FileStreamDrainedCallback, 292 base::Bind(&download_net_logs::FileStreamDrainedCallback,
289 total_incoming_data_size, num_buffers)); 293 total_incoming_data_size, num_buffers));
290 } 294 }
291 } 295 }
292 296
293 void DownloadFileImpl::SendUpdate() { 297 void DownloadFileImpl::SendUpdate() {
294 BrowserThread::PostTask( 298 BrowserThread::PostTask(
295 BrowserThread::UI, FROM_HERE, 299 BrowserThread::UI, FROM_HERE,
296 base::Bind(&content::DownloadDestinationObserver::DestinationUpdate, 300 base::Bind(&DownloadManager::UpdateDownload,
297 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); 301 download_manager_, id_.local(),
302 BytesSoFar(), CurrentSpeed(), GetHashState()));
298 } 303 }
299
300 // static
301 int content::DownloadFile::GetNumberOfDownloadFiles() {
302 return number_active_objects_;
303 }
304
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