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