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/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/timer.h" | 13 #include "base/timer.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
16 #include "chrome/browser/browser_thread.h" | 16 #include "chrome/browser/browser_thread.h" |
17 #include "chrome/browser/download/download_extensions.h" | 17 #include "chrome/browser/download/download_extensions.h" |
18 #include "chrome/browser/download/download_file_manager.h" | 18 #include "chrome/browser/download/download_file_manager.h" |
19 #include "chrome/browser/download/download_history.h" | 19 #include "chrome/browser/download/download_history.h" |
20 #include "chrome/browser/download/download_manager.h" | 20 #include "chrome/browser/download/download_manager.h" |
21 #include "chrome/browser/download/download_prefs.h" | 21 #include "chrome/browser/download/download_prefs.h" |
22 #include "chrome/browser/download/download_util.h" | 22 #include "chrome/browser/download/download_util.h" |
23 #include "chrome/browser/history/download_create_info.h" | 23 #include "chrome/browser/history/download_create_info.h" |
24 #include "chrome/browser/platform_util.h" | 24 #include "chrome/browser/platform_util.h" |
25 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 | 29 |
| 30 // A DownloadItem normally goes through the following states: |
| 31 // * Created (when download starts) |
| 32 // * Made visible to consumers (e.g. Javascript) after the |
| 33 // destination file has been determined. |
| 34 // * Entered into the history database. |
| 35 // * Made visible in the download shelf. |
| 36 // * All data is received. Note that the actual data download occurs |
| 37 // in parallel with the above steps, but until those steps are |
| 38 // complete, completion of the data download will be ignored. |
| 39 // * Download file is renamed to its final name, and possibly |
| 40 // auto-opened. |
| 41 // TODO(rdsmith): This progress should be reflected in |
| 42 // DownloadItem::DownloadState and a state transition table/state diagram. |
| 43 // |
| 44 // TODO(rdsmith): This description should be updated to reflect the cancel |
| 45 // pathways. |
| 46 |
30 namespace { | 47 namespace { |
31 | 48 |
32 // Update frequency (milliseconds). | 49 // Update frequency (milliseconds). |
33 const int kUpdateTimeMs = 1000; | 50 const int kUpdateTimeMs = 1000; |
34 | 51 |
35 void DeleteDownloadedFile(const FilePath& path) { | 52 void DeleteDownloadedFile(const FilePath& path) { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
37 | 54 |
38 // Make sure we only delete files. | 55 // Make sure we only delete files. |
39 if (!file_util::DirectoryExists(path)) | 56 if (!file_util::DirectoryExists(path)) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 safety_state_(SAFE), | 111 safety_state_(SAFE), |
95 auto_opened_(false), | 112 auto_opened_(false), |
96 target_name_(info.original_name), | 113 target_name_(info.original_name), |
97 render_process_id_(-1), | 114 render_process_id_(-1), |
98 request_id_(-1), | 115 request_id_(-1), |
99 save_as_(false), | 116 save_as_(false), |
100 is_otr_(false), | 117 is_otr_(false), |
101 is_extension_install_(info.is_extension_install), | 118 is_extension_install_(info.is_extension_install), |
102 name_finalized_(false), | 119 name_finalized_(false), |
103 is_temporary_(false), | 120 is_temporary_(false), |
| 121 all_data_saved_(false), |
104 opened_(false) { | 122 opened_(false) { |
105 if (state_ == IN_PROGRESS) | 123 if (state_ == IN_PROGRESS) |
106 state_ = CANCELLED; | 124 state_ = CANCELLED; |
| 125 if (state_ == COMPLETE) |
| 126 all_data_saved_ = true; |
107 Init(false /* don't start progress timer */); | 127 Init(false /* don't start progress timer */); |
108 } | 128 } |
109 | 129 |
110 // Constructing for a regular download: | 130 // Constructing for a regular download: |
111 DownloadItem::DownloadItem(DownloadManager* download_manager, | 131 DownloadItem::DownloadItem(DownloadManager* download_manager, |
112 const DownloadCreateInfo& info, | 132 const DownloadCreateInfo& info, |
113 bool is_otr) | 133 bool is_otr) |
114 : id_(info.download_id), | 134 : id_(info.download_id), |
115 full_path_(info.path), | 135 full_path_(info.path), |
116 path_uniquifier_(info.path_uniquifier), | 136 path_uniquifier_(info.path_uniquifier), |
(...skipping 13 matching lines...) Expand all Loading... |
130 safety_state_(info.is_dangerous ? DANGEROUS : SAFE), | 150 safety_state_(info.is_dangerous ? DANGEROUS : SAFE), |
131 auto_opened_(false), | 151 auto_opened_(false), |
132 target_name_(info.original_name), | 152 target_name_(info.original_name), |
133 render_process_id_(info.child_id), | 153 render_process_id_(info.child_id), |
134 request_id_(info.request_id), | 154 request_id_(info.request_id), |
135 save_as_(info.prompt_user_for_save_location), | 155 save_as_(info.prompt_user_for_save_location), |
136 is_otr_(is_otr), | 156 is_otr_(is_otr), |
137 is_extension_install_(info.is_extension_install), | 157 is_extension_install_(info.is_extension_install), |
138 name_finalized_(false), | 158 name_finalized_(false), |
139 is_temporary_(!info.save_info.file_path.empty()), | 159 is_temporary_(!info.save_info.file_path.empty()), |
| 160 all_data_saved_(false), |
140 opened_(false) { | 161 opened_(false) { |
141 Init(true /* start progress timer */); | 162 Init(true /* start progress timer */); |
142 } | 163 } |
143 | 164 |
144 // Constructing for the "Save Page As..." feature: | 165 // Constructing for the "Save Page As..." feature: |
145 DownloadItem::DownloadItem(DownloadManager* download_manager, | 166 DownloadItem::DownloadItem(DownloadManager* download_manager, |
146 const FilePath& path, | 167 const FilePath& path, |
147 const GURL& url, | 168 const GURL& url, |
148 bool is_otr) | 169 bool is_otr) |
149 : id_(1), | 170 : id_(1), |
(...skipping 14 matching lines...) Expand all Loading... |
164 open_when_complete_(false), | 185 open_when_complete_(false), |
165 safety_state_(SAFE), | 186 safety_state_(SAFE), |
166 auto_opened_(false), | 187 auto_opened_(false), |
167 render_process_id_(-1), | 188 render_process_id_(-1), |
168 request_id_(-1), | 189 request_id_(-1), |
169 save_as_(false), | 190 save_as_(false), |
170 is_otr_(is_otr), | 191 is_otr_(is_otr), |
171 is_extension_install_(false), | 192 is_extension_install_(false), |
172 name_finalized_(false), | 193 name_finalized_(false), |
173 is_temporary_(false), | 194 is_temporary_(false), |
| 195 all_data_saved_(false), |
174 opened_(false) { | 196 opened_(false) { |
175 Init(true /* start progress timer */); | 197 Init(true /* start progress timer */); |
176 } | 198 } |
177 | 199 |
178 DownloadItem::~DownloadItem() { | 200 DownloadItem::~DownloadItem() { |
179 state_ = REMOVING; | 201 state_ = REMOVING; |
180 UpdateObservers(); | 202 UpdateObservers(); |
181 } | 203 } |
182 | 204 |
183 void DownloadItem::AddObserver(Observer* observer) { | 205 void DownloadItem::AddObserver(Observer* observer) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 // Small downloads might be complete before this method has a chance to run. | 311 // Small downloads might be complete before this method has a chance to run. |
290 return; | 312 return; |
291 } | 313 } |
292 state_ = CANCELLED; | 314 state_ = CANCELLED; |
293 UpdateObservers(); | 315 UpdateObservers(); |
294 StopProgressTimer(); | 316 StopProgressTimer(); |
295 if (update_history) | 317 if (update_history) |
296 download_manager_->DownloadCancelled(id_); | 318 download_manager_->DownloadCancelled(id_); |
297 } | 319 } |
298 | 320 |
| 321 void DownloadItem::MarkAsComplete() { |
| 322 DCHECK(all_data_saved_); |
| 323 state_ = COMPLETE; |
| 324 } |
| 325 |
299 void DownloadItem::OnAllDataSaved(int64 size) { | 326 void DownloadItem::OnAllDataSaved(int64 size) { |
300 state_ = COMPLETE; | 327 DCHECK(!all_data_saved_); |
| 328 all_data_saved_ = true; |
301 UpdateSize(size); | 329 UpdateSize(size); |
302 StopProgressTimer(); | 330 StopProgressTimer(); |
303 } | 331 } |
304 | 332 |
305 void DownloadItem::Finished() { | 333 void DownloadItem::Finished() { |
306 // Handle chrome extensions explicitly and skip the shell execute. | 334 // Handle chrome extensions explicitly and skip the shell execute. |
307 if (is_extension_install()) { | 335 if (is_extension_install()) { |
308 download_util::OpenChromeExtension(download_manager_->profile(), | 336 download_util::OpenChromeExtension(download_manager_->profile(), |
309 download_manager_, | 337 download_manager_, |
310 *this); | 338 *this); |
311 auto_opened_ = true; | 339 auto_opened_ = true; |
312 } else if (open_when_complete() || | 340 } else if (open_when_complete() || |
313 download_manager_->ShouldOpenFileBasedOnExtension( | 341 download_manager_->ShouldOpenFileBasedOnExtension( |
314 GetUserVerifiedFilePath()) || | 342 GetUserVerifiedFilePath()) || |
315 is_temporary()) { | 343 is_temporary()) { |
316 // If the download is temporary, like in drag-and-drop, do not open it but | 344 // If the download is temporary, like in drag-and-drop, do not open it but |
317 // we still need to set it auto-opened so that it can be removed from the | 345 // we still need to set it auto-opened so that it can be removed from the |
318 // download shelf. | 346 // download shelf. |
319 if (!is_temporary()) | 347 if (!is_temporary()) |
320 OpenDownload(); | 348 OpenDownload(); |
321 auto_opened_ = true; | 349 auto_opened_ = true; |
322 } | 350 } |
323 | 351 |
324 // Notify our observers that we are complete (the call to OnAllDataSaved() | 352 // Notify our observers that we are complete (the call to MarkAsComplete() |
325 // set the state to complete but did not notify). | 353 // set the state to complete but did not notify). |
326 UpdateObservers(); | 354 UpdateObservers(); |
327 | 355 |
328 // The download file is meant to be completed if both the filename is | 356 // The download file is meant to be completed if both the filename is |
329 // finalized and the file data is downloaded. The ordering of these two | 357 // finalized and the file data is downloaded. The ordering of these two |
330 // actions is indeterministic. Thus, if the filename is not finalized yet, | 358 // actions is indeterministic. Thus, if the filename is not finalized yet, |
331 // delay the notification. | 359 // delay the notification. |
332 if (name_finalized()) { | 360 if (name_finalized()) { |
333 NotifyObserversDownloadFileCompleted(); | 361 NotifyObserversDownloadFileCompleted(); |
334 download_manager_->RemoveFromActiveList(id()); | 362 download_manager_->RemoveFromActiveList(id()); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 target_name_.value().c_str(), | 575 target_name_.value().c_str(), |
548 full_path().value().c_str()); | 576 full_path().value().c_str()); |
549 } else { | 577 } else { |
550 description += base::StringPrintf(" url = \"%s\"", url().spec().c_str()); | 578 description += base::StringPrintf(" url = \"%s\"", url().spec().c_str()); |
551 } | 579 } |
552 | 580 |
553 description += " }"; | 581 description += " }"; |
554 | 582 |
555 return description; | 583 return description; |
556 } | 584 } |
OLD | NEW |