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

Side by Side 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: Address comments 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/webui/print_preview/print_preview_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/ui/webui/print_preview/print_preview_handler.h" 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 base::FilePath unique_path = path; 451 base::FilePath unique_path = path;
452 int uniquifier = 452 int uniquifier =
453 base::GetUniquePathNumber(path, base::FilePath::StringType()); 453 base::GetUniquePathNumber(path, base::FilePath::StringType());
454 if (uniquifier > 0) { 454 if (uniquifier > 0) {
455 unique_path = unique_path.InsertBeforeExtensionASCII( 455 unique_path = unique_path.InsertBeforeExtensionASCII(
456 base::StringPrintf(" (%d)", uniquifier)); 456 base::StringPrintf(" (%d)", uniquifier));
457 } 457 }
458 return unique_path; 458 return unique_path;
459 } 459 }
460 460
461 void CreateDirectoryIfNeeded(const base::FilePath& path) {
462 if (!base::DirectoryExists(path))
463 base::CreateDirectory(path);
464 }
465
461 bool PrivetPrintingEnabled() { 466 bool PrivetPrintingEnabled() {
462 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) 467 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
463 return true; 468 return true;
464 #else 469 #else
465 return false; 470 return false;
466 #endif 471 #endif
467 } 472 }
468 473
469 } // namespace 474 } // namespace
470 475
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 ChromeSelectFilePolicy policy(GetInitiator()); 1430 ChromeSelectFilePolicy policy(GetInitiator());
1426 if (!policy.CanOpenSelectFileDialog()) { 1431 if (!policy.CanOpenSelectFileDialog()) {
1427 policy.SelectFileDenied(); 1432 policy.SelectFileDenied();
1428 return ClosePreviewDialog(); 1433 return ClosePreviewDialog();
1429 } 1434 }
1430 } 1435 }
1431 1436
1432 // Get save location from Download Preferences. 1437 // Get save location from Download Preferences.
1433 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( 1438 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
1434 preview_web_contents()->GetBrowserContext()); 1439 preview_web_contents()->GetBrowserContext());
1435 base::FilePath file_path = download_prefs->SaveFilePath(); 1440 base::FilePath path = download_prefs->SaveFilePath();
1436 printing::StickySettings* sticky_settings = GetStickySettings(); 1441 printing::StickySettings* sticky_settings = GetStickySettings();
1437 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( 1442 sticky_settings->SaveInPrefs(Profile::FromBrowserContext(
1438 preview_web_contents()->GetBrowserContext())->GetPrefs()); 1443 preview_web_contents()->GetBrowserContext())->GetPrefs());
1444
1439 // Handle the no prompting case. Like the dialog prompt, this function 1445 // Handle the no prompting case. Like the dialog prompt, this function
1440 // returns and eventually FileSelected() gets called. 1446 // returns and eventually FileSelected() gets called.
1441 if (!prompt_user) { 1447 if (!prompt_user) {
1442 base::PostTaskWithTraitsAndReplyWithResult( 1448 base::PostTaskWithTraitsAndReplyWithResult(
1443 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, 1449 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
1444 base::Bind(&GetUniquePath, 1450 base::Bind(&GetUniquePath, path.Append(default_filename)),
1445 download_prefs->SaveFilePath().Append(default_filename)),
1446 base::Bind(&PrintPreviewHandler::OnGotUniqueFileName, 1451 base::Bind(&PrintPreviewHandler::OnGotUniqueFileName,
1447 weak_factory_.GetWeakPtr())); 1452 weak_factory_.GetWeakPtr()));
1448 return; 1453 return;
1449 } 1454 }
1450 1455
1451 // Otherwise prompt the user. 1456 // If the directory is empty there is no reason to create it.
1457 if (path.empty()) {
1458 OnDirectoryCreated(default_filename);
1459 return;
1460 }
1461
1462 // Create the directory to save in if it does not exist.
1463 base::PostTaskWithTraitsAndReply(
1464 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
1465 base::Bind(&CreateDirectoryIfNeeded, path),
1466 base::Bind(&PrintPreviewHandler::OnDirectoryCreated,
1467 weak_factory_.GetWeakPtr(), path.Append(default_filename)));
1468 }
1469
1470 void PrintPreviewHandler::OnDirectoryCreated(const base::FilePath& path) {
1471 // Prompts the user to select the file.
1452 ui::SelectFileDialog::FileTypeInfo file_type_info; 1472 ui::SelectFileDialog::FileTypeInfo file_type_info;
1453 file_type_info.extensions.resize(1); 1473 file_type_info.extensions.resize(1);
1454 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); 1474 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf"));
1475 file_type_info.include_all_files = true;
1476 file_type_info.allowed_paths =
1477 ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH;
1455 1478
1456 select_file_dialog_ = 1479 select_file_dialog_ =
1457 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); 1480 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/);
1458 select_file_dialog_->SelectFile( 1481 select_file_dialog_->SelectFile(
1459 ui::SelectFileDialog::SELECT_SAVEAS_FILE, 1482 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), path,
1460 base::string16(), 1483 &file_type_info, 0, base::FilePath::StringType(),
1461 download_prefs->SaveFilePath().Append(default_filename),
1462 &file_type_info,
1463 0,
1464 base::FilePath::StringType(),
1465 platform_util::GetTopLevel(preview_web_contents()->GetNativeView()), 1484 platform_util::GetTopLevel(preview_web_contents()->GetNativeView()),
1466 NULL); 1485 NULL);
1467 } 1486 }
1468 1487
1469 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) { 1488 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) {
1470 FileSelected(path, 0, nullptr); 1489 FileSelected(path, 0, nullptr);
1471 } 1490 }
1472 1491
1473 void PrintPreviewHandler::OnPrintPreviewFailed() { 1492 void PrintPreviewHandler::OnPrintPreviewFailed() {
1474 if (reported_failed_preview_) 1493 if (reported_failed_preview_)
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
1828 1847
1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { 1848 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
1830 if (gaia_cookie_manager_service_) 1849 if (gaia_cookie_manager_service_)
1831 gaia_cookie_manager_service_->RemoveObserver(this); 1850 gaia_cookie_manager_service_->RemoveObserver(this);
1832 } 1851 }
1833 1852
1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( 1853 void PrintPreviewHandler::SetPdfSavedClosureForTesting(
1835 const base::Closure& closure) { 1854 const base::Closure& closure) {
1836 pdf_file_saved_closure_ = closure; 1855 pdf_file_saved_closure_ = closure;
1837 } 1856 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/print_preview/print_preview_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698