Chromium Code Reviews| 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 |
| 16 namespace { | |
| 17 | |
| 18 int GetNextSaveItemId() { | |
| 19 static int g_next_save_item_id = 0; | |
| 20 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 21 return g_next_save_item_id++; | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 15 // Constructor for SaveItem when creating each saving job. | 26 // Constructor for SaveItem when creating each saving job. |
| 16 SaveItem::SaveItem(const GURL& url, | 27 SaveItem::SaveItem(const GURL& url, |
| 17 const Referrer& referrer, | 28 const Referrer& referrer, |
| 18 SavePackage* package, | 29 SavePackage* package, |
| 19 SaveFileCreateInfo::SaveFileSource save_source) | 30 SaveFileCreateInfo::SaveFileSource save_source) |
| 20 : save_id_(-1), | 31 : save_item_id_(GetNextSaveItemId()), |
| 21 url_(url), | 32 url_(url), |
| 22 referrer_(referrer), | 33 referrer_(referrer), |
| 23 total_bytes_(0), | 34 total_bytes_(0), |
| 24 received_bytes_(0), | 35 received_bytes_(0), |
| 25 state_(WAIT_START), | 36 state_(WAIT_START), |
| 26 has_final_name_(false), | 37 has_final_name_(false), |
| 27 is_success_(false), | 38 is_success_(false), |
| 28 save_source_(save_source), | 39 save_source_(save_source), |
| 29 package_(package) { | 40 package_(package) { |
| 30 DCHECK(package); | 41 DCHECK(package); |
| 31 } | 42 } |
| 32 | 43 |
| 33 SaveItem::~SaveItem() { | 44 SaveItem::~SaveItem() { |
| 34 } | 45 } |
| 35 | 46 |
| 36 // Set start state for save item. | 47 // Set start state for save item. |
| 37 void SaveItem::Start() { | 48 void SaveItem::Start() { |
| 38 DCHECK(state_ == WAIT_START); | 49 DCHECK(state_ == WAIT_START); |
| 39 state_ = IN_PROGRESS; | 50 state_ = IN_PROGRESS; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 Finish(received_bytes_, false); | 84 Finish(received_bytes_, false); |
| 74 package_->SaveCanceled(this); | 85 package_->SaveCanceled(this); |
| 75 } | 86 } |
| 76 | 87 |
| 77 // Set finish state for a save item | 88 // Set finish state for a save item |
| 78 void SaveItem::Finish(int64 size, bool is_success) { | 89 void SaveItem::Finish(int64 size, bool is_success) { |
| 79 // When this function is called, the SaveItem should be one of following | 90 // When this function is called, the SaveItem should be one of following |
| 80 // three situations. | 91 // three situations. |
| 81 // a) The data of this SaveItem is finished saving. So it should have | 92 // a) The data of this SaveItem is finished saving. So it should have |
| 82 // generated final name. | 93 // generated final name. |
| 83 // b) Error happened before the start of saving process. So no |save_id_| is | 94 // b) Error happened before the start of saving process. |
| 84 // generated for this SaveItem and the |is_success_| should be false. | 95 // c) Error happened in the start of saving process. |
|
Randy Smith (Not in Mondays)
2015/12/03 20:58:28
nit: I think this comment was originally intended
Łukasz Anforowicz
2015/12/04 21:16:45
Done (I've removed the comment altogether - I agre
| |
| 85 // c) Error happened in the start of saving process, the SaveItem has a save | 96 DCHECK(has_final_name() || !is_success_); |
| 86 // id, |is_success_| should be false, and the |size| should be 0. | |
| 87 DCHECK(has_final_name() || (save_id_ == -1 && !is_success_) || | |
| 88 (save_id_ != -1 && !is_success_ && !size)); | |
| 89 state_ = COMPLETE; | 97 state_ = COMPLETE; |
| 90 is_success_ = is_success; | 98 is_success_ = is_success; |
| 91 UpdateSize(size); | 99 UpdateSize(size); |
| 92 } | 100 } |
| 93 | 101 |
| 94 // Calculate the percentage of the save item | 102 // Calculate the percentage of the save item |
| 95 int SaveItem::PercentComplete() const { | 103 int SaveItem::PercentComplete() const { |
| 96 switch (state_) { | 104 switch (state_) { |
| 97 case COMPLETE: | 105 case COMPLETE: |
| 98 case CANCELED: | 106 case CANCELED: |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 113 } | 121 } |
| 114 | 122 |
| 115 // Rename the save item with new path. | 123 // Rename the save item with new path. |
| 116 void SaveItem::Rename(const base::FilePath& full_path) { | 124 void SaveItem::Rename(const base::FilePath& full_path) { |
| 117 DCHECK(!full_path.empty() && !has_final_name()); | 125 DCHECK(!full_path.empty() && !has_final_name()); |
| 118 full_path_ = full_path; | 126 full_path_ = full_path; |
| 119 file_name_ = full_path_.BaseName(); | 127 file_name_ = full_path_.BaseName(); |
| 120 has_final_name_ = true; | 128 has_final_name_ = true; |
| 121 } | 129 } |
| 122 | 130 |
| 123 void SaveItem::SetSaveId(int32 save_id) { | |
| 124 DCHECK_EQ(-1, save_id_); | |
| 125 save_id_ = save_id; | |
| 126 } | |
| 127 | |
| 128 void SaveItem::SetTotalBytes(int64 total_bytes) { | 131 void SaveItem::SetTotalBytes(int64 total_bytes) { |
| 129 DCHECK_EQ(0, total_bytes_); | 132 DCHECK_EQ(0, total_bytes_); |
| 130 total_bytes_ = total_bytes; | 133 total_bytes_ = total_bytes; |
| 131 } | 134 } |
| 132 | 135 |
| 133 } // namespace content | 136 } // namespace content |
| OLD | NEW |