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> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/base64.h" | 15 #include "base/base64.h" |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/bind_helpers.h" | 17 #include "base/bind_helpers.h" |
| 18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 19 #include "base/i18n/file_util_icu.h" | 19 #include "base/i18n/file_util_icu.h" |
| 20 #include "base/i18n/number_formatting.h" | 20 #include "base/i18n/number_formatting.h" |
| 21 #include "base/json/json_reader.h" | 21 #include "base/json/json_reader.h" |
| 22 #include "base/lazy_instance.h" | 22 #include "base/lazy_instance.h" |
| 23 #include "base/macros.h" | 23 #include "base/macros.h" |
| 24 #include "base/memory/ref_counted_memory.h" | 24 #include "base/memory/ref_counted_memory.h" |
| 25 #include "base/metrics/histogram.h" | 25 #include "base/metrics/histogram.h" |
| 26 #include "base/path_service.h" | |
| 27 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
| 28 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 29 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
| 30 #include "base/task_runner_util.h" | 29 #include "base/task_runner_util.h" |
| 31 #include "base/threading/thread.h" | 30 #include "base/threading/thread.h" |
| 32 #include "base/threading/thread_restrictions.h" | |
| 33 #include "base/values.h" | 31 #include "base/values.h" |
| 34 #include "build/build_config.h" | 32 #include "build/build_config.h" |
| 35 #include "chrome/browser/app_mode/app_mode_utils.h" | 33 #include "chrome/browser/app_mode/app_mode_utils.h" |
| 36 #include "chrome/browser/browser_process.h" | 34 #include "chrome/browser/browser_process.h" |
| 35 #include "chrome/browser/download/download_prefs.h" | |
| 37 #include "chrome/browser/platform_util.h" | 36 #include "chrome/browser/platform_util.h" |
| 38 #include "chrome/browser/printing/print_dialog_cloud.h" | 37 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 39 #include "chrome/browser/printing/print_error_dialog.h" | 38 #include "chrome/browser/printing/print_error_dialog.h" |
| 40 #include "chrome/browser/printing/print_job_manager.h" | 39 #include "chrome/browser/printing/print_job_manager.h" |
| 41 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 40 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 42 #include "chrome/browser/printing/print_view_manager.h" | 41 #include "chrome/browser/printing/print_view_manager.h" |
| 43 #include "chrome/browser/printing/printer_manager_dialog.h" | 42 #include "chrome/browser/printing/printer_manager_dialog.h" |
| 44 #include "chrome/browser/profiles/profile.h" | 43 #include "chrome/browser/profiles/profile.h" |
| 45 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" | 44 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 46 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 45 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 47 #include "chrome/browser/signin/signin_manager_factory.h" | 46 #include "chrome/browser/signin/signin_manager_factory.h" |
| 48 #include "chrome/browser/ui/browser_finder.h" | 47 #include "chrome/browser/ui/browser_finder.h" |
| 49 #include "chrome/browser/ui/browser_tabstrip.h" | 48 #include "chrome/browser/ui/browser_tabstrip.h" |
| 50 #include "chrome/browser/ui/chrome_select_file_policy.h" | 49 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 51 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 50 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 52 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 51 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 53 #include "chrome/browser/ui/webui/print_preview/printer_handler.h" | 52 #include "chrome/browser/ui/webui/print_preview/printer_handler.h" |
| 54 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 53 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| 55 #include "chrome/common/chrome_paths.h" | |
| 56 #include "chrome/common/chrome_switches.h" | 54 #include "chrome/common/chrome_switches.h" |
| 57 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" | 55 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" |
| 58 #include "chrome/common/cloud_print/cloud_print_constants.h" | 56 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 59 #include "chrome/common/crash_keys.h" | 57 #include "chrome/common/crash_keys.h" |
| 60 #include "chrome/common/pref_names.h" | 58 #include "chrome/common/pref_names.h" |
| 61 #include "components/cloud_devices/common/cloud_device_description.h" | 59 #include "components/cloud_devices/common/cloud_device_description.h" |
| 62 #include "components/cloud_devices/common/cloud_devices_urls.h" | 60 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 63 #include "components/cloud_devices/common/printer_description.h" | 61 #include "components/cloud_devices/common/printer_description.h" |
| 64 #include "components/prefs/pref_service.h" | 62 #include "components/prefs/pref_service.h" |
| 65 #include "components/printing/common/print_messages.h" | 63 #include "components/printing/common/print_messages.h" |
| (...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1350 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, | 1348 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, |
| 1351 bool prompt_user) { | 1349 bool prompt_user) { |
| 1352 if (prompt_user) { | 1350 if (prompt_user) { |
| 1353 ChromeSelectFilePolicy policy(GetInitiator()); | 1351 ChromeSelectFilePolicy policy(GetInitiator()); |
| 1354 if (!policy.CanOpenSelectFileDialog()) { | 1352 if (!policy.CanOpenSelectFileDialog()) { |
| 1355 policy.SelectFileDenied(); | 1353 policy.SelectFileDenied(); |
| 1356 return ClosePreviewDialog(); | 1354 return ClosePreviewDialog(); |
| 1357 } | 1355 } |
| 1358 } | 1356 } |
| 1359 | 1357 |
| 1360 // Initializing |save_path_| if it is not already initialized. | 1358 // Get save location from Download Preferences. |
| 1359 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
| 1360 preview_web_contents()->GetBrowserContext()); | |
| 1361 base::FilePath file_path = download_prefs->SaveFilePath(); | |
| 1361 printing::StickySettings* sticky_settings = GetStickySettings(); | 1362 printing::StickySettings* sticky_settings = GetStickySettings(); |
| 1362 if (!sticky_settings->save_path()) { | 1363 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( |
| 1363 // Allowing IO operation temporarily. It is ok to do so here because | 1364 preview_web_contents()->GetBrowserContext())->GetPrefs()); |
| 1364 // the select file dialog performs IO anyway in order to display the | |
| 1365 // folders and also it is modal. | |
| 1366 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 1367 base::FilePath file_path; | |
| 1368 PathService::Get(chrome::DIR_USER_DOCUMENTS, &file_path); | |
| 1369 sticky_settings->StoreSavePath(file_path); | |
| 1370 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( | |
| 1371 preview_web_contents()->GetBrowserContext())->GetPrefs()); | |
| 1372 } | |
| 1373 | |
| 1374 // Handle the no prompting case. Like the dialog prompt, this function | 1365 // Handle the no prompting case. Like the dialog prompt, this function |
| 1375 // returns and eventually FileSelected() gets called. | 1366 // returns and eventually FileSelected() gets called. |
| 1376 if (!prompt_user) { | 1367 if (!prompt_user) { |
| 1377 base::PostTaskAndReplyWithResult( | 1368 base::PostTaskAndReplyWithResult( |
| 1378 BrowserThread::GetBlockingPool(), | 1369 BrowserThread::GetBlockingPool(), |
| 1379 FROM_HERE, | 1370 FROM_HERE, |
| 1380 base::Bind(&GetUniquePath, | 1371 base::Bind(&GetUniquePath, |
| 1381 sticky_settings->save_path()->Append(default_filename)), | 1372 download_prefs->SaveFilePath().Append(default_filename)), |
| 1382 base::Bind(&PrintPreviewHandler::OnGotUniqueFileName, | 1373 base::Bind(&PrintPreviewHandler::OnGotUniqueFileName, |
| 1383 weak_factory_.GetWeakPtr())); | 1374 weak_factory_.GetWeakPtr())); |
| 1384 return; | 1375 return; |
| 1385 } | 1376 } |
| 1386 | 1377 |
| 1387 // Otherwise prompt the user. | 1378 // Otherwise prompt the user. |
| 1388 ui::SelectFileDialog::FileTypeInfo file_type_info; | 1379 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 1389 file_type_info.extensions.resize(1); | 1380 file_type_info.extensions.resize(1); |
| 1390 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 1381 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 1391 | 1382 |
| 1392 select_file_dialog_ = | 1383 select_file_dialog_ = |
| 1393 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); | 1384 ui::SelectFileDialog::Create(this, nullptr /*policy already checked*/); |
| 1394 select_file_dialog_->SelectFile( | 1385 select_file_dialog_->SelectFile( |
| 1395 ui::SelectFileDialog::SELECT_SAVEAS_FILE, | 1386 ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
| 1396 base::string16(), | 1387 base::string16(), |
| 1397 sticky_settings->save_path()->Append(default_filename), | 1388 download_prefs->SaveFilePath().Append(default_filename), |
| 1398 &file_type_info, | 1389 &file_type_info, |
| 1399 0, | 1390 0, |
| 1400 base::FilePath::StringType(), | 1391 base::FilePath::StringType(), |
| 1401 platform_util::GetTopLevel(preview_web_contents()->GetNativeView()), | 1392 platform_util::GetTopLevel(preview_web_contents()->GetNativeView()), |
| 1402 NULL); | 1393 NULL); |
| 1403 } | 1394 } |
| 1404 | 1395 |
| 1405 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) { | 1396 void PrintPreviewHandler::OnGotUniqueFileName(const base::FilePath& path) { |
| 1406 FileSelected(path, 0, nullptr); | 1397 FileSelected(path, 0, nullptr); |
| 1407 } | 1398 } |
| 1408 | 1399 |
| 1409 void PrintPreviewHandler::OnPrintPreviewFailed() { | 1400 void PrintPreviewHandler::OnPrintPreviewFailed() { |
| 1410 if (reported_failed_preview_) | 1401 if (reported_failed_preview_) |
| 1411 return; | 1402 return; |
| 1412 reported_failed_preview_ = true; | 1403 reported_failed_preview_ = true; |
| 1413 ReportUserActionHistogram(PREVIEW_FAILED); | 1404 ReportUserActionHistogram(PREVIEW_FAILED); |
| 1414 } | 1405 } |
| 1415 | 1406 |
| 1416 #if defined(ENABLE_BASIC_PRINTING) | 1407 #if defined(ENABLE_BASIC_PRINTING) |
| 1417 void PrintPreviewHandler::ShowSystemDialog() { | 1408 void PrintPreviewHandler::ShowSystemDialog() { |
| 1418 HandleShowSystemDialog(NULL); | 1409 HandleShowSystemDialog(NULL); |
| 1419 } | 1410 } |
| 1420 #endif // ENABLE_BASIC_PRINTING | 1411 #endif // ENABLE_BASIC_PRINTING |
| 1421 | 1412 |
| 1422 void PrintPreviewHandler::FileSelected(const base::FilePath& path, | 1413 void PrintPreviewHandler::FileSelected(const base::FilePath& path, |
| 1423 int /* index */, | 1414 int /* index */, |
| 1424 void* /* params */) { | 1415 void* /* params */) { |
| 1425 // Updating |save_path_| to the newly selected folder. | 1416 // Update downloads location and save sticky settings. |
| 1417 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
| 1418 preview_web_contents()->GetBrowserContext()); | |
| 1419 download_prefs->SetSaveFilePath(path.DirName()); | |
| 1426 printing::StickySettings* sticky_settings = GetStickySettings(); | 1420 printing::StickySettings* sticky_settings = GetStickySettings(); |
| 1427 sticky_settings->StoreSavePath(path.DirName()); | 1421 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( |
|
Lei Zhang
2016/07/14 09:43:08
indentation is off
| |
| 1428 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( | |
| 1429 preview_web_contents()->GetBrowserContext())->GetPrefs()); | 1422 preview_web_contents()->GetBrowserContext())->GetPrefs()); |
| 1430 web_ui()->CallJavascriptFunctionUnsafe("fileSelectionCompleted"); | 1423 web_ui()->CallJavascriptFunctionUnsafe("fileSelectionCompleted"); |
| 1431 print_to_pdf_path_ = path; | 1424 print_to_pdf_path_ = path; |
| 1432 PostPrintToPdfTask(); | 1425 PostPrintToPdfTask(); |
| 1433 } | 1426 } |
| 1434 | 1427 |
| 1435 void PrintPreviewHandler::PostPrintToPdfTask() { | 1428 void PrintPreviewHandler::PostPrintToPdfTask() { |
| 1436 scoped_refptr<base::RefCountedBytes> data; | 1429 scoped_refptr<base::RefCountedBytes> data; |
| 1437 base::string16 title; | 1430 base::string16 title; |
| 1438 if (!GetPreviewDataAndTitle(&data, &title)) { | 1431 if (!GetPreviewDataAndTitle(&data, &title)) { |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1765 | 1758 |
| 1766 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1759 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1767 if (gaia_cookie_manager_service_) | 1760 if (gaia_cookie_manager_service_) |
| 1768 gaia_cookie_manager_service_->RemoveObserver(this); | 1761 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1769 } | 1762 } |
| 1770 | 1763 |
| 1771 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1764 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1772 const base::Closure& closure) { | 1765 const base::Closure& closure) { |
| 1773 pdf_file_saved_closure_ = closure; | 1766 pdf_file_saved_closure_ = closure; |
| 1774 } | 1767 } |
| OLD | NEW |