| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/save_item.h" | 5 #include "content/browser/download/save_item.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "content/browser/download/save_file.h" | 9 #include "content/browser/download/save_file.h" |
| 10 #include "content/browser/download/save_file_manager.h" | 10 #include "content/browser/download/save_file_manager.h" |
| 11 #include "content/browser/download/save_package.h" | 11 #include "content/browser/download/save_package.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 12 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 | 15 |
| 15 // Constructor for SaveItem when creating each saving job. | 16 // Constructor for SaveItem when creating each saving job. |
| 16 SaveItem::SaveItem(const GURL& url, | 17 SaveItem::SaveItem(const GURL& url, |
| 17 const Referrer& referrer, | |
| 18 SavePackage* package, | 18 SavePackage* package, |
| 19 SaveFileCreateInfo::SaveFileSource save_source) | 19 SaveFileCreateInfo::SaveFileSource save_source) |
| 20 : save_id_(-1), | 20 : save_id_(-1), |
| 21 url_(url), | 21 url_(url), |
| 22 referrer_(referrer), | 22 referrer_(), |
| 23 total_bytes_(0), | 23 total_bytes_(0), |
| 24 received_bytes_(0), | 24 received_bytes_(0), |
| 25 state_(WAIT_START), | 25 state_(WAIT_START), |
| 26 has_final_name_(false), | 26 has_final_name_(false), |
| 27 is_success_(false), | 27 is_success_(false), |
| 28 save_source_(save_source), | 28 save_source_(save_source), |
| 29 package_(package) { | 29 package_(package) { |
| 30 DCHECK(package); | 30 DCHECK(package); |
| 31 } | 31 } |
| 32 | 32 |
| 33 SaveItem::~SaveItem() { | 33 SaveItem::~SaveItem() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Set start state for save item. | 36 // Set start state for save item. |
| 37 void SaveItem::Start() { | 37 void SaveItem::Start() { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 38 DCHECK(state_ == WAIT_START); | 39 DCHECK(state_ == WAIT_START); |
| 39 state_ = IN_PROGRESS; | 40 state_ = IN_PROGRESS; |
| 40 } | 41 } |
| 41 | 42 |
| 42 // If we've received more data than we were expecting (bad server info?), | 43 // If we've received more data than we were expecting (bad server info?), |
| 43 // revert to 'unknown size mode'. | 44 // revert to 'unknown size mode'. |
| 44 void SaveItem::UpdateSize(int64 bytes_so_far) { | 45 void SaveItem::UpdateSize(int64 bytes_so_far) { |
| 45 received_bytes_ = bytes_so_far; | 46 received_bytes_ = bytes_so_far; |
| 46 if (received_bytes_ >= total_bytes_) | 47 if (received_bytes_ >= total_bytes_) |
| 47 total_bytes_ = 0; | 48 total_bytes_ = 0; |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Updates from the file thread may have been posted while this saving job | 51 // Updates from the file thread may have been posted while this saving job |
| 51 // was being canceled in the UI thread, so we'll accept them unless we're | 52 // was being canceled in the UI thread, so we'll accept them unless we're |
| 52 // complete. | 53 // complete. |
| 53 void SaveItem::Update(int64 bytes_so_far) { | 54 void SaveItem::Update(int64 bytes_so_far) { |
| 54 if (state_ != IN_PROGRESS) { | 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 56 if (state_ != IN_PROGRESS && state_ != COMPLETING) { |
| 55 NOTREACHED(); | 57 NOTREACHED(); |
| 56 return; | 58 return; |
| 57 } | 59 } |
| 58 UpdateSize(bytes_so_far); | 60 UpdateSize(bytes_so_far); |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Cancel this saving item job. If the job is not in progress, ignore | 63 // Cancel this saving item job. If the job is not in progress, ignore |
| 62 // this command. The SavePackage will each in-progress SaveItem's cancel | 64 // this command. The SavePackage will each in-progress SaveItem's cancel |
| 63 // when canceling whole saving page job. | 65 // when canceling whole saving page job. |
| 64 void SaveItem::Cancel() { | 66 void SaveItem::Cancel() { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 |
| 65 // If item is in WAIT_START mode, which means no request has been sent. | 69 // If item is in WAIT_START mode, which means no request has been sent. |
| 66 // So we need not to cancel it. | 70 // So we need not to cancel it. |
| 67 if (state_ != IN_PROGRESS) { | 71 if (state_ != IN_PROGRESS) { |
| 68 // Small downloads might be complete before method has a chance to run. | 72 // Small downloads might be complete before method has a chance to run. |
| 69 return; | 73 return; |
| 70 } | 74 } |
| 71 state_ = CANCELED; | 75 state_ = CANCELED; |
| 72 is_success_ = false; | 76 is_success_ = false; |
| 73 Finish(received_bytes_, false); | 77 Finish(received_bytes_, false); |
| 74 package_->SaveCanceled(this); | 78 package_->SaveCanceled(this); |
| 75 } | 79 } |
| 76 | 80 |
| 77 // Set finish state for a save item | 81 // Set finish state for a save item |
| 78 void SaveItem::Finish(int64 size, bool is_success) { | 82 void SaveItem::Finish(int64 size, bool is_success) { |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 84 |
| 79 // When this function is called, the SaveItem should be one of following | 85 // When this function is called, the SaveItem should be one of following |
| 80 // three situations. | 86 // three situations. |
| 81 // a) The data of this SaveItem is finished saving. So it should have | 87 // a) The data of this SaveItem is finished saving. So it should have |
| 82 // generated final name. | 88 // generated final name. |
| 83 // b) Error happened before the start of saving process. So no |save_id_| is | 89 // b) Error happened before the start of saving process. So no |save_id_| is |
| 84 // generated for this SaveItem and the |is_success_| should be false. | 90 // generated for this SaveItem and the |is_success_| should be false. |
| 85 // c) Error happened in the start of saving process, the SaveItem has a save | 91 // c) Error happened in the start of saving process, the SaveItem has a save |
| 86 // id, |is_success_| should be false, and the |size| should be 0. | 92 // id, |is_success_| should be false, and the |size| should be 0. |
| 87 DCHECK(has_final_name() || (save_id_ == -1 && !is_success_) || | 93 DCHECK(has_final_name() || (save_id_ == -1 && !is_success_) || |
| 88 (save_id_ != -1 && !is_success_ && !size)); | 94 (save_id_ != -1 && !is_success_ && !size)); |
| 89 state_ = COMPLETE; | 95 state_ = COMPLETE; |
| 90 is_success_ = is_success; | 96 is_success_ = is_success; |
| 91 UpdateSize(size); | 97 UpdateSize(size); |
| 92 } | 98 } |
| 93 | 99 |
| 94 // Calculate the percentage of the save item | 100 // Calculate the percentage of the save item |
| 95 int SaveItem::PercentComplete() const { | 101 int SaveItem::PercentComplete() const { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 103 |
| 96 switch (state_) { | 104 switch (state_) { |
| 97 case COMPLETE: | 105 case COMPLETE: |
| 106 case COMPLETING: |
| 98 case CANCELED: | 107 case CANCELED: |
| 99 return 100; | 108 return 100; |
| 100 case WAIT_START: | 109 case WAIT_START: |
| 101 return 0; | 110 return 0; |
| 102 case IN_PROGRESS: { | 111 case IN_PROGRESS: { |
| 103 int percent = 0; | 112 int percent = 0; |
| 104 if (total_bytes_ > 0) | 113 if (total_bytes_ > 0) |
| 105 percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_); | 114 percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_); |
| 106 return percent; | 115 return percent; |
| 107 } | 116 } |
| 108 default: { | 117 default: { |
| 109 NOTREACHED(); | 118 NOTREACHED(); |
| 110 return -1; | 119 return -1; |
| 111 } | 120 } |
| 112 } | 121 } |
| 113 } | 122 } |
| 114 | 123 |
| 115 // Rename the save item with new path. | 124 // Rename the save item with new path. |
| 116 void SaveItem::Rename(const base::FilePath& full_path) { | 125 void SaveItem::Rename(const base::FilePath& full_path) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 DCHECK(!full_path.empty() && !has_final_name()); | 127 DCHECK(!full_path.empty() && !has_final_name()); |
| 118 full_path_ = full_path; | 128 full_path_ = full_path; |
| 119 file_name_ = full_path_.BaseName(); | 129 file_name_ = full_path_.BaseName(); |
| 120 has_final_name_ = true; | 130 has_final_name_ = true; |
| 121 } | 131 } |
| 122 | 132 |
| 123 void SaveItem::SetSaveId(int32 save_id) { | 133 void SaveItem::SetSaveId(int32 save_id) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 124 DCHECK_EQ(-1, save_id_); | 135 DCHECK_EQ(-1, save_id_); |
| 125 save_id_ = save_id; | 136 save_id_ = save_id; |
| 126 } | 137 } |
| 127 | 138 |
| 128 void SaveItem::SetTotalBytes(int64 total_bytes) { | 139 void SaveItem::SetTotalBytes(int64 total_bytes) { |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 129 DCHECK_EQ(0, total_bytes_); | 141 DCHECK_EQ(0, total_bytes_); |
| 130 total_bytes_ = total_bytes; | 142 total_bytes_ = total_bytes; |
| 131 } | 143 } |
| 132 | 144 |
| 145 void SaveItem::MarkAsCompleting() { |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 147 DCHECK(state_ == IN_PROGRESS); |
| 148 state_ = COMPLETING; |
| 149 } |
| 150 |
| 133 } // namespace content | 151 } // namespace content |
| OLD | NEW |