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" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 referrer_url_(info.referrer_url), | 125 referrer_url_(info.referrer_url), |
126 total_bytes_(info.total_bytes), | 126 total_bytes_(info.total_bytes), |
127 received_bytes_(info.received_bytes), | 127 received_bytes_(info.received_bytes), |
128 start_tick_(base::TimeTicks()), | 128 start_tick_(base::TimeTicks()), |
129 state_(static_cast<DownloadState>(info.state)), | 129 state_(static_cast<DownloadState>(info.state)), |
130 start_time_(info.start_time), | 130 start_time_(info.start_time), |
131 db_handle_(info.db_handle), | 131 db_handle_(info.db_handle), |
132 download_manager_(download_manager), | 132 download_manager_(download_manager), |
133 is_paused_(false), | 133 is_paused_(false), |
134 open_when_complete_(false), | 134 open_when_complete_(false), |
| 135 file_externally_removed_(false), |
135 safety_state_(SAFE), | 136 safety_state_(SAFE), |
136 auto_opened_(false), | 137 auto_opened_(false), |
137 is_otr_(false), | 138 is_otr_(false), |
138 is_temporary_(false), | 139 is_temporary_(false), |
139 all_data_saved_(false), | 140 all_data_saved_(false), |
140 opened_(false), | 141 opened_(false), |
141 open_enabled_(true) { | 142 open_enabled_(true) { |
142 if (IsInProgress()) | 143 if (IsInProgress()) |
143 state_ = CANCELLED; | 144 state_ = CANCELLED; |
144 if (IsComplete()) | 145 if (IsComplete()) |
(...skipping 21 matching lines...) Expand all Loading... |
166 total_bytes_(info.total_bytes), | 167 total_bytes_(info.total_bytes), |
167 received_bytes_(0), | 168 received_bytes_(0), |
168 last_os_error_(0), | 169 last_os_error_(0), |
169 start_tick_(base::TimeTicks::Now()), | 170 start_tick_(base::TimeTicks::Now()), |
170 state_(IN_PROGRESS), | 171 state_(IN_PROGRESS), |
171 start_time_(info.start_time), | 172 start_time_(info.start_time), |
172 db_handle_(DownloadHistory::kUninitializedHandle), | 173 db_handle_(DownloadHistory::kUninitializedHandle), |
173 download_manager_(download_manager), | 174 download_manager_(download_manager), |
174 is_paused_(false), | 175 is_paused_(false), |
175 open_when_complete_(false), | 176 open_when_complete_(false), |
| 177 file_externally_removed_(false), |
176 safety_state_(SAFE), | 178 safety_state_(SAFE), |
177 auto_opened_(false), | 179 auto_opened_(false), |
178 is_otr_(is_otr), | 180 is_otr_(is_otr), |
179 is_temporary_(!info.save_info.file_path.empty()), | 181 is_temporary_(!info.save_info.file_path.empty()), |
180 all_data_saved_(false), | 182 all_data_saved_(false), |
181 opened_(false), | 183 opened_(false), |
182 open_enabled_(true) { | 184 open_enabled_(true) { |
183 Init(true /* actively downloading */); | 185 Init(true /* actively downloading */); |
184 } | 186 } |
185 | 187 |
186 // Constructing for the "Save Page As..." feature: | 188 // Constructing for the "Save Page As..." feature: |
187 DownloadItem::DownloadItem(DownloadManager* download_manager, | 189 DownloadItem::DownloadItem(DownloadManager* download_manager, |
188 const FilePath& path, | 190 const FilePath& path, |
189 const GURL& url, | 191 const GURL& url, |
190 bool is_otr) | 192 bool is_otr) |
191 : download_id_(1), | 193 : download_id_(1), |
192 full_path_(path), | 194 full_path_(path), |
193 url_chain_(1, url), | 195 url_chain_(1, url), |
194 referrer_url_(GURL()), | 196 referrer_url_(GURL()), |
195 total_bytes_(0), | 197 total_bytes_(0), |
196 received_bytes_(0), | 198 received_bytes_(0), |
197 last_os_error_(0), | 199 last_os_error_(0), |
198 start_tick_(base::TimeTicks::Now()), | 200 start_tick_(base::TimeTicks::Now()), |
199 state_(IN_PROGRESS), | 201 state_(IN_PROGRESS), |
200 start_time_(base::Time::Now()), | 202 start_time_(base::Time::Now()), |
201 db_handle_(DownloadHistory::kUninitializedHandle), | 203 db_handle_(DownloadHistory::kUninitializedHandle), |
202 download_manager_(download_manager), | 204 download_manager_(download_manager), |
203 is_paused_(false), | 205 is_paused_(false), |
204 open_when_complete_(false), | 206 open_when_complete_(false), |
| 207 file_externally_removed_(false), |
205 safety_state_(SAFE), | 208 safety_state_(SAFE), |
206 auto_opened_(false), | 209 auto_opened_(false), |
207 is_otr_(is_otr), | 210 is_otr_(is_otr), |
208 is_temporary_(false), | 211 is_temporary_(false), |
209 all_data_saved_(false), | 212 all_data_saved_(false), |
210 opened_(false), | 213 opened_(false), |
211 open_enabled_(true) { | 214 open_enabled_(true) { |
212 Init(true /* actively downloading */); | 215 Init(true /* actively downloading */); |
213 } | 216 } |
214 | 217 |
(...skipping 20 matching lines...) Expand all Loading... |
235 observers_.RemoveObserver(observer); | 238 observers_.RemoveObserver(observer); |
236 } | 239 } |
237 | 240 |
238 void DownloadItem::UpdateObservers() { | 241 void DownloadItem::UpdateObservers() { |
239 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 242 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
240 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 243 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
241 | 244 |
242 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 245 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
243 } | 246 } |
244 | 247 |
| 248 bool DownloadItem::CanShowInFolder() { |
| 249 return !IsCancelled() && !file_externally_removed_; |
| 250 } |
| 251 |
245 bool DownloadItem::CanOpenDownload() { | 252 bool DownloadItem::CanOpenDownload() { |
246 return !Extension::IsExtension(state_info_.target_name); | 253 return !Extension::IsExtension(state_info_.target_name) && |
| 254 !file_externally_removed_; |
247 } | 255 } |
248 | 256 |
249 bool DownloadItem::ShouldOpenFileBasedOnExtension() { | 257 bool DownloadItem::ShouldOpenFileBasedOnExtension() { |
250 return download_manager_->ShouldOpenFileBasedOnExtension( | 258 return download_manager_->ShouldOpenFileBasedOnExtension( |
251 GetUserVerifiedFilePath()); | 259 GetUserVerifiedFilePath()); |
252 } | 260 } |
253 | 261 |
254 void DownloadItem::OpenFilesBasedOnExtension(bool open) { | 262 void DownloadItem::OpenFilesBasedOnExtension(bool open) { |
255 DownloadPrefs* prefs = download_manager_->download_prefs(); | 263 DownloadPrefs* prefs = download_manager_->download_prefs(); |
256 if (open) | 264 if (open) |
257 prefs->EnableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); | 265 prefs->EnableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); |
258 else | 266 else |
259 prefs->DisableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); | 267 prefs->DisableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); |
260 } | 268 } |
261 | 269 |
262 void DownloadItem::OpenDownload() { | 270 void DownloadItem::OpenDownload() { |
263 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 271 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
264 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 272 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
265 | 273 |
266 if (IsPartialDownload()) { | 274 if (IsPartialDownload()) { |
267 open_when_complete_ = !open_when_complete_; | 275 open_when_complete_ = !open_when_complete_; |
268 } else if (IsComplete()) { | 276 } else if (IsComplete() && !file_externally_removed_) { |
| 277 // Ideally, we want to detect errors in opening and report them, but we |
| 278 // don't generally have the proper interface for that to the external |
| 279 // program that opens the file. So instead we spawn a check to update |
| 280 // the UI if the file has been deleted in parallel with the open. |
| 281 download_manager_->CheckForFileRemoval(this); |
269 opened_ = true; | 282 opened_ = true; |
270 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 283 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
271 | 284 |
272 // For testing: If download opening is disabled on this item, | 285 // For testing: If download opening is disabled on this item, |
273 // make the rest of the routine a no-op. | 286 // make the rest of the routine a no-op. |
274 if (!open_enabled_) | 287 if (!open_enabled_) |
275 return; | 288 return; |
276 | 289 |
277 if (is_extension_install()) { | 290 if (is_extension_install()) { |
278 download_util::OpenChromeExtension(download_manager_->profile(), *this); | 291 download_util::OpenChromeExtension(download_manager_->profile(), *this); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 void DownloadItem::OnAllDataSaved(int64 size) { | 402 void DownloadItem::OnAllDataSaved(int64 size) { |
390 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 403 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
391 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
392 | 405 |
393 DCHECK(!all_data_saved_); | 406 DCHECK(!all_data_saved_); |
394 all_data_saved_ = true; | 407 all_data_saved_ = true; |
395 UpdateSize(size); | 408 UpdateSize(size); |
396 StopProgressTimer(); | 409 StopProgressTimer(); |
397 } | 410 } |
398 | 411 |
| 412 void DownloadItem::OnDownloadedFileRemoved() { |
| 413 file_externally_removed_ = true; |
| 414 UpdateObservers(); |
| 415 } |
| 416 |
399 void DownloadItem::Completed() { | 417 void DownloadItem::Completed() { |
400 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 418 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
401 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 419 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
402 | 420 |
403 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 421 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
404 | 422 |
405 DCHECK(all_data_saved_); | 423 DCHECK(all_data_saved_); |
406 state_ = COMPLETE; | 424 state_ = COMPLETE; |
407 UpdateObservers(); | 425 UpdateObservers(); |
408 download_manager_->DownloadCompleted(id()); | 426 download_manager_->DownloadCompleted(id()); |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 state_info_.target_name.value().c_str(), | 829 state_info_.target_name.value().c_str(), |
812 full_path().value().c_str()); | 830 full_path().value().c_str()); |
813 } else { | 831 } else { |
814 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 832 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
815 } | 833 } |
816 | 834 |
817 description += " }"; | 835 description += " }"; |
818 | 836 |
819 return description; | 837 return description; |
820 } | 838 } |
OLD | NEW |