Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: chrome/browser/download/download_item.cc

Issue 6905049: Detect removed files and reflect the state in chrome://downloads and the download shelf (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Remove REMOVED state from Download.States in downloads.js Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
215 DownloadItem::~DownloadItem() { 218 DownloadItem::~DownloadItem() {
216 state_ = REMOVING; 219 state_ = REMOVING;
217 UpdateObservers(); 220 UpdateObservers();
218 } 221 }
219 222
220 void DownloadItem::AddObserver(Observer* observer) { 223 void DownloadItem::AddObserver(Observer* observer) {
221 observers_.AddObserver(observer); 224 observers_.AddObserver(observer);
222 } 225 }
223 226
224 void DownloadItem::RemoveObserver(Observer* observer) { 227 void DownloadItem::RemoveObserver(Observer* observer) {
225 observers_.RemoveObserver(observer); 228 observers_.RemoveObserver(observer);
226 } 229 }
227 230
228 void DownloadItem::UpdateObservers() { 231 void DownloadItem::UpdateObservers() {
229 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); 232 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
230 } 233 }
231 234
235 bool DownloadItem::CanShowInFolder() {
236 return !IsCancelled() && !file_externally_removed_;
237 }
238
232 bool DownloadItem::CanOpenDownload() { 239 bool DownloadItem::CanOpenDownload() {
233 return !Extension::IsExtension(state_info_.target_name); 240 return !Extension::IsExtension(state_info_.target_name);
Paweł Hajdan Jr. 2011/06/08 09:58:41 Shouldn't you check file_externally_removed_ here?
haraken1 2011/06/08 10:35:07 I fixed it. (However, we may not have to check |
234 } 241 }
235 242
236 bool DownloadItem::ShouldOpenFileBasedOnExtension() { 243 bool DownloadItem::ShouldOpenFileBasedOnExtension() {
237 return download_manager_->ShouldOpenFileBasedOnExtension( 244 return download_manager_->ShouldOpenFileBasedOnExtension(
238 GetUserVerifiedFilePath()); 245 GetUserVerifiedFilePath());
239 } 246 }
240 247
241 void DownloadItem::OpenFilesBasedOnExtension(bool open) { 248 void DownloadItem::OpenFilesBasedOnExtension(bool open) {
242 DownloadPrefs* prefs = download_manager_->download_prefs(); 249 DownloadPrefs* prefs = download_manager_->download_prefs();
243 if (open) 250 if (open)
244 prefs->EnableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); 251 prefs->EnableAutoOpenBasedOnExtension(GetUserVerifiedFilePath());
245 else 252 else
246 prefs->DisableAutoOpenBasedOnExtension(GetUserVerifiedFilePath()); 253 prefs->DisableAutoOpenBasedOnExtension(GetUserVerifiedFilePath());
247 } 254 }
248 255
249 void DownloadItem::OpenDownload() { 256 void DownloadItem::OpenDownload() {
250 if (IsPartialDownload()) { 257 if (IsPartialDownload()) {
251 open_when_complete_ = !open_when_complete_; 258 open_when_complete_ = !open_when_complete_;
252 } else if (IsComplete()) { 259 } else if (IsComplete() && !file_externally_removed_) {
260 // Ideally, we want to detect errors in opening and report them, but we
261 // don't generally have the proper interface for that to the external
262 // program that opens the file. So instead we spawn a check to update
263 // the UI if the file has been deleted in parallel with the open.
264 download_manager_->CheckForFileRemoval(this);
253 opened_ = true; 265 opened_ = true;
254 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); 266 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
255 267
256 // For testing: If download opening is disabled on this item, 268 // For testing: If download opening is disabled on this item,
257 // make the rest of the routine a no-op. 269 // make the rest of the routine a no-op.
258 if (!open_enabled_) 270 if (!open_enabled_)
259 return; 271 return;
260 272
261 if (is_extension_install()) { 273 if (is_extension_install()) {
262 download_util::OpenChromeExtension(download_manager_->profile(), *this); 274 download_util::OpenChromeExtension(download_manager_->profile(), *this);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 UpdateObservers(); 358 UpdateObservers();
347 } 359 }
348 360
349 void DownloadItem::OnAllDataSaved(int64 size) { 361 void DownloadItem::OnAllDataSaved(int64 size) {
350 DCHECK(!all_data_saved_); 362 DCHECK(!all_data_saved_);
351 all_data_saved_ = true; 363 all_data_saved_ = true;
352 UpdateSize(size); 364 UpdateSize(size);
353 StopProgressTimer(); 365 StopProgressTimer();
354 } 366 }
355 367
368 void DownloadItem::OnDownloadedFileRemoved() {
369 file_externally_removed_ = true;
370 UpdateObservers();
371 }
372
356 void DownloadItem::Completed() { 373 void DownloadItem::Completed() {
357 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); 374 VLOG(20) << __FUNCTION__ << "() " << DebugString(false);
358 375
359 DCHECK(all_data_saved_); 376 DCHECK(all_data_saved_);
360 state_ = COMPLETE; 377 state_ = COMPLETE;
361 UpdateObservers(); 378 UpdateObservers();
362 download_manager_->DownloadCompleted(id()); 379 download_manager_->DownloadCompleted(id());
363 download_util::RecordDownloadCompleted(start_tick_); 380 download_util::RecordDownloadCompleted(start_tick_);
364 381
365 if (is_extension_install()) { 382 if (is_extension_install()) {
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 state_info_.target_name.value().c_str(), 737 state_info_.target_name.value().c_str(),
721 full_path().value().c_str()); 738 full_path().value().c_str());
722 } else { 739 } else {
723 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); 740 description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
724 } 741 }
725 742
726 description += " }"; 743 description += " }";
727 744
728 return description; 745 return description;
729 } 746 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698