| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "chrome/browser/download/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 DCHECK(web_content); | 137 DCHECK(web_content); |
| 138 const GURL& current_page_url = tab_contents_->GetURL(); | 138 const GURL& current_page_url = tab_contents_->GetURL(); |
| 139 DCHECK(current_page_url.is_valid()); | 139 DCHECK(current_page_url.is_valid()); |
| 140 page_url_ = current_page_url; | 140 page_url_ = current_page_url; |
| 141 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || | 141 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || |
| 142 save_type_ == SAVE_AS_COMPLETE_HTML); | 142 save_type_ == SAVE_AS_COMPLETE_HTML); |
| 143 DCHECK(!saved_main_file_path_.empty() && | 143 DCHECK(!saved_main_file_path_.empty() && |
| 144 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 144 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
| 145 DCHECK(!saved_main_directory_path_.empty() && | 145 DCHECK(!saved_main_directory_path_.empty() && |
| 146 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 146 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
| 147 InternalInit(); |
| 147 } | 148 } |
| 148 | 149 |
| 149 SavePackage::SavePackage(TabContents* tab_contents) | 150 SavePackage::SavePackage(TabContents* tab_contents) |
| 150 : file_manager_(NULL), | 151 : file_manager_(NULL), |
| 151 tab_contents_(tab_contents), | 152 tab_contents_(tab_contents), |
| 152 download_(NULL), | 153 download_(NULL), |
| 153 finished_(false), | 154 finished_(false), |
| 154 user_canceled_(false), | 155 user_canceled_(false), |
| 155 disk_error_occurred_(false), | 156 disk_error_occurred_(false), |
| 156 save_type_(SAVE_TYPE_UNKNOWN), | 157 save_type_(SAVE_TYPE_UNKNOWN), |
| 157 all_save_items_count_(0), | 158 all_save_items_count_(0), |
| 158 wait_state_(INITIALIZE), | 159 wait_state_(INITIALIZE), |
| 159 tab_id_(tab_contents->process()->id()) { | 160 tab_id_(tab_contents->process()->id()) { |
| 160 const GURL& current_page_url = tab_contents_->GetURL(); | 161 const GURL& current_page_url = tab_contents_->GetURL(); |
| 161 DCHECK(current_page_url.is_valid()); | 162 DCHECK(current_page_url.is_valid()); |
| 162 page_url_ = current_page_url; | 163 page_url_ = current_page_url; |
| 164 InternalInit(); |
| 163 } | 165 } |
| 164 | 166 |
| 165 // This is for testing use. Set |finished_| as true because we don't want | 167 // This is for testing use. Set |finished_| as true because we don't want |
| 166 // method Cancel to be be called in destructor in test mode. | 168 // method Cancel to be be called in destructor in test mode. |
| 169 // We also don't call InternalInit(). |
| 167 SavePackage::SavePackage(const FilePath& file_full_path, | 170 SavePackage::SavePackage(const FilePath& file_full_path, |
| 168 const FilePath& directory_full_path) | 171 const FilePath& directory_full_path) |
| 169 : file_manager_(NULL), | 172 : file_manager_(NULL), |
| 170 download_(NULL), | 173 download_(NULL), |
| 171 saved_main_file_path_(file_full_path), | 174 saved_main_file_path_(file_full_path), |
| 172 saved_main_directory_path_(directory_full_path), | 175 saved_main_directory_path_(directory_full_path), |
| 173 finished_(true), | 176 finished_(true), |
| 174 user_canceled_(false), | 177 user_canceled_(false), |
| 175 disk_error_occurred_(false), | 178 disk_error_occurred_(false), |
| 176 save_type_(SAVE_TYPE_UNKNOWN), | 179 save_type_(SAVE_TYPE_UNKNOWN), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 void SavePackage::Cancel(bool user_action) { | 227 void SavePackage::Cancel(bool user_action) { |
| 225 if (!canceled()) { | 228 if (!canceled()) { |
| 226 if (user_action) | 229 if (user_action) |
| 227 user_canceled_ = true; | 230 user_canceled_ = true; |
| 228 else | 231 else |
| 229 disk_error_occurred_ = true; | 232 disk_error_occurred_ = true; |
| 230 Stop(); | 233 Stop(); |
| 231 } | 234 } |
| 232 } | 235 } |
| 233 | 236 |
| 237 // Init() can be called directly, or indirectly via GetSaveInfo(). In both |
| 238 // cases, we need file_manager_ to be initialized, so we do this first. |
| 239 void SavePackage::InternalInit() { |
| 240 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
| 241 if (!rdh) { |
| 242 NOTREACHED(); |
| 243 return; |
| 244 } |
| 245 |
| 246 file_manager_ = rdh->save_file_manager(); |
| 247 if (!file_manager_) { |
| 248 NOTREACHED(); |
| 249 return; |
| 250 } |
| 251 } |
| 252 |
| 234 // Initialize the SavePackage. | 253 // Initialize the SavePackage. |
| 235 bool SavePackage::Init() { | 254 bool SavePackage::Init() { |
| 236 // Set proper running state. | 255 // Set proper running state. |
| 237 if (wait_state_ != INITIALIZE) | 256 if (wait_state_ != INITIALIZE) |
| 238 return false; | 257 return false; |
| 239 | 258 |
| 240 wait_state_ = START_PROCESS; | 259 wait_state_ = START_PROCESS; |
| 241 | 260 |
| 242 // Initialize the request context and resource dispatcher. | 261 // Initialize the request context and resource dispatcher. |
| 243 Profile* profile = tab_contents_->profile(); | 262 Profile* profile = tab_contents_->profile(); |
| 244 if (!profile) { | 263 if (!profile) { |
| 245 NOTREACHED(); | 264 NOTREACHED(); |
| 246 return false; | 265 return false; |
| 247 } | 266 } |
| 248 | 267 |
| 249 request_context_ = profile->GetRequestContext(); | 268 request_context_ = profile->GetRequestContext(); |
| 250 | 269 |
| 251 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); | |
| 252 if (!rdh) { | |
| 253 NOTREACHED(); | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 file_manager_ = rdh->save_file_manager(); | |
| 258 if (!file_manager_) { | |
| 259 NOTREACHED(); | |
| 260 return false; | |
| 261 } | |
| 262 | |
| 263 // Create the fake DownloadItem and display the view. | 270 // Create the fake DownloadItem and display the view. |
| 264 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, GURL(), | 271 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, GURL(), |
| 265 "", FilePath(), Time::Now(), 0, -1, -1, false, false); | 272 "", FilePath(), Time::Now(), 0, -1, -1, false, false); |
| 266 download_->set_manager(tab_contents_->profile()->GetDownloadManager()); | 273 download_->set_manager(tab_contents_->profile()->GetDownloadManager()); |
| 267 tab_contents_->OnStartDownload(download_); | 274 tab_contents_->OnStartDownload(download_); |
| 268 | 275 |
| 269 // Check save type and process the save page job. | 276 // Check save type and process the save page job. |
| 270 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 277 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
| 271 // Get directory | 278 // Get directory |
| 272 DCHECK(!saved_main_directory_path_.empty()); | 279 DCHECK(!saved_main_directory_path_.empty()); |
| (...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1037 | 1044 |
| 1038 // Get the directory from preference. | 1045 // Get the directory from preference. |
| 1039 StringPrefMember save_file_path; | 1046 StringPrefMember save_file_path; |
| 1040 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); | 1047 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); |
| 1041 DCHECK(!(*save_file_path).empty()); | 1048 DCHECK(!(*save_file_path).empty()); |
| 1042 | 1049 |
| 1043 return FilePath::FromWStringHack(*save_file_path); | 1050 return FilePath::FromWStringHack(*save_file_path); |
| 1044 } | 1051 } |
| 1045 | 1052 |
| 1046 void SavePackage::GetSaveInfo() { | 1053 void SavePackage::GetSaveInfo() { |
| 1054 FilePath save_dir = |
| 1055 GetSaveDirPreference(tab_contents_->profile()->GetPrefs()); |
| 1056 file_manager_->file_loop()->PostTask(FROM_HERE, |
| 1057 NewRunnableMethod(file_manager_, |
| 1058 &SaveFileManager::CreateDownloadDirectory, |
| 1059 save_dir, |
| 1060 this)); |
| 1061 // CreateDownloadDirectory() calls ContinueGetSaveInfo() below. |
| 1062 } |
| 1063 |
| 1064 void SavePackage::ContinueGetSaveInfo(FilePath save_dir) { |
| 1047 // Use "Web Page, Complete" option as default choice of saving page. | 1065 // Use "Web Page, Complete" option as default choice of saving page. |
| 1048 int file_type_index = 2; | 1066 int file_type_index = 2; |
| 1049 SelectFileDialog::FileTypeInfo file_type_info; | 1067 SelectFileDialog::FileTypeInfo file_type_info; |
| 1050 FilePath::StringType default_extension; | 1068 FilePath::StringType default_extension; |
| 1051 | 1069 |
| 1052 SavePackageParam* save_params = | 1070 SavePackageParam* save_params = |
| 1053 new SavePackageParam(tab_contents_->contents_mime_type()); | 1071 new SavePackageParam(tab_contents_->contents_mime_type()); |
| 1054 | 1072 |
| 1055 bool can_save_as_complete = | 1073 bool can_save_as_complete = |
| 1056 CanSaveAsComplete(save_params->current_tab_mime_type); | 1074 CanSaveAsComplete(save_params->current_tab_mime_type); |
| 1057 | 1075 |
| 1058 FilePath title = | 1076 FilePath title = |
| 1059 FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle())); | 1077 FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle())); |
| 1060 FilePath save_dir = | |
| 1061 GetSaveDirPreference(tab_contents_->profile()->GetPrefs()); | |
| 1062 FilePath suggested_path = | 1078 FilePath suggested_path = |
| 1063 save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete)); | 1079 save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete)); |
| 1064 | 1080 |
| 1065 // If the contents can not be saved as complete-HTML, do not show the | 1081 // If the contents can not be saved as complete-HTML, do not show the |
| 1066 // file filters. | 1082 // file filters. |
| 1067 if (can_save_as_complete) { | 1083 if (can_save_as_complete) { |
| 1068 file_type_info.extensions.resize(2); | 1084 file_type_info.extensions.resize(2); |
| 1069 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); | 1085 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); |
| 1070 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); | 1086 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); |
| 1071 file_type_info.extension_description_overrides.push_back( | 1087 file_type_info.extension_description_overrides.push_back( |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1202 int index, void* params) { | 1218 int index, void* params) { |
| 1203 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1219 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1204 ContinueSave(save_params, path, index); | 1220 ContinueSave(save_params, path, index); |
| 1205 delete save_params; | 1221 delete save_params; |
| 1206 } | 1222 } |
| 1207 | 1223 |
| 1208 void SavePackage::FileSelectionCanceled(void* params) { | 1224 void SavePackage::FileSelectionCanceled(void* params) { |
| 1209 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1225 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1210 delete save_params; | 1226 delete save_params; |
| 1211 } | 1227 } |
| OLD | NEW |