Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1107)

Unified Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 2952043002: Print Preview: Create Save As PDF directory if it does not exist. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
}

Powered by Google App Engine
This is Rietveld 408576698