| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 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_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
| 20 #include "content/browser/download/download_file.h" | 20 #include "content/browser/download/download_file.h" |
| 21 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
| 22 #include "content/browser/download/download_id.h" | 22 #include "content/browser/download/download_id.h" |
| 23 #include "content/browser/download/download_manager.h" | |
| 24 #include "content/browser/download/download_persistent_store_info.h" | 23 #include "content/browser/download/download_persistent_store_info.h" |
| 25 #include "content/browser/download/download_request_handle.h" | 24 #include "content/browser/download/download_request_handle.h" |
| 26 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
| 27 #include "content/browser/download/interrupt_reasons.h" | 26 #include "content/browser/download/interrupt_reasons.h" |
| 28 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
| 29 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
| 31 #include "content/public/browser/download_manager_delegate.h" | |
| 32 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
| 33 | 31 |
| 34 using content::BrowserThread; | 32 using content::BrowserThread; |
| 35 | 33 |
| 36 // A DownloadItem normally goes through the following states: | 34 // A DownloadItem normally goes through the following states: |
| 37 // * Created (when download starts) | 35 // * Created (when download starts) |
| 38 // * Made visible to consumers (e.g. Javascript) after the | 36 // * Made visible to consumers (e.g. Javascript) after the |
| 39 // destination file has been determined. | 37 // destination file has been determined. |
| 40 // * Entered into the history database. | 38 // * Entered into the history database. |
| 41 // * Made visible in the download shelf. | 39 // * Made visible in the download shelf. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 }; | 115 }; |
| 118 | 116 |
| 119 } // namespace | 117 } // namespace |
| 120 | 118 |
| 121 // Our download table ID starts at 1, so we use 0 to represent a download that | 119 // Our download table ID starts at 1, so we use 0 to represent a download that |
| 122 // has started, but has not yet had its data persisted in the table. We use fake | 120 // has started, but has not yet had its data persisted in the table. We use fake |
| 123 // database handles in incognito mode starting at -1 and progressively getting | 121 // database handles in incognito mode starting at -1 and progressively getting |
| 124 // more negative. | 122 // more negative. |
| 125 | 123 |
| 126 // Constructor for reading from the history service. | 124 // Constructor for reading from the history service. |
| 127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 125 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
| 126 DownloadId download_id, |
| 128 const DownloadPersistentStoreInfo& info) | 127 const DownloadPersistentStoreInfo& info) |
| 129 : download_id_(download_manager->GetNextId()), | 128 : download_id_(download_id), |
| 130 full_path_(info.path), | 129 full_path_(info.path), |
| 131 url_chain_(1, info.url), | 130 url_chain_(1, info.url), |
| 132 referrer_url_(info.referrer_url), | 131 referrer_url_(info.referrer_url), |
| 133 total_bytes_(info.total_bytes), | 132 total_bytes_(info.total_bytes), |
| 134 received_bytes_(info.received_bytes), | 133 received_bytes_(info.received_bytes), |
| 135 bytes_per_sec_(0), | 134 bytes_per_sec_(0), |
| 136 start_tick_(base::TimeTicks()), | 135 start_tick_(base::TimeTicks()), |
| 137 state_(static_cast<DownloadState>(info.state)), | 136 state_(static_cast<DownloadState>(info.state)), |
| 138 start_time_(info.start_time), | 137 start_time_(info.start_time), |
| 139 end_time_(info.end_time), | 138 end_time_(info.end_time), |
| 140 db_handle_(info.db_handle), | 139 db_handle_(info.db_handle), |
| 141 download_manager_(download_manager), | 140 delegate_(delegate), |
| 142 is_paused_(false), | 141 is_paused_(false), |
| 143 open_when_complete_(false), | 142 open_when_complete_(false), |
| 144 file_externally_removed_(false), | 143 file_externally_removed_(false), |
| 145 safety_state_(SAFE), | 144 safety_state_(SAFE), |
| 146 auto_opened_(false), | 145 auto_opened_(false), |
| 147 is_otr_(false), | 146 is_otr_(false), |
| 148 is_temporary_(false), | 147 is_temporary_(false), |
| 149 all_data_saved_(false), | 148 all_data_saved_(false), |
| 150 opened_(info.opened), | 149 opened_(info.opened), |
| 151 open_enabled_(true), | 150 open_enabled_(true), |
| 152 delegate_delayed_complete_(false) { | 151 delegate_delayed_complete_(false) { |
| 153 if (IsInProgress()) | 152 if (IsInProgress()) |
| 154 state_ = CANCELLED; | 153 state_ = CANCELLED; |
| 155 if (IsComplete()) | 154 if (IsComplete()) |
| 156 all_data_saved_ = true; | 155 all_data_saved_ = true; |
| 157 Init(false /* not actively downloading */); | 156 Init(false /* not actively downloading */); |
| 158 } | 157 } |
| 159 | 158 |
| 160 // Constructing for a regular download: | 159 // Constructing for a regular download: |
| 161 DownloadItemImpl::DownloadItemImpl( | 160 DownloadItemImpl::DownloadItemImpl( |
| 162 DownloadManager* download_manager, | 161 Delegate* delegate, |
| 163 const DownloadCreateInfo& info, | 162 const DownloadCreateInfo& info, |
| 164 DownloadRequestHandleInterface* request_handle, | 163 DownloadRequestHandleInterface* request_handle, |
| 165 bool is_otr) | 164 bool is_otr) |
| 166 : state_info_(info.original_name, info.save_info.file_path, | 165 : state_info_(info.original_name, info.save_info.file_path, |
| 167 info.has_user_gesture, info.transition_type, | 166 info.has_user_gesture, info.transition_type, |
| 168 info.prompt_user_for_save_location, info.path_uniquifier, | 167 info.prompt_user_for_save_location, info.path_uniquifier, |
| 169 DownloadStateInfo::NOT_DANGEROUS), | 168 DownloadStateInfo::NOT_DANGEROUS), |
| 170 request_handle_(request_handle), | 169 request_handle_(request_handle), |
| 171 download_id_(info.download_id), | 170 download_id_(info.download_id), |
| 172 full_path_(info.path), | 171 full_path_(info.path), |
| 173 url_chain_(info.url_chain), | 172 url_chain_(info.url_chain), |
| 174 referrer_url_(info.referrer_url), | 173 referrer_url_(info.referrer_url), |
| 175 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 174 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
| 176 content_disposition_(info.content_disposition), | 175 content_disposition_(info.content_disposition), |
| 177 mime_type_(info.mime_type), | 176 mime_type_(info.mime_type), |
| 178 original_mime_type_(info.original_mime_type), | 177 original_mime_type_(info.original_mime_type), |
| 179 referrer_charset_(info.referrer_charset), | 178 referrer_charset_(info.referrer_charset), |
| 180 total_bytes_(info.total_bytes), | 179 total_bytes_(info.total_bytes), |
| 181 received_bytes_(0), | 180 received_bytes_(0), |
| 182 bytes_per_sec_(0), | 181 bytes_per_sec_(0), |
| 183 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 182 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 184 start_tick_(base::TimeTicks::Now()), | 183 start_tick_(base::TimeTicks::Now()), |
| 185 state_(IN_PROGRESS), | 184 state_(IN_PROGRESS), |
| 186 start_time_(info.start_time), | 185 start_time_(info.start_time), |
| 187 db_handle_(DownloadItem::kUninitializedHandle), | 186 db_handle_(DownloadItem::kUninitializedHandle), |
| 188 download_manager_(download_manager), | 187 delegate_(delegate), |
| 189 is_paused_(false), | 188 is_paused_(false), |
| 190 open_when_complete_(false), | 189 open_when_complete_(false), |
| 191 file_externally_removed_(false), | 190 file_externally_removed_(false), |
| 192 safety_state_(SAFE), | 191 safety_state_(SAFE), |
| 193 auto_opened_(false), | 192 auto_opened_(false), |
| 194 is_otr_(is_otr), | 193 is_otr_(is_otr), |
| 195 is_temporary_(!info.save_info.file_path.empty()), | 194 is_temporary_(!info.save_info.file_path.empty()), |
| 196 all_data_saved_(false), | 195 all_data_saved_(false), |
| 197 opened_(false), | 196 opened_(false), |
| 198 open_enabled_(true), | 197 open_enabled_(true), |
| 199 delegate_delayed_complete_(false) { | 198 delegate_delayed_complete_(false) { |
| 200 Init(true /* actively downloading */); | 199 Init(true /* actively downloading */); |
| 201 } | 200 } |
| 202 | 201 |
| 203 // Constructing for the "Save Page As..." feature: | 202 // Constructing for the "Save Page As..." feature: |
| 204 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 203 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
| 205 const FilePath& path, | 204 const FilePath& path, |
| 206 const GURL& url, | 205 const GURL& url, |
| 207 bool is_otr, | 206 bool is_otr, |
| 208 DownloadId download_id) | 207 DownloadId download_id) |
| 209 : request_handle_(new NullDownloadRequestHandle()), | 208 : request_handle_(new NullDownloadRequestHandle()), |
| 210 download_id_(download_id), | 209 download_id_(download_id), |
| 211 full_path_(path), | 210 full_path_(path), |
| 212 url_chain_(1, url), | 211 url_chain_(1, url), |
| 213 referrer_url_(GURL()), | 212 referrer_url_(GURL()), |
| 214 total_bytes_(0), | 213 total_bytes_(0), |
| 215 received_bytes_(0), | 214 received_bytes_(0), |
| 216 bytes_per_sec_(0), | 215 bytes_per_sec_(0), |
| 217 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 216 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 218 start_tick_(base::TimeTicks::Now()), | 217 start_tick_(base::TimeTicks::Now()), |
| 219 state_(IN_PROGRESS), | 218 state_(IN_PROGRESS), |
| 220 start_time_(base::Time::Now()), | 219 start_time_(base::Time::Now()), |
| 221 db_handle_(DownloadItem::kUninitializedHandle), | 220 db_handle_(DownloadItem::kUninitializedHandle), |
| 222 download_manager_(download_manager), | 221 delegate_(delegate), |
| 223 is_paused_(false), | 222 is_paused_(false), |
| 224 open_when_complete_(false), | 223 open_when_complete_(false), |
| 225 file_externally_removed_(false), | 224 file_externally_removed_(false), |
| 226 safety_state_(SAFE), | 225 safety_state_(SAFE), |
| 227 auto_opened_(false), | 226 auto_opened_(false), |
| 228 is_otr_(is_otr), | 227 is_otr_(is_otr), |
| 229 is_temporary_(false), | 228 is_temporary_(false), |
| 230 all_data_saved_(false), | 229 all_data_saved_(false), |
| 231 opened_(false), | 230 opened_(false), |
| 232 open_enabled_(true), | 231 open_enabled_(true), |
| 233 delegate_delayed_complete_(false) { | 232 delegate_delayed_complete_(false) { |
| 234 Init(true /* actively downloading */); | 233 Init(true /* actively downloading */); |
| 235 } | 234 } |
| 236 | 235 |
| 237 DownloadItemImpl::~DownloadItemImpl() { | 236 DownloadItemImpl::~DownloadItemImpl() { |
| 238 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 237 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 239 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 238 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 240 | 239 |
| 241 TransitionTo(REMOVING); | 240 TransitionTo(REMOVING); |
| 242 download_manager_->AssertQueueStateConsistent(this); | 241 delegate_->AssertStateConsistent(this); |
| 243 } | 242 } |
| 244 | 243 |
| 245 void DownloadItemImpl::AddObserver(Observer* observer) { | 244 void DownloadItemImpl::AddObserver(Observer* observer) { |
| 246 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 245 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 247 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 246 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 248 | 247 |
| 249 observers_.AddObserver(observer); | 248 observers_.AddObserver(observer); |
| 250 } | 249 } |
| 251 | 250 |
| 252 void DownloadItemImpl::RemoveObserver(Observer* observer) { | 251 void DownloadItemImpl::RemoveObserver(Observer* observer) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 265 | 264 |
| 266 bool DownloadItemImpl::CanShowInFolder() { | 265 bool DownloadItemImpl::CanShowInFolder() { |
| 267 return !IsCancelled() && !file_externally_removed_; | 266 return !IsCancelled() && !file_externally_removed_; |
| 268 } | 267 } |
| 269 | 268 |
| 270 bool DownloadItemImpl::CanOpenDownload() { | 269 bool DownloadItemImpl::CanOpenDownload() { |
| 271 return !file_externally_removed_; | 270 return !file_externally_removed_; |
| 272 } | 271 } |
| 273 | 272 |
| 274 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { | 273 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { |
| 275 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( | 274 return delegate_->ShouldOpenFileBasedOnExtension(GetUserVerifiedFilePath()); |
| 276 GetUserVerifiedFilePath()); | |
| 277 } | 275 } |
| 278 | 276 |
| 279 void DownloadItemImpl::OpenDownload() { | 277 void DownloadItemImpl::OpenDownload() { |
| 280 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 278 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 281 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 279 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 282 | 280 |
| 283 if (IsPartialDownload()) { | 281 if (IsPartialDownload()) { |
| 284 open_when_complete_ = !open_when_complete_; | 282 open_when_complete_ = !open_when_complete_; |
| 285 return; | 283 return; |
| 286 } | 284 } |
| 287 | 285 |
| 288 if (!IsComplete() || file_externally_removed_) | 286 if (!IsComplete() || file_externally_removed_) |
| 289 return; | 287 return; |
| 290 | 288 |
| 291 // Ideally, we want to detect errors in opening and report them, but we | 289 // Ideally, we want to detect errors in opening and report them, but we |
| 292 // don't generally have the proper interface for that to the external | 290 // don't generally have the proper interface for that to the external |
| 293 // program that opens the file. So instead we spawn a check to update | 291 // program that opens the file. So instead we spawn a check to update |
| 294 // the UI if the file has been deleted in parallel with the open. | 292 // the UI if the file has been deleted in parallel with the open. |
| 295 download_manager_->CheckForFileRemoval(this); | 293 delegate_->CheckForFileRemoval(this); |
| 296 download_stats::RecordOpen(GetEndTime(), !GetOpened()); | 294 download_stats::RecordOpen(GetEndTime(), !GetOpened()); |
| 297 opened_ = true; | 295 opened_ = true; |
| 298 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 296 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
| 299 download_manager_->MarkDownloadOpened(this); | 297 delegate_->DownloadOpened(this); |
| 300 | 298 |
| 301 // For testing: If download opening is disabled on this item, | 299 // For testing: If download opening is disabled on this item, |
| 302 // make the rest of the routine a no-op. | 300 // make the rest of the routine a no-op. |
| 303 if (!open_enabled_) | 301 if (!open_enabled_) |
| 304 return; | 302 return; |
| 305 | 303 |
| 306 content::GetContentClient()->browser()->OpenItem(GetFullPath()); | 304 content::GetContentClient()->browser()->OpenItem(GetFullPath()); |
| 307 } | 305 } |
| 308 | 306 |
| 309 void DownloadItemImpl::ShowDownloadInShell() { | 307 void DownloadItemImpl::ShowDownloadInShell() { |
| 310 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 308 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 311 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 309 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 312 | 310 |
| 313 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); | 311 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); |
| 314 } | 312 } |
| 315 | 313 |
| 316 void DownloadItemImpl::DangerousDownloadValidated() { | 314 void DownloadItemImpl::DangerousDownloadValidated() { |
| 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 318 DCHECK_EQ(DANGEROUS, GetSafetyState()); | 316 DCHECK_EQ(DANGEROUS, GetSafetyState()); |
| 319 | 317 |
| 320 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 318 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
| 321 GetDangerType(), | 319 GetDangerType(), |
| 322 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 320 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
| 323 | 321 |
| 324 safety_state_ = DANGEROUS_BUT_VALIDATED; | 322 safety_state_ = DANGEROUS_BUT_VALIDATED; |
| 325 UpdateObservers(); | 323 UpdateObservers(); |
| 326 | 324 |
| 327 download_manager_->MaybeCompleteDownload(this); | 325 delegate_->MaybeCompleteDownload(this); |
| 328 } | 326 } |
| 329 | 327 |
| 330 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { | 328 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { |
| 331 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 329 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 333 | 331 |
| 334 received_bytes_ = bytes_so_far; | 332 received_bytes_ = bytes_so_far; |
| 335 | 333 |
| 336 // If we've received more data than we were expecting (bad server info?), | 334 // If we've received more data than we were expecting (bad server info?), |
| 337 // revert to 'unknown size mode'. | 335 // revert to 'unknown size mode'. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 368 if (!IsPartialDownload()) { | 366 if (!IsPartialDownload()) { |
| 369 // Small downloads might be complete before this method has | 367 // Small downloads might be complete before this method has |
| 370 // a chance to run. | 368 // a chance to run. |
| 371 return; | 369 return; |
| 372 } | 370 } |
| 373 | 371 |
| 374 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 372 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
| 375 | 373 |
| 376 TransitionTo(CANCELLED); | 374 TransitionTo(CANCELLED); |
| 377 if (user_cancel) | 375 if (user_cancel) |
| 378 download_manager_->DownloadCancelledInternal(this); | 376 delegate_->DownloadCancelled(this); |
| 379 } | 377 } |
| 380 | 378 |
| 381 void DownloadItemImpl::MarkAsComplete() { | 379 void DownloadItemImpl::MarkAsComplete() { |
| 382 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 380 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 383 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 381 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 384 | 382 |
| 385 DCHECK(all_data_saved_); | 383 DCHECK(all_data_saved_); |
| 386 end_time_ = base::Time::Now(); | 384 end_time_ = base::Time::Now(); |
| 387 TransitionTo(COMPLETE); | 385 TransitionTo(COMPLETE); |
| 388 } | 386 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 401 all_data_saved_ = true; | 399 all_data_saved_ = true; |
| 402 UpdateSize(size); | 400 UpdateSize(size); |
| 403 hash_ = final_hash; | 401 hash_ = final_hash; |
| 404 } | 402 } |
| 405 | 403 |
| 406 void DownloadItemImpl::OnDownloadedFileRemoved() { | 404 void DownloadItemImpl::OnDownloadedFileRemoved() { |
| 407 file_externally_removed_ = true; | 405 file_externally_removed_ = true; |
| 408 UpdateObservers(); | 406 UpdateObservers(); |
| 409 } | 407 } |
| 410 | 408 |
| 409 void DownloadItemImpl::MaybeCompleteDownload() { |
| 410 // TODO(rdsmith): Move logic for this function here. |
| 411 delegate_->MaybeCompleteDownload(this); |
| 412 } |
| 413 |
| 411 void DownloadItemImpl::Completed() { | 414 void DownloadItemImpl::Completed() { |
| 412 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 415 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 416 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 414 | 417 |
| 415 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 418 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
| 416 | 419 |
| 417 DCHECK(all_data_saved_); | 420 DCHECK(all_data_saved_); |
| 418 end_time_ = base::Time::Now(); | 421 end_time_ = base::Time::Now(); |
| 419 TransitionTo(COMPLETE); | 422 TransitionTo(COMPLETE); |
| 420 download_manager_->DownloadCompleted(GetId()); | 423 delegate_->DownloadCompleted(this); |
| 421 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); | 424 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
| 422 | 425 |
| 423 if (auto_opened_) { | 426 if (auto_opened_) { |
| 424 // If it was already handled by the delegate, do nothing. | 427 // If it was already handled by the delegate, do nothing. |
| 425 } else if (GetOpenWhenComplete() || | 428 } else if (GetOpenWhenComplete() || |
| 426 ShouldOpenFileBasedOnExtension() || | 429 ShouldOpenFileBasedOnExtension() || |
| 427 IsTemporary()) { | 430 IsTemporary()) { |
| 428 // If the download is temporary, like in drag-and-drop, do not open it but | 431 // If the download is temporary, like in drag-and-drop, do not open it but |
| 429 // we still need to set it auto-opened so that it can be removed from the | 432 // we still need to set it auto-opened so that it can be removed from the |
| 430 // download shelf. | 433 // download shelf. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 499 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 497 base::Bind(&DeleteDownloadedFile, full_path_)); | 500 base::Bind(&DeleteDownloadedFile, full_path_)); |
| 498 Remove(); | 501 Remove(); |
| 499 // We have now been deleted. | 502 // We have now been deleted. |
| 500 } | 503 } |
| 501 | 504 |
| 502 void DownloadItemImpl::Remove() { | 505 void DownloadItemImpl::Remove() { |
| 503 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 506 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 504 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 507 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 505 | 508 |
| 506 download_manager_->AssertQueueStateConsistent(this); | 509 delegate_->AssertStateConsistent(this); |
| 507 Cancel(true); | 510 Cancel(true); |
| 508 download_manager_->AssertQueueStateConsistent(this); | 511 delegate_->AssertStateConsistent(this); |
| 509 | 512 |
| 510 TransitionTo(REMOVING); | 513 TransitionTo(REMOVING); |
| 511 download_manager_->RemoveDownload(db_handle_); | 514 delegate_->DownloadRemoved(this); |
| 512 // We have now been deleted. | 515 // We have now been deleted. |
| 513 } | 516 } |
| 514 | 517 |
| 515 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { | 518 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { |
| 516 if (total_bytes_ <= 0) | 519 if (total_bytes_ <= 0) |
| 517 return false; // We never received the content_length for this download. | 520 return false; // We never received the content_length for this download. |
| 518 | 521 |
| 519 int64 speed = CurrentSpeed(); | 522 int64 speed = CurrentSpeed(); |
| 520 if (speed == 0) | 523 if (speed == 0) |
| 521 return false; | 524 return false; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 | 578 |
| 576 VLOG(20) << __FUNCTION__ << "()" | 579 VLOG(20) << __FUNCTION__ << "()" |
| 577 << " needs rename = " << NeedsRename() | 580 << " needs rename = " << NeedsRename() |
| 578 << " " << DebugString(true); | 581 << " " << DebugString(true); |
| 579 DCHECK_NE(DANGEROUS, GetSafetyState()); | 582 DCHECK_NE(DANGEROUS, GetSafetyState()); |
| 580 DCHECK(file_manager); | 583 DCHECK(file_manager); |
| 581 | 584 |
| 582 if (NeedsRename()) { | 585 if (NeedsRename()) { |
| 583 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 586 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 584 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, | 587 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, |
| 585 file_manager, GetGlobalId(), | 588 file_manager, download_id_, |
| 586 GetTargetFilePath(), GetSafetyState() == SAFE)); | 589 GetTargetFilePath(), GetSafetyState() == SAFE)); |
| 587 return; | 590 return; |
| 588 } | 591 } |
| 589 | 592 |
| 590 Completed(); | 593 Completed(); |
| 591 | 594 |
| 592 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 595 BrowserThread::PostTask( |
| 593 base::Bind(&DownloadFileManager::CompleteDownload, | 596 BrowserThread::FILE, FROM_HERE, |
| 594 file_manager, GetGlobalId())); | 597 base::Bind(&DownloadFileManager::CompleteDownload, |
| 598 file_manager, download_id_)); |
| 595 } | 599 } |
| 596 | 600 |
| 597 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 601 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
| 598 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 602 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 599 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 603 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 600 | 604 |
| 601 VLOG(20) << __FUNCTION__ << "()" | 605 VLOG(20) << __FUNCTION__ << "()" |
| 602 << " full_path = \"" << full_path.value() << "\"" | 606 << " full_path = \"" << full_path.value() << "\"" |
| 603 << " needed rename = " << NeedsRename() | 607 << " needed rename = " << NeedsRename() |
| 604 << " " << DebugString(false); | 608 << " " << DebugString(false); |
| 605 DCHECK(NeedsRename()); | 609 DCHECK(NeedsRename()); |
| 606 | 610 |
| 607 Rename(full_path); | 611 Rename(full_path); |
| 608 | 612 |
| 609 if (download_manager_->delegate()->ShouldOpenDownload(this)) { | 613 if (delegate_->ShouldOpenDownload(this)) { |
| 610 Completed(); | 614 Completed(); |
| 611 } else { | 615 } else { |
| 612 delegate_delayed_complete_ = true; | 616 delegate_delayed_complete_ = true; |
| 613 } | 617 } |
| 614 } | 618 } |
| 615 | 619 |
| 616 bool DownloadItemImpl::MatchesQuery(const string16& query) const { | 620 bool DownloadItemImpl::MatchesQuery(const string16& query) const { |
| 617 if (query.empty()) | 621 if (query.empty()) |
| 618 return true; | 622 return true; |
| 619 | 623 |
| 620 DCHECK_EQ(query, base::i18n::ToLower(query)); | 624 DCHECK_EQ(query, base::i18n::ToLower(query)); |
| 621 | 625 |
| 622 string16 url_raw(UTF8ToUTF16(GetURL().spec())); | 626 string16 url_raw(UTF8ToUTF16(GetURL().spec())); |
| 623 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) | 627 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) |
| 624 return true; | 628 return true; |
| 625 | 629 |
| 626 // TODO(phajdan.jr): write a test case for the following code. | 630 // TODO(phajdan.jr): write a test case for the following code. |
| 627 // A good test case would be: | 631 // A good test case would be: |
| 628 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 632 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
| 629 // L"/\x4f60\x597d\x4f60\x597d", | 633 // L"/\x4f60\x597d\x4f60\x597d", |
| 630 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 634 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
| 631 std::string languages; | 635 std::string languages; |
| 632 TabContents* tab = GetTabContents(); | 636 languages = content::GetContentClient()->browser()->GetAcceptLangs( |
| 633 if (tab) { | 637 BrowserContext()); |
| 634 languages = content::GetContentClient()->browser()->GetAcceptLangs( | |
| 635 tab->browser_context()); | |
| 636 } | |
| 637 string16 url_formatted(net::FormatUrl(GetURL(), languages)); | 638 string16 url_formatted(net::FormatUrl(GetURL(), languages)); |
| 638 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) | 639 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) |
| 639 return true; | 640 return true; |
| 640 | 641 |
| 641 string16 path(GetFullPath().LossyDisplayName()); | 642 string16 path(GetFullPath().LossyDisplayName()); |
| 642 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); | 643 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); |
| 643 } | 644 } |
| 644 | 645 |
| 645 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { | 646 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { |
| 646 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 647 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 TabContents* DownloadItemImpl::GetTabContents() const { | 699 TabContents* DownloadItemImpl::GetTabContents() const { |
| 699 // TODO(rdsmith): Remove null check after removing GetTabContents() from | 700 // TODO(rdsmith): Remove null check after removing GetTabContents() from |
| 700 // paths that might be used by DownloadItems created from history import. | 701 // paths that might be used by DownloadItems created from history import. |
| 701 // Currently such items have null request_handle_s, where other items | 702 // Currently such items have null request_handle_s, where other items |
| 702 // (regular and SavePackage downloads) have actual objects off the pointer. | 703 // (regular and SavePackage downloads) have actual objects off the pointer. |
| 703 if (request_handle_.get()) | 704 if (request_handle_.get()) |
| 704 return request_handle_->GetTabContents(); | 705 return request_handle_->GetTabContents(); |
| 705 return NULL; | 706 return NULL; |
| 706 } | 707 } |
| 707 | 708 |
| 709 content::BrowserContext* DownloadItemImpl::BrowserContext() const { |
| 710 return delegate_->BrowserContext(); |
| 711 } |
| 712 |
| 708 FilePath DownloadItemImpl::GetTargetFilePath() const { | 713 FilePath DownloadItemImpl::GetTargetFilePath() const { |
| 709 return full_path_.DirName().Append(state_info_.target_name); | 714 return full_path_.DirName().Append(state_info_.target_name); |
| 710 } | 715 } |
| 711 | 716 |
| 712 FilePath DownloadItemImpl::GetFileNameToReportUser() const { | 717 FilePath DownloadItemImpl::GetFileNameToReportUser() const { |
| 713 if (state_info_.path_uniquifier > 0) { | 718 if (state_info_.path_uniquifier > 0) { |
| 714 FilePath name(state_info_.target_name); | 719 FilePath name(state_info_.target_name); |
| 715 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); | 720 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); |
| 716 return name; | 721 return name; |
| 717 } | 722 } |
| 718 return state_info_.target_name; | 723 return state_info_.target_name; |
| 719 } | 724 } |
| 720 | 725 |
| 721 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { | 726 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { |
| 722 return (safety_state_ == DownloadItem::SAFE) ? | 727 return (safety_state_ == DownloadItem::SAFE) ? |
| 723 GetTargetFilePath() : full_path_; | 728 GetTargetFilePath() : full_path_; |
| 724 } | 729 } |
| 725 | 730 |
| 726 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { | 731 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { |
| 727 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 732 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 728 request_handle_->CancelRequest(); | 733 request_handle_->CancelRequest(); |
| 729 | 734 |
| 730 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 735 BrowserThread::PostTask( |
| 731 base::Bind(&DownloadFileManager::CancelDownload, | 736 BrowserThread::FILE, FROM_HERE, |
| 732 file_manager, GetGlobalId())); | 737 base::Bind(&DownloadFileManager::CancelDownload, |
| 738 file_manager, download_id_)); |
| 733 } | 739 } |
| 734 | 740 |
| 735 void DownloadItemImpl::Init(bool active) { | 741 void DownloadItemImpl::Init(bool active) { |
| 736 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 742 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 743 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 738 | 744 |
| 739 UpdateTarget(); | 745 UpdateTarget(); |
| 740 if (active) { | 746 if (active) { |
| 741 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 747 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
| 742 } | 748 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 total_bytes_ = total_bytes; | 860 total_bytes_ = total_bytes; |
| 855 } | 861 } |
| 856 const std::string& DownloadItemImpl::GetHash() const { return hash_; } | 862 const std::string& DownloadItemImpl::GetHash() const { return hash_; } |
| 857 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } | 863 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } |
| 858 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } | 864 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } |
| 859 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } | 865 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } |
| 860 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } | 866 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
| 861 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } | 867 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
| 862 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } | 868 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
| 863 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } | 869 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
| 864 DownloadManager* DownloadItemImpl::GetDownloadManager() { | |
| 865 return download_manager_; | |
| 866 } | |
| 867 bool DownloadItemImpl::IsPaused() const { return is_paused_; } | 870 bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
| 868 bool DownloadItemImpl::GetOpenWhenComplete() const { | 871 bool DownloadItemImpl::GetOpenWhenComplete() const { |
| 869 return open_when_complete_; | 872 return open_when_complete_; |
| 870 } | 873 } |
| 871 void DownloadItemImpl::SetOpenWhenComplete(bool open) { | 874 void DownloadItemImpl::SetOpenWhenComplete(bool open) { |
| 872 open_when_complete_ = open; | 875 open_when_complete_ = open; |
| 873 } | 876 } |
| 874 bool DownloadItemImpl::GetFileExternallyRemoved() const { | 877 bool DownloadItemImpl::GetFileExternallyRemoved() const { |
| 875 return file_externally_removed_; | 878 return file_externally_removed_; |
| 876 } | 879 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 892 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } | 895 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
| 893 bool DownloadItemImpl::GetOpened() const { return opened_; } | 896 bool DownloadItemImpl::GetOpened() const { return opened_; } |
| 894 InterruptReason DownloadItemImpl::GetLastReason() const { | 897 InterruptReason DownloadItemImpl::GetLastReason() const { |
| 895 return last_reason_; | 898 return last_reason_; |
| 896 } | 899 } |
| 897 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } | 900 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
| 898 bool DownloadItemImpl::NeedsRename() const { | 901 bool DownloadItemImpl::NeedsRename() const { |
| 899 return state_info_.target_name != full_path_.BaseName(); | 902 return state_info_.target_name != full_path_.BaseName(); |
| 900 } | 903 } |
| 901 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } | 904 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
| OLD | NEW |