OLD | NEW |
---|---|
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 // File method ordering: Methods in this file are in the same order as | 5 // File method ordering: Methods in this file are in the same order as |
6 // in download_item_impl.h, with the following exception: The public | 6 // in download_item_impl.h, with the following exception: The public |
7 // interface Start is placed in chronological order with the other | 7 // interface Start is placed in chronological order with the other |
8 // (private) routines that together define a DownloadItem's state | 8 // (private) routines that together define a DownloadItem's state |
9 // transitions as the download progresses. See "Download progression | 9 // transitions as the download progresses. See "Download progression |
10 // cascade" later in this file. | 10 // cascade" later in this file. |
11 | 11 |
12 // A regular DownloadItem (created for a download in this session of the | 12 // A regular DownloadItem (created for a download in this session of the |
13 // browser) normally goes through the following states: | 13 // browser) normally goes through the following states: |
14 // * Created (when download starts) | 14 // * Created (when download starts) |
15 // * Destination filename determined | 15 // * Destination filename determined |
16 // * Entered into the history database. | 16 // * Entered into the history database. |
17 // * Made visible in the download shelf. | 17 // * Made visible in the download shelf. |
18 // * All the data is saved. Note that the actual data download occurs | 18 // * All the data is saved. Note that the actual data download occurs |
19 // in parallel with the above steps, but until those steps are | 19 // in parallel with the above steps, but until those steps are |
20 // complete, the state of the data save will be ignored. | 20 // complete, the state of the data save will be ignored. |
21 // * Download file is renamed to its final name, and possibly | 21 // * Download file is renamed to its final name, and possibly |
22 // auto-opened. | 22 // auto-opened. |
23 | 23 |
24 #include "content/browser/download/download_item_impl.h" | 24 #include "content/browser/download/download_item_impl.h" |
25 | 25 |
26 #include <vector> | 26 #include <vector> |
27 | 27 |
28 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
29 #include "base/bind.h" | 29 #include "base/bind.h" |
30 #include "base/command_line.h" | |
30 #include "base/file_util.h" | 31 #include "base/file_util.h" |
31 #include "base/format_macros.h" | 32 #include "base/format_macros.h" |
32 #include "base/logging.h" | 33 #include "base/logging.h" |
33 #include "base/metrics/histogram.h" | 34 #include "base/metrics/histogram.h" |
34 #include "base/stl_util.h" | 35 #include "base/stl_util.h" |
35 #include "base/stringprintf.h" | 36 #include "base/stringprintf.h" |
36 #include "base/utf_string_conversions.h" | 37 #include "base/utf_string_conversions.h" |
37 #include "content/browser/download/download_create_info.h" | 38 #include "content/browser/download/download_create_info.h" |
38 #include "content/browser/download/download_file.h" | 39 #include "content/browser/download/download_file.h" |
39 #include "content/browser/download/download_interrupt_reasons_impl.h" | 40 #include "content/browser/download/download_interrupt_reasons_impl.h" |
40 #include "content/browser/download/download_item_impl_delegate.h" | 41 #include "content/browser/download/download_item_impl_delegate.h" |
41 #include "content/browser/download/download_request_handle.h" | 42 #include "content/browser/download/download_request_handle.h" |
42 #include "content/browser/download/download_stats.h" | 43 #include "content/browser/download/download_stats.h" |
44 #include "content/browser/renderer_host/render_view_host_impl.h" | |
43 #include "content/browser/web_contents/web_contents_impl.h" | 45 #include "content/browser/web_contents/web_contents_impl.h" |
46 #include "content/public/browser/browser_context.h" | |
44 #include "content/public/browser/browser_thread.h" | 47 #include "content/public/browser/browser_thread.h" |
45 #include "content/public/browser/content_browser_client.h" | 48 #include "content/public/browser/content_browser_client.h" |
49 #include "content/public/browser/download_interrupt_reasons.h" | |
50 #include "content/public/browser/download_url_parameters.h" | |
51 #include "content/public/common/content_switches.h" | |
52 #include "content/public/common/referrer.h" | |
46 #include "net/base/net_util.h" | 53 #include "net/base/net_util.h" |
47 | 54 |
48 namespace content { | 55 namespace content { |
56 | |
49 namespace { | 57 namespace { |
50 | 58 |
51 static void DeleteDownloadedFile(const FilePath& path) { | 59 void DeleteDownloadedFile(const FilePath& path) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
53 | 61 |
54 // Make sure we only delete files. | 62 // Make sure we only delete files. |
55 if (!file_util::DirectoryExists(path)) | 63 if (!file_util::DirectoryExists(path)) |
56 file_util::Delete(path, false); | 64 file_util::Delete(path, false); |
57 } | 65 } |
58 | 66 |
59 const char* DebugSafetyStateString(DownloadItem::SafetyState state) { | 67 const char* DebugSafetyStateString(DownloadItem::SafetyState state) { |
60 switch (state) { | 68 switch (state) { |
61 case DownloadItem::SAFE: | 69 case DownloadItem::SAFE: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 | 112 |
105 static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) { | 113 static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
107 download_file->Cancel(); | 115 download_file->Cancel(); |
108 } | 116 } |
109 | 117 |
110 } // namespace | 118 } // namespace |
111 | 119 |
112 const char DownloadItem::kEmptyFileHash[] = ""; | 120 const char DownloadItem::kEmptyFileHash[] = ""; |
113 | 121 |
122 // The maximum number of attempts we will make to resume automatically. | |
123 const int DownloadItemImpl::kMaxAutoResumeAttempts = 5; | |
124 | |
114 // Constructor for reading from the history service. | 125 // Constructor for reading from the history service. |
115 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, | 126 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, |
116 DownloadId download_id, | 127 DownloadId download_id, |
117 const FilePath& path, | 128 const FilePath& path, |
118 const GURL& url, | 129 const GURL& url, |
119 const GURL& referrer_url, | 130 const GURL& referrer_url, |
120 const base::Time& start_time, | 131 const base::Time& start_time, |
121 const base::Time& end_time, | 132 const base::Time& end_time, |
122 int64 received_bytes, | 133 int64 received_bytes, |
123 int64 total_bytes, | 134 int64 total_bytes, |
(...skipping 13 matching lines...) Expand all Loading... | |
137 received_bytes_(received_bytes), | 148 received_bytes_(received_bytes), |
138 bytes_per_sec_(0), | 149 bytes_per_sec_(0), |
139 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 150 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
140 start_tick_(base::TimeTicks()), | 151 start_tick_(base::TimeTicks()), |
141 state_(ExternalToInternalState(state)), | 152 state_(ExternalToInternalState(state)), |
142 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | 153 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
143 start_time_(start_time), | 154 start_time_(start_time), |
144 end_time_(end_time), | 155 end_time_(end_time), |
145 delegate_(delegate), | 156 delegate_(delegate), |
146 is_paused_(false), | 157 is_paused_(false), |
158 auto_resume_count_(0), | |
147 open_when_complete_(false), | 159 open_when_complete_(false), |
148 file_externally_removed_(false), | 160 file_externally_removed_(false), |
149 safety_state_(SAFE), | 161 safety_state_(SAFE), |
150 auto_opened_(false), | 162 auto_opened_(false), |
151 is_temporary_(false), | 163 is_temporary_(false), |
152 all_data_saved_(false), | 164 all_data_saved_(false), |
153 opened_(opened), | 165 opened_(opened), |
154 open_enabled_(true), | 166 open_enabled_(true), |
155 delegate_delayed_complete_(false), | 167 delegate_delayed_complete_(false), |
156 bound_net_log_(bound_net_log), | 168 bound_net_log_(bound_net_log), |
157 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 169 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
158 delegate_->Attach(); | 170 delegate_->Attach(); |
159 if (state_ == IN_PROGRESS_INTERNAL) | 171 if (state_ == IN_PROGRESS_INTERNAL) |
160 state_ = CANCELLED_INTERNAL; | 172 state_ = CANCELLED_INTERNAL; |
161 if (state_ == COMPLETE_INTERNAL) | 173 if (state_ == COMPLETE_INTERNAL) |
162 all_data_saved_ = true; | 174 all_data_saved_ = true; |
163 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); | 175 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); |
164 } | 176 } |
165 | 177 |
166 // Constructing for a regular download: | 178 // Constructing for a regular download: |
167 DownloadItemImpl::DownloadItemImpl( | 179 DownloadItemImpl::DownloadItemImpl( |
168 DownloadItemImplDelegate* delegate, | 180 DownloadItemImplDelegate* delegate, |
169 const DownloadCreateInfo& info, | 181 const DownloadCreateInfo& info, |
170 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
171 const net::BoundNetLog& bound_net_log) | 182 const net::BoundNetLog& bound_net_log) |
172 : is_save_package_download_(false), | 183 : is_save_package_download_(false), |
173 request_handle_(request_handle.Pass()), | |
174 download_id_(info.download_id), | 184 download_id_(info.download_id), |
175 target_disposition_( | 185 target_disposition_( |
176 (info.save_info->prompt_for_save_location) ? | 186 (info.save_info->prompt_for_save_location) ? |
177 TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE), | 187 TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE), |
178 url_chain_(info.url_chain), | 188 url_chain_(info.url_chain), |
179 referrer_url_(info.referrer_url), | 189 referrer_url_(info.referrer_url), |
180 suggested_filename_(UTF16ToUTF8(info.save_info->suggested_name)), | 190 suggested_filename_(UTF16ToUTF8(info.save_info->suggested_name)), |
181 forced_file_path_(info.save_info->file_path), | 191 forced_file_path_(info.save_info->file_path), |
182 transition_type_(info.transition_type), | 192 transition_type_(info.transition_type), |
183 has_user_gesture_(info.has_user_gesture), | 193 has_user_gesture_(info.has_user_gesture), |
184 content_disposition_(info.content_disposition), | 194 content_disposition_(info.content_disposition), |
185 mime_type_(info.mime_type), | 195 mime_type_(info.mime_type), |
186 original_mime_type_(info.original_mime_type), | 196 original_mime_type_(info.original_mime_type), |
187 remote_address_(info.remote_address), | 197 remote_address_(info.remote_address), |
188 total_bytes_(info.total_bytes), | 198 total_bytes_(info.total_bytes), |
189 received_bytes_(0), | 199 received_bytes_(0), |
190 bytes_per_sec_(0), | 200 bytes_per_sec_(0), |
191 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 201 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
192 start_tick_(base::TimeTicks::Now()), | 202 start_tick_(base::TimeTicks::Now()), |
193 state_(IN_PROGRESS_INTERNAL), | 203 state_(IN_PROGRESS_INTERNAL), |
194 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | 204 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
195 start_time_(info.start_time), | 205 start_time_(info.start_time), |
196 delegate_(delegate), | 206 delegate_(delegate), |
197 is_paused_(false), | 207 is_paused_(false), |
208 auto_resume_count_(0), | |
198 open_when_complete_(false), | 209 open_when_complete_(false), |
199 file_externally_removed_(false), | 210 file_externally_removed_(false), |
200 safety_state_(SAFE), | 211 safety_state_(SAFE), |
201 auto_opened_(false), | 212 auto_opened_(false), |
202 is_temporary_(!info.save_info->file_path.empty()), | 213 is_temporary_(!info.save_info->file_path.empty()), |
203 all_data_saved_(false), | 214 all_data_saved_(false), |
204 opened_(false), | 215 opened_(false), |
205 open_enabled_(true), | 216 open_enabled_(true), |
206 delegate_delayed_complete_(false), | 217 delegate_delayed_complete_(false), |
207 bound_net_log_(bound_net_log), | 218 bound_net_log_(bound_net_log), |
208 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 219 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
209 delegate_->Attach(); | 220 delegate_->Attach(); |
210 Init(true /* actively downloading */, SRC_NEW_DOWNLOAD); | 221 Init(true /* actively downloading */, SRC_ACTIVE_DOWNLOAD); |
211 | 222 |
212 // Link the event sources. | 223 // Link the event sources. |
213 bound_net_log_.AddEvent( | 224 bound_net_log_.AddEvent( |
214 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST, | 225 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST, |
215 info.request_bound_net_log.source().ToEventParametersCallback()); | 226 info.request_bound_net_log.source().ToEventParametersCallback()); |
216 | 227 |
217 info.request_bound_net_log.AddEvent( | 228 info.request_bound_net_log.AddEvent( |
218 net::NetLog::TYPE_DOWNLOAD_STARTED, | 229 net::NetLog::TYPE_DOWNLOAD_STARTED, |
219 bound_net_log_.source().ToEventParametersCallback()); | 230 bound_net_log_.source().ToEventParametersCallback()); |
220 } | 231 } |
(...skipping 20 matching lines...) Expand all Loading... | |
241 total_bytes_(0), | 252 total_bytes_(0), |
242 received_bytes_(0), | 253 received_bytes_(0), |
243 bytes_per_sec_(0), | 254 bytes_per_sec_(0), |
244 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 255 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
245 start_tick_(base::TimeTicks::Now()), | 256 start_tick_(base::TimeTicks::Now()), |
246 state_(IN_PROGRESS_INTERNAL), | 257 state_(IN_PROGRESS_INTERNAL), |
247 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | 258 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
248 start_time_(base::Time::Now()), | 259 start_time_(base::Time::Now()), |
249 delegate_(delegate), | 260 delegate_(delegate), |
250 is_paused_(false), | 261 is_paused_(false), |
262 auto_resume_count_(0), | |
251 open_when_complete_(false), | 263 open_when_complete_(false), |
252 file_externally_removed_(false), | 264 file_externally_removed_(false), |
253 safety_state_(SAFE), | 265 safety_state_(SAFE), |
254 auto_opened_(false), | 266 auto_opened_(false), |
255 is_temporary_(false), | 267 is_temporary_(false), |
256 all_data_saved_(false), | 268 all_data_saved_(false), |
257 opened_(false), | 269 opened_(false), |
258 open_enabled_(true), | 270 open_enabled_(true), |
259 delegate_delayed_complete_(false), | 271 delegate_delayed_complete_(false), |
260 bound_net_log_(bound_net_log), | 272 bound_net_log_(bound_net_log), |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 base::Bind(&ItemCheckedNetLogCallback, | 326 base::Bind(&ItemCheckedNetLogCallback, |
315 GetDangerType(), GetSafetyState())); | 327 GetDangerType(), GetSafetyState())); |
316 | 328 |
317 UpdateObservers(); | 329 UpdateObservers(); |
318 | 330 |
319 MaybeCompleteDownload(); | 331 MaybeCompleteDownload(); |
320 } | 332 } |
321 | 333 |
322 void DownloadItemImpl::TogglePause() { | 334 void DownloadItemImpl::TogglePause() { |
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
324 DCHECK(state_ == IN_PROGRESS_INTERNAL || state_ == COMPLETING_INTERNAL); | 336 DCHECK(IsPartialDownload()); |
325 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 337 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
326 | 338 |
327 // Ignore pauses when we've passed the commit point. | 339 // Ignore pause/resumes when we've passed the commit point. |
328 if (state_ == COMPLETING_INTERNAL) | 340 if (state_ == COMPLETING_INTERNAL || state_ == COMPLETE_INTERNAL) |
329 return; | 341 return; |
330 | 342 |
331 if (is_paused_) | 343 if (IsInProgress()) { |
332 request_handle_->ResumeRequest(); | 344 if (is_paused_) |
333 else | 345 request_handle_->ResumeRequest(); |
334 request_handle_->PauseRequest(); | 346 else |
335 is_paused_ = !is_paused_; | 347 request_handle_->PauseRequest(); |
348 is_paused_ = !is_paused_; | |
349 } else if (IsInterrupted()) { | |
350 auto_resume_count_ = 0; // User input resets the counter. | |
351 ResumeInterruptedDownload(); | |
352 } | |
353 | |
336 UpdateObservers(); | 354 UpdateObservers(); |
337 } | 355 } |
338 | 356 |
339 void DownloadItemImpl::Cancel(bool user_cancel) { | 357 void DownloadItemImpl::Cancel(bool user_cancel) { |
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
341 | 359 |
342 last_reason_ = user_cancel ? | |
343 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | |
344 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
345 | |
346 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 360 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
347 if (state_ != IN_PROGRESS_INTERNAL) { | 361 if (state_ != IN_PROGRESS_INTERNAL && state_ != INTERRUPTED_INTERNAL) { |
348 // Small downloads might be complete before this method has | 362 // Small downloads might be complete before this method has |
349 // a chance to run. | 363 // a chance to run. |
350 return; | 364 return; |
351 } | 365 } |
352 | 366 |
367 last_reason_ = user_cancel ? | |
368 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | |
369 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
370 | |
353 RecordDownloadCount(CANCELLED_COUNT); | 371 RecordDownloadCount(CANCELLED_COUNT); |
354 | 372 |
355 TransitionTo(CANCELLED_INTERNAL); | 373 TransitionTo(CANCELLED_INTERNAL); |
356 | 374 |
357 CancelDownloadFile(); | 375 CancelDownloadFile(); |
358 | 376 |
359 // Cancel the originating URL request. | 377 // Cancel the originating URL request. |
360 request_handle_->CancelRequest(); | 378 request_handle_->CancelRequest(); |
361 } | 379 } |
362 | 380 |
363 void DownloadItemImpl::Delete(DeleteReason reason) { | 381 void DownloadItemImpl::Delete(DeleteReason reason) { |
382 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
365 | 384 |
366 switch (reason) { | 385 switch (reason) { |
367 case DELETE_DUE_TO_USER_DISCARD: | 386 case DELETE_DUE_TO_USER_DISCARD: |
368 UMA_HISTOGRAM_ENUMERATION( | 387 UMA_HISTOGRAM_ENUMERATION( |
369 "Download.UserDiscard", GetDangerType(), | 388 "Download.UserDiscard", GetDangerType(), |
370 DOWNLOAD_DANGER_TYPE_MAX); | 389 DOWNLOAD_DANGER_TYPE_MAX); |
371 break; | 390 break; |
372 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 391 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
373 UMA_HISTOGRAM_ENUMERATION( | 392 UMA_HISTOGRAM_ENUMERATION( |
374 "Download.Discard", GetDangerType(), | 393 "Download.Discard", GetDangerType(), |
375 DOWNLOAD_DANGER_TYPE_MAX); | 394 DOWNLOAD_DANGER_TYPE_MAX); |
376 break; | 395 break; |
377 default: | 396 default: |
378 NOTREACHED(); | 397 NOTREACHED(); |
379 } | 398 } |
380 | 399 |
381 // Delete the file if it exists and is not owned by a DownloadFile object. | 400 // Delete the file if it exists and is not owned by a DownloadFile object. |
382 // (In the latter case the DownloadFile object will delete it on cancel.) | 401 // (In the latter case the DownloadFile object will delete it on cancel.) |
383 if (!current_path_.empty() && download_file_.get() == NULL) { | 402 if (!current_path_.empty() && download_file_.get() == NULL) { |
384 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 403 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
385 base::Bind(&DeleteDownloadedFile, current_path_)); | 404 base::Bind(&DeleteDownloadedFile, current_path_)); |
386 } | 405 } |
387 Remove(); | 406 Remove(); |
388 // We have now been deleted. | 407 // We have now been deleted. |
389 } | 408 } |
390 | 409 |
391 void DownloadItemImpl::Remove() { | 410 void DownloadItemImpl::Remove() { |
411 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
393 | 413 |
394 delegate_->AssertStateConsistent(this); | 414 delegate_->AssertStateConsistent(this); |
395 Cancel(true); | 415 Cancel(true); |
396 delegate_->AssertStateConsistent(this); | 416 delegate_->AssertStateConsistent(this); |
397 | 417 |
398 NotifyRemoved(); | 418 NotifyRemoved(); |
399 delegate_->DownloadRemoved(this); | 419 delegate_->DownloadRemoved(this); |
400 // We have now been deleted. | 420 // We have now been deleted. |
401 } | 421 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
455 } | 475 } |
456 | 476 |
457 bool DownloadItemImpl::IsPaused() const { | 477 bool DownloadItemImpl::IsPaused() const { |
458 return is_paused_; | 478 return is_paused_; |
459 } | 479 } |
460 | 480 |
461 bool DownloadItemImpl::IsTemporary() const { | 481 bool DownloadItemImpl::IsTemporary() const { |
462 return is_temporary_; | 482 return is_temporary_; |
463 } | 483 } |
464 | 484 |
465 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | |
466 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | |
467 bool DownloadItemImpl::IsPartialDownload() const { | 485 bool DownloadItemImpl::IsPartialDownload() const { |
468 return InternalToExternalState(state_) == IN_PROGRESS; | 486 DownloadState state = InternalToExternalState(state_); |
487 return (state == IN_PROGRESS) || (state == INTERRUPTED); | |
469 } | 488 } |
470 | 489 |
471 bool DownloadItemImpl::IsInProgress() const { | 490 bool DownloadItemImpl::IsInProgress() const { |
472 return InternalToExternalState(state_) == IN_PROGRESS; | 491 return InternalToExternalState(state_) == IN_PROGRESS; |
473 } | 492 } |
474 | 493 |
475 bool DownloadItemImpl::IsCancelled() const { | 494 bool DownloadItemImpl::IsCancelled() const { |
476 DownloadState external_state = InternalToExternalState(state_); | 495 return InternalToExternalState(state_) == CANCELLED; |
477 return external_state == CANCELLED || external_state == INTERRUPTED; | |
478 } | 496 } |
479 | 497 |
480 bool DownloadItemImpl::IsInterrupted() const { | 498 bool DownloadItemImpl::IsInterrupted() const { |
481 return InternalToExternalState(state_) == INTERRUPTED; | 499 return InternalToExternalState(state_) == INTERRUPTED; |
482 } | 500 } |
483 | 501 |
484 bool DownloadItemImpl::IsComplete() const { | 502 bool DownloadItemImpl::IsComplete() const { |
485 return InternalToExternalState(state_) == COMPLETE; | 503 return InternalToExternalState(state_) == COMPLETE; |
486 } | 504 } |
487 | 505 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
743 url_list += next_url.spec(); | 761 url_list += next_url.spec(); |
744 } | 762 } |
745 } | 763 } |
746 | 764 |
747 if (verbose) { | 765 if (verbose) { |
748 description += base::StringPrintf( | 766 description += base::StringPrintf( |
749 " total = %" PRId64 | 767 " total = %" PRId64 |
750 " received = %" PRId64 | 768 " received = %" PRId64 |
751 " reason = %s" | 769 " reason = %s" |
752 " paused = %c" | 770 " paused = %c" |
771 " resume_mode = %s" | |
772 " auto_resume_count = %d" | |
753 " safety = %s" | 773 " safety = %s" |
774 " all_data_saved = %c" | |
754 " last_modified = '%s'" | 775 " last_modified = '%s'" |
755 " etag = '%s'" | 776 " etag = '%s'" |
777 " has_download_file = %s" | |
756 " url_chain = \n\t\"%s\"\n\t" | 778 " url_chain = \n\t\"%s\"\n\t" |
757 " full_path = \"%" PRFilePath "\"" | 779 " full_path = \"%" PRFilePath "\"\n\t" |
758 " target_path = \"%" PRFilePath "\"" | 780 " target_path = \"%" PRFilePath "\"", |
759 " has download file = %s", | |
760 GetTotalBytes(), | 781 GetTotalBytes(), |
761 GetReceivedBytes(), | 782 GetReceivedBytes(), |
762 InterruptReasonDebugString(last_reason_).c_str(), | 783 InterruptReasonDebugString(last_reason_).c_str(), |
763 IsPaused() ? 'T' : 'F', | 784 IsPaused() ? 'T' : 'F', |
785 DebugResumeModeString(GetResumeMode()), | |
786 auto_resume_count_, | |
764 DebugSafetyStateString(GetSafetyState()), | 787 DebugSafetyStateString(GetSafetyState()), |
788 AllDataSaved() ? 'T' : 'F', | |
765 GetLastModifiedTime().c_str(), | 789 GetLastModifiedTime().c_str(), |
766 GetETag().c_str(), | 790 GetETag().c_str(), |
791 download_file_.get() ? "true" : "false", | |
767 url_list.c_str(), | 792 url_list.c_str(), |
768 GetFullPath().value().c_str(), | 793 GetFullPath().value().c_str(), |
769 GetTargetFilePath().value().c_str(), | 794 GetTargetFilePath().value().c_str()); |
770 download_file_.get() ? "true" : "false"); | |
771 } else { | 795 } else { |
772 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 796 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
773 } | 797 } |
774 | 798 |
775 description += " }"; | 799 description += " }"; |
776 | 800 |
777 return description; | 801 return description; |
778 } | 802 } |
779 | 803 |
780 void DownloadItemImpl::MockDownloadOpenForTesting() { | 804 void DownloadItemImpl::MockDownloadOpenForTesting() { |
781 open_enabled_ = false; | 805 open_enabled_ = false; |
782 } | 806 } |
783 | 807 |
808 DownloadItemImpl::ResumeMode DownloadItemImpl::GetResumeMode() const { | |
809 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
810 if (!IsInterrupted()) | |
811 return RESUME_MODE_INVALID; | |
812 | |
813 // We can't continue without a handle on the intermediate file. | |
814 const bool force_restart = current_path_.empty(); | |
815 | |
816 // We won't auto-restart if we've used up our attempts or the | |
817 // download has been paused by user action. | |
818 const bool force_user = | |
819 (auto_resume_count_ >= kMaxAutoResumeAttempts || is_paused_); | |
820 | |
821 ResumeMode mode = RESUME_MODE_INVALID; | |
822 | |
823 switch(last_reason_) { | |
824 case DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR: | |
825 case DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT: | |
826 if (force_restart && force_user) | |
827 mode = RESUME_MODE_USER_RESTART; | |
828 else if (force_restart) | |
829 mode = RESUME_MODE_IMMEDIATE_RESTART; | |
830 else if (force_user) | |
831 mode = RESUME_MODE_USER_CONTINUE; | |
832 else | |
833 mode = RESUME_MODE_IMMEDIATE_CONTINUE; | |
834 break; | |
835 | |
836 case DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION: | |
837 case DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE: | |
838 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT: | |
839 if (force_user) | |
840 mode = RESUME_MODE_USER_RESTART; | |
841 else | |
842 mode = RESUME_MODE_IMMEDIATE_RESTART; | |
843 break; | |
844 | |
845 case DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED: | |
846 case DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED: | |
847 case DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN: | |
848 case DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED: | |
849 case DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN: | |
850 case DOWNLOAD_INTERRUPT_REASON_CRASH: | |
851 if (force_restart) | |
852 mode = RESUME_MODE_USER_RESTART; | |
853 else | |
854 mode = RESUME_MODE_USER_CONTINUE; | |
855 break; | |
856 | |
857 case DOWNLOAD_INTERRUPT_REASON_FILE_FAILED: | |
858 case DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | |
859 case DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | |
860 case DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG: | |
861 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE: | |
862 mode = RESUME_MODE_USER_RESTART; | |
863 break; | |
864 | |
865 case DOWNLOAD_INTERRUPT_REASON_NONE: | |
866 case DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED: | |
867 case DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT: | |
868 case DOWNLOAD_INTERRUPT_REASON_USER_CANCELED: | |
869 case DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED: | |
870 case DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED: | |
871 mode = RESUME_MODE_INVALID; | |
872 break; | |
873 } | |
874 | |
875 return mode; | |
876 } | |
877 | |
878 void DownloadItemImpl::ResumeInterruptedDownload() { | |
879 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
880 | |
881 // If the flag for downloads resumption isn't enabled, ignore | |
882 // this request. | |
883 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
884 if (!command_line.HasSwitch(switches::kEnableDownloadResumption)) | |
885 return; | |
886 | |
887 // Handle the case of clicking 'Resume' in the download shelf. | |
888 DCHECK(IsInterrupted()); | |
889 | |
890 DVLOG(20) << __FUNCTION__ << "()" << DebugString(true); | |
891 | |
892 // If we can't get a web contents, we can't resume the download. | |
893 // TODO(rdsmith): Find some alternative web contents to use--this | |
894 // means we can't restart a download if it's a download imported | |
895 // from the history. | |
896 if (!GetWebContents()) | |
897 return; | |
898 | |
899 // Reset the appropriate state if restarting. | |
900 ResumeMode mode = GetResumeMode(); | |
901 if (mode == RESUME_MODE_IMMEDIATE_RESTART || | |
902 mode == RESUME_MODE_USER_RESTART) { | |
903 received_bytes_ = 0; | |
904 hash_state_ = ""; | |
905 last_modified_time_ = ""; | |
906 etag_ = ""; | |
907 } | |
908 | |
909 scoped_ptr<DownloadUrlParameters> download_params( | |
910 DownloadUrlParameters::FromWebContents(GetWebContents(), | |
911 GetOriginalUrl())); | |
912 | |
913 download_params->set_file_path(GetFullPath()); | |
914 download_params->set_offset(GetReceivedBytes()); | |
915 download_params->set_hash_state(GetHashState()); | |
916 | |
917 Referrer referrer(GetReferrerUrl(), WebKit::WebReferrerPolicyDefault); | |
918 download_params->set_referrer(referrer); | |
919 download_params->set_last_modified(GetLastModifiedTime()); | |
920 download_params->set_etag(GetETag()); | |
921 download_params->set_callback(DownloadUrlParameters::OnStartedCallback()); | |
benjhayden
2013/01/08 16:41:17
This looks unnecessary.
Randy Smith (Not in Mondays)
2013/01/09 22:34:37
Done.
| |
922 | |
923 delegate_->ResumeInterruptedDownload(download_params.Pass(), GetGlobalId()); | |
924 | |
925 // Just in case we were interrupted while paused. | |
926 is_paused_ = false; | |
927 } | |
928 | |
784 void DownloadItemImpl::NotifyRemoved() { | 929 void DownloadItemImpl::NotifyRemoved() { |
785 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this)); | 930 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this)); |
786 } | 931 } |
787 | 932 |
788 void DownloadItemImpl::OnDownloadedFileRemoved() { | 933 void DownloadItemImpl::OnDownloadedFileRemoved() { |
789 file_externally_removed_ = true; | 934 file_externally_removed_ = true; |
790 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 935 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
791 UpdateObservers(); | 936 UpdateObservers(); |
792 } | 937 } |
793 | 938 |
794 base::WeakPtr<DownloadDestinationObserver> | 939 base::WeakPtr<DownloadDestinationObserver> |
795 DownloadItemImpl::DestinationObserverAsWeakPtr() { | 940 DownloadItemImpl::DestinationObserverAsWeakPtr() { |
796 return weak_ptr_factory_.GetWeakPtr(); | 941 return weak_ptr_factory_.GetWeakPtr(); |
797 } | 942 } |
798 | 943 |
944 const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const { | |
945 return bound_net_log_; | |
946 } | |
947 | |
799 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { | 948 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { |
800 total_bytes_ = total_bytes; | 949 total_bytes_ = total_bytes; |
801 } | 950 } |
802 | 951 |
803 // Updates from the download thread may have been posted while this download | 952 // Updates from the download thread may have been posted while this download |
804 // was being cancelled in the UI thread, so we'll accept them unless we're | 953 // was being cancelled in the UI thread, so we'll accept them unless we're |
805 // complete. | 954 // complete. |
806 void DownloadItemImpl::UpdateProgress(int64 bytes_so_far, | 955 void DownloadItemImpl::UpdateProgress(int64 bytes_so_far, |
807 int64 bytes_per_sec, | 956 int64 bytes_per_sec, |
808 const std::string& hash_state) { | 957 const std::string& hash_state) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
945 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); | 1094 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); |
946 } else { | 1095 } else { |
947 bound_net_log_.AddEvent( | 1096 bound_net_log_.AddEvent( |
948 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); | 1097 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); |
949 } | 1098 } |
950 | 1099 |
951 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 1100 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
952 } | 1101 } |
953 | 1102 |
954 // We're starting the download. | 1103 // We're starting the download. |
955 void DownloadItemImpl::Start(scoped_ptr<DownloadFile> file) { | 1104 void DownloadItemImpl::Start( |
1105 scoped_ptr<DownloadFile> file, | |
1106 scoped_ptr<DownloadRequestHandleInterface> req_handle) { | |
1107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
956 DCHECK(!download_file_.get()); | 1108 DCHECK(!download_file_.get()); |
957 DCHECK(file.get()); | 1109 DCHECK(file.get()); |
1110 DCHECK(req_handle.get()); | |
1111 | |
958 download_file_ = file.Pass(); | 1112 download_file_ = file.Pass(); |
1113 request_handle_ = req_handle.Pass(); | |
1114 | |
1115 TransitionTo(IN_PROGRESS_INTERNAL); | |
1116 | |
1117 last_reason_ = DOWNLOAD_INTERRUPT_REASON_NONE; | |
959 | 1118 |
960 BrowserThread::PostTask( | 1119 BrowserThread::PostTask( |
961 BrowserThread::FILE, FROM_HERE, | 1120 BrowserThread::FILE, FROM_HERE, |
962 base::Bind(&DownloadFile::Initialize, | 1121 base::Bind(&DownloadFile::Initialize, |
963 // Safe because we control download file lifetime. | 1122 // Safe because we control download file lifetime. |
964 base::Unretained(download_file_.get()), | 1123 base::Unretained(download_file_.get()), |
965 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, | 1124 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, |
966 weak_ptr_factory_.GetWeakPtr()))); | 1125 weak_ptr_factory_.GetWeakPtr()))); |
967 } | 1126 } |
968 | 1127 |
969 void DownloadItemImpl::OnDownloadFileInitialized( | 1128 void DownloadItemImpl::OnDownloadFileInitialized( |
970 DownloadInterruptReason result) { | 1129 DownloadInterruptReason result) { |
1130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
971 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { | 1131 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { |
972 Interrupt(result); | 1132 Interrupt(result); |
973 // TODO(rdsmith): It makes no sense to continue along the | 1133 // TODO(rdsmith): It makes no sense to continue along the |
974 // regular download path after we've gotten an error. But it's | 1134 // regular download path after we've gotten an error. But it's |
975 // the way the code has historically worked, and this allows us | 1135 // the way the code has historically worked, and this allows us |
976 // to get the download persisted and observers of the download manager | 1136 // to get the download persisted and observers of the download manager |
977 // notified, so tests work. When we execute all side effects of cancel | 1137 // notified, so tests work. When we execute all side effects of cancel |
978 // (including queue removal) immedately rather than waiting for | 1138 // (including queue removal) immediately rather than waiting for |
979 // persistence we should replace this comment with a "return;". | 1139 // persistence we should replace this comment with a "return;". |
980 } | 1140 } |
981 | 1141 |
1142 // If we're resuming an interrupted download, we may already know | |
1143 // the download target so we can skip target name determination. | |
1144 if (!GetTargetFilePath().empty() && !GetFullPath().empty()) { | |
1145 delegate_->ShowDownloadInBrowser(this); | |
1146 MaybeCompleteDownload(); | |
1147 return; | |
1148 } | |
1149 | |
1150 // The target path might be set and the full path empty if we failed | |
1151 // the intermediate rename--re-do file name determination in this case. | |
1152 // TODO(rdsmith,asanka): Clean up this logic. | |
1153 target_path_ = FilePath(); | |
1154 | |
982 delegate_->DetermineDownloadTarget( | 1155 delegate_->DetermineDownloadTarget( |
983 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, | 1156 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, |
984 weak_ptr_factory_.GetWeakPtr())); | 1157 weak_ptr_factory_.GetWeakPtr())); |
985 } | 1158 } |
986 | 1159 |
987 // Called by delegate_ when the download target path has been | 1160 // Called by delegate_ when the download target path has been |
988 // determined. | 1161 // determined. |
989 void DownloadItemImpl::OnDownloadTargetDetermined( | 1162 void DownloadItemImpl::OnDownloadTargetDetermined( |
990 const FilePath& target_path, | 1163 const FilePath& target_path, |
991 TargetDisposition disposition, | 1164 TargetDisposition disposition, |
992 DownloadDangerType danger_type, | 1165 DownloadDangerType danger_type, |
993 const FilePath& intermediate_path) { | 1166 const FilePath& intermediate_path) { |
994 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
995 | 1168 |
996 // If the |target_path| is empty, then we consider this download to be | 1169 // If the |target_path| is empty, then we consider this download to be |
997 // canceled. | 1170 // canceled. |
998 if (target_path.empty()) { | 1171 if (target_path.empty()) { |
999 Cancel(true); | 1172 Cancel(true); |
1000 return; | 1173 return; |
1001 } | 1174 } |
1002 | 1175 |
1176 // TODO(rdsmith,asanka): We are ignoring the possibility that the download | |
1177 // has been interrupted at this point until we finish the intermediate | |
1178 // rename and set the full path. That's dangerous, because we might race | |
1179 // with resumption, either manual (because the interrupt is visible to the | |
1180 // UI) or automatic. If we keep the "ignore an error on download until file | |
1181 // name determination complete" semantics, we need to make sure that the | |
1182 // error is kept completely invisible until that point. | |
1183 | |
1003 VLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition | 1184 VLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition |
1004 << " " << danger_type << " " << DebugString(true); | 1185 << " " << danger_type << " " << DebugString(true); |
1005 | 1186 |
1006 target_path_ = target_path; | 1187 target_path_ = target_path; |
1007 target_disposition_ = disposition; | 1188 target_disposition_ = disposition; |
1008 SetDangerType(danger_type); | 1189 SetDangerType(danger_type); |
1009 // TODO(asanka): SetDangerType() doesn't need to send a notification here. | 1190 // TODO(asanka): SetDangerType() doesn't need to send a notification here. |
1010 | 1191 |
1011 // We want the intermediate and target paths to refer to the same directory so | 1192 // We want the intermediate and target paths to refer to the same directory so |
1012 // that they are both on the same device and subject to same | 1193 // that they are both on the same device and subject to same |
1013 // space/permission/availability constraints. | 1194 // space/permission/availability constraints. |
1014 DCHECK(intermediate_path.DirName() == target_path.DirName()); | 1195 DCHECK(intermediate_path.DirName() == target_path.DirName()); |
1015 | 1196 |
1016 if (state_ != IN_PROGRESS_INTERNAL) { | |
benjhayden
2013/01/08 16:41:17
Why was this removed?
Randy Smith (Not in Mondays)
2013/01/09 22:34:37
So as you're probably aware, the interaction betwe
| |
1017 // If we've been cancelled or interrupted while the target was being | |
1018 // determined, continue the cascade with a null name. | |
1019 // The error doesn't matter as the cause of download stoppage | |
1020 // will already have been recorded and will be retained, but we use | |
1021 // whatever was recorded for consistency. | |
1022 OnDownloadRenamedToIntermediateName(last_reason_, FilePath()); | |
1023 return; | |
1024 } | |
1025 | |
1026 // Rename to intermediate name. | 1197 // Rename to intermediate name. |
1027 // TODO(asanka): Skip this rename if AllDataSaved() is true. This avoids a | 1198 // TODO(asanka): Skip this rename if AllDataSaved() is true. This avoids a |
1028 // spurious rename when we can just rename to the final | 1199 // spurious rename when we can just rename to the final |
1029 // filename. Unnecessary renames may cause bugs like | 1200 // filename. Unnecessary renames may cause bugs like |
1030 // http://crbug.com/74187. | 1201 // http://crbug.com/74187. |
1031 DCHECK(!is_save_package_download_); | 1202 DCHECK(!is_save_package_download_); |
1032 DCHECK(download_file_.get()); | 1203 DCHECK(download_file_.get()); |
1033 DownloadFile::RenameCompletionCallback callback = | 1204 DownloadFile::RenameCompletionCallback callback = |
1034 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName, | 1205 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName, |
1035 weak_ptr_factory_.GetWeakPtr()); | 1206 weak_ptr_factory_.GetWeakPtr()); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1167 if (delegate_->ShouldOpenDownload( | 1338 if (delegate_->ShouldOpenDownload( |
1168 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, | 1339 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, |
1169 weak_ptr_factory_.GetWeakPtr()))) { | 1340 weak_ptr_factory_.GetWeakPtr()))) { |
1170 Completed(); | 1341 Completed(); |
1171 } else { | 1342 } else { |
1172 delegate_delayed_complete_ = true; | 1343 delegate_delayed_complete_ = true; |
1173 } | 1344 } |
1174 } | 1345 } |
1175 | 1346 |
1176 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { | 1347 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { |
1348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1349 | |
1177 auto_opened_ = auto_opened; | 1350 auto_opened_ = auto_opened; |
1178 Completed(); | 1351 Completed(); |
1179 } | 1352 } |
1180 | 1353 |
1181 void DownloadItemImpl::Completed() { | 1354 void DownloadItemImpl::Completed() { |
1182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1183 | 1356 |
1184 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 1357 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
1185 | 1358 |
1186 DCHECK(all_data_saved_); | 1359 DCHECK(all_data_saved_); |
(...skipping 14 matching lines...) Expand all Loading... | |
1201 | 1374 |
1202 auto_opened_ = true; | 1375 auto_opened_ = true; |
1203 UpdateObservers(); | 1376 UpdateObservers(); |
1204 } | 1377 } |
1205 } | 1378 } |
1206 | 1379 |
1207 // **** End of Download progression cascade | 1380 // **** End of Download progression cascade |
1208 | 1381 |
1209 // An error occurred somewhere. | 1382 // An error occurred somewhere. |
1210 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { | 1383 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { |
1384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1385 | |
1211 // Somewhat counter-intuitively, it is possible for us to receive an | 1386 // Somewhat counter-intuitively, it is possible for us to receive an |
1212 // interrupt after we've already been interrupted. The generation of | 1387 // interrupt after we've already been interrupted. The generation of |
1213 // interrupts from the file thread Renames and the generation of | 1388 // interrupts from the file thread Renames and the generation of |
1214 // interrupts from disk writes go through two different mechanisms (driven | 1389 // interrupts from disk writes go through two different mechanisms (driven |
1215 // by rename requests from UI thread and by write requests from IO thread, | 1390 // by rename requests from UI thread and by write requests from IO thread, |
1216 // respectively), and since we choose not to keep state on the File thread, | 1391 // respectively), and since we choose not to keep state on the File thread, |
1217 // this is the place where the races collide. It's also possible for | 1392 // this is the place where the races collide. It's also possible for |
1218 // interrupts to race with cancels. | 1393 // interrupts to race with cancels. |
1219 | 1394 |
1220 // Whatever happens, the first one to hit the UI thread wins. | 1395 // Whatever happens, the first one to hit the UI thread wins. |
1221 if (state_ != IN_PROGRESS_INTERNAL) | 1396 if (state_ != IN_PROGRESS_INTERNAL) |
1222 return; | 1397 return; |
1223 | 1398 |
1224 last_reason_ = reason; | 1399 last_reason_ = reason; |
1400 | |
1225 TransitionTo(INTERRUPTED_INTERNAL); | 1401 TransitionTo(INTERRUPTED_INTERNAL); |
1226 | 1402 |
1227 CancelDownloadFile(); | 1403 ResumeMode resume_mode = GetResumeMode(); |
1404 if (resume_mode == RESUME_MODE_IMMEDIATE_RESTART || | |
1405 resume_mode == RESUME_MODE_USER_RESTART) { | |
1406 // Remove the download file; no point in leaving data around we | |
1407 // aren't going to use. | |
1408 CancelDownloadFile(); | |
1409 } else { | |
1410 // Keep the file around and maybe re-use it. | |
1411 BrowserThread::PostTask( | |
1412 BrowserThread::FILE, FROM_HERE, | |
1413 base::Bind(&DownloadFileDetach, base::Passed(&download_file_))); | |
1414 } | |
1228 | 1415 |
1229 // Cancel the originating URL request. | 1416 // Cancel the originating URL request. |
1230 request_handle_->CancelRequest(); | 1417 request_handle_->CancelRequest(); |
1231 | 1418 |
1232 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); | 1419 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); |
1420 AutoResumeIfValid(); | |
1233 } | 1421 } |
1234 | 1422 |
1235 void DownloadItemImpl::CancelDownloadFile() { | 1423 void DownloadItemImpl::CancelDownloadFile() { |
1424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1425 | |
1236 // TODO(rdsmith/benjhayden): Remove condition as part of | 1426 // TODO(rdsmith/benjhayden): Remove condition as part of |
1237 // SavePackage integration. | 1427 // |SavePackage| integration. |
1238 // download_file_ can be NULL if Interrupt() is called after the download file | 1428 // |download_file_| can be NULL if Interrupt() is called after the |
1239 // has been released. | 1429 // download file has been released. |
1240 if (!is_save_package_download_ && download_file_.get()) { | 1430 if (!is_save_package_download_ && download_file_.get()) { |
1241 BrowserThread::PostTask( | 1431 BrowserThread::PostTask( |
1242 BrowserThread::FILE, FROM_HERE, | 1432 BrowserThread::FILE, FROM_HERE, |
1243 // Will be deleted at end of task execution. | 1433 // Will be deleted at end of task execution. |
1244 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); | 1434 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); |
1245 } | 1435 } |
1246 } | 1436 } |
1247 | 1437 |
1248 bool DownloadItemImpl::IsDownloadReadyForCompletion( | 1438 bool DownloadItemImpl::IsDownloadReadyForCompletion( |
1249 const base::Closure& state_change_notification) { | 1439 const base::Closure& state_change_notification) { |
(...skipping 24 matching lines...) Expand all Loading... | |
1274 | 1464 |
1275 // Give the delegate a chance to hold up a stop sign. It'll call | 1465 // Give the delegate a chance to hold up a stop sign. It'll call |
1276 // use back through the passed callback if it does and that state changes. | 1466 // use back through the passed callback if it does and that state changes. |
1277 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) | 1467 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) |
1278 return false; | 1468 return false; |
1279 | 1469 |
1280 return true; | 1470 return true; |
1281 } | 1471 } |
1282 | 1472 |
1283 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state) { | 1473 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state) { |
1474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1475 | |
1284 if (state_ == new_state) | 1476 if (state_ == new_state) |
1285 return; | 1477 return; |
1286 | 1478 |
1287 DownloadInternalState old_state = state_; | 1479 DownloadInternalState old_state = state_; |
1288 state_ = new_state; | 1480 state_ = new_state; |
1289 | 1481 |
1290 switch (state_) { | 1482 switch (state_) { |
1291 case COMPLETING_INTERNAL: | 1483 case COMPLETING_INTERNAL: |
1292 bound_net_log_.AddEvent( | 1484 bound_net_log_.AddEvent( |
1293 net::NetLog::TYPE_DOWNLOAD_ITEM_COMPLETING, | 1485 net::NetLog::TYPE_DOWNLOAD_ITEM_COMPLETING, |
1294 base::Bind(&ItemCompletingNetLogCallback, received_bytes_, &hash_)); | 1486 base::Bind(&ItemCompletingNetLogCallback, received_bytes_, &hash_)); |
1295 break; | 1487 break; |
1296 case COMPLETE_INTERNAL: | 1488 case COMPLETE_INTERNAL: |
1297 bound_net_log_.AddEvent( | 1489 bound_net_log_.AddEvent( |
1298 net::NetLog::TYPE_DOWNLOAD_ITEM_FINISHED, | 1490 net::NetLog::TYPE_DOWNLOAD_ITEM_FINISHED, |
1299 base::Bind(&ItemFinishedNetLogCallback, auto_opened_)); | 1491 base::Bind(&ItemFinishedNetLogCallback, auto_opened_)); |
1300 break; | 1492 break; |
1301 case INTERRUPTED_INTERNAL: | 1493 case INTERRUPTED_INTERNAL: |
1302 bound_net_log_.AddEvent( | 1494 bound_net_log_.AddEvent( |
1303 net::NetLog::TYPE_DOWNLOAD_ITEM_INTERRUPTED, | 1495 net::NetLog::TYPE_DOWNLOAD_ITEM_INTERRUPTED, |
1304 base::Bind(&ItemInterruptedNetLogCallback, last_reason_, | 1496 base::Bind(&ItemInterruptedNetLogCallback, last_reason_, |
1305 received_bytes_, &hash_state_)); | 1497 received_bytes_, &hash_state_)); |
1306 break; | 1498 break; |
1499 case IN_PROGRESS_INTERNAL: | |
1500 if (old_state == INTERRUPTED_INTERNAL) { | |
1501 bound_net_log_.AddEvent( | |
1502 net::NetLog::TYPE_DOWNLOAD_ITEM_RESUMED, | |
1503 base::Bind(&ItemResumingNetLogCallback, | |
1504 false, last_reason_, received_bytes_, &hash_state_)); | |
1505 } | |
1506 break; | |
1307 case CANCELLED_INTERNAL: | 1507 case CANCELLED_INTERNAL: |
1308 bound_net_log_.AddEvent( | 1508 bound_net_log_.AddEvent( |
1309 net::NetLog::TYPE_DOWNLOAD_ITEM_CANCELED, | 1509 net::NetLog::TYPE_DOWNLOAD_ITEM_CANCELED, |
1310 base::Bind(&ItemCanceledNetLogCallback, received_bytes_, | 1510 base::Bind(&ItemCanceledNetLogCallback, received_bytes_, |
1311 &hash_state_)); | 1511 &hash_state_)); |
1312 break; | 1512 break; |
1313 default: | 1513 default: |
1314 break; | 1514 break; |
1315 } | 1515 } |
1316 | 1516 |
1317 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true) | 1517 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true) |
1318 << " " << InternalToExternalState(old_state) | 1518 << " " << InternalToExternalState(old_state) |
1319 << " " << InternalToExternalState(state_); | 1519 << " " << InternalToExternalState(state_); |
1320 | 1520 |
1321 // Only update observers on user visible state changes. | 1521 // Only update observers on user visible state changes. |
1322 if (InternalToExternalState(old_state) != InternalToExternalState(state_)) | 1522 if (InternalToExternalState(state_) != InternalToExternalState(old_state)) |
1323 UpdateObservers(); | 1523 UpdateObservers(); |
1324 | 1524 |
1325 bool is_done = (state_ != IN_PROGRESS_INTERNAL && | 1525 bool is_done = (state_ != IN_PROGRESS_INTERNAL && |
1326 state_ != COMPLETING_INTERNAL); | 1526 state_ != COMPLETING_INTERNAL); |
1327 bool was_done = (old_state != IN_PROGRESS_INTERNAL && | 1527 bool was_done = (old_state != IN_PROGRESS_INTERNAL && |
1328 old_state != COMPLETING_INTERNAL); | 1528 old_state != COMPLETING_INTERNAL); |
1529 // Termination | |
1329 if (is_done && !was_done) | 1530 if (is_done && !was_done) |
1330 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); | 1531 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); |
1532 | |
1533 // Resumption | |
1534 if (was_done && !is_done) { | |
1535 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); | |
1536 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, | |
1537 base::Bind(&ItemActivatedNetLogCallback, | |
1538 this, SRC_ACTIVE_DOWNLOAD, | |
1539 &file_name)); | |
1540 } | |
1331 } | 1541 } |
1332 | 1542 |
1333 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) { | 1543 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) { |
1334 danger_type_ = danger_type; | 1544 danger_type_ = danger_type; |
1335 // Notify observers if the safety state has changed as a result of the new | 1545 // Notify observers if the safety state has changed as a result of the new |
1336 // danger type. | 1546 // danger type. |
1337 SafetyState updated_value = IsDangerous() ? | 1547 SafetyState updated_value = IsDangerous() ? |
1338 DownloadItem::DANGEROUS : DownloadItem::SAFE; | 1548 DownloadItem::DANGEROUS : DownloadItem::SAFE; |
1339 if (updated_value != safety_state_) { | 1549 if (updated_value != safety_state_) { |
1340 safety_state_ = updated_value; | 1550 safety_state_ = updated_value; |
(...skipping 12 matching lines...) Expand all Loading... | |
1353 DCHECK(!new_path.empty()); | 1563 DCHECK(!new_path.empty()); |
1354 | 1564 |
1355 bound_net_log_.AddEvent( | 1565 bound_net_log_.AddEvent( |
1356 net::NetLog::TYPE_DOWNLOAD_ITEM_RENAMED, | 1566 net::NetLog::TYPE_DOWNLOAD_ITEM_RENAMED, |
1357 base::Bind(&ItemRenamedNetLogCallback, ¤t_path_, &new_path)); | 1567 base::Bind(&ItemRenamedNetLogCallback, ¤t_path_, &new_path)); |
1358 | 1568 |
1359 current_path_ = new_path; | 1569 current_path_ = new_path; |
1360 UpdateObservers(); | 1570 UpdateObservers(); |
1361 } | 1571 } |
1362 | 1572 |
1573 void DownloadItemImpl::AutoResumeIfValid() { | |
1574 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true); | |
1575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1576 ResumeMode mode = GetResumeMode(); | |
1577 | |
1578 if (mode != RESUME_MODE_IMMEDIATE_RESTART && | |
1579 mode != RESUME_MODE_IMMEDIATE_CONTINUE) { | |
1580 return; | |
1581 } | |
1582 | |
1583 auto_resume_count_++; | |
1584 | |
1585 ResumeInterruptedDownload(); | |
1586 } | |
1587 | |
1363 // static | 1588 // static |
1364 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( | 1589 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( |
1365 DownloadInternalState internal_state) { | 1590 DownloadInternalState internal_state) { |
1366 switch (internal_state) { | 1591 switch (internal_state) { |
1367 case IN_PROGRESS_INTERNAL: | 1592 case IN_PROGRESS_INTERNAL: |
1368 return IN_PROGRESS; | 1593 return IN_PROGRESS; |
1369 case COMPLETING_INTERNAL: | 1594 case COMPLETING_INTERNAL: |
1370 return IN_PROGRESS; | 1595 return IN_PROGRESS; |
1371 case COMPLETE_INTERNAL: | 1596 case COMPLETE_INTERNAL: |
1372 return COMPLETE; | 1597 return COMPLETE; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1411 case CANCELLED_INTERNAL: | 1636 case CANCELLED_INTERNAL: |
1412 return "CANCELLED"; | 1637 return "CANCELLED"; |
1413 case INTERRUPTED_INTERNAL: | 1638 case INTERRUPTED_INTERNAL: |
1414 return "INTERRUPTED"; | 1639 return "INTERRUPTED"; |
1415 default: | 1640 default: |
1416 NOTREACHED() << "Unknown download state " << state; | 1641 NOTREACHED() << "Unknown download state " << state; |
1417 return "unknown"; | 1642 return "unknown"; |
1418 }; | 1643 }; |
1419 } | 1644 } |
1420 | 1645 |
1646 const char* DownloadItemImpl::DebugResumeModeString(ResumeMode mode) { | |
1647 switch (mode) { | |
1648 case RESUME_MODE_INVALID: | |
1649 return "INVALID"; | |
1650 case RESUME_MODE_IMMEDIATE_CONTINUE: | |
1651 return "IMMEDIATE_CONTINUE"; | |
1652 case RESUME_MODE_IMMEDIATE_RESTART: | |
1653 return "IMMEDIATE_RESTART"; | |
1654 case RESUME_MODE_USER_CONTINUE: | |
1655 return "USER_CONTINUE"; | |
1656 case RESUME_MODE_USER_RESTART: | |
1657 return "USER_RESTART"; | |
1658 } | |
1659 NOTREACHED() << "Unknown resume mode " << mode; | |
1660 return "unknown"; | |
1661 } | |
1662 | |
1421 } // namespace content | 1663 } // namespace content |
OLD | NEW |