OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/timer.h" | 10 #include "base/timer.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 DownloadItem::DownloadItem(DownloadManager* download_manager, | 41 DownloadItem::DownloadItem(DownloadManager* download_manager, |
42 const DownloadCreateInfo& info) | 42 const DownloadCreateInfo& info) |
43 : id_(-1), | 43 : id_(-1), |
44 full_path_(info.path), | 44 full_path_(info.path), |
45 url_(info.url), | 45 url_(info.url), |
46 referrer_url_(info.referrer_url), | 46 referrer_url_(info.referrer_url), |
47 mime_type_(info.mime_type), | 47 mime_type_(info.mime_type), |
48 original_mime_type_(info.original_mime_type), | 48 original_mime_type_(info.original_mime_type), |
49 total_bytes_(info.total_bytes), | 49 total_bytes_(info.total_bytes), |
50 received_bytes_(info.received_bytes), | 50 received_bytes_(info.received_bytes), |
| 51 last_os_error_(0), |
51 start_tick_(base::TimeTicks()), | 52 start_tick_(base::TimeTicks()), |
52 state_(static_cast<DownloadState>(info.state)), | 53 state_(static_cast<DownloadState>(info.state)), |
53 start_time_(info.start_time), | 54 start_time_(info.start_time), |
54 db_handle_(info.db_handle), | 55 db_handle_(info.db_handle), |
55 download_manager_(download_manager), | 56 download_manager_(download_manager), |
56 is_paused_(false), | 57 is_paused_(false), |
57 open_when_complete_(false), | 58 open_when_complete_(false), |
58 safety_state_(SAFE), | 59 safety_state_(SAFE), |
59 auto_opened_(false), | 60 auto_opened_(false), |
60 original_name_(info.original_name), | 61 original_name_(info.original_name), |
(...skipping 17 matching lines...) Expand all Loading... |
78 bool is_otr) | 79 bool is_otr) |
79 : id_(info.download_id), | 80 : id_(info.download_id), |
80 full_path_(info.path), | 81 full_path_(info.path), |
81 path_uniquifier_(info.path_uniquifier), | 82 path_uniquifier_(info.path_uniquifier), |
82 url_(info.url), | 83 url_(info.url), |
83 referrer_url_(info.referrer_url), | 84 referrer_url_(info.referrer_url), |
84 mime_type_(info.mime_type), | 85 mime_type_(info.mime_type), |
85 original_mime_type_(info.original_mime_type), | 86 original_mime_type_(info.original_mime_type), |
86 total_bytes_(info.total_bytes), | 87 total_bytes_(info.total_bytes), |
87 received_bytes_(0), | 88 received_bytes_(0), |
| 89 last_os_error_(0), |
88 start_tick_(base::TimeTicks::Now()), | 90 start_tick_(base::TimeTicks::Now()), |
89 state_(IN_PROGRESS), | 91 state_(IN_PROGRESS), |
90 start_time_(info.start_time), | 92 start_time_(info.start_time), |
91 db_handle_(DownloadHistory::kUninitializedHandle), | 93 db_handle_(DownloadHistory::kUninitializedHandle), |
92 download_manager_(download_manager), | 94 download_manager_(download_manager), |
93 is_paused_(false), | 95 is_paused_(false), |
94 open_when_complete_(false), | 96 open_when_complete_(false), |
95 safety_state_(info.is_dangerous ? DANGEROUS : SAFE), | 97 safety_state_(info.is_dangerous ? DANGEROUS : SAFE), |
96 auto_opened_(false), | 98 auto_opened_(false), |
97 original_name_(info.original_name), | 99 original_name_(info.original_name), |
98 render_process_id_(info.child_id), | 100 render_process_id_(info.child_id), |
99 request_id_(info.request_id), | 101 request_id_(info.request_id), |
100 save_as_(info.prompt_user_for_save_location), | 102 save_as_(info.prompt_user_for_save_location), |
101 is_otr_(is_otr), | 103 is_otr_(is_otr), |
102 is_extension_install_(info.is_extension_install), | 104 is_extension_install_(info.is_extension_install), |
103 name_finalized_(false), | 105 name_finalized_(false), |
104 is_temporary_(!info.save_info.file_path.empty()), | 106 is_temporary_(!info.save_info.file_path.empty() && |
| 107 !info.is_partial_download), |
105 need_final_rename_(false), | 108 need_final_rename_(false), |
106 opened_(false) { | 109 opened_(false) { |
107 Init(true /* start progress timer */); | 110 Init(true /* start progress timer */); |
108 } | 111 } |
109 | 112 |
110 // Constructing for the "Save Page As..." feature: | 113 // Constructing for the "Save Page As..." feature: |
111 DownloadItem::DownloadItem(DownloadManager* download_manager, | 114 DownloadItem::DownloadItem(DownloadManager* download_manager, |
112 const FilePath& path, | 115 const FilePath& path, |
113 const GURL& url, | 116 const GURL& url, |
114 bool is_otr) | 117 bool is_otr) |
115 : id_(1), | 118 : id_(1), |
116 full_path_(path), | 119 full_path_(path), |
117 path_uniquifier_(0), | 120 path_uniquifier_(0), |
118 url_(url), | 121 url_(url), |
119 referrer_url_(GURL()), | 122 referrer_url_(GURL()), |
120 mime_type_(std::string()), | 123 mime_type_(std::string()), |
121 original_mime_type_(std::string()), | 124 original_mime_type_(std::string()), |
122 total_bytes_(0), | 125 total_bytes_(0), |
123 received_bytes_(0), | 126 received_bytes_(0), |
| 127 last_os_error_(0), |
124 start_tick_(base::TimeTicks::Now()), | 128 start_tick_(base::TimeTicks::Now()), |
125 state_(IN_PROGRESS), | 129 state_(IN_PROGRESS), |
126 start_time_(base::Time::Now()), | 130 start_time_(base::Time::Now()), |
127 db_handle_(DownloadHistory::kUninitializedHandle), | 131 db_handle_(DownloadHistory::kUninitializedHandle), |
128 download_manager_(download_manager), | 132 download_manager_(download_manager), |
129 is_paused_(false), | 133 is_paused_(false), |
130 open_when_complete_(false), | 134 open_when_complete_(false), |
131 safety_state_(SAFE), | 135 safety_state_(SAFE), |
132 auto_opened_(false), | 136 auto_opened_(false), |
133 original_name_(FilePath()), | 137 original_name_(FilePath()), |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 183 |
180 void DownloadItem::OpenFilesBasedOnExtension(bool open) { | 184 void DownloadItem::OpenFilesBasedOnExtension(bool open) { |
181 DownloadPrefs* prefs = download_manager_->download_prefs(); | 185 DownloadPrefs* prefs = download_manager_->download_prefs(); |
182 if (open) | 186 if (open) |
183 prefs->EnableAutoOpenBasedOnExtension(full_path()); | 187 prefs->EnableAutoOpenBasedOnExtension(full_path()); |
184 else | 188 else |
185 prefs->DisableAutoOpenBasedOnExtension(full_path()); | 189 prefs->DisableAutoOpenBasedOnExtension(full_path()); |
186 } | 190 } |
187 | 191 |
188 void DownloadItem::OpenDownload() { | 192 void DownloadItem::OpenDownload() { |
189 if (state() == DownloadItem::IN_PROGRESS) { | 193 if (state() == IN_PROGRESS) { |
190 open_when_complete_ = !open_when_complete_; | 194 open_when_complete_ = !open_when_complete_; |
191 } else if (state() == DownloadItem::COMPLETE) { | 195 } else if (state() == COMPLETE) { |
192 opened_ = true; | 196 opened_ = true; |
193 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 197 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
194 if (is_extension_install()) { | 198 if (is_extension_install()) { |
195 download_util::OpenChromeExtension(download_manager_->profile(), | 199 download_util::OpenChromeExtension(download_manager_->profile(), |
196 download_manager_, | 200 download_manager_, |
197 *this); | 201 *this); |
198 return; | 202 return; |
199 } | 203 } |
200 #if defined(OS_MACOSX) | 204 #if defined(OS_MACOSX) |
201 // Mac OS X requires opening downloads on the UI thread. | 205 // Mac OS X requires opening downloads on the UI thread. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 247 } |
244 | 248 |
245 // Updates from the download thread may have been posted while this download | 249 // Updates from the download thread may have been posted while this download |
246 // was being cancelled in the UI thread, so we'll accept them unless we're | 250 // was being cancelled in the UI thread, so we'll accept them unless we're |
247 // complete. | 251 // complete. |
248 void DownloadItem::Update(int64 bytes_so_far) { | 252 void DownloadItem::Update(int64 bytes_so_far) { |
249 if (state_ == COMPLETE) { | 253 if (state_ == COMPLETE) { |
250 NOTREACHED(); | 254 NOTREACHED(); |
251 return; | 255 return; |
252 } | 256 } |
| 257 // May get called once in the INTERRUPTED state. |
| 258 if (state_ == INTERRUPTED) |
| 259 return; |
253 UpdateSize(bytes_so_far); | 260 UpdateSize(bytes_so_far); |
254 UpdateObservers(); | 261 UpdateObservers(); |
255 } | 262 } |
256 | 263 |
257 // Triggered by a user action. | 264 // Triggered by a user action. |
258 void DownloadItem::Cancel(bool update_history) { | 265 void DownloadItem::Cancel(bool update_history) { |
259 if (state_ != IN_PROGRESS) { | 266 if (!IsPartialDownload()) { |
260 // Small downloads might be complete before this method has a chance to run. | 267 // Small downloads might be complete or interrupted before this method has |
| 268 // a chance to run. |
261 return; | 269 return; |
262 } | 270 } |
263 state_ = CANCELLED; | 271 state_ = CANCELLED; |
264 UpdateObservers(); | 272 UpdateObservers(); |
265 StopProgressTimer(); | 273 StopProgressTimer(); |
266 if (update_history) | 274 if (update_history) |
267 download_manager_->DownloadCancelled(id_); | 275 download_manager_->DownloadCancelled(id_); |
268 } | 276 } |
269 | 277 |
270 void DownloadItem::OnAllDataSaved(int64 size) { | 278 void DownloadItem::OnAllDataSaved(int64 size) { |
(...skipping 25 matching lines...) Expand all Loading... |
296 UpdateObservers(); | 304 UpdateObservers(); |
297 | 305 |
298 // The download file is meant to be completed if both the filename is | 306 // The download file is meant to be completed if both the filename is |
299 // finalized and the file data is downloaded. The ordering of these two | 307 // finalized and the file data is downloaded. The ordering of these two |
300 // actions is indeterministic. Thus, if the filename is not finalized yet, | 308 // actions is indeterministic. Thus, if the filename is not finalized yet, |
301 // delay the notification. | 309 // delay the notification. |
302 if (name_finalized()) | 310 if (name_finalized()) |
303 NotifyObserversDownloadFileCompleted(); | 311 NotifyObserversDownloadFileCompleted(); |
304 } | 312 } |
305 | 313 |
| 314 void DownloadItem::Interrupted(int64 size, int os_error) { |
| 315 if (state_ != IN_PROGRESS) |
| 316 return; |
| 317 state_ = INTERRUPTED; |
| 318 last_os_error_ = os_error; |
| 319 UpdateSize(size); |
| 320 StopProgressTimer(); |
| 321 } |
| 322 |
| 323 void DownloadItem::Resumed(int new_request_id) { |
| 324 if (state_ != INTERRUPTED) |
| 325 return; |
| 326 state_ = IN_PROGRESS; |
| 327 request_id_ = new_request_id; |
| 328 StartProgressTimer(); |
| 329 } |
| 330 |
306 void DownloadItem::Remove(bool delete_on_disk) { | 331 void DownloadItem::Remove(bool delete_on_disk) { |
307 Cancel(true); | 332 Cancel(true); |
308 state_ = REMOVING; | 333 state_ = REMOVING; |
309 if (delete_on_disk) { | 334 if (delete_on_disk) { |
310 ChromeThread::PostTask( | 335 ChromeThread::PostTask( |
311 ChromeThread::FILE, FROM_HERE, | 336 ChromeThread::FILE, FROM_HERE, |
312 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 337 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); |
313 } | 338 } |
314 download_manager_->RemoveDownload(db_handle_); | 339 download_manager_->RemoveDownload(db_handle_); |
315 // We have now been deleted. | 340 // We have now been deleted. |
(...skipping 25 matching lines...) Expand all Loading... |
341 return percent; | 366 return percent; |
342 } | 367 } |
343 | 368 |
344 void DownloadItem::Rename(const FilePath& full_path) { | 369 void DownloadItem::Rename(const FilePath& full_path) { |
345 DCHECK(!full_path.empty()); | 370 DCHECK(!full_path.empty()); |
346 full_path_ = full_path; | 371 full_path_ = full_path; |
347 file_name_ = full_path_.BaseName(); | 372 file_name_ = full_path_.BaseName(); |
348 } | 373 } |
349 | 374 |
350 void DownloadItem::TogglePause() { | 375 void DownloadItem::TogglePause() { |
351 DCHECK(state_ == IN_PROGRESS); | 376 DCHECK(IsPartialDownload()); |
352 download_manager_->PauseDownload(id_, !is_paused_); | 377 if (state_ == IN_PROGRESS) { |
353 is_paused_ = !is_paused_; | 378 download_manager_->PauseDownload(id_, !is_paused_); |
| 379 is_paused_ = !is_paused_; |
| 380 } else { |
| 381 download_manager_->RestartDownload(id_); |
| 382 } |
354 UpdateObservers(); | 383 UpdateObservers(); |
355 } | 384 } |
356 | 385 |
357 void DownloadItem::OnNameFinalized() { | 386 void DownloadItem::OnNameFinalized() { |
358 name_finalized_ = true; | 387 name_finalized_ = true; |
359 | 388 |
360 // The download file is meant to be completed if both the filename is | 389 // The download file is meant to be completed if both the filename is |
361 // finalized and the file data is downloaded. The ordering of these two | 390 // finalized and the file data is downloaded. The ordering of these two |
362 // actions is indeterministic. Thus, if we are still in downloading the | 391 // actions is indeterministic. Thus, if we are still in downloading the |
363 // file, delay the notification. | 392 // file, delay the notification. |
(...skipping 22 matching lines...) Expand all Loading... |
386 if (url_formatted.find(query) != string16::npos) | 415 if (url_formatted.find(query) != string16::npos) |
387 return true; | 416 return true; |
388 | 417 |
389 string16 path(l10n_util::ToLower(WideToUTF16(full_path_.ToWStringHack()))); | 418 string16 path(l10n_util::ToLower(WideToUTF16(full_path_.ToWStringHack()))); |
390 if (path.find(query) != std::wstring::npos) | 419 if (path.find(query) != std::wstring::npos) |
391 return true; | 420 return true; |
392 | 421 |
393 return false; | 422 return false; |
394 } | 423 } |
395 | 424 |
| 425 bool DownloadItem::IsPartialDownload() const { |
| 426 return (state_ == IN_PROGRESS) || (state_ == INTERRUPTED); |
| 427 } |
| 428 |
| 429 bool DownloadItem::IsInterruptedDownload() const { |
| 430 return (state_ == INTERRUPTED); |
| 431 } |
| 432 |
396 FilePath DownloadItem::GetFileName() const { | 433 FilePath DownloadItem::GetFileName() const { |
397 if (safety_state_ == DownloadItem::SAFE) | 434 if (safety_state_ == DownloadItem::SAFE) |
398 return file_name_; | 435 return file_name_; |
399 if (path_uniquifier_ > 0) { | 436 if (path_uniquifier_ > 0) { |
400 FilePath name(original_name_); | 437 FilePath name(original_name_); |
401 download_util::AppendNumberToPath(&name, path_uniquifier_); | 438 download_util::AppendNumberToPath(&name, path_uniquifier_); |
402 return name; | 439 return name; |
403 } | 440 } |
404 return original_name_; | 441 return original_name_; |
405 } | 442 } |
406 | 443 |
407 void DownloadItem::Init(bool start_timer) { | 444 void DownloadItem::Init(bool start_timer) { |
408 file_name_ = full_path_.BaseName(); | 445 file_name_ = full_path_.BaseName(); |
409 if (start_timer) | 446 if (start_timer) |
410 StartProgressTimer(); | 447 StartProgressTimer(); |
411 } | 448 } |
OLD | NEW |