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