-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
-#include "base/callback.h"
-#include "base/macros.h"
-#include "base/strings/string16.h"
+#include "base/functional/callback.h"
+#include "base/memory/raw_ptr.h"
#include "base/values.h"
+#include "build/build_config.h"
+#include "printing/buildflags/buildflags.h"
+#include "printing/mojom/print.mojom.h"
#include "printing/native_drawing_context.h"
#include "printing/print_settings.h"
#include "ui/gfx/native_widget_types.h"
namespace printing {
+class MetafilePlayer;
+class PrintingContextFactoryForTest;
+
+#if BUILDFLAG(IS_WIN)
+class PageSetup;
+class PrintedPage;
+#endif
+
// An abstraction of a printer context, implemented by objects that describe the
// user selected printing context. This includes the OS-dependent UI to ask the
// user about the print settings. Concrete implementations directly talk to the
// printer and manage the document and page breaks.
-class PRINTING_EXPORT PrintingContext {
+class COMPONENT_EXPORT(PRINTING) PrintingContext {
public:
// Printing context delegate.
class Delegate {
virtual std::string GetAppLocale() = 0;
};
- // Tri-state result for user behavior-dependent functions.
- enum Result {
- OK,
- CANCEL,
- FAILED,
+ struct PrinterSettings {
+#if BUILDFLAG(IS_MAC)
+ // True if the to-be-printed PDF is going to be opened in external
+ // preview. Used by macOS only to open Preview.app.
+ bool external_preview;
+#endif
+
+ // Whether to show the system dialog.
+ bool show_system_dialog;
+
+#if BUILDFLAG(IS_WIN)
+ // If showing the system dialog, the number of pages in the to-be-printed
+ // PDF. Only used on Windows.
+ int page_count;
+#endif
};
+ PrintingContext(const PrintingContext&) = delete;
+ PrintingContext& operator=(const PrintingContext&) = delete;
virtual ~PrintingContext();
// Callback of AskUserForSettings, used to notify the PrintJobWorker when
// print settings are available.
- using PrintSettingsCallback = base::OnceCallback<void(Result)>;
+ using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>;
// Asks the user what printer and format should be used to print. Updates the
// context with the select device settings. The result of the call is returned
// in the callback. This is necessary for Linux, which only has an
// asynchronous printing API.
- // On Android, when |is_scripted| is true, calling it initiates a full
+ // On Android, when `is_scripted` is true, calling it initiates a full
// printing flow from the framework's PrintManager.
// (see https://codereview.chromium.org/740983002/)
virtual void AskUserForSettings(int max_pages,
// Selects the user's default printer and format. Updates the context with the
// default device settings.
- virtual Result UseDefaultSettings() = 0;
+ virtual mojom::ResultCode UseDefaultSettings() = 0;
- // Updates the context with PDF printer settings.
- Result UsePdfSettings();
+ // Updates the context with PDF printer settings. The PDF settings are
+ // guaranteed to be valid.
+ void UsePdfSettings();
// Returns paper size to be used for PDF or Cloud Print in device units.
virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
// Updates printer settings.
- // |external_preview| is true if pdf is going to be opened in external
- // preview. Used by MacOS only now to open Preview.app.
- virtual Result UpdatePrinterSettings(bool external_preview,
- bool show_system_dialog,
- int page_count) = 0;
+ virtual mojom::ResultCode UpdatePrinterSettings(
+ const PrinterSettings& printer_settings) = 0;
- // Updates Print Settings. |job_settings| contains all print job
- // settings information.
- Result UpdatePrintSettings(base::Value job_settings);
+ // Updates Print Settings. `job_settings` contains all print job settings
+ // information.
+ mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings);
-#if defined(OS_CHROMEOS)
+#if BUILDFLAG(IS_CHROMEOS)
// Updates Print Settings.
- Result UpdatePrintSettingsFromPOD(
+ mojom::ResultCode UpdatePrintSettingsFromPOD(
std::unique_ptr<PrintSettings> job_settings);
#endif
+ // Sets the print settings to `settings`.
+ void SetPrintSettings(const PrintSettings& settings);
+
+ // Set the printable area in print settings to be the default printable area.
+ // Intended to be used only for virtual printers.
+ void SetDefaultPrintableAreaForVirtualPrinters();
+
// Does platform specific setup of the printer before the printing. Signal the
// printer that a document is about to be spooled.
// Warning: This function enters a message loop. That may cause side effects
// like IPC message processing! Some printers have side-effects on this call
// like virtual printers that ask the user for the path of the saved document;
// for example a PDF printer.
- virtual Result NewDocument(const base::string16& document_name) = 0;
+ virtual mojom::ResultCode NewDocument(
+ const std::u16string& document_name) = 0;
- // Starts a new page.
- virtual Result NewPage() = 0;
+#if BUILDFLAG(IS_WIN)
+ // Renders a page.
+ virtual mojom::ResultCode RenderPage(const PrintedPage& page,
+ const PageSetup& page_setup) = 0;
+#endif
- // Closes the printed page.
- virtual Result PageDone() = 0;
+ // Prints the document contained in `metafile`.
+ virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
+ const PrintSettings& settings,
+ uint32_t num_pages) = 0;
// Closes the printing job. After this call the object is ready to start a new
// document.
- virtual Result DocumentDone() = 0;
+ virtual mojom::ResultCode DocumentDone() = 0;
// Cancels printing. Can be used in a multi-threaded context. Takes effect
// immediately.
// Returns the native context used to print.
virtual printing::NativeDrawingContext context() const = 0;
- // Creates an instance of this object. Implementers of this interface should
- // implement this method to create an object of their implementation.
- static std::unique_ptr<PrintingContext> Create(Delegate* delegate);
+#if BUILDFLAG(IS_WIN)
+ // Initializes with predefined settings.
+ virtual mojom::ResultCode InitWithSettingsForTest(
+ std::unique_ptr<PrintSettings> settings) = 0;
+#endif
+
+ // Creates an instance of this object.
+ static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
+ bool skip_system_calls);
- void set_margin_type(MarginType type);
+ // Test method for generating printing contexts for testing. This overrides
+ // the platform-specific implementations of CreateImpl().
+ static void SetPrintingContextFactoryForTest(
+ PrintingContextFactoryForTest* factory);
+
+ void set_margin_type(mojom::MarginType type);
void set_is_modifiable(bool is_modifiable);
- const PrintSettings& settings() const {
- return settings_;
- }
+ const PrintSettings& settings() const;
+
+ std::unique_ptr<PrintSettings> TakeAndResetSettings();
+
+ bool PrintingAborted() const { return abort_printing_; }
int job_id() const { return job_id_; }
protected:
explicit PrintingContext(Delegate* delegate);
+ // Creates an instance of this object. Implementers of this interface should
+ // implement this method to create an object of their implementation.
+ static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
+ bool skip_system_calls);
+
// Reinitializes the settings for object reuse.
void ResetSettings();
+ // Determine if system calls should be skipped by this instance.
+ bool skip_system_calls() const {
+#if BUILDFLAG(ENABLE_OOP_PRINTING)
+ return skip_system_calls_;
+#else
+ return false;
+#endif
+ }
+
+#if BUILDFLAG(ENABLE_OOP_PRINTING)
+ // Make the one-way adjustment to have all system calls skipped by this
+ // `PrintingContext` instance.
+ void set_skip_system_calls() { skip_system_calls_ = true; }
+#endif
+
// Does bookkeeping when an error occurs.
- PrintingContext::Result OnError();
+ virtual mojom::ResultCode OnError();
// Complete print context settings.
- PrintSettings settings_;
+ std::unique_ptr<PrintSettings> settings_;
// Printing context delegate.
- Delegate* const delegate_;
+ const raw_ptr<Delegate> delegate_;
// Is a print job being done.
volatile bool in_print_job_;
int job_id_;
private:
- DISALLOW_COPY_AND_ASSIGN(PrintingContext);
+#if BUILDFLAG(ENABLE_OOP_PRINTING)
+ // If this instance of PrintingContext should skip making any system calls
+ // to the operating system.
+ bool skip_system_calls_ = false;
+#endif
};
} // namespace printing