From 6ddda2321e36dbe985aa67a6711c4ec3ce5b2285 Mon Sep 17 00:00:00 2001 From: "abodenha@google.com" Date: Fri, 22 Apr 2011 15:41:47 +0000 Subject: [PATCH] Fix deletion of temp files from port monitor. Adds a new switch to Chrome that will delete the printed file once the print dialog finishes. BUG= TEST= Review URL: http://codereview.chromium.org/6882092 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82651 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/printing/print_dialog_cloud.cc | 16 ++++++++++++- .../printing/print_dialog_cloud_internal.h | 3 +++ chrome/common/chrome_switches.cc | 16 ++++++++----- chrome/common/chrome_switches.h | 1 + .../win/port_monitor/port_monitor.cc | 24 +++++++------------ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/chrome/browser/printing/print_dialog_cloud.cc b/chrome/browser/printing/print_dialog_cloud.cc index ad3e7a56328cad..83f94e51f1fe48 100644 --- a/chrome/browser/printing/print_dialog_cloud.cc +++ b/chrome/browser/printing/print_dialog_cloud.cc @@ -413,7 +413,8 @@ CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( print_job_title, file_type)), modal_(modal), - owns_flow_handler_(true) { + owns_flow_handler_(true), + path_to_file_(path_to_file) { Init(width, height, json_arguments); } @@ -489,6 +490,14 @@ void CloudPrintHtmlDialogDelegate::OnDialogClosed( const std::string& json_retval) { // Get the final dialog size and store it. flow_handler_->StoreDialogClientSize(); + + if (CommandLine::ForCurrentProcess()->HasSwitch( + switches::kCloudPrintDeleteFile)) { + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, + NewRunnableFunction(&internal_cloud_print_helpers::Delete, + path_to_file_)); + } + // If we're modal we can show the dialog with no browser. // End the keep-alive so that Chrome can exit. if (!modal_) @@ -558,6 +567,11 @@ void CreateDialogImpl(const FilePath& path_to_file, } } +// Provides a runnable function to delete a file. +void Delete(const FilePath& file_path) { + file_util::Delete(file_path, false); +} + } // namespace internal_cloud_print_helpers namespace print_dialog_cloud { diff --git a/chrome/browser/printing/print_dialog_cloud_internal.h b/chrome/browser/printing/print_dialog_cloud_internal.h index b508a56f0b934a..1ceb2fb94207e8 100644 --- a/chrome/browser/printing/print_dialog_cloud_internal.h +++ b/chrome/browser/printing/print_dialog_cloud_internal.h @@ -173,6 +173,7 @@ class CloudPrintHtmlDialogDelegate : public HtmlDialogUIDelegate { CloudPrintFlowHandler* flow_handler_; bool modal_; mutable bool owns_flow_handler_; + FilePath path_to_file_; // The parameters needed to display a modal HTML dialog. HtmlDialogUI::HtmlDialogParams params_; @@ -185,6 +186,8 @@ void CreateDialogImpl(const FilePath& path_to_file, const std::string& file_type, bool modal); +void Delete(const FilePath& path_to_file); + } // namespace internal_cloud_print_helpers #endif // CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_ diff --git a/chrome/common/chrome_switches.cc b/chrome/common/chrome_switches.cc index f90aaafa89a00b..564c6ee24451f3 100644 --- a/chrome/common/chrome_switches.cc +++ b/chrome/common/chrome_switches.cc @@ -107,22 +107,26 @@ const char kChromeFrame[] = "chrome-frame"; // this version cannot be loaded, Chrome will exit. const char kChromeVersion[] = "chrome-version"; -// The unique id to be used for this cloud print proxy instance. -const char kCloudPrintProxyId[] = "cloud-print-proxy-id"; +// Used with kCloudPrintFile. Tells Chrome to delete the file when +// finished displaying the print dialog. +const char kCloudPrintDeleteFile[] = "cloud-print-delete-file"; // Tells chrome to display the cloud print dialog and upload the // specified file for printing. const char kCloudPrintFile[] = "cloud-print-file"; -// Used with kCloudPrintFile to specify a title for the resulting print -// job. -const char kCloudPrintJobTitle[] = "cloud-print-job-title"; - // Specifies the mime type to be used when uploading data from the // file referenced by cloud-print-file. // Defaults to "application/pdf" if unspecified. const char kCloudPrintFileType[] = "cloud-print-file-type"; +// Used with kCloudPrintFile to specify a title for the resulting print +// job. +const char kCloudPrintJobTitle[] = "cloud-print-job-title"; + +// The unique id to be used for this cloud print proxy instance. +const char kCloudPrintProxyId[] = "cloud-print-proxy-id"; + // The URL of the cloud print service to use, overrides any value // stored in preferences, and the default. Only used if the cloud // print service has been enabled (see enable-cloud-print). diff --git a/chrome/common/chrome_switches.h b/chrome/common/chrome_switches.h index 0a5518c867b6cb..cb48314652b133 100644 --- a/chrome/common/chrome_switches.h +++ b/chrome/common/chrome_switches.h @@ -47,6 +47,7 @@ extern const char kBrowserCrashTest[]; extern const char kCheckForUpdateIntervalSec[]; extern const char kChromeFrame[]; extern const char kChromeVersion[]; +extern const char kCloudPrintDeleteFile[]; extern const char kCloudPrintFile[]; extern const char kCloudPrintJobTitle[]; extern const char kCloudPrintFileType[]; diff --git a/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc b/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc index 5141f99615b32d..450ef5611ad4ae 100644 --- a/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc +++ b/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc @@ -28,6 +28,10 @@ namespace switches { // in order to avoid dependency problems. // TODO(abodenha@chromium.org) Reunify them in some sensible manner. +// Used with kCloudPrintFile. Tells Chrome to delete the file when +// finished displaying the print dialog. +const char kCloudPrintDeleteFile[] = "cloud-print-delete-file"; + // Tells chrome to display the cloud print dialog and upload the // specified file for printing. const char kCloudPrintFile[] = "cloud-print-file"; @@ -191,11 +195,8 @@ void HandlePortUi(HWND hwnd, const string16& caption) { // Launches the Cloud Print dialog in Chrome. // xps_path references a file to print. // job_title is the title to be used for the resulting print job. -// process_handle is set to the handle of the resulting process. bool LaunchPrintDialog(const string16& xps_path, - const string16& job_title, - base::ProcessHandle* process_handle) { - DCHECK(process_handle != NULL); + const string16& job_title) { HANDLE token = NULL; if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY, @@ -227,10 +228,11 @@ bool LaunchPrintDialog(const string16& xps_path, kXpsMimeType); command_line.AppendSwitchNative(switches::kCloudPrintJobTitle, job_title); + command_line.AppendSwitch(switches::kCloudPrintDeleteFile); base::LaunchAppAsUser(primary_token_scoped, command_line.command_line_string(), false, - process_handle); + NULL); return true; } @@ -452,7 +454,6 @@ BOOL WINAPI Monitor2ReadPort(HANDLE, BYTE*, DWORD, DWORD* read_bytes) { BOOL WINAPI Monitor2EndDocPort(HANDLE port_handle) { LOG(INFO) << "Monitor2EndDocPort"; - HANDLE process_handle = NULL; if (!ValidateCurrentUser()) { // TODO(abodenha@chromium.org) Abort the print job. return FALSE; @@ -473,16 +474,7 @@ BOOL WINAPI Monitor2EndDocPort(HANDLE port_handle) { &job_title); } LaunchPrintDialog(port_data->file_path->value().c_str(), - job_title, - &process_handle); - - // Wait for the print dialog process to exit and then delete the file. - // TODO(abodenha@chromium.org) Consider launching a thread to handle the - // deletion. - if (process_handle != NULL) { - WaitForSingleObject(process_handle, INFINITE); - } - file_util::Delete(*(port_data->file_path), false); + job_title); } if (port_data->printer_handle != NULL) { // Tell the spooler that the job is complete.