Index: chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
diff --git a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
index b4ceb816e49a79177a1e882ae57422e66f952db4..3d46d9344b5b8a1b94c82cdcbb6c187596cbc98b 100644 |
--- a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
+++ b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
@@ -458,6 +458,11 @@ base::FilePath GetUniquePath(const base::FilePath& path) { |
return unique_path; |
} |
+void CreateDirectoryIfNeeded(const base::FilePath& path) { |
+ if (!base::DirectoryExists(path) && !path.empty()) |
Lei Zhang
2017/06/22 01:39:56
Check !path.empty() first since that's cheaper and
rbpotter
2017/06/22 19:57:26
Done.
|
+ base::CreateDirectory(path); |
+} |
+ |
bool PrivetPrintingEnabled() { |
#if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
return true; |
@@ -1432,10 +1437,10 @@ void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, |
// Get save location from Download Preferences. |
DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
preview_web_contents()->GetBrowserContext()); |
- base::FilePath file_path = download_prefs->SaveFilePath(); |
printing::StickySettings* sticky_settings = GetStickySettings(); |
sticky_settings->SaveInPrefs(Profile::FromBrowserContext( |
preview_web_contents()->GetBrowserContext())->GetPrefs()); |
+ |
// Handle the no prompting case. Like the dialog prompt, this function |
// returns and eventually FileSelected() gets called. |
if (!prompt_user) { |
@@ -1448,20 +1453,29 @@ void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, |
return; |
} |
- // Otherwise prompt the user. |
+ // Create the directory to save in if it does not exist. |
+ base::PostTaskWithTraitsAndReply( |
+ FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
+ base::Bind(&CreateDirectoryIfNeeded, download_prefs->SaveFilePath()), |
+ base::Bind(&PrintPreviewHandler::OnDirectoryCreated, |
+ weak_factory_.GetWeakPtr(), |
+ download_prefs->SaveFilePath().Append(default_filename))); |
+} |
+ |
+void PrintPreviewHandler::OnDirectoryCreated(base::FilePath path) { |
+ // Prompts the user to select the file. |
ui::SelectFileDialog::FileTypeInfo file_type_info; |
file_type_info.extensions.resize(1); |
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
+ file_type_info.include_all_files = true; |
+ file_type_info.allowed_paths = |
+ ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH; |
select_file_dialog_ = |
ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); |
select_file_dialog_->SelectFile( |
- ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
- base::string16(), |
- download_prefs->SaveFilePath().Append(default_filename), |
- &file_type_info, |
- 0, |
- base::FilePath::StringType(), |
+ ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), path, |
+ &file_type_info, 0, base::FilePath::StringType(), |
platform_util::GetTopLevel(preview_web_contents()->GetNativeView()), |
NULL); |
} |