| // Copyright 2017 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PDF_PRINTER_HANDLER_H_ |
| #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PDF_PRINTER_HANDLER_H_ |
| |
| #include <memory> |
| #include <string> |
| |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| #include "build/chromeos_buildflags.h" |
| #include "chrome/browser/ui/webui/print_preview/printer_handler.h" |
| #include "ui/shell_dialogs/select_file_dialog.h" |
| |
| namespace base { |
| class FilePath; |
| class RefCountedMemory; |
| } |
| |
| namespace content { |
| class WebContents; |
| } |
| |
| class GURL; |
| class Profile; |
| |
| namespace printing { |
| |
| class PrintPreviewStickySettings; |
| |
| class PdfPrinterHandler : public PrinterHandler, |
| public ui::SelectFileDialog::Listener { |
| public: |
| PdfPrinterHandler(Profile* profile, |
| content::WebContents* preview_web_contents, |
| PrintPreviewStickySettings* sticky_settings); |
| |
| ~PdfPrinterHandler() override; |
| |
| // PrinterHandler implementation |
| void Reset() override; |
| // Required by PrinterHandler implementation but should never be called. |
| void StartGetPrinters(AddedPrintersCallback added_printers_callback, |
| GetPrintersDoneCallback done_callback) override; |
| void StartGetCapability(const std::string& destination_id, |
| GetCapabilityCallback callback) override; |
| void StartPrint(const std::u16string& job_title, |
| base::Value settings, |
| scoped_refptr<base::RefCountedMemory> print_data, |
| PrintCallback callback) override; |
| |
| // SelectFileDialog::Listener implementation. |
| void FileSelected(const base::FilePath& path, |
| int index, |
| void* params) override; |
| void FileSelectionCanceled(void* params) override; |
| |
| // Sets |pdf_file_saved_closure_| to |closure|. |
| void SetPdfSavedClosureForTesting(base::OnceClosure closure); |
| |
| // Sets |print_to_pdf_path_| to |path|. |
| void SetPrintToPdfPathForTesting(const base::FilePath& path); |
| |
| // Exposed for testing. |
| static base::FilePath GetFileNameForPrintJobTitle( |
| const std::u16string& job_title); |
| static base::FilePath GetFileNameForURL(const GURL& url); |
| static base::FilePath GetFileName(const GURL& url, |
| const std::u16string& job_title, |
| bool is_savable); |
| |
| protected: |
| virtual void SelectFile(const base::FilePath& default_filename, |
| content::WebContents* initiator, |
| bool prompt_user); |
| |
| // The print preview web contents. Protected so unit tests can access it. |
| content::WebContents* const preview_web_contents_; |
| |
| // The underlying dialog object. Protected so unit tests can access it. |
| scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
| |
| private: |
| void PostPrintToPdfTask(); |
| void OnGotUniqueFileName(const base::FilePath& path); |
| |
| // Prompts the user to save the file. The dialog will default to saving |
| // the file with name |filename| in |directory|. |
| void OnDirectorySelected(const base::FilePath& filename, |
| const base::FilePath& directory); |
| |
| // Return save location as the Drive mount or fetch from Download Preferences. |
| base::FilePath GetSaveLocation() const; |
| |
| Profile* const profile_; |
| PrintPreviewStickySettings* const sticky_settings_; |
| |
| // Holds the path to the print to pdf request. It is empty if no such request |
| // exists. |
| base::FilePath print_to_pdf_path_; |
| |
| // Notifies tests that want to know if the PDF has been saved. This doesn't |
| // notify the test if it was a successful save, only that it was attempted. |
| base::OnceClosure pdf_file_saved_closure_; |
| |
| // The data to print |
| scoped_refptr<base::RefCountedMemory> print_data_; |
| |
| // The callback to call when complete. |
| PrintCallback print_callback_; |
| |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| // Determines if the local Drive mount is sent to the file picker as the |
| // default save location. Set to true for Save to Drive print jobs. |
| bool use_drive_mount_ = false; |
| #endif |
| |
| base::WeakPtrFactory<PdfPrinterHandler> weak_ptr_factory_{this}; |
| |
| DISALLOW_COPY_AND_ASSIGN(PdfPrinterHandler); |
| }; |
| |
| } // namespace printing |
| |
| #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PDF_PRINTER_HANDLER_H_ |