chromium/chrome/services/printing/public/mojom/print_backend_service.mojom

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module printing.mojom;

import "mojo/public/mojom/base/shared_memory.mojom";
import "mojo/public/mojom/base/string16.mojom";
import "mojo/public/mojom/base/values.mojom";
import "printing/backend/mojom/print_backend.mojom";
import "printing/mojom/print.mojom";
import "printing/mojom/printing_context.mojom";
import "sandbox/policy/mojom/sandbox.mojom";
import "ui/gfx/geometry/mojom/geometry.mojom";

[EnableIf=is_win]
import "chrome/services/printing/public/mojom/printer_xml_parser.mojom";

// The default printer name, or the `ResultCode` if there was an error when
// trying to retrieve this data.
union DefaultPrinterNameResult {
  string default_printer_name;
  ResultCode result_code;
};

// The list of installed printers, or the `ResultCode` if there was an error
// when trying to retrieve this data.
union PrinterListResult {
  array<PrinterBasicInfo> printer_list;
  ResultCode result_code;
};

// The capabilities and defaults of a printer, or the `ResultCode` if there was
// an error when trying to retrieve this data.
union PrinterSemanticCapsAndDefaultsResult {
  PrinterSemanticCapsAndDefaults printer_caps;
  ResultCode result_code;
};

// The set of basic info and capabilities/defaults for a printer.
struct PrinterCapsAndInfo {
  PrinterBasicInfo printer_info;
  PrinterSemanticCapsAndDefaults printer_caps;
};

// The set of basic info, paper details, and capabilities/defaults for a
// printer, or the `ResultCode` if there was an error when trying to retrieve
// this data.
union PrinterCapsAndInfoResult {
  PrinterCapsAndInfo printer_caps_and_info;
  ResultCode result_code;
};

// The print settings for a job, or the `ResultCode` if there was an error when
// trying to update this data.
union PrintSettingsResult {
  PrintSettings settings;
  ResultCode result_code;
};

// Hosts the PrintBackendService but does so without sandboxing the service -
// this is required if print drivers need UI access or cannot otherwise
// operate in the normal sandbox. There is a 1:1 relationship and `service`
// should only be bound once.
[ServiceSandbox=sandbox.mojom.Sandbox.kNoSandbox]
interface UnsandboxedPrintBackendHost {
  // Binds the underlying implementation.
  BindBackend(pending_receiver<PrintBackendService> service);
};

// Hosts the PrintBackendService in a sandbox. There is a 1:1 relationship
// and `service` should only be bound once.
[ServiceSandbox=sandbox.mojom.Sandbox.kPrintBackend]
interface SandboxedPrintBackendHost {
  // Binds the underlying implementation.
  BindBackend(pending_receiver<PrintBackendService> service);
};

// The main interface to Chrome's Print Backend Service, which performs
// printer queries and commands to operating system printer drivers in an
// isolated process.
interface PrintBackendService {
  // Establish the locale to be used for calls with this service and the
  // interface to the underlying data source.
  // For Windows, also establish the interface to the underlying data source.
  // If `remote` is valid, binds an interface that is used to communicate with
  // the browser process to parse XML in a separate process.
  Init(string locale,
       [EnableIf=is_win]
       pending_remote<PrinterXmlParser>? remote);

  // TODO(crbug.com/40775634)  Message with no arguments and no reply that is
  // useful to ensure that an idle timeout change takes effect.
  Poke();

  // Enumerates the list of installed local and network printers.
  EnumeratePrinters()
    => (PrinterListResult printer_list);

  // Gets the default printer name from the data source.
  GetDefaultPrinterName()
    => (DefaultPrinterNameResult printer_name);

  // Gets the semantic capabilities and defaults for a specific printer.
  [EnableIf=is_chromeos_ash]
  GetPrinterSemanticCapsAndDefaults(string printer_name)
    => (PrinterSemanticCapsAndDefaultsResult printer_caps);

  // Gets the basic info, paper sizes, and semantic capabilities and defaults
  // for a specific printer.
  FetchCapabilities(string printer_name)
    => (PrinterCapsAndInfoResult printer_caps_and_info);

  // Gets the printable area for a particular paper size.  Windows only, as a
  // workaround for the high cost of trying to get printable area for all paper
  // sizes in `FetchCapabilities()`.  Returns an empty area if there is any
  // error in retrieving the printable area.
  // TODO(crbug.com/40260379):  Remove this if
  // `PrintBackendWin::GetPrinterSemanticCapsAndDefaults()` can be made to
  // fetch the all printable areas in a performant manner.
  [EnableIf=is_win]
  GetPaperPrintableArea(string printer_name, RequestedMedia media)
    => (gfx.mojom.Rect printable_area_um);

  // Creates a printing context that can persist across multiple calls.
  // Necessary to ensure that the context used for system dialogs is still
  // available by the time that printing is started.  The printing context
  // is to be referenced by the provided `context_id` in all future calls that
  // need to use it.
  // Once an established printing context has been attached to a print job by
  // calling StartPrinting() then it will be automatically cleaned up as a
  // result of that print job completing successfully or being canceled.  It is
  // automatically destroyed upon any failure during UseDefaultSettings() or
  // AskUserForSettings().
  // The `parent_window_id` parameter, for configurations where it is
  // applicable, specifies an ID which is cross-process safe to represent the
  // native window which owns the modal print dialog used for prompting the
  // user. This can be needed if:
  //   - The AskUserForSettings() call will be invoked.
  //   - (Windows-specific)  If the job settings provided to StartPrinting()
  //     signal to use a system dialog.
  EstablishPrintingContext(uint32 context_id,
                           [EnableIf=enable_oop_basic_print_dialog]
                           uint32 parent_window_id);

  // Generates a print settings object based upon the default settings for the
  // default printer.  `context_id` must be for a context previously created
  // by a call to `EstablishPrintingContext()`.
  UseDefaultSettings(uint32 context_id)
    => (PrintSettingsResult settings);

  // Generates a print settings object from user-selected options via a system
  // print dialog.  `context_id` must be for a context previously created by a
  // call to `EstablishPrintingContext()`.
  // The previously established context has the necessary parent window ID for
  // displaying a system dialog.  Other parameters are the same as used in
  // `PrintingContext::AskUserForSettings()`.
  // Currently only supported on Windows:
  //   - It is impossible for macOS `//ui` code to display a system print
  //     dialog from a service utility process that is modal to a window in the
  //     browser process.  Achieving OOP support for a modal macOS system print
  //     may be possible if all of UI is moved out of the browser process.  See
  //     https://docs.google.com/document/d/1uAxz9pk3twwkzamchxHTQiImIbKrc0cuKVDpzbn2wsQ
  //     for discussion on such an effort.
  //   - TODO(crbug.com/40561724):  Determine if Linux Wayland can be made to
  //     have a system dialog be modal against an application window in the
  //     browser process.
  //   - Platforms other than those identified above do not support a system
  //     print dialog.
  [EnableIf=enable_oop_basic_print_dialog]
  AskUserForSettings(uint32 context_id,
                     int32 max_pages,
                     bool has_selection,
                     bool is_scripted)
    => (PrintSettingsResult settings);

  // Updates the indicated printing context based upon the job settings used
  // with the device settings.  The driver to be used is expected to be
  // identified in the `job_settings` map by an entry with key
  // `printing::kSettingDeviceName`.  `context_id` must be for a context
  // previously created by a call to `EstablishPrintingContext()`.  The updated
  // print settings remain in the context for use when a system dialog is
  // initiated and/or printing of a document is started.
  UpdatePrintSettings(uint32 context_id,
                      mojo_base.mojom.DictionaryValue job_settings)
    => (PrintSettingsResult settings);

  // Submit the document identified by `document_cookie` to be printed.
  // A printing context must already have been setup previously with a call to
  // `EstablishPrintingContext()`.
  // This call to `StartPrinting()` will take ownership of the indicated
  // context and automatically free it once the print job has completed (either
  // with success or via a cancel after failure).
  // Starting the device initialization may be delayed if it needs to wait for
  // prior print jobs to complete before a connection to the system to become
  // available.
  // For platforms which do not support an out-of-process system print dialog,
  // when doing system print the settings are specified from the system dialog
  // that is invoked from the browser process.  The settings are collected from
  // that and then are provided at start of printing via `settings`.  This is
  // optional since such settings are not necessary when printing from Print
  // Preview, where the print settings are provided via `UpdatePrintSettings()`.
  // A `job_id` is also provided back as a result from starting the print job.
  // This value will be non-zero if the job was successfully started, and zero
  // if there is any error.  This matches the behavior for the job ID in
  // `PrintingContext`.
  StartPrinting(uint32 context_id,
                int32 document_cookie,
                mojo_base.mojom.String16 document_name,
                [EnableIfNot=enable_oop_basic_print_dialog]
                PrintSettings? settings)
    => (ResultCode result_code,
        int32 job_id);

  // Render the indicated page as part of print job for `document_cookie`.
  [EnableIf=is_win]
  RenderPrintedPage(int32 document_cookie,
                    uint32 page_index,
                    MetafileDataType page_data_type,
                    mojo_base.mojom.ReadOnlySharedMemoryRegion serialized_page,
                    gfx.mojom.Size page_size,
                    gfx.mojom.Rect page_content_rect,
                    float shrink_factor)
    => (ResultCode result_code);

 // Render the print job document for `document_cookie`.
  RenderPrintedDocument(
      int32 document_cookie,
      uint32 page_count,
      MetafileDataType data_type,
      mojo_base.mojom.ReadOnlySharedMemoryRegion serialized_doc)
    => (ResultCode result_code);

  // Signals that all data for the document has been provided and final
  // processing and cleanup for the print job should be performed.
  DocumentDone(int32 document_cookie)
    => (ResultCode result_code);

  // Signal that the document has been canceled and its resources should
  // be released.
  Cancel(int32 document_cookie) => ();
};