| OLD | NEW |
| 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/save_package.h" | 5 #include "content/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 256 } |
| 257 | 257 |
| 258 bool SavePackage::Init() { | 258 bool SavePackage::Init() { |
| 259 // Set proper running state. | 259 // Set proper running state. |
| 260 if (wait_state_ != INITIALIZE) | 260 if (wait_state_ != INITIALIZE) |
| 261 return false; | 261 return false; |
| 262 | 262 |
| 263 wait_state_ = START_PROCESS; | 263 wait_state_ = START_PROCESS; |
| 264 | 264 |
| 265 // Initialize the request context and resource dispatcher. | 265 // Initialize the request context and resource dispatcher. |
| 266 Profile* profile = tab_contents()->profile(); | 266 Profile* profile = static_cast<Profile*>(tab_contents()->context()); |
| 267 if (!profile) { | 267 if (!profile) { |
| 268 NOTREACHED(); | 268 NOTREACHED(); |
| 269 return false; | 269 return false; |
| 270 } | 270 } |
| 271 | 271 |
| 272 // Create the fake DownloadItem and display the view. | 272 // Create the fake DownloadItem and display the view. |
| 273 DownloadManager* download_manager = | 273 DownloadManager* download_manager = profile->GetDownloadManager(); |
| 274 tab_contents()->profile()->GetDownloadManager(); | |
| 275 download_ = new DownloadItem(download_manager, | 274 download_ = new DownloadItem(download_manager, |
| 276 saved_main_file_path_, | 275 saved_main_file_path_, |
| 277 page_url_, | 276 page_url_, |
| 278 profile->IsOffTheRecord()); | 277 profile->IsOffTheRecord()); |
| 279 | 278 |
| 280 // Transfer the ownership to the download manager. We need the DownloadItem | 279 // Transfer the ownership to the download manager. We need the DownloadItem |
| 281 // to be alive as long as the Profile is alive. | 280 // to be alive as long as the Profile is alive. |
| 282 download_manager->SavePageAsDownloadStarted(download_); | 281 download_manager->SavePageAsDownloadStarted(download_); |
| 283 | 282 |
| 284 tab_contents()->OnStartDownload(download_); | 283 tab_contents()->OnStartDownload(download_); |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 save_item->url().spec()); | 812 save_item->url().spec()); |
| 814 DCHECK(it == in_progress_items_.end()); | 813 DCHECK(it == in_progress_items_.end()); |
| 815 in_progress_items_[save_item->url().spec()] = save_item; | 814 in_progress_items_[save_item->url().spec()] = save_item; |
| 816 save_item->Start(); | 815 save_item->Start(); |
| 817 file_manager_->SaveURL(save_item->url(), | 816 file_manager_->SaveURL(save_item->url(), |
| 818 save_item->referrer(), | 817 save_item->referrer(), |
| 819 tab_contents()->GetRenderProcessHost()->id(), | 818 tab_contents()->GetRenderProcessHost()->id(), |
| 820 routing_id(), | 819 routing_id(), |
| 821 save_item->save_source(), | 820 save_item->save_source(), |
| 822 save_item->full_path(), | 821 save_item->full_path(), |
| 823 tab_contents()->profile()->GetResourceContext(), | 822 tab_contents()->context()->GetResourceContext(), |
| 824 this); | 823 this); |
| 825 } while (process_all_remaining_items && waiting_item_queue_.size()); | 824 } while (process_all_remaining_items && waiting_item_queue_.size()); |
| 826 } | 825 } |
| 827 | 826 |
| 828 | 827 |
| 829 // Open download page in windows explorer on file thread, to avoid blocking the | 828 // Open download page in windows explorer on file thread, to avoid blocking the |
| 830 // user interface. | 829 // user interface. |
| 831 void SavePackage::ShowDownloadInShell() { | 830 void SavePackage::ShowDownloadInShell() { |
| 832 DCHECK(file_manager_); | 831 DCHECK(file_manager_); |
| 833 DCHECK(finished_ && !canceled() && !saved_main_file_path_.empty()); | 832 DCHECK(finished_ && !canceled() && !saved_main_file_path_.empty()); |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 FilePath save_file_path = prefs->GetFilePath( | 1199 FilePath save_file_path = prefs->GetFilePath( |
| 1201 prefs::kSaveFileDefaultDirectory); | 1200 prefs::kSaveFileDefaultDirectory); |
| 1202 DCHECK(!save_file_path.empty()); | 1201 DCHECK(!save_file_path.empty()); |
| 1203 | 1202 |
| 1204 return save_file_path; | 1203 return save_file_path; |
| 1205 } | 1204 } |
| 1206 | 1205 |
| 1207 void SavePackage::GetSaveInfo() { | 1206 void SavePackage::GetSaveInfo() { |
| 1208 // Can't use tab_contents_ in the file thread, so get the data that we need | 1207 // Can't use tab_contents_ in the file thread, so get the data that we need |
| 1209 // before calling to it. | 1208 // before calling to it. |
| 1210 PrefService* prefs = tab_contents()->profile()->GetPrefs(); | 1209 Profile* profile = static_cast<Profile*>(tab_contents()->context()); |
| 1210 PrefService* prefs = profile->GetPrefs(); |
| 1211 FilePath website_save_dir = GetSaveDirPreference(prefs); | 1211 FilePath website_save_dir = GetSaveDirPreference(prefs); |
| 1212 FilePath download_save_dir = prefs->GetFilePath( | 1212 FilePath download_save_dir = prefs->GetFilePath( |
| 1213 prefs::kDownloadDefaultDirectory); | 1213 prefs::kDownloadDefaultDirectory); |
| 1214 std::string mime_type = tab_contents()->contents_mime_type(); | 1214 std::string mime_type = tab_contents()->contents_mime_type(); |
| 1215 | 1215 |
| 1216 BrowserThread::PostTask( | 1216 BrowserThread::PostTask( |
| 1217 BrowserThread::FILE, FROM_HERE, | 1217 BrowserThread::FILE, FROM_HERE, |
| 1218 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, | 1218 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, |
| 1219 website_save_dir, download_save_dir, mime_type)); | 1219 website_save_dir, download_save_dir, mime_type)); |
| 1220 } | 1220 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1276 // Called after the save file dialog box returns. | 1276 // Called after the save file dialog box returns. |
| 1277 void SavePackage::OnPathPicked(const FilePath& final_name, | 1277 void SavePackage::OnPathPicked(const FilePath& final_name, |
| 1278 SavePackageType type) { | 1278 SavePackageType type) { |
| 1279 // Ensure the filename is safe. | 1279 // Ensure the filename is safe. |
| 1280 saved_main_file_path_ = final_name; | 1280 saved_main_file_path_ = final_name; |
| 1281 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), | 1281 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), |
| 1282 &saved_main_file_path_); | 1282 &saved_main_file_path_); |
| 1283 | 1283 |
| 1284 saved_main_directory_path_ = saved_main_file_path_.DirName(); | 1284 saved_main_directory_path_ = saved_main_file_path_.DirName(); |
| 1285 | 1285 |
| 1286 PrefService* prefs = tab_contents()->profile()->GetPrefs(); | 1286 Profile* profile = static_cast<Profile*>(tab_contents()->context()); |
| 1287 PrefService* prefs = profile->GetPrefs(); |
| 1287 StringPrefMember save_file_path; | 1288 StringPrefMember save_file_path; |
| 1288 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); | 1289 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); |
| 1289 #if defined(OS_POSIX) | 1290 #if defined(OS_POSIX) |
| 1290 std::string path_string = saved_main_directory_path_.value(); | 1291 std::string path_string = saved_main_directory_path_.value(); |
| 1291 #elif defined(OS_WIN) | 1292 #elif defined(OS_WIN) |
| 1292 std::string path_string = WideToUTF8(saved_main_directory_path_.value()); | 1293 std::string path_string = WideToUTF8(saved_main_directory_path_.value()); |
| 1293 #endif | 1294 #endif |
| 1294 // If user change the default saving directory, we will remember it just | 1295 // If user change the default saving directory, we will remember it just |
| 1295 // like IE and FireFox. | 1296 // like IE and FireFox. |
| 1296 if (!tab_contents()->profile()->IsOffTheRecord() && | 1297 if (!tab_contents()->context()->IsOffTheRecord() && |
| 1297 save_file_path.GetValue() != path_string) { | 1298 save_file_path.GetValue() != path_string) { |
| 1298 save_file_path.SetValue(path_string); | 1299 save_file_path.SetValue(path_string); |
| 1299 } | 1300 } |
| 1300 | 1301 |
| 1301 save_type_ = type; | 1302 save_type_ = type; |
| 1302 | 1303 |
| 1303 prefs->SetInteger(prefs::kSaveFileType, save_type_); | 1304 prefs->SetInteger(prefs::kSaveFileType, save_type_); |
| 1304 | 1305 |
| 1305 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { | 1306 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { |
| 1306 // Make new directory for saving complete file. | 1307 // Make new directory for saving complete file. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1326 bool SavePackage::IsSavableContents(const std::string& contents_mime_type) { | 1327 bool SavePackage::IsSavableContents(const std::string& contents_mime_type) { |
| 1327 // WebKit creates Document object when MIME type is application/xhtml+xml, | 1328 // WebKit creates Document object when MIME type is application/xhtml+xml, |
| 1328 // so we also support this MIME type. | 1329 // so we also support this MIME type. |
| 1329 return contents_mime_type == "text/html" || | 1330 return contents_mime_type == "text/html" || |
| 1330 contents_mime_type == "text/xml" || | 1331 contents_mime_type == "text/xml" || |
| 1331 contents_mime_type == "application/xhtml+xml" || | 1332 contents_mime_type == "application/xhtml+xml" || |
| 1332 contents_mime_type == "text/plain" || | 1333 contents_mime_type == "text/plain" || |
| 1333 contents_mime_type == "text/css" || | 1334 contents_mime_type == "text/css" || |
| 1334 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); | 1335 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); |
| 1335 } | 1336 } |
| OLD | NEW |