| 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 "chrome/browser/download/download_item.h" | 5 #include "chrome/browser/download/download_item.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/timer.h" | 14 #include "base/timer.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 17 #include "chrome/browser/download/download_create_info.h" |
| 17 #include "chrome/browser/download/download_extensions.h" | 18 #include "chrome/browser/download/download_extensions.h" |
| 18 #include "chrome/browser/download/download_file_manager.h" | 19 #include "chrome/browser/download/download_file_manager.h" |
| 19 #include "chrome/browser/download/download_history.h" | 20 #include "chrome/browser/download/download_history.h" |
| 20 #include "chrome/browser/download/download_manager.h" | 21 #include "chrome/browser/download/download_manager.h" |
| 21 #include "chrome/browser/download/download_prefs.h" | 22 #include "chrome/browser/download/download_prefs.h" |
| 23 #include "chrome/browser/download/download_state_info.h" |
| 22 #include "chrome/browser/download/download_util.h" | 24 #include "chrome/browser/download/download_util.h" |
| 23 #include "chrome/browser/history/download_create_info.h" | |
| 24 #include "chrome/browser/platform_util.h" | 25 #include "chrome/browser/platform_util.h" |
| 25 #include "chrome/browser/prefs/pref_service.h" | 26 #include "chrome/browser/prefs/pref_service.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
| 28 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 29 #include "content/browser/browser_thread.h" | 30 #include "content/browser/browser_thread.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 31 | 32 |
| 32 // A DownloadItem normally goes through the following states: | 33 // A DownloadItem normally goes through the following states: |
| 33 // * Created (when download starts) | 34 // * Created (when download starts) |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 return DownloadItem::DANGEROUS_URL; | 108 return DownloadItem::DANGEROUS_URL; |
| 108 } | 109 } |
| 109 return dangerous_file ? | 110 return dangerous_file ? |
| 110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; | 111 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; |
| 111 } | 112 } |
| 112 | 113 |
| 113 } // namespace | 114 } // namespace |
| 114 | 115 |
| 115 // Constructor for reading from the history service. | 116 // Constructor for reading from the history service. |
| 116 DownloadItem::DownloadItem(DownloadManager* download_manager, | 117 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 117 const DownloadCreateInfo& info) | 118 const DownloadHistoryInfo& info) |
| 118 : id_(-1), | 119 : history_info_(info.path, info.url_chain, info.referrer_url, |
| 119 full_path_(info.path), | 120 info.start_time, info.received_bytes, info.total_bytes, |
| 120 path_uniquifier_(0), | 121 info.state, info.db_handle, info.download_id), |
| 121 url_chain_(info.url_chain), | |
| 122 referrer_url_(info.referrer_url), | |
| 123 mime_type_(info.mime_type), | |
| 124 original_mime_type_(info.original_mime_type), | |
| 125 total_bytes_(info.total_bytes), | |
| 126 received_bytes_(info.received_bytes), | |
| 127 start_tick_(base::TimeTicks()), | 122 start_tick_(base::TimeTicks()), |
| 128 state_(static_cast<DownloadState>(info.state)), | |
| 129 start_time_(info.start_time), | |
| 130 db_handle_(info.db_handle), | |
| 131 download_manager_(download_manager), | 123 download_manager_(download_manager), |
| 132 is_paused_(false), | 124 is_paused_(false), |
| 133 open_when_complete_(false), | 125 open_when_complete_(false), |
| 134 safety_state_(SAFE), | 126 safety_state_(SAFE), |
| 135 danger_type_(NOT_DANGEROUS), | |
| 136 auto_opened_(false), | 127 auto_opened_(false), |
| 137 target_name_(info.original_name), | |
| 138 save_as_(false), | |
| 139 is_otr_(false), | 128 is_otr_(false), |
| 140 is_extension_install_(info.is_extension_install), | |
| 141 is_temporary_(false), | 129 is_temporary_(false), |
| 142 all_data_saved_(false), | 130 all_data_saved_(false), |
| 143 opened_(false) { | 131 opened_(false) { |
| 144 if (IsInProgress()) | 132 if (IsInProgress()) |
| 145 state_ = CANCELLED; | 133 history_info_.state = CANCELLED; |
| 146 if (IsComplete()) | 134 if (IsComplete()) |
| 147 all_data_saved_ = true; | 135 all_data_saved_ = true; |
| 148 Init(false /* don't start progress timer */); | 136 Init(false /* don't start progress timer */); |
| 149 } | 137 } |
| 150 | 138 |
| 151 // Constructing for a regular download: | 139 // Constructing for a regular download: |
| 152 DownloadItem::DownloadItem(DownloadManager* download_manager, | 140 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 153 const DownloadCreateInfo& info, | 141 const DownloadCreateInfo& info, |
| 154 bool is_otr) | 142 bool is_otr) |
| 155 : id_(info.download_id), | 143 : history_info_(info.path, info.url_chain, info.referrer_url, |
| 156 full_path_(info.path), | 144 info.start_time, info.received_bytes, info.total_bytes, |
| 157 path_uniquifier_(info.path_uniquifier), | 145 IN_PROGRESS, DownloadHistory::kUninitializedHandle, |
| 158 url_chain_(info.url_chain), | 146 info.download_id), |
| 159 referrer_url_(info.referrer_url), | 147 state_info_(info.original_name, info.save_info.file_path, |
| 148 info.has_user_gesture, info.prompt_user_for_save_location, |
| 149 info.path_uniquifier, info.is_dangerous_file, |
| 150 info.is_dangerous_url, info.is_extension_install), |
| 151 process_handle_(info.process_handle), |
| 152 content_disposition_(info.content_disposition), |
| 160 mime_type_(info.mime_type), | 153 mime_type_(info.mime_type), |
| 161 original_mime_type_(info.original_mime_type), | 154 original_mime_type_(info.original_mime_type), |
| 162 total_bytes_(info.total_bytes), | 155 referrer_charset_(info.referrer_charset), |
| 163 received_bytes_(0), | |
| 164 last_os_error_(0), | 156 last_os_error_(0), |
| 165 start_tick_(base::TimeTicks::Now()), | 157 start_tick_(base::TimeTicks::Now()), |
| 166 state_(IN_PROGRESS), | |
| 167 start_time_(info.start_time), | |
| 168 db_handle_(DownloadHistory::kUninitializedHandle), | |
| 169 download_manager_(download_manager), | 158 download_manager_(download_manager), |
| 170 is_paused_(false), | 159 is_paused_(false), |
| 171 open_when_complete_(false), | 160 open_when_complete_(false), |
| 172 safety_state_(GetSafetyState(info.is_dangerous_file, | 161 safety_state_(GetSafetyState(info.is_dangerous_file, |
| 173 info.is_dangerous_url)), | 162 info.is_dangerous_url)), |
| 174 danger_type_(GetDangerType(info.is_dangerous_file, | |
| 175 info.is_dangerous_url)), | |
| 176 auto_opened_(false), | 163 auto_opened_(false), |
| 177 target_name_(info.original_name), | |
| 178 process_handle_(info.process_handle), | |
| 179 save_as_(info.prompt_user_for_save_location), | |
| 180 is_otr_(is_otr), | 164 is_otr_(is_otr), |
| 181 is_extension_install_(info.is_extension_install), | |
| 182 is_temporary_(!info.save_info.file_path.empty()), | 165 is_temporary_(!info.save_info.file_path.empty()), |
| 183 all_data_saved_(false), | 166 all_data_saved_(false), |
| 184 opened_(false) { | 167 opened_(false) { |
| 185 Init(true /* start progress timer */); | 168 Init(true /* start progress timer */); |
| 186 } | 169 } |
| 187 | 170 |
| 188 // Constructing for the "Save Page As..." feature: | 171 // Constructing for the "Save Page As..." feature: |
| 189 DownloadItem::DownloadItem(DownloadManager* download_manager, | 172 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 190 const FilePath& path, | 173 const FilePath& path, |
| 191 const GURL& url, | 174 const GURL& url, |
| 192 bool is_otr) | 175 bool is_otr) |
| 193 : id_(1), | 176 : history_info_(path, url, base::Time::Now(), 0, 0, IN_PROGRESS), |
| 194 full_path_(path), | |
| 195 path_uniquifier_(0), | |
| 196 url_chain_(1, url), | |
| 197 referrer_url_(GURL()), | |
| 198 mime_type_(std::string()), | |
| 199 original_mime_type_(std::string()), | |
| 200 total_bytes_(0), | |
| 201 received_bytes_(0), | |
| 202 last_os_error_(0), | 177 last_os_error_(0), |
| 203 start_tick_(base::TimeTicks::Now()), | 178 start_tick_(base::TimeTicks::Now()), |
| 204 state_(IN_PROGRESS), | |
| 205 start_time_(base::Time::Now()), | |
| 206 db_handle_(DownloadHistory::kUninitializedHandle), | |
| 207 download_manager_(download_manager), | 179 download_manager_(download_manager), |
| 208 is_paused_(false), | 180 is_paused_(false), |
| 209 open_when_complete_(false), | 181 open_when_complete_(false), |
| 210 safety_state_(SAFE), | 182 safety_state_(SAFE), |
| 211 danger_type_(NOT_DANGEROUS), | |
| 212 auto_opened_(false), | 183 auto_opened_(false), |
| 213 save_as_(false), | |
| 214 is_otr_(is_otr), | 184 is_otr_(is_otr), |
| 215 is_extension_install_(false), | |
| 216 is_temporary_(false), | 185 is_temporary_(false), |
| 217 all_data_saved_(false), | 186 all_data_saved_(false), |
| 218 opened_(false) { | 187 opened_(false) { |
| 219 Init(true /* start progress timer */); | 188 Init(true /* start progress timer */); |
| 220 } | 189 } |
| 221 | 190 |
| 222 DownloadItem::~DownloadItem() { | 191 DownloadItem::~DownloadItem() { |
| 223 state_ = REMOVING; | 192 history_info_.state = REMOVING; |
| 224 UpdateObservers(); | 193 UpdateObservers(); |
| 225 } | 194 } |
| 226 | 195 |
| 227 void DownloadItem::AddObserver(Observer* observer) { | 196 void DownloadItem::AddObserver(Observer* observer) { |
| 228 observers_.AddObserver(observer); | 197 observers_.AddObserver(observer); |
| 229 } | 198 } |
| 230 | 199 |
| 231 void DownloadItem::RemoveObserver(Observer* observer) { | 200 void DownloadItem::RemoveObserver(Observer* observer) { |
| 232 observers_.RemoveObserver(observer); | 201 observers_.RemoveObserver(observer); |
| 233 } | 202 } |
| 234 | 203 |
| 235 void DownloadItem::UpdateObservers() { | 204 void DownloadItem::UpdateObservers() { |
| 236 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 205 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
| 237 } | 206 } |
| 238 | 207 |
| 239 bool DownloadItem::CanOpenDownload() { | 208 bool DownloadItem::CanOpenDownload() { |
| 240 return !Extension::IsExtension(target_name_); | 209 return !Extension::IsExtension(state_info_.target_name); |
| 241 } | 210 } |
| 242 | 211 |
| 243 bool DownloadItem::ShouldOpenFileBasedOnExtension() { | 212 bool DownloadItem::ShouldOpenFileBasedOnExtension() { |
| 244 return download_manager_->ShouldOpenFileBasedOnExtension( | 213 return download_manager_->ShouldOpenFileBasedOnExtension( |
| 245 GetUserVerifiedFilePath()); | 214 GetUserVerifiedFilePath()); |
| 246 } | 215 } |
| 247 | 216 |
| 248 void DownloadItem::OpenFilesBasedOnExtension(bool open) { | 217 void DownloadItem::OpenFilesBasedOnExtension(bool open) { |
| 249 DownloadPrefs* prefs = download_manager_->download_prefs(); | 218 DownloadPrefs* prefs = download_manager_->download_prefs(); |
| 250 if (open) | 219 if (open) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 #else | 252 #else |
| 284 BrowserThread::PostTask( | 253 BrowserThread::PostTask( |
| 285 BrowserThread::FILE, FROM_HERE, | 254 BrowserThread::FILE, FROM_HERE, |
| 286 NewRunnableFunction(&platform_util::ShowItemInFolder, | 255 NewRunnableFunction(&platform_util::ShowItemInFolder, |
| 287 full_path())); | 256 full_path())); |
| 288 #endif | 257 #endif |
| 289 } | 258 } |
| 290 | 259 |
| 291 void DownloadItem::DangerousDownloadValidated() { | 260 void DownloadItem::DangerousDownloadValidated() { |
| 292 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 261 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
| 293 danger_type_, | 262 GetDangerType(), |
| 294 DANGEROUS_TYPE_MAX); | 263 DANGEROUS_TYPE_MAX); |
| 295 download_manager_->DangerousDownloadValidated(this); | 264 download_manager_->DangerousDownloadValidated(this); |
| 296 } | 265 } |
| 297 | 266 |
| 298 void DownloadItem::UpdateSize(int64 bytes_so_far) { | 267 void DownloadItem::UpdateSize(int64 bytes_so_far) { |
| 299 received_bytes_ = bytes_so_far; | 268 history_info_.received_bytes = bytes_so_far; |
| 300 | 269 |
| 301 // If we've received more data than we were expecting (bad server info?), | 270 // If we've received more data than we were expecting (bad server info?), |
| 302 // revert to 'unknown size mode'. | 271 // revert to 'unknown size mode'. |
| 303 if (received_bytes_ > total_bytes_) | 272 if (history_info_.received_bytes > history_info_.total_bytes) |
| 304 total_bytes_ = 0; | 273 history_info_.total_bytes = 0; |
| 305 } | 274 } |
| 306 | 275 |
| 307 void DownloadItem::StartProgressTimer() { | 276 void DownloadItem::StartProgressTimer() { |
| 308 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, | 277 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
| 309 &DownloadItem::UpdateObservers); | 278 &DownloadItem::UpdateObservers); |
| 310 } | 279 } |
| 311 | 280 |
| 312 void DownloadItem::StopProgressTimer() { | 281 void DownloadItem::StopProgressTimer() { |
| 313 update_timer_.Stop(); | 282 update_timer_.Stop(); |
| 314 } | 283 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 329 void DownloadItem::Cancel(bool update_history) { | 298 void DownloadItem::Cancel(bool update_history) { |
| 330 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 299 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 331 if (!IsPartialDownload()) { | 300 if (!IsPartialDownload()) { |
| 332 // Small downloads might be complete before this method has | 301 // Small downloads might be complete before this method has |
| 333 // a chance to run. | 302 // a chance to run. |
| 334 return; | 303 return; |
| 335 } | 304 } |
| 336 | 305 |
| 337 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); | 306 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
| 338 | 307 |
| 339 state_ = CANCELLED; | 308 history_info_.state = CANCELLED; |
| 340 UpdateObservers(); | 309 UpdateObservers(); |
| 341 StopProgressTimer(); | 310 StopProgressTimer(); |
| 342 if (update_history) | 311 if (update_history) |
| 343 download_manager_->DownloadCancelled(id_); | 312 download_manager_->DownloadCancelled(history_info_.download_id); |
| 344 } | 313 } |
| 345 | 314 |
| 346 void DownloadItem::MarkAsComplete() { | 315 void DownloadItem::MarkAsComplete() { |
| 347 DCHECK(all_data_saved_); | 316 DCHECK(all_data_saved_); |
| 348 state_ = COMPLETE; | 317 history_info_.state = COMPLETE; |
| 349 UpdateObservers(); | 318 UpdateObservers(); |
| 350 } | 319 } |
| 351 | 320 |
| 352 void DownloadItem::OnAllDataSaved(int64 size) { | 321 void DownloadItem::OnAllDataSaved(int64 size) { |
| 353 DCHECK(!all_data_saved_); | 322 DCHECK(!all_data_saved_); |
| 354 all_data_saved_ = true; | 323 all_data_saved_ = true; |
| 355 UpdateSize(size); | 324 UpdateSize(size); |
| 356 StopProgressTimer(); | 325 StopProgressTimer(); |
| 357 } | 326 } |
| 358 | 327 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 373 is_temporary()) { | 342 is_temporary()) { |
| 374 // If the download is temporary, like in drag-and-drop, do not open it but | 343 // If the download is temporary, like in drag-and-drop, do not open it but |
| 375 // we still need to set it auto-opened so that it can be removed from the | 344 // we still need to set it auto-opened so that it can be removed from the |
| 376 // download shelf. | 345 // download shelf. |
| 377 if (!is_temporary()) | 346 if (!is_temporary()) |
| 378 OpenDownload(); | 347 OpenDownload(); |
| 379 auto_opened_ = true; | 348 auto_opened_ = true; |
| 380 } | 349 } |
| 381 | 350 |
| 382 DCHECK(all_data_saved_); | 351 DCHECK(all_data_saved_); |
| 383 state_ = COMPLETE; | 352 history_info_.state = COMPLETE; |
| 384 UpdateObservers(); | 353 UpdateObservers(); |
| 385 download_manager_->DownloadCompleted(id()); | 354 download_manager_->DownloadCompleted(id()); |
| 386 } | 355 } |
| 387 | 356 |
| 388 void DownloadItem::Interrupted(int64 size, int os_error) { | 357 void DownloadItem::Interrupted(int64 size, int os_error) { |
| 389 if (!IsInProgress()) | 358 if (!IsInProgress()) |
| 390 return; | 359 return; |
| 391 state_ = INTERRUPTED; | 360 history_info_.state = INTERRUPTED; |
| 392 last_os_error_ = os_error; | 361 last_os_error_ = os_error; |
| 393 UpdateSize(size); | 362 UpdateSize(size); |
| 394 StopProgressTimer(); | 363 StopProgressTimer(); |
| 395 UpdateObservers(); | 364 UpdateObservers(); |
| 396 } | 365 } |
| 397 | 366 |
| 398 void DownloadItem::Delete(DeleteReason reason) { | 367 void DownloadItem::Delete(DeleteReason reason) { |
| 399 switch (reason) { | 368 switch (reason) { |
| 400 case DELETE_DUE_TO_USER_DISCARD: | 369 case DELETE_DUE_TO_USER_DISCARD: |
| 401 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", danger_type_, | 370 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
| 402 DANGEROUS_TYPE_MAX); | 371 DANGEROUS_TYPE_MAX); |
| 403 break; | 372 break; |
| 404 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 373 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
| 405 UMA_HISTOGRAM_ENUMERATION("Download.Discard", danger_type_, | 374 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), |
| 406 DANGEROUS_TYPE_MAX); | 375 DANGEROUS_TYPE_MAX); |
| 407 break; | 376 break; |
| 408 default: | 377 default: |
| 409 NOTREACHED(); | 378 NOTREACHED(); |
| 410 } | 379 } |
| 411 | 380 |
| 412 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 381 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 413 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 382 NewRunnableFunction(&DeleteDownloadedFile, history_info_.path)); |
| 414 Remove(); | 383 Remove(); |
| 415 // We have now been deleted. | 384 // We have now been deleted. |
| 416 } | 385 } |
| 417 | 386 |
| 418 void DownloadItem::Remove() { | 387 void DownloadItem::Remove() { |
| 419 Cancel(true); | 388 Cancel(true); |
| 420 state_ = REMOVING; | 389 history_info_.state = REMOVING; |
| 421 download_manager_->RemoveDownload(db_handle_); | 390 download_manager_->RemoveDownload(history_info_.db_handle); |
| 422 // We have now been deleted. | 391 // We have now been deleted. |
| 423 } | 392 } |
| 424 | 393 |
| 425 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 394 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
| 426 if (total_bytes_ <= 0) | 395 if (history_info_.total_bytes <= 0) |
| 427 return false; // We never received the content_length for this download. | 396 return false; // We never received the content_length for this download. |
| 428 | 397 |
| 429 int64 speed = CurrentSpeed(); | 398 int64 speed = CurrentSpeed(); |
| 430 if (speed == 0) | 399 if (speed == 0) |
| 431 return false; | 400 return false; |
| 432 | 401 |
| 433 *remaining = | 402 *remaining = base::TimeDelta::FromSeconds( |
| 434 base::TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed); | 403 (history_info_.total_bytes - history_info_.received_bytes) / speed); |
| 435 return true; | 404 return true; |
| 436 } | 405 } |
| 437 | 406 |
| 438 int64 DownloadItem::CurrentSpeed() const { | 407 int64 DownloadItem::CurrentSpeed() const { |
| 439 if (is_paused_) | 408 if (is_paused_) |
| 440 return 0; | 409 return 0; |
| 441 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; | 410 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; |
| 442 int64 diff_ms = diff.InMilliseconds(); | 411 int64 diff_ms = diff.InMilliseconds(); |
| 443 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms; | 412 return diff_ms == 0 ? 0 : history_info_.received_bytes * 1000 / diff_ms; |
| 444 } | 413 } |
| 445 | 414 |
| 446 int DownloadItem::PercentComplete() const { | 415 int DownloadItem::PercentComplete() const { |
| 447 return (total_bytes_ > 0) ? | 416 return (history_info_.total_bytes > 0) ? |
| 448 static_cast<int>(received_bytes_ * 100.0 / total_bytes_) : -1; | 417 static_cast<int>(history_info_.received_bytes * 100.0 / |
| 418 history_info_.total_bytes) : |
| 419 -1; |
| 449 } | 420 } |
| 450 | 421 |
| 451 void DownloadItem::Rename(const FilePath& full_path) { | 422 void DownloadItem::Rename(const FilePath& full_path) { |
| 452 VLOG(20) << __FUNCTION__ << "()" | 423 VLOG(20) << __FUNCTION__ << "()" |
| 453 << " full_path = \"" << full_path.value() << "\"" | 424 << " full_path = \"" << full_path.value() << "\"" |
| 454 << " " << DebugString(true); | 425 << " " << DebugString(true); |
| 455 DCHECK(!full_path.empty()); | 426 DCHECK(!full_path.empty()); |
| 456 full_path_ = full_path; | 427 history_info_.path = full_path; |
| 457 } | 428 } |
| 458 | 429 |
| 459 void DownloadItem::TogglePause() { | 430 void DownloadItem::TogglePause() { |
| 460 DCHECK(IsInProgress()); | 431 DCHECK(IsInProgress()); |
| 461 download_manager_->PauseDownload(id_, !is_paused_); | 432 download_manager_->PauseDownload(history_info_.download_id, !is_paused_); |
| 462 is_paused_ = !is_paused_; | 433 is_paused_ = !is_paused_; |
| 463 UpdateObservers(); | 434 UpdateObservers(); |
| 464 } | 435 } |
| 465 | 436 |
| 466 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { | 437 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { |
| 467 VLOG(20) << __FUNCTION__ << "()" | 438 VLOG(20) << __FUNCTION__ << "()" |
| 468 << " needs rename = " << NeedsRename() | 439 << " needs rename = " << NeedsRename() |
| 469 << " " << DebugString(true); | 440 << " " << DebugString(true); |
| 470 DCHECK_NE(DANGEROUS, safety_state()); | 441 DCHECK_NE(DANGEROUS, safety_state()); |
| 471 DCHECK(file_manager); | 442 DCHECK(file_manager); |
| 472 | 443 |
| 473 if (NeedsRename()) { | 444 if (NeedsRename()) { |
| 474 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 445 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 475 NewRunnableMethod(file_manager, | 446 NewRunnableMethod(file_manager, |
| 476 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 447 &DownloadFileManager::RenameCompletingDownloadFile, id(), |
| 477 GetTargetFilePath(), safety_state() == SAFE)); | 448 GetTargetFilePath(), safety_state() == SAFE)); |
| 478 return; | 449 return; |
| 479 } | 450 } |
| 480 | 451 |
| 481 Completed(); | 452 Completed(); |
| 482 | 453 |
| 483 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 454 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 484 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 455 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, |
| 485 id())); | 456 id())); |
| 486 } | 457 } |
| 487 | 458 |
| 488 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 459 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
| 489 VLOG(20) << __FUNCTION__ << "()" | 460 VLOG(20) << __FUNCTION__ << "()" |
| 490 << " full_path = " << full_path.value() | 461 << " full_path = \"" << full_path.value() << "\"" |
| 491 << " needed rename = " << NeedsRename() | 462 << " needed rename = " << NeedsRename() |
| 492 << " " << DebugString(false); | 463 << " " << DebugString(false); |
| 493 DCHECK(NeedsRename()); | 464 DCHECK(NeedsRename()); |
| 494 | 465 |
| 495 Rename(full_path); | 466 Rename(full_path); |
| 496 | 467 |
| 497 Completed(); | 468 Completed(); |
| 498 } | 469 } |
| 499 | 470 |
| 500 bool DownloadItem::MatchesQuery(const string16& query) const { | 471 bool DownloadItem::MatchesQuery(const string16& query) const { |
| 501 if (query.empty()) | 472 if (query.empty()) |
| 502 return true; | 473 return true; |
| 503 | 474 |
| 504 DCHECK_EQ(query, base::i18n::ToLower(query)); | 475 DCHECK_EQ(query, base::i18n::ToLower(query)); |
| 505 | 476 |
| 506 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(url().spec()))); | 477 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(GetURL().spec()))); |
| 507 if (url_raw.find(query) != string16::npos) | 478 if (url_raw.find(query) != string16::npos) |
| 508 return true; | 479 return true; |
| 509 | 480 |
| 510 // TODO(phajdan.jr): write a test case for the following code. | 481 // TODO(phajdan.jr): write a test case for the following code. |
| 511 // A good test case would be: | 482 // A good test case would be: |
| 512 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 483 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
| 513 // L"/\x4f60\x597d\x4f60\x597d", | 484 // L"/\x4f60\x597d\x4f60\x597d", |
| 514 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 485 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
| 515 PrefService* prefs = download_manager_->profile()->GetPrefs(); | 486 PrefService* prefs = download_manager_->profile()->GetPrefs(); |
| 516 std::string languages(prefs->GetString(prefs::kAcceptLanguages)); | 487 std::string languages(prefs->GetString(prefs::kAcceptLanguages)); |
| 517 string16 url_formatted(base::i18n::ToLower(net::FormatUrl(url(), languages))); | 488 string16 url_formatted( |
| 489 base::i18n::ToLower(net::FormatUrl(GetURL(), languages))); |
| 518 if (url_formatted.find(query) != string16::npos) | 490 if (url_formatted.find(query) != string16::npos) |
| 519 return true; | 491 return true; |
| 520 | 492 |
| 521 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); | 493 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); |
| 522 // This shouldn't just do a substring match; it is wrong for Unicode | 494 // This shouldn't just do a substring match; it is wrong for Unicode |
| 523 // due to normalization and we have a fancier search-query system | 495 // due to normalization and we have a fancier search-query system |
| 524 // used elsewhere. | 496 // used elsewhere. |
| 525 // http://code.google.com/p/chromium/issues/detail?id=71982 | 497 // http://code.google.com/p/chromium/issues/detail?id=71982 |
| 526 return (path.find(query) != string16::npos); | 498 return (path.find(query) != string16::npos); |
| 527 } | 499 } |
| 528 | 500 |
| 529 void DownloadItem::SetFileCheckResults(const FilePath& path, | 501 void DownloadItem::SetFileCheckResults(const DownloadStateInfo& state) { |
| 530 bool is_dangerous_file, | 502 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
| 531 bool is_dangerous_url, | 503 state_info_ = state; |
| 532 int path_uniquifier, | 504 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
| 533 bool prompt, | |
| 534 bool is_extension_install, | |
| 535 const FilePath& original_name) { | |
| 536 VLOG(20) << " " << __FUNCTION__ << "()" | |
| 537 << " path = \"" << path.value() << "\"" | |
| 538 << " is_dangerous_file = " << is_dangerous_file | |
| 539 << " is_dangerous_url = " << is_dangerous_url | |
| 540 << " path_uniquifier = " << path_uniquifier | |
| 541 << " prompt = " << prompt | |
| 542 << " is_extension_install = " << is_extension_install | |
| 543 << " path = \"" << path.value() << "\"" | |
| 544 << " original_name = \"" << original_name.value() << "\"" | |
| 545 << " " << DebugString(true); | |
| 546 // Make sure the initial file name is set only once. | |
| 547 DCHECK(full_path_.empty()); | |
| 548 DCHECK(!path.empty()); | |
| 549 | 505 |
| 550 full_path_ = path; | 506 safety_state_ = GetSafetyState(state_info_.is_dangerous_file, |
| 551 safety_state_ = GetSafetyState(is_dangerous_file, is_dangerous_url); | 507 state_info_.is_dangerous_url); |
| 552 danger_type_ = GetDangerType(is_dangerous_file, is_dangerous_url); | 508 } |
| 553 path_uniquifier_ = path_uniquifier; | |
| 554 save_as_ = prompt; | |
| 555 is_extension_install_ = is_extension_install; | |
| 556 target_name_ = original_name; | |
| 557 | 509 |
| 558 if (target_name_.value().empty()) | 510 void DownloadItem::UpdateTarget() { |
| 559 target_name_ = full_path_.BaseName(); | 511 if (state_info_.target_name.value().empty()) |
| 512 state_info_.target_name = history_info_.path.BaseName(); |
| 513 } |
| 514 |
| 515 DownloadItem::DangerType DownloadItem::GetDangerType() const { |
| 516 return ::GetDangerType(state_info_.is_dangerous_file, |
| 517 state_info_.is_dangerous_url); |
| 518 } |
| 519 |
| 520 bool DownloadItem::IsDangerous() const { |
| 521 return GetDangerType() != DownloadItem::NOT_DANGEROUS; |
| 522 } |
| 523 |
| 524 void DownloadItem::MarkUrlDangerous() { |
| 525 state_info_.is_dangerous_url = true; |
| 560 } | 526 } |
| 561 | 527 |
| 562 FilePath DownloadItem::GetTargetFilePath() const { | 528 FilePath DownloadItem::GetTargetFilePath() const { |
| 563 return full_path_.DirName().Append(target_name_); | 529 return history_info_.path.DirName().Append(state_info_.target_name); |
| 564 } | 530 } |
| 565 | 531 |
| 566 FilePath DownloadItem::GetFileNameToReportUser() const { | 532 FilePath DownloadItem::GetFileNameToReportUser() const { |
| 567 if (path_uniquifier_ > 0) { | 533 if (state_info_.path_uniquifier > 0) { |
| 568 FilePath name(target_name_); | 534 FilePath name(state_info_.target_name); |
| 569 download_util::AppendNumberToPath(&name, path_uniquifier_); | 535 download_util::AppendNumberToPath(&name, state_info_.path_uniquifier); |
| 570 return name; | 536 return name; |
| 571 } | 537 } |
| 572 return target_name_; | 538 return state_info_.target_name; |
| 573 } | 539 } |
| 574 | 540 |
| 575 FilePath DownloadItem::GetUserVerifiedFilePath() const { | 541 FilePath DownloadItem::GetUserVerifiedFilePath() const { |
| 576 return (safety_state_ == DownloadItem::SAFE) ? | 542 return (safety_state_ == DownloadItem::SAFE) ? |
| 577 GetTargetFilePath() : full_path_; | 543 GetTargetFilePath() : history_info_.path; |
| 578 } | 544 } |
| 579 | 545 |
| 580 void DownloadItem::Init(bool start_timer) { | 546 void DownloadItem::Init(bool start_timer) { |
| 581 if (target_name_.value().empty()) | 547 UpdateTarget(); |
| 582 target_name_ = full_path_.BaseName(); | |
| 583 if (start_timer) | 548 if (start_timer) |
| 584 StartProgressTimer(); | 549 StartProgressTimer(); |
| 585 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 550 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
| 586 } | 551 } |
| 587 | 552 |
| 588 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 553 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
| 589 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 554 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
| 590 bool DownloadItem::IsPartialDownload() const { | 555 bool DownloadItem::IsPartialDownload() const { |
| 591 return (state_ == IN_PROGRESS); | 556 return (history_info_.state == IN_PROGRESS); |
| 592 } | 557 } |
| 593 | 558 |
| 594 bool DownloadItem::IsInProgress() const { | 559 bool DownloadItem::IsInProgress() const { |
| 595 return (state_ == IN_PROGRESS); | 560 return (history_info_.state == IN_PROGRESS); |
| 596 } | 561 } |
| 597 | 562 |
| 598 bool DownloadItem::IsCancelled() const { | 563 bool DownloadItem::IsCancelled() const { |
| 599 return (state_ == CANCELLED) || (state_ == INTERRUPTED); | 564 return (history_info_.state == CANCELLED) || |
| 565 (history_info_.state == INTERRUPTED); |
| 600 } | 566 } |
| 601 | 567 |
| 602 bool DownloadItem::IsInterrupted() const { | 568 bool DownloadItem::IsInterrupted() const { |
| 603 return (state_ == INTERRUPTED); | 569 return (history_info_.state == INTERRUPTED); |
| 604 } | 570 } |
| 605 | 571 |
| 606 bool DownloadItem::IsComplete() const { | 572 bool DownloadItem::IsComplete() const { |
| 607 return (state_ == COMPLETE); | 573 return (history_info_.state == COMPLETE); |
| 574 } |
| 575 |
| 576 // This function converts history_info_.state (which is an int32) to a |
| 577 // DownloadItem::DownloadState enum, and returns it. |
| 578 DownloadItem::DownloadState DownloadItem::state() const { |
| 579 if ((history_info_.state < 0) || |
| 580 (history_info_.state >= MAX_DOWNLOAD_STATE)) { |
| 581 NOTREACHED() << " state = " << history_info_.state; |
| 582 return IN_PROGRESS; |
| 583 } |
| 584 return static_cast<DownloadState>(history_info_.state); |
| 585 } |
| 586 |
| 587 const GURL& DownloadItem::GetURL() const { |
| 588 return history_info_.url_chain.empty() ? |
| 589 GURL::EmptyGURL() : history_info_.url_chain.back(); |
| 608 } | 590 } |
| 609 | 591 |
| 610 std::string DownloadItem::DebugString(bool verbose) const { | 592 std::string DownloadItem::DebugString(bool verbose) const { |
| 611 std::string description = base::StringPrintf( | 593 std::string description = |
| 612 "{ id_ = %d state = %s", id_, DebugDownloadStateString(state())); | 594 base::StringPrintf("{ id = %d" |
| 595 " state = %s", |
| 596 history_info_.download_id, |
| 597 DebugDownloadStateString(state())); |
| 613 | 598 |
| 614 // Construct a string of the URL chain. | 599 // Construct a string of the URL chain. |
| 615 std::string url_list("<none>"); | 600 std::string url_list("<none>"); |
| 616 if (!url_chain_.empty()) { | 601 if (!history_info_.url_chain.empty()) { |
| 617 std::vector<GURL>::const_iterator iter = url_chain_.begin(); | 602 std::vector<GURL>::const_iterator iter = history_info_.url_chain.begin(); |
| 618 std::vector<GURL>::const_iterator last = url_chain_.end(); | 603 std::vector<GURL>::const_iterator last = history_info_.url_chain.end(); |
| 619 url_list = (*iter).spec(); | 604 url_list = (*iter).spec(); |
| 620 ++iter; | 605 ++iter; |
| 621 for ( ; verbose && (iter != last); ++iter) { | 606 for ( ; verbose && (iter != last); ++iter) { |
| 622 url_list += " -> "; | 607 url_list += " ->\n\t"; |
| 623 const GURL& next_url = *iter; | 608 const GURL& next_url = *iter; |
| 624 url_list += next_url.spec(); | 609 url_list += next_url.spec(); |
| 625 } | 610 } |
| 626 } | 611 } |
| 627 | 612 |
| 628 if (verbose) { | 613 if (verbose) { |
| 629 description += base::StringPrintf( | 614 description += base::StringPrintf( |
| 630 " db_handle = %" PRId64 | 615 " db_handle = %" PRId64 |
| 631 " total_bytes = %" PRId64 | 616 " total_bytes = %" PRId64 |
| 632 " is_paused = " "%c" | 617 " is_paused = %c" |
| 633 " is_extension_install = " "%c" | 618 " is_extension_install = %c" |
| 634 " is_otr = " "%c" | 619 " is_otr = %c" |
| 635 " safety_state = " "%s" | 620 " safety_state = %s" |
| 636 " url_chain = " "\"%s\"" | 621 " url_chain = \n\t\"%s\"\n\t" |
| 637 " target_name_ = \"%" PRFilePath "\"" | 622 " target_name = \"%" PRFilePath "\"" |
| 638 " full_path = \"%" PRFilePath "\"", | 623 " full_path = \"%" PRFilePath "\"", |
| 639 db_handle(), | 624 db_handle(), |
| 640 total_bytes(), | 625 total_bytes(), |
| 641 is_paused() ? 'T' : 'F', | 626 is_paused() ? 'T' : 'F', |
| 642 is_extension_install() ? 'T' : 'F', | 627 is_extension_install() ? 'T' : 'F', |
| 643 is_otr() ? 'T' : 'F', | 628 is_otr() ? 'T' : 'F', |
| 644 DebugSafetyStateString(safety_state()), | 629 DebugSafetyStateString(safety_state()), |
| 645 url_list.c_str(), | 630 url_list.c_str(), |
| 646 target_name_.value().c_str(), | 631 state_info_.target_name.value().c_str(), |
| 647 full_path().value().c_str()); | 632 full_path().value().c_str()); |
| 648 } else { | 633 } else { |
| 649 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 634 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 650 } | 635 } |
| 636 |
| 637 description += " }"; |
| 638 |
| 651 return description; | 639 return description; |
| 652 } | 640 } |
| OLD | NEW |