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 "content/browser/download/download_item.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 return NULL; | 109 return NULL; |
110 } | 110 } |
111 virtual void PauseRequest() const OVERRIDE {} | 111 virtual void PauseRequest() const OVERRIDE {} |
112 virtual void ResumeRequest() const OVERRIDE {} | 112 virtual void ResumeRequest() const OVERRIDE {} |
113 virtual void CancelRequest() const OVERRIDE {} | 113 virtual void CancelRequest() const OVERRIDE {} |
114 virtual std::string DebugString() const OVERRIDE { | 114 virtual std::string DebugString() const OVERRIDE { |
115 return "Null DownloadRequestHandle"; | 115 return "Null DownloadRequestHandle"; |
116 } | 116 } |
117 }; | 117 }; |
118 | 118 |
119 | |
120 } // namespace | 119 } // namespace |
121 | 120 |
122 // Our download table ID starts at 1, so we use 0 to represent a download that | 121 // Our download table ID starts at 1, so we use 0 to represent a download that |
123 // has started, but has not yet had its data persisted in the table. We use fake | 122 // has started, but has not yet had its data persisted in the table. We use fake |
124 // database handles in incognito mode starting at -1 and progressively getting | 123 // database handles in incognito mode starting at -1 and progressively getting |
125 // more negative. | 124 // more negative. |
126 // static | |
127 const int DownloadItem::kUninitializedHandle = 0; | |
128 | |
129 const char DownloadItem::kEmptyFileHash[] = ""; | |
130 | 125 |
131 // Constructor for reading from the history service. | 126 // Constructor for reading from the history service. |
132 DownloadItem::DownloadItem(DownloadManager* download_manager, | 127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
133 const DownloadPersistentStoreInfo& info) | 128 const DownloadPersistentStoreInfo& info) |
134 : download_id_(download_manager->GetNextId()), | 129 : download_id_(download_manager->GetNextId()), |
135 full_path_(info.path), | 130 full_path_(info.path), |
136 url_chain_(1, info.url), | 131 url_chain_(1, info.url), |
137 referrer_url_(info.referrer_url), | 132 referrer_url_(info.referrer_url), |
138 total_bytes_(info.total_bytes), | 133 total_bytes_(info.total_bytes), |
139 received_bytes_(info.received_bytes), | 134 received_bytes_(info.received_bytes), |
140 start_tick_(base::TimeTicks()), | 135 start_tick_(base::TimeTicks()), |
141 state_(static_cast<DownloadState>(info.state)), | 136 state_(static_cast<DownloadState>(info.state)), |
142 start_time_(info.start_time), | 137 start_time_(info.start_time), |
143 end_time_(info.end_time), | 138 end_time_(info.end_time), |
(...skipping 11 matching lines...) Expand all Loading... |
155 open_enabled_(true), | 150 open_enabled_(true), |
156 delegate_delayed_complete_(false) { | 151 delegate_delayed_complete_(false) { |
157 if (IsInProgress()) | 152 if (IsInProgress()) |
158 state_ = CANCELLED; | 153 state_ = CANCELLED; |
159 if (IsComplete()) | 154 if (IsComplete()) |
160 all_data_saved_ = true; | 155 all_data_saved_ = true; |
161 Init(false /* not actively downloading */); | 156 Init(false /* not actively downloading */); |
162 } | 157 } |
163 | 158 |
164 // Constructing for a regular download: | 159 // Constructing for a regular download: |
165 DownloadItem::DownloadItem(DownloadManager* download_manager, | 160 DownloadItemImpl::DownloadItemImpl( |
166 const DownloadCreateInfo& info, | 161 DownloadManager* download_manager, |
167 DownloadRequestHandleInterface* request_handle, | 162 const DownloadCreateInfo& info, |
168 bool is_otr) | 163 DownloadRequestHandleInterface* request_handle, |
| 164 bool is_otr) |
169 : state_info_(info.original_name, info.save_info.file_path, | 165 : state_info_(info.original_name, info.save_info.file_path, |
170 info.has_user_gesture, info.transition_type, | 166 info.has_user_gesture, info.transition_type, |
171 info.prompt_user_for_save_location, info.path_uniquifier, | 167 info.prompt_user_for_save_location, info.path_uniquifier, |
172 DownloadStateInfo::NOT_DANGEROUS), | 168 DownloadStateInfo::NOT_DANGEROUS), |
173 request_handle_(request_handle), | 169 request_handle_(request_handle), |
174 download_id_(info.download_id), | 170 download_id_(info.download_id), |
175 full_path_(info.path), | 171 full_path_(info.path), |
176 url_chain_(info.url_chain), | 172 url_chain_(info.url_chain), |
177 referrer_url_(info.referrer_url), | 173 referrer_url_(info.referrer_url), |
178 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 174 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
(...skipping 17 matching lines...) Expand all Loading... |
196 is_otr_(is_otr), | 192 is_otr_(is_otr), |
197 is_temporary_(!info.save_info.file_path.empty()), | 193 is_temporary_(!info.save_info.file_path.empty()), |
198 all_data_saved_(false), | 194 all_data_saved_(false), |
199 opened_(false), | 195 opened_(false), |
200 open_enabled_(true), | 196 open_enabled_(true), |
201 delegate_delayed_complete_(false) { | 197 delegate_delayed_complete_(false) { |
202 Init(true /* actively downloading */); | 198 Init(true /* actively downloading */); |
203 } | 199 } |
204 | 200 |
205 // Constructing for the "Save Page As..." feature: | 201 // Constructing for the "Save Page As..." feature: |
206 DownloadItem::DownloadItem(DownloadManager* download_manager, | 202 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
207 const FilePath& path, | 203 const FilePath& path, |
208 const GURL& url, | 204 const GURL& url, |
209 bool is_otr, | 205 bool is_otr, |
210 DownloadId download_id) | 206 DownloadId download_id) |
211 : request_handle_(new NullDownloadRequestHandle()), | 207 : request_handle_(new NullDownloadRequestHandle()), |
212 download_id_(download_id), | 208 download_id_(download_id), |
213 full_path_(path), | 209 full_path_(path), |
214 url_chain_(1, url), | 210 url_chain_(1, url), |
215 referrer_url_(GURL()), | 211 referrer_url_(GURL()), |
216 total_bytes_(0), | 212 total_bytes_(0), |
217 received_bytes_(0), | 213 received_bytes_(0), |
218 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 214 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
219 start_tick_(base::TimeTicks::Now()), | 215 start_tick_(base::TimeTicks::Now()), |
220 state_(IN_PROGRESS), | 216 state_(IN_PROGRESS), |
221 start_time_(base::Time::Now()), | 217 start_time_(base::Time::Now()), |
222 db_handle_(DownloadItem::kUninitializedHandle), | 218 db_handle_(DownloadItem::kUninitializedHandle), |
223 download_manager_(download_manager), | 219 download_manager_(download_manager), |
224 is_paused_(false), | 220 is_paused_(false), |
225 open_when_complete_(false), | 221 open_when_complete_(false), |
226 file_externally_removed_(false), | 222 file_externally_removed_(false), |
227 safety_state_(SAFE), | 223 safety_state_(SAFE), |
228 auto_opened_(false), | 224 auto_opened_(false), |
229 is_otr_(is_otr), | 225 is_otr_(is_otr), |
230 is_temporary_(false), | 226 is_temporary_(false), |
231 all_data_saved_(false), | 227 all_data_saved_(false), |
232 opened_(false), | 228 opened_(false), |
233 open_enabled_(true), | 229 open_enabled_(true), |
234 delegate_delayed_complete_(false) { | 230 delegate_delayed_complete_(false) { |
235 Init(true /* actively downloading */); | 231 Init(true /* actively downloading */); |
236 } | 232 } |
237 | 233 |
238 DownloadItem::~DownloadItem() { | 234 DownloadItemImpl::~DownloadItemImpl() { |
239 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 235 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
240 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 236 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
241 | 237 |
242 TransitionTo(REMOVING); | 238 TransitionTo(REMOVING); |
243 download_manager_->AssertQueueStateConsistent(this); | 239 download_manager_->AssertQueueStateConsistent(this); |
244 } | 240 } |
245 | 241 |
246 void DownloadItem::AddObserver(Observer* observer) { | 242 void DownloadItemImpl::AddObserver(Observer* observer) { |
247 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 243 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 244 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
249 | 245 |
250 observers_.AddObserver(observer); | 246 observers_.AddObserver(observer); |
251 } | 247 } |
252 | 248 |
253 void DownloadItem::RemoveObserver(Observer* observer) { | 249 void DownloadItemImpl::RemoveObserver(Observer* observer) { |
254 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 250 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
255 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 251 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
256 | 252 |
257 observers_.RemoveObserver(observer); | 253 observers_.RemoveObserver(observer); |
258 } | 254 } |
259 | 255 |
260 void DownloadItem::UpdateObservers() { | 256 void DownloadItemImpl::UpdateObservers() { |
261 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 257 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
262 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 258 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
263 | 259 |
264 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 260 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
265 } | 261 } |
266 | 262 |
267 bool DownloadItem::CanShowInFolder() { | 263 bool DownloadItemImpl::CanShowInFolder() { |
268 return !IsCancelled() && !file_externally_removed_; | 264 return !IsCancelled() && !file_externally_removed_; |
269 } | 265 } |
270 | 266 |
271 bool DownloadItem::CanOpenDownload() { | 267 bool DownloadItemImpl::CanOpenDownload() { |
272 return !file_externally_removed_; | 268 return !file_externally_removed_; |
273 } | 269 } |
274 | 270 |
275 bool DownloadItem::ShouldOpenFileBasedOnExtension() { | 271 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { |
276 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( | 272 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( |
277 GetUserVerifiedFilePath()); | 273 GetUserVerifiedFilePath()); |
278 } | 274 } |
279 | 275 |
280 void DownloadItem::OpenDownload() { | 276 void DownloadItemImpl::OpenDownload() { |
281 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 277 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
282 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 278 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
283 | 279 |
284 if (IsPartialDownload()) { | 280 if (IsPartialDownload()) { |
285 open_when_complete_ = !open_when_complete_; | 281 open_when_complete_ = !open_when_complete_; |
286 return; | 282 return; |
287 } | 283 } |
288 | 284 |
289 if (!IsComplete() || file_externally_removed_) | 285 if (!IsComplete() || file_externally_removed_) |
290 return; | 286 return; |
291 | 287 |
292 // Ideally, we want to detect errors in opening and report them, but we | 288 // Ideally, we want to detect errors in opening and report them, but we |
293 // don't generally have the proper interface for that to the external | 289 // don't generally have the proper interface for that to the external |
294 // program that opens the file. So instead we spawn a check to update | 290 // program that opens the file. So instead we spawn a check to update |
295 // the UI if the file has been deleted in parallel with the open. | 291 // the UI if the file has been deleted in parallel with the open. |
296 download_manager_->CheckForFileRemoval(this); | 292 download_manager_->CheckForFileRemoval(this); |
297 download_stats::RecordOpen(end_time(), !opened()); | 293 download_stats::RecordOpen(GetEndTime(), !GetOpened()); |
298 opened_ = true; | 294 opened_ = true; |
299 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 295 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
300 download_manager_->MarkDownloadOpened(this); | 296 download_manager_->MarkDownloadOpened(this); |
301 | 297 |
302 // For testing: If download opening is disabled on this item, | 298 // For testing: If download opening is disabled on this item, |
303 // make the rest of the routine a no-op. | 299 // make the rest of the routine a no-op. |
304 if (!open_enabled_) | 300 if (!open_enabled_) |
305 return; | 301 return; |
306 | 302 |
307 content::GetContentClient()->browser()->OpenItem(full_path()); | 303 content::GetContentClient()->browser()->OpenItem(GetFullPath()); |
308 } | 304 } |
309 | 305 |
310 void DownloadItem::ShowDownloadInShell() { | 306 void DownloadItemImpl::ShowDownloadInShell() { |
311 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 307 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
312 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 308 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
313 | 309 |
314 content::GetContentClient()->browser()->ShowItemInFolder(full_path()); | 310 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); |
315 } | 311 } |
316 | 312 |
317 void DownloadItem::DangerousDownloadValidated() { | 313 void DownloadItemImpl::DangerousDownloadValidated() { |
318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
319 DCHECK_EQ(DANGEROUS, safety_state()); | 315 DCHECK_EQ(DANGEROUS, GetSafetyState()); |
320 | 316 |
321 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 317 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
322 GetDangerType(), | 318 GetDangerType(), |
323 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 319 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
324 | 320 |
325 safety_state_ = DANGEROUS_BUT_VALIDATED; | 321 safety_state_ = DANGEROUS_BUT_VALIDATED; |
326 UpdateObservers(); | 322 UpdateObservers(); |
327 | 323 |
328 download_manager_->MaybeCompleteDownload(this); | 324 download_manager_->MaybeCompleteDownload(this); |
329 } | 325 } |
330 | 326 |
331 void DownloadItem::UpdateSize(int64 bytes_so_far) { | 327 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { |
332 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 328 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
333 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 329 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
334 | 330 |
335 received_bytes_ = bytes_so_far; | 331 received_bytes_ = bytes_so_far; |
336 | 332 |
337 // If we've received more data than we were expecting (bad server info?), | 333 // If we've received more data than we were expecting (bad server info?), |
338 // revert to 'unknown size mode'. | 334 // revert to 'unknown size mode'. |
339 if (received_bytes_ > total_bytes_) | 335 if (received_bytes_ > total_bytes_) |
340 total_bytes_ = 0; | 336 total_bytes_ = 0; |
341 } | 337 } |
342 | 338 |
343 // Updates from the download thread may have been posted while this download | 339 // Updates from the download thread may have been posted while this download |
344 // was being cancelled in the UI thread, so we'll accept them unless we're | 340 // was being cancelled in the UI thread, so we'll accept them unless we're |
345 // complete. | 341 // complete. |
346 void DownloadItem::Update(int64 bytes_so_far) { | 342 void DownloadItemImpl::Update(int64 bytes_so_far) { |
347 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 343 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
348 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
349 | 345 |
350 if (!IsInProgress()) { | 346 if (!IsInProgress()) { |
351 NOTREACHED(); | 347 NOTREACHED(); |
352 return; | 348 return; |
353 } | 349 } |
354 UpdateSize(bytes_so_far); | 350 UpdateSize(bytes_so_far); |
355 UpdateObservers(); | 351 UpdateObservers(); |
356 } | 352 } |
357 | 353 |
358 // Triggered by a user action. | 354 // Triggered by a user action. |
359 void DownloadItem::Cancel(bool user_cancel) { | 355 void DownloadItemImpl::Cancel(bool user_cancel) { |
360 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 356 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 357 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
362 | 358 |
363 last_reason_ = user_cancel ? | 359 last_reason_ = user_cancel ? |
364 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | 360 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : |
365 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | 361 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; |
366 | 362 |
367 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 363 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
368 if (!IsPartialDownload()) { | 364 if (!IsPartialDownload()) { |
369 // Small downloads might be complete before this method has | 365 // Small downloads might be complete before this method has |
370 // a chance to run. | 366 // a chance to run. |
371 return; | 367 return; |
372 } | 368 } |
373 | 369 |
374 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 370 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
375 | 371 |
376 TransitionTo(CANCELLED); | 372 TransitionTo(CANCELLED); |
377 if (user_cancel) | 373 if (user_cancel) |
378 download_manager_->DownloadCancelledInternal(this); | 374 download_manager_->DownloadCancelledInternal(this); |
379 } | 375 } |
380 | 376 |
381 void DownloadItem::MarkAsComplete() { | 377 void DownloadItemImpl::MarkAsComplete() { |
382 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 378 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
383 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
384 | 380 |
385 DCHECK(all_data_saved_); | 381 DCHECK(all_data_saved_); |
386 end_time_ = base::Time::Now(); | 382 end_time_ = base::Time::Now(); |
387 TransitionTo(COMPLETE); | 383 TransitionTo(COMPLETE); |
388 } | 384 } |
389 | 385 |
390 void DownloadItem::DelayedDownloadOpened() { | 386 void DownloadItemImpl::DelayedDownloadOpened() { |
391 auto_opened_ = true; | 387 auto_opened_ = true; |
392 Completed(); | 388 Completed(); |
393 } | 389 } |
394 | 390 |
395 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) { | 391 void DownloadItemImpl::OnAllDataSaved( |
| 392 int64 size, const std::string& final_hash) { |
396 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 393 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
398 | 395 |
399 DCHECK(!all_data_saved_); | 396 DCHECK(!all_data_saved_); |
400 all_data_saved_ = true; | 397 all_data_saved_ = true; |
401 UpdateSize(size); | 398 UpdateSize(size); |
402 hash_ = final_hash; | 399 hash_ = final_hash; |
403 } | 400 } |
404 | 401 |
405 void DownloadItem::OnDownloadedFileRemoved() { | 402 void DownloadItemImpl::OnDownloadedFileRemoved() { |
406 file_externally_removed_ = true; | 403 file_externally_removed_ = true; |
407 UpdateObservers(); | 404 UpdateObservers(); |
408 } | 405 } |
409 | 406 |
410 void DownloadItem::Completed() { | 407 void DownloadItemImpl::Completed() { |
411 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 408 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
412 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 409 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
413 | 410 |
414 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 411 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
415 | 412 |
416 DCHECK(all_data_saved_); | 413 DCHECK(all_data_saved_); |
417 end_time_ = base::Time::Now(); | 414 end_time_ = base::Time::Now(); |
418 TransitionTo(COMPLETE); | 415 TransitionTo(COMPLETE); |
419 download_manager_->DownloadCompleted(id()); | 416 download_manager_->DownloadCompleted(GetId()); |
420 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); | 417 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
421 | 418 |
422 if (auto_opened_) { | 419 if (auto_opened_) { |
423 // If it was already handled by the delegate, do nothing. | 420 // If it was already handled by the delegate, do nothing. |
424 } else if (open_when_complete() || | 421 } else if (GetOpenWhenComplete() || |
425 ShouldOpenFileBasedOnExtension() || | 422 ShouldOpenFileBasedOnExtension() || |
426 is_temporary()) { | 423 IsTemporary()) { |
427 // If the download is temporary, like in drag-and-drop, do not open it but | 424 // If the download is temporary, like in drag-and-drop, do not open it but |
428 // we still need to set it auto-opened so that it can be removed from the | 425 // we still need to set it auto-opened so that it can be removed from the |
429 // download shelf. | 426 // download shelf. |
430 if (!is_temporary()) | 427 if (!IsTemporary()) |
431 OpenDownload(); | 428 OpenDownload(); |
432 | 429 |
433 auto_opened_ = true; | 430 auto_opened_ = true; |
434 UpdateObservers(); | 431 UpdateObservers(); |
435 } | 432 } |
436 } | 433 } |
437 | 434 |
438 void DownloadItem::TransitionTo(DownloadState new_state) { | 435 void DownloadItemImpl::TransitionTo(DownloadState new_state) { |
439 if (state_ == new_state) | 436 if (state_ == new_state) |
440 return; | 437 return; |
441 | 438 |
442 state_ = new_state; | 439 state_ = new_state; |
443 UpdateObservers(); | 440 UpdateObservers(); |
444 } | 441 } |
445 | 442 |
446 void DownloadItem::UpdateSafetyState() { | 443 void DownloadItemImpl::UpdateSafetyState() { |
447 SafetyState updated_value = state_info_.IsDangerous() ? | 444 SafetyState updated_value = state_info_.IsDangerous() ? |
448 DownloadItem::DANGEROUS : DownloadItem::SAFE; | 445 DownloadItem::DANGEROUS : DownloadItem::SAFE; |
449 if (updated_value != safety_state_) { | 446 if (updated_value != safety_state_) { |
450 safety_state_ = updated_value; | 447 safety_state_ = updated_value; |
451 UpdateObservers(); | 448 UpdateObservers(); |
452 } | 449 } |
453 } | 450 } |
454 | 451 |
455 void DownloadItem::UpdateTarget() { | 452 void DownloadItemImpl::UpdateTarget() { |
456 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 453 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
457 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 454 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
458 | 455 |
459 if (state_info_.target_name.value().empty()) | 456 if (state_info_.target_name.value().empty()) |
460 state_info_.target_name = full_path_.BaseName(); | 457 state_info_.target_name = full_path_.BaseName(); |
461 } | 458 } |
462 | 459 |
463 void DownloadItem::Interrupted(int64 size, InterruptReason reason) { | 460 void DownloadItemImpl::Interrupted(int64 size, InterruptReason reason) { |
464 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 461 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
465 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 462 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
466 | 463 |
467 if (!IsInProgress()) | 464 if (!IsInProgress()) |
468 return; | 465 return; |
469 | 466 |
470 last_reason_ = reason; | 467 last_reason_ = reason; |
471 UpdateSize(size); | 468 UpdateSize(size); |
472 download_stats::RecordDownloadInterrupted(reason, | 469 download_stats::RecordDownloadInterrupted(reason, |
473 received_bytes_, | 470 received_bytes_, |
474 total_bytes_); | 471 total_bytes_); |
475 TransitionTo(INTERRUPTED); | 472 TransitionTo(INTERRUPTED); |
476 } | 473 } |
477 | 474 |
478 void DownloadItem::Delete(DeleteReason reason) { | 475 void DownloadItemImpl::Delete(DeleteReason reason) { |
479 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 476 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
480 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 477 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
481 | 478 |
482 switch (reason) { | 479 switch (reason) { |
483 case DELETE_DUE_TO_USER_DISCARD: | 480 case DELETE_DUE_TO_USER_DISCARD: |
484 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), | 481 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
485 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 482 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
486 break; | 483 break; |
487 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 484 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
488 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), | 485 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), |
489 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 486 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
490 break; | 487 break; |
491 default: | 488 default: |
492 NOTREACHED(); | 489 NOTREACHED(); |
493 } | 490 } |
494 | 491 |
495 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 492 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
496 base::Bind(&DeleteDownloadedFile, full_path_)); | 493 base::Bind(&DeleteDownloadedFile, full_path_)); |
497 Remove(); | 494 Remove(); |
498 // We have now been deleted. | 495 // We have now been deleted. |
499 } | 496 } |
500 | 497 |
501 void DownloadItem::Remove() { | 498 void DownloadItemImpl::Remove() { |
502 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 499 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
503 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 500 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
504 | 501 |
505 download_manager_->AssertQueueStateConsistent(this); | 502 download_manager_->AssertQueueStateConsistent(this); |
506 Cancel(true); | 503 Cancel(true); |
507 download_manager_->AssertQueueStateConsistent(this); | 504 download_manager_->AssertQueueStateConsistent(this); |
508 | 505 |
509 TransitionTo(REMOVING); | 506 TransitionTo(REMOVING); |
510 download_manager_->RemoveDownload(db_handle_); | 507 download_manager_->RemoveDownload(db_handle_); |
511 // We have now been deleted. | 508 // We have now been deleted. |
512 } | 509 } |
513 | 510 |
514 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 511 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { |
515 if (total_bytes_ <= 0) | 512 if (total_bytes_ <= 0) |
516 return false; // We never received the content_length for this download. | 513 return false; // We never received the content_length for this download. |
517 | 514 |
518 int64 speed = CurrentSpeed(); | 515 int64 speed = CurrentSpeed(); |
519 if (speed == 0) | 516 if (speed == 0) |
520 return false; | 517 return false; |
521 | 518 |
522 *remaining = base::TimeDelta::FromSeconds( | 519 *remaining = base::TimeDelta::FromSeconds( |
523 (total_bytes_ - received_bytes_) / speed); | 520 (total_bytes_ - received_bytes_) / speed); |
524 return true; | 521 return true; |
525 } | 522 } |
526 | 523 |
527 int64 DownloadItem::CurrentSpeed() const { | 524 int64 DownloadItemImpl::CurrentSpeed() const { |
528 if (is_paused_) | 525 if (is_paused_) |
529 return 0; | 526 return 0; |
530 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; | 527 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; |
531 int64 diff_ms = diff.InMilliseconds(); | 528 int64 diff_ms = diff.InMilliseconds(); |
532 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms; | 529 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms; |
533 } | 530 } |
534 | 531 |
535 int DownloadItem::PercentComplete() const { | 532 int DownloadItemImpl::PercentComplete() const { |
536 // If the delegate is delaying completion of the download, then we have no | 533 // If the delegate is delaying completion of the download, then we have no |
537 // idea how long it will take. | 534 // idea how long it will take. |
538 if (delegate_delayed_complete_ || total_bytes_ <= 0) | 535 if (delegate_delayed_complete_ || total_bytes_ <= 0) |
539 return -1; | 536 return -1; |
540 | 537 |
541 return static_cast<int>(received_bytes_ * 100.0 / total_bytes_); | 538 return static_cast<int>(received_bytes_ * 100.0 / total_bytes_); |
542 } | 539 } |
543 | 540 |
544 void DownloadItem::OnPathDetermined(const FilePath& path) { | 541 void DownloadItemImpl::OnPathDetermined(const FilePath& path) { |
545 full_path_ = path; | 542 full_path_ = path; |
546 UpdateTarget(); | 543 UpdateTarget(); |
547 } | 544 } |
548 | 545 |
549 void DownloadItem::Rename(const FilePath& full_path) { | 546 void DownloadItemImpl::Rename(const FilePath& full_path) { |
550 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 547 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
551 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 548 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
552 | 549 |
553 VLOG(20) << __FUNCTION__ << "()" | 550 VLOG(20) << __FUNCTION__ << "()" |
554 << " full_path = \"" << full_path.value() << "\"" | 551 << " full_path = \"" << full_path.value() << "\"" |
555 << " " << DebugString(true); | 552 << " " << DebugString(true); |
556 DCHECK(!full_path.empty()); | 553 DCHECK(!full_path.empty()); |
557 full_path_ = full_path; | 554 full_path_ = full_path; |
558 } | 555 } |
559 | 556 |
560 void DownloadItem::TogglePause() { | 557 void DownloadItemImpl::TogglePause() { |
561 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 558 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
562 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 559 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
563 | 560 |
564 DCHECK(IsInProgress()); | 561 DCHECK(IsInProgress()); |
565 if (is_paused_) | 562 if (is_paused_) |
566 request_handle_->ResumeRequest(); | 563 request_handle_->ResumeRequest(); |
567 else | 564 else |
568 request_handle_->PauseRequest(); | 565 request_handle_->PauseRequest(); |
569 is_paused_ = !is_paused_; | 566 is_paused_ = !is_paused_; |
570 UpdateObservers(); | 567 UpdateObservers(); |
571 } | 568 } |
572 | 569 |
573 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { | 570 void DownloadItemImpl::OnDownloadCompleting(DownloadFileManager* file_manager) { |
574 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 571 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
575 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 572 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
576 | 573 |
577 VLOG(20) << __FUNCTION__ << "()" | 574 VLOG(20) << __FUNCTION__ << "()" |
578 << " needs rename = " << NeedsRename() | 575 << " needs rename = " << NeedsRename() |
579 << " " << DebugString(true); | 576 << " " << DebugString(true); |
580 DCHECK_NE(DANGEROUS, safety_state()); | 577 DCHECK_NE(DANGEROUS, GetSafetyState()); |
581 DCHECK(file_manager); | 578 DCHECK(file_manager); |
582 | 579 |
583 if (NeedsRename()) { | 580 if (NeedsRename()) { |
584 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 581 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
585 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, | 582 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, |
586 file_manager, global_id(), | 583 file_manager, GetGlobalId(), |
587 GetTargetFilePath(), safety_state() == SAFE)); | 584 GetTargetFilePath(), GetSafetyState() == SAFE)); |
588 return; | 585 return; |
589 } | 586 } |
590 | 587 |
591 Completed(); | 588 Completed(); |
592 | 589 |
593 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 590 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
594 base::Bind(&DownloadFileManager::CompleteDownload, | 591 base::Bind(&DownloadFileManager::CompleteDownload, |
595 file_manager, global_id())); | 592 file_manager, GetGlobalId())); |
596 } | 593 } |
597 | 594 |
598 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 595 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
599 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 596 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
600 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 597 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
601 | 598 |
602 VLOG(20) << __FUNCTION__ << "()" | 599 VLOG(20) << __FUNCTION__ << "()" |
603 << " full_path = \"" << full_path.value() << "\"" | 600 << " full_path = \"" << full_path.value() << "\"" |
604 << " needed rename = " << NeedsRename() | 601 << " needed rename = " << NeedsRename() |
605 << " " << DebugString(false); | 602 << " " << DebugString(false); |
606 DCHECK(NeedsRename()); | 603 DCHECK(NeedsRename()); |
607 | 604 |
608 Rename(full_path); | 605 Rename(full_path); |
609 | 606 |
610 if (download_manager_->delegate()->ShouldOpenDownload(this)) { | 607 if (download_manager_->delegate()->ShouldOpenDownload(this)) { |
611 Completed(); | 608 Completed(); |
612 } else { | 609 } else { |
613 delegate_delayed_complete_ = true; | 610 delegate_delayed_complete_ = true; |
614 } | 611 } |
615 } | 612 } |
616 | 613 |
617 bool DownloadItem::MatchesQuery(const string16& query) const { | 614 bool DownloadItemImpl::MatchesQuery(const string16& query) const { |
618 if (query.empty()) | 615 if (query.empty()) |
619 return true; | 616 return true; |
620 | 617 |
621 DCHECK_EQ(query, base::i18n::ToLower(query)); | 618 DCHECK_EQ(query, base::i18n::ToLower(query)); |
622 | 619 |
623 string16 url_raw(UTF8ToUTF16(GetURL().spec())); | 620 string16 url_raw(UTF8ToUTF16(GetURL().spec())); |
624 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) | 621 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) |
625 return true; | 622 return true; |
626 | 623 |
627 // TODO(phajdan.jr): write a test case for the following code. | 624 // TODO(phajdan.jr): write a test case for the following code. |
628 // A good test case would be: | 625 // A good test case would be: |
629 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 626 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
630 // L"/\x4f60\x597d\x4f60\x597d", | 627 // L"/\x4f60\x597d\x4f60\x597d", |
631 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 628 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
632 std::string languages; | 629 std::string languages; |
633 TabContents* tab = GetTabContents(); | 630 TabContents* tab = GetTabContents(); |
634 if (tab) { | 631 if (tab) { |
635 languages = content::GetContentClient()->browser()->GetAcceptLangs( | 632 languages = content::GetContentClient()->browser()->GetAcceptLangs( |
636 tab->browser_context()); | 633 tab->browser_context()); |
637 } | 634 } |
638 string16 url_formatted(net::FormatUrl(GetURL(), languages)); | 635 string16 url_formatted(net::FormatUrl(GetURL(), languages)); |
639 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) | 636 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) |
640 return true; | 637 return true; |
641 | 638 |
642 string16 path(full_path().LossyDisplayName()); | 639 string16 path(GetFullPath().LossyDisplayName()); |
643 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); | 640 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); |
644 } | 641 } |
645 | 642 |
646 void DownloadItem::SetFileCheckResults(const DownloadStateInfo& state) { | 643 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { |
647 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 644 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
648 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 645 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
649 | 646 |
650 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); | 647 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
651 state_info_ = state; | 648 state_info_ = state; |
652 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); | 649 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
653 | 650 |
654 UpdateSafetyState(); | 651 UpdateSafetyState(); |
655 } | 652 } |
656 | 653 |
657 DownloadStateInfo::DangerType DownloadItem::GetDangerType() const { | 654 DownloadStateInfo::DangerType DownloadItemImpl::GetDangerType() const { |
658 return state_info_.danger; | 655 return state_info_.danger; |
659 } | 656 } |
660 | 657 |
661 bool DownloadItem::IsDangerous() const { | 658 bool DownloadItemImpl::IsDangerous() const { |
662 return state_info_.IsDangerous(); | 659 return state_info_.IsDangerous(); |
663 } | 660 } |
664 | 661 |
665 void DownloadItem::MarkFileDangerous() { | 662 void DownloadItemImpl::MarkFileDangerous() { |
666 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 663 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
667 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 664 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
668 state_info_.danger = DownloadStateInfo::DANGEROUS_FILE; | 665 state_info_.danger = DownloadStateInfo::DANGEROUS_FILE; |
669 UpdateSafetyState(); | 666 UpdateSafetyState(); |
670 } | 667 } |
671 | 668 |
672 void DownloadItem::MarkUrlDangerous() { | 669 void DownloadItemImpl::MarkUrlDangerous() { |
673 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 670 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
674 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 671 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
675 state_info_.danger = DownloadStateInfo::DANGEROUS_URL; | 672 state_info_.danger = DownloadStateInfo::DANGEROUS_URL; |
676 UpdateSafetyState(); | 673 UpdateSafetyState(); |
677 } | 674 } |
678 | 675 |
679 void DownloadItem::MarkContentDangerous() { | 676 void DownloadItemImpl::MarkContentDangerous() { |
680 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 677 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
681 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
682 state_info_.danger = DownloadStateInfo::DANGEROUS_CONTENT; | 679 state_info_.danger = DownloadStateInfo::DANGEROUS_CONTENT; |
683 UpdateSafetyState(); | 680 UpdateSafetyState(); |
684 } | 681 } |
685 | 682 |
686 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { | 683 DownloadPersistentStoreInfo DownloadItemImpl::GetPersistentStoreInfo() const { |
687 return DownloadPersistentStoreInfo(full_path(), | 684 return DownloadPersistentStoreInfo(GetFullPath(), |
688 GetURL(), | 685 GetURL(), |
689 referrer_url(), | 686 GetReferrerUrl(), |
690 start_time(), | 687 GetStartTime(), |
691 end_time(), | 688 GetEndTime(), |
692 received_bytes(), | 689 GetReceivedBytes(), |
693 total_bytes(), | 690 GetTotalBytes(), |
694 state(), | 691 GetState(), |
695 db_handle(), | 692 GetDbHandle(), |
696 opened()); | 693 GetOpened()); |
697 } | 694 } |
698 | 695 |
699 TabContents* DownloadItem::GetTabContents() const { | 696 TabContents* DownloadItemImpl::GetTabContents() const { |
700 // TODO(rdsmith): Remove null check after removing GetTabContents() from | 697 // TODO(rdsmith): Remove null check after removing GetTabContents() from |
701 // paths that might be used by DownloadItems created from history import. | 698 // paths that might be used by DownloadItems created from history import. |
702 // Currently such items have null request_handle_s, where other items | 699 // Currently such items have null request_handle_s, where other items |
703 // (regular and SavePackage downloads) have actual objects off the pointer. | 700 // (regular and SavePackage downloads) have actual objects off the pointer. |
704 if (request_handle_.get()) | 701 if (request_handle_.get()) |
705 return request_handle_->GetTabContents(); | 702 return request_handle_->GetTabContents(); |
706 return NULL; | 703 return NULL; |
707 } | 704 } |
708 | 705 |
709 FilePath DownloadItem::GetTargetFilePath() const { | 706 FilePath DownloadItemImpl::GetTargetFilePath() const { |
710 return full_path_.DirName().Append(state_info_.target_name); | 707 return full_path_.DirName().Append(state_info_.target_name); |
711 } | 708 } |
712 | 709 |
713 FilePath DownloadItem::GetFileNameToReportUser() const { | 710 FilePath DownloadItemImpl::GetFileNameToReportUser() const { |
714 if (state_info_.path_uniquifier > 0) { | 711 if (state_info_.path_uniquifier > 0) { |
715 FilePath name(state_info_.target_name); | 712 FilePath name(state_info_.target_name); |
716 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); | 713 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); |
717 return name; | 714 return name; |
718 } | 715 } |
719 return state_info_.target_name; | 716 return state_info_.target_name; |
720 } | 717 } |
721 | 718 |
722 FilePath DownloadItem::GetUserVerifiedFilePath() const { | 719 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { |
723 return (safety_state_ == DownloadItem::SAFE) ? | 720 return (safety_state_ == DownloadItem::SAFE) ? |
724 GetTargetFilePath() : full_path_; | 721 GetTargetFilePath() : full_path_; |
725 } | 722 } |
726 | 723 |
727 void DownloadItem::OffThreadCancel(DownloadFileManager* file_manager) { | 724 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { |
728 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
729 request_handle_->CancelRequest(); | 726 request_handle_->CancelRequest(); |
730 | 727 |
731 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 728 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
732 base::Bind(&DownloadFileManager::CancelDownload, | 729 base::Bind(&DownloadFileManager::CancelDownload, |
733 file_manager, global_id())); | 730 file_manager, GetGlobalId())); |
734 } | 731 } |
735 | 732 |
736 void DownloadItem::Init(bool active) { | 733 void DownloadItemImpl::Init(bool active) { |
737 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 734 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
738 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 735 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
739 | 736 |
740 UpdateTarget(); | 737 UpdateTarget(); |
741 if (active) { | 738 if (active) { |
742 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 739 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
743 } | 740 } |
744 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 741 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
745 } | 742 } |
746 | 743 |
747 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 744 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
748 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 745 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
749 bool DownloadItem::IsPartialDownload() const { | 746 bool DownloadItemImpl::IsPartialDownload() const { |
750 return (state_ == IN_PROGRESS); | 747 return (state_ == IN_PROGRESS); |
751 } | 748 } |
752 | 749 |
753 bool DownloadItem::IsInProgress() const { | 750 bool DownloadItemImpl::IsInProgress() const { |
754 return (state_ == IN_PROGRESS); | 751 return (state_ == IN_PROGRESS); |
755 } | 752 } |
756 | 753 |
757 bool DownloadItem::IsCancelled() const { | 754 bool DownloadItemImpl::IsCancelled() const { |
758 return (state_ == CANCELLED) || | 755 return (state_ == CANCELLED) || |
759 (state_ == INTERRUPTED); | 756 (state_ == INTERRUPTED); |
760 } | 757 } |
761 | 758 |
762 bool DownloadItem::IsInterrupted() const { | 759 bool DownloadItemImpl::IsInterrupted() const { |
763 return (state_ == INTERRUPTED); | 760 return (state_ == INTERRUPTED); |
764 } | 761 } |
765 | 762 |
766 bool DownloadItem::IsComplete() const { | 763 bool DownloadItemImpl::IsComplete() const { |
767 return (state_ == COMPLETE); | 764 return (state_ == COMPLETE); |
768 } | 765 } |
769 | 766 |
770 const GURL& DownloadItem::GetURL() const { | 767 const GURL& DownloadItemImpl::GetURL() const { |
771 return url_chain_.empty() ? | 768 return url_chain_.empty() ? |
772 GURL::EmptyGURL() : url_chain_.back(); | 769 GURL::EmptyGURL() : url_chain_.back(); |
773 } | 770 } |
774 | 771 |
775 std::string DownloadItem::DebugString(bool verbose) const { | 772 std::string DownloadItemImpl::DebugString(bool verbose) const { |
776 std::string description = | 773 std::string description = |
777 base::StringPrintf("{ id = %d" | 774 base::StringPrintf("{ id = %d" |
778 " state = %s", | 775 " state = %s", |
779 download_id_.local(), | 776 download_id_.local(), |
780 DebugDownloadStateString(state())); | 777 DebugDownloadStateString(GetState())); |
781 | 778 |
782 // Construct a string of the URL chain. | 779 // Construct a string of the URL chain. |
783 std::string url_list("<none>"); | 780 std::string url_list("<none>"); |
784 if (!url_chain_.empty()) { | 781 if (!url_chain_.empty()) { |
785 std::vector<GURL>::const_iterator iter = url_chain_.begin(); | 782 std::vector<GURL>::const_iterator iter = url_chain_.begin(); |
786 std::vector<GURL>::const_iterator last = url_chain_.end(); | 783 std::vector<GURL>::const_iterator last = url_chain_.end(); |
787 url_list = (*iter).spec(); | 784 url_list = (*iter).spec(); |
788 ++iter; | 785 ++iter; |
789 for ( ; verbose && (iter != last); ++iter) { | 786 for ( ; verbose && (iter != last); ++iter) { |
790 url_list += " ->\n\t"; | 787 url_list += " ->\n\t"; |
791 const GURL& next_url = *iter; | 788 const GURL& next_url = *iter; |
792 url_list += next_url.spec(); | 789 url_list += next_url.spec(); |
793 } | 790 } |
794 } | 791 } |
795 | 792 |
796 if (verbose) { | 793 if (verbose) { |
797 description += base::StringPrintf( | 794 description += base::StringPrintf( |
798 " db_handle = %" PRId64 | 795 " db_handle = %" PRId64 |
799 " total_bytes = %" PRId64 | 796 " total_bytes = %" PRId64 |
800 " received_bytes = %" PRId64 | 797 " received_bytes = %" PRId64 |
801 " is_paused = %c" | 798 " is_paused = %c" |
802 " is_otr = %c" | 799 " is_otr = %c" |
803 " safety_state = %s" | 800 " safety_state = %s" |
804 " url_chain = \n\t\"%s\"\n\t" | 801 " url_chain = \n\t\"%s\"\n\t" |
805 " target_name = \"%" PRFilePath "\"" | 802 " target_name = \"%" PRFilePath "\"" |
806 " full_path = \"%" PRFilePath "\"", | 803 " full_path = \"%" PRFilePath "\"", |
807 db_handle(), | 804 GetDbHandle(), |
808 total_bytes(), | 805 GetTotalBytes(), |
809 received_bytes(), | 806 GetReceivedBytes(), |
810 is_paused() ? 'T' : 'F', | 807 IsPaused() ? 'T' : 'F', |
811 is_otr() ? 'T' : 'F', | 808 IsOtr() ? 'T' : 'F', |
812 DebugSafetyStateString(safety_state()), | 809 DebugSafetyStateString(GetSafetyState()), |
813 url_list.c_str(), | 810 url_list.c_str(), |
814 state_info_.target_name.value().c_str(), | 811 state_info_.target_name.value().c_str(), |
815 full_path().value().c_str()); | 812 GetFullPath().value().c_str()); |
816 } else { | 813 } else { |
817 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 814 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
818 } | 815 } |
819 | 816 |
820 description += " }"; | 817 description += " }"; |
821 | 818 |
822 return description; | 819 return description; |
823 } | 820 } |
| 821 |
| 822 bool DownloadItemImpl::AllDataSaved() const { return all_data_saved_; } |
| 823 DownloadItem::DownloadState DownloadItemImpl::GetState() const { |
| 824 return state_; |
| 825 } |
| 826 const FilePath& DownloadItemImpl::GetFullPath() const { return full_path_; } |
| 827 void DownloadItemImpl::SetPathUniquifier(int uniquifier) { |
| 828 state_info_.path_uniquifier = uniquifier; |
| 829 } |
| 830 const std::vector<GURL>& DownloadItemImpl::GetUrlChain() const { |
| 831 return url_chain_; |
| 832 } |
| 833 const GURL& DownloadItemImpl::GetOriginalUrl() const { |
| 834 return url_chain_.front(); |
| 835 } |
| 836 const GURL& DownloadItemImpl::GetReferrerUrl() const { return referrer_url_; } |
| 837 std::string DownloadItemImpl::GetSuggestedFilename() const { |
| 838 return suggested_filename_; |
| 839 } |
| 840 std::string DownloadItemImpl::GetContentDisposition() const { |
| 841 return content_disposition_; |
| 842 } |
| 843 std::string DownloadItemImpl::GetMimeType() const { return mime_type_; } |
| 844 std::string DownloadItemImpl::GetOriginalMimeType() const { |
| 845 return original_mime_type_; |
| 846 } |
| 847 std::string DownloadItemImpl::GetReferrerCharset() const { |
| 848 return referrer_charset_; |
| 849 } |
| 850 int64 DownloadItemImpl::GetTotalBytes() const { return total_bytes_; } |
| 851 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { |
| 852 total_bytes_ = total_bytes; |
| 853 } |
| 854 const std::string& DownloadItemImpl::GetHash() const { return hash_; } |
| 855 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } |
| 856 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } |
| 857 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } |
| 858 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
| 859 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
| 860 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
| 861 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
| 862 DownloadManager* DownloadItemImpl::GetDownloadManager() { |
| 863 return download_manager_; |
| 864 } |
| 865 bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
| 866 bool DownloadItemImpl::GetOpenWhenComplete() const { |
| 867 return open_when_complete_; |
| 868 } |
| 869 void DownloadItemImpl::SetOpenWhenComplete(bool open) { |
| 870 open_when_complete_ = open; |
| 871 } |
| 872 bool DownloadItemImpl::GetFileExternallyRemoved() const { |
| 873 return file_externally_removed_; |
| 874 } |
| 875 DownloadItem::SafetyState DownloadItemImpl::GetSafetyState() const { |
| 876 return safety_state_; |
| 877 } |
| 878 bool DownloadItemImpl::IsOtr() const { return is_otr_; } |
| 879 bool DownloadItemImpl::GetAutoOpened() { return auto_opened_; } |
| 880 const FilePath& DownloadItemImpl::GetTargetName() const { |
| 881 return state_info_.target_name; |
| 882 } |
| 883 bool DownloadItemImpl::PromptUserForSaveLocation() const { |
| 884 return state_info_.prompt_user_for_save_location; |
| 885 } |
| 886 const FilePath& DownloadItemImpl::GetSuggestedPath() const { |
| 887 return state_info_.suggested_path; |
| 888 } |
| 889 bool DownloadItemImpl::IsTemporary() const { return is_temporary_; } |
| 890 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
| 891 bool DownloadItemImpl::GetOpened() const { return opened_; } |
| 892 InterruptReason DownloadItemImpl::GetLastReason() const { |
| 893 return last_reason_; |
| 894 } |
| 895 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
| 896 bool DownloadItemImpl::NeedsRename() const { |
| 897 return state_info_.target_name != full_path_.BaseName(); |
| 898 } |
| 899 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
OLD | NEW |