Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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) && !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.
| |
| 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 Loading... | |
| 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(); | |
| 1436 printing::StickySettings* sticky_settings = GetStickySettings(); | 1440 printing::StickySettings* sticky_settings = GetStickySettings(); |
| 1437 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( | 1441 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( |
| 1438 preview_web_contents()->GetBrowserContext())->GetPrefs()); | 1442 preview_web_contents()->GetBrowserContext())->GetPrefs()); |
| 1443 | |
| 1439 // Handle the no prompting case. Like the dialog prompt, this function | 1444 // Handle the no prompting case. Like the dialog prompt, this function |
| 1440 // returns and eventually FileSelected() gets called. | 1445 // returns and eventually FileSelected() gets called. |
| 1441 if (!prompt_user) { | 1446 if (!prompt_user) { |
| 1442 base::PostTaskWithTraitsAndReplyWithResult( | 1447 base::PostTaskWithTraitsAndReplyWithResult( |
| 1443 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, | 1448 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
| 1444 base::Bind(&GetUniquePath, | 1449 base::Bind(&GetUniquePath, |
| 1445 download_prefs->SaveFilePath().Append(default_filename)), | 1450 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 // Create the directory to save in if it does not exist. |
| 1457 base::PostTaskWithTraitsAndReply( | |
| 1458 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, | |
| 1459 base::Bind(&CreateDirectoryIfNeeded, download_prefs->SaveFilePath()), | |
| 1460 base::Bind(&PrintPreviewHandler::OnDirectoryCreated, | |
| 1461 weak_factory_.GetWeakPtr(), | |
| 1462 download_prefs->SaveFilePath().Append(default_filename))); | |
| 1463 } | |
| 1464 | |
| 1465 void PrintPreviewHandler::OnDirectoryCreated(base::FilePath path) { | |
| 1466 // Prompts the user to select the file. | |
| 1452 ui::SelectFileDialog::FileTypeInfo file_type_info; | 1467 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 1453 file_type_info.extensions.resize(1); | 1468 file_type_info.extensions.resize(1); |
| 1454 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 1469 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 1470 file_type_info.include_all_files = true; | |
| 1471 file_type_info.allowed_paths = | |
| 1472 ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH; | |
| 1455 | 1473 |
| 1456 select_file_dialog_ = | 1474 select_file_dialog_ = |
| 1457 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); | 1475 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); |
| 1458 select_file_dialog_->SelectFile( | 1476 select_file_dialog_->SelectFile( |
| 1459 ui::SelectFileDialog::SELECT_SAVEAS_FILE, | 1477 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), path, |
| 1460 base::string16(), | 1478 &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()), | 1479 platform_util::GetTopLevel(preview_web_contents()->GetNativeView()), |
| 1466 NULL); | 1480 NULL); |
| 1467 } | 1481 } |
| 1468 | 1482 |
| 1469 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) { | 1483 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) { |
| 1470 FileSelected(path, 0, nullptr); | 1484 FileSelected(path, 0, nullptr); |
| 1471 } | 1485 } |
| 1472 | 1486 |
| 1473 void PrintPreviewHandler::OnPrintPreviewFailed() { | 1487 void PrintPreviewHandler::OnPrintPreviewFailed() { |
| 1474 if (reported_failed_preview_) | 1488 if (reported_failed_preview_) |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1828 | 1842 |
| 1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1843 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1830 if (gaia_cookie_manager_service_) | 1844 if (gaia_cookie_manager_service_) |
| 1831 gaia_cookie_manager_service_->RemoveObserver(this); | 1845 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1832 } | 1846 } |
| 1833 | 1847 |
| 1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1848 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1835 const base::Closure& closure) { | 1849 const base::Closure& closure) { |
| 1836 pdf_file_saved_closure_ = closure; | 1850 pdf_file_saved_closure_ = closure; |
| 1837 } | 1851 } |
| OLD | NEW |