| 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_package.h" | 5 #include "content/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/i18n/file_util_icu.h" | 12 #include "base/i18n/file_util_icu.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
| 18 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 21 #include "content/browser/download/download_item_impl.h" | 22 #include "content/browser/download/download_item_impl.h" |
| 22 #include "content/browser/download/download_manager_impl.h" | 23 #include "content/browser/download/download_manager_impl.h" |
| 23 #include "content/browser/download/download_stats.h" | 24 #include "content/browser/download/download_stats.h" |
| 24 #include "content/browser/download/save_file.h" | 25 #include "content/browser/download/save_file.h" |
| 25 #include "content/browser/download/save_file_manager.h" | 26 #include "content/browser/download/save_file_manager.h" |
| 26 #include "content/browser/download/save_item.h" | 27 #include "content/browser/download/save_item.h" |
| 27 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 28 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| (...skipping 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1086 file_manager_, | 1087 file_manager_, |
| 1087 it->second->save_id(), | 1088 it->second->save_id(), |
| 1088 it->second->url(), | 1089 it->second->url(), |
| 1089 id, | 1090 id, |
| 1090 true)); | 1091 true)); |
| 1091 } | 1092 } |
| 1092 return; | 1093 return; |
| 1093 } | 1094 } |
| 1094 | 1095 |
| 1095 SaveUrlItemMap::iterator it = in_progress_items_.find(frame_url.spec()); | 1096 SaveUrlItemMap::iterator it = in_progress_items_.find(frame_url.spec()); |
| 1096 if (it == in_progress_items_.end()) { | 1097 SaveItem* save_item = NULL; |
| 1098 if (it != in_progress_items_.end()) { |
| 1099 save_item = it->second; |
| 1100 if (save_item->last_data_received()) { |
| 1101 OnWriteToFinishedFile(false, *save_item); |
| 1102 // No point in writing the received data in such case. |
| 1103 // Otherwise we might stop the process of writing the already existing |
| 1104 // file (of the save_item). |
| 1105 return; |
| 1106 } |
| 1107 } else { |
| 1097 for (SavedItemMap::iterator saved_it = saved_success_items_.begin(); | 1108 for (SavedItemMap::iterator saved_it = saved_success_items_.begin(); |
| 1098 saved_it != saved_success_items_.end(); ++saved_it) { | 1109 saved_it != saved_success_items_.end(); ++saved_it) { |
| 1099 if (saved_it->second->url() == frame_url) { | 1110 if (saved_it->second->url() == frame_url) { |
| 1100 wrote_to_completed_file_ = true; | 1111 OnWriteToFinishedFile(false, *saved_it->second); |
| 1101 break; | 1112 break; |
| 1102 } | 1113 } |
| 1103 } | 1114 } |
| 1104 | 1115 |
| 1105 it = saved_failed_items_.find(frame_url.spec()); | 1116 it = saved_failed_items_.find(frame_url.spec()); |
| 1106 if (it != saved_failed_items_.end()) | 1117 if (it != saved_failed_items_.end()) |
| 1107 wrote_to_failed_file_ = true; | 1118 OnWriteToFinishedFile(true, *it->second); |
| 1108 | 1119 |
| 1109 return; | 1120 return; |
| 1110 } | 1121 } |
| 1111 | 1122 |
| 1112 SaveItem* save_item = it->second; | |
| 1113 DCHECK(save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM); | 1123 DCHECK(save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM); |
| 1114 | 1124 |
| 1115 if (!data.empty()) { | 1125 if (!data.empty()) { |
| 1116 // Prepare buffer for saving HTML data. | 1126 // Prepare buffer for saving HTML data. |
| 1117 scoped_refptr<net::IOBuffer> new_data(new net::IOBuffer(data.size())); | 1127 scoped_refptr<net::IOBuffer> new_data(new net::IOBuffer(data.size())); |
| 1118 memcpy(new_data->data(), data.data(), data.size()); | 1128 memcpy(new_data->data(), data.data(), data.size()); |
| 1119 | 1129 |
| 1120 // Call write file functionality in file thread. | 1130 // Call write file functionality in file thread. |
| 1121 BrowserThread::PostTask( | 1131 BrowserThread::PostTask( |
| 1122 BrowserThread::FILE, FROM_HERE, | 1132 BrowserThread::FILE, FROM_HERE, |
| 1123 base::Bind(&SaveFileManager::UpdateSaveProgress, | 1133 base::Bind(&SaveFileManager::UpdateSaveProgress, |
| 1124 file_manager_, | 1134 file_manager_, |
| 1125 save_item->save_id(), | 1135 save_item->save_id(), |
| 1126 new_data, | 1136 new_data, |
| 1127 static_cast<int>(data.size()))); | 1137 static_cast<int>(data.size()))); |
| 1128 } | 1138 } |
| 1129 | 1139 |
| 1130 // Current frame is completed saving, call finish in file thread. | 1140 // Current frame is completed saving, call finish in file thread. |
| 1131 if (flag == WebPageSerializerClient::CurrentFrameIsFinished) { | 1141 if (flag == WebPageSerializerClient::CurrentFrameIsFinished) { |
| 1132 VLOG(20) << " " << __FUNCTION__ << "()" | 1142 VLOG(20) << " " << __FUNCTION__ << "()" |
| 1133 << " save_id = " << save_item->save_id() | 1143 << " save_id = " << save_item->save_id() |
| 1134 << " url = \"" << save_item->url().spec() << "\""; | 1144 << " url = \"" << save_item->url().spec() << "\""; |
| 1145 |
| 1146 save_item->SetLastDataReceived(); |
| 1147 |
| 1135 BrowserThread::PostTask( | 1148 BrowserThread::PostTask( |
| 1136 BrowserThread::FILE, FROM_HERE, | 1149 BrowserThread::FILE, FROM_HERE, |
| 1137 base::Bind(&SaveFileManager::SaveFinished, | 1150 base::Bind(&SaveFileManager::SaveFinished, |
| 1138 file_manager_, | 1151 file_manager_, |
| 1139 save_item->save_id(), | 1152 save_item->save_id(), |
| 1140 save_item->url(), | 1153 save_item->url(), |
| 1141 id, | 1154 id, |
| 1142 true)); | 1155 true)); |
| 1143 } | 1156 } |
| 1144 } | 1157 } |
| 1145 | 1158 |
| 1159 void SavePackage::OnWriteToFinishedFile(bool failed, const SaveItem& item) { |
| 1160 std::string warning = base::StringPrintf( |
| 1161 "Received more data for save item with save_id = %d, url = \"%s\".", |
| 1162 item.save_id(), |
| 1163 item.url().spec().c_str()); |
| 1164 if (failed) { |
| 1165 wrote_to_failed_file_ = true; |
| 1166 LOG(WARNING) << warning << " The item is failed - ignoring the data."; |
| 1167 } else { |
| 1168 wrote_to_completed_file_ = true; |
| 1169 LOG(WARNING) << warning |
| 1170 << " The item is already finished - ignoring the data."; |
| 1171 } |
| 1172 } |
| 1173 |
| 1146 // Ask for all savable resource links from backend, include main frame and | 1174 // Ask for all savable resource links from backend, include main frame and |
| 1147 // sub-frame. | 1175 // sub-frame. |
| 1148 void SavePackage::GetAllSavableResourceLinksForCurrentPage() { | 1176 void SavePackage::GetAllSavableResourceLinksForCurrentPage() { |
| 1149 if (wait_state_ != START_PROCESS) | 1177 if (wait_state_ != START_PROCESS) |
| 1150 return; | 1178 return; |
| 1151 | 1179 |
| 1152 wait_state_ = RESOURCES_LIST; | 1180 wait_state_ = RESOURCES_LIST; |
| 1153 Send(new ViewMsg_GetAllSavableResourceLinksForCurrentPage(routing_id(), | 1181 Send(new ViewMsg_GetAllSavableResourceLinksForCurrentPage(routing_id(), |
| 1154 page_url_)); | 1182 page_url_)); |
| 1155 } | 1183 } |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1439 } | 1467 } |
| 1440 | 1468 |
| 1441 void SavePackage::OnDownloadDestroyed(DownloadItem* download) { | 1469 void SavePackage::OnDownloadDestroyed(DownloadItem* download) { |
| 1442 StopObservation(); | 1470 StopObservation(); |
| 1443 } | 1471 } |
| 1444 | 1472 |
| 1445 void SavePackage::FinalizeDownloadEntry() { | 1473 void SavePackage::FinalizeDownloadEntry() { |
| 1446 DCHECK(download_); | 1474 DCHECK(download_); |
| 1447 DCHECK(download_manager_); | 1475 DCHECK(download_manager_); |
| 1448 | 1476 |
| 1449 download_manager_->OnSavePackageSuccessfullyFinished(download_); | 1477 download_manager_->OnSavePackageFinished( |
| 1478 wait_state_ == FAILED ? false : true, download_); |
| 1450 StopObservation(); | 1479 StopObservation(); |
| 1451 } | 1480 } |
| 1452 | 1481 |
| 1453 } // namespace content | 1482 } // namespace content |
| OLD | NEW |