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

Side by Side Diff: content/browser/download/download_item_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698