1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
#define CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
#include <memory>
#include <optional>
#include "base/functional/callback.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/public/browser/global_routing_id.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"
#include "printing/printing_context.h"
#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/browser/printing/print_backend_service_manager.h"
#endif
namespace content {
class WebContents;
}
namespace printing {
class PrintJob;
class PrintJobWorker;
class PrinterQuery;
using CreatePrinterQueryCallback =
base::RepeatingCallback<std::unique_ptr<PrinterQuery>(
content::GlobalRenderFrameHostId rfh_id)>;
// Query the printer for settings. It initializes the PrintingContext, which can
// be blocking and/or run a message loop, and eventually is transferred to a new
// PrintJobWorker. All code in this class runs in the UI thread.
class PrinterQuery {
public:
using SettingsCallback =
base::OnceCallback<void(std::unique_ptr<PrintSettings>,
mojom::ResultCode)>;
#if BUILDFLAG(IS_WIN)
using OnDidUpdatePrintableAreaCallback =
base::OnceCallback<void(bool success)>;
#endif
static std::unique_ptr<PrinterQuery> Create(
content::GlobalRenderFrameHostId rfh_id);
PrinterQuery(const PrinterQuery&) = delete;
PrinterQuery& operator=(const PrinterQuery&) = delete;
virtual ~PrinterQuery();
// Creates a PrintJobWorker from this object's PrintingContext, transferring
// ownership. This instance becomes invalid after calling this function.
virtual std::unique_ptr<PrintJobWorker> TransferContextToNewWorker(
PrintJob* print_job);
const PrintSettings& settings() const;
content::GlobalRenderFrameHostId rfh_id() const { return rfh_id_; }
std::unique_ptr<PrintSettings> ExtractSettings();
// Initializes the printing context. It is fine to call this function multiple
// times to reinitialize the settings.
// Caller has to ensure that `this` is alive until `callback` is run.
void GetDefaultSettings(base::OnceClosure callback,
bool is_modifiable,
bool want_pdf_settings);
void GetSettingsFromUser(uint32_t expected_page_count,
bool has_selection,
mojom::MarginType margin_type,
bool is_scripted,
bool is_modifiable,
base::OnceClosure callback);
// Updates the current settings with `new_settings` dictionary values.
// Caller has to ensure that `this` is alive until `callback` is run.
virtual void SetSettings(base::Value::Dict new_settings,
base::OnceClosure callback);
#if BUILDFLAG(IS_CHROMEOS)
// Updates the current settings with `new_settings`.
// Caller has to ensure that `this` is alive until `callback` is run.
void SetSettingsFromPOD(std::unique_ptr<PrintSettings> new_settings,
base::OnceClosure callback);
#endif
#if BUILDFLAG(IS_WIN)
// Updates the printable area of the provided `PrintSettings` object.
// Caller has to ensure that `this` and `print_settings` are alive until
// `callback` runs.
// TODO(crbug.com/40260379): Remove this if the printable areas can be made
// fully available from `PrintBackend::GetPrinterSemanticCapsAndDefaults()`.
virtual void UpdatePrintableArea(PrintSettings* print_settings,
OnDidUpdatePrintableAreaCallback callback);
#endif
// Sets the printable area in `print_settings` to be the default printable
// area. Intended to be used only for virtual printers. Does not communicate
// with printer drivers, so it does not require special OOPPD handling.
static void ApplyDefaultPrintableAreaToVirtualPrinterPrintSettings(
PrintSettings& print_settings);
#if BUILDFLAG(ENABLE_OOP_PRINTING)
// Provide the client ID when the caller has registered with the
// `PrintBackendServiceManager` for getting settings for system print.
// Only intended to be used when out-of-process printing is in use, will
// DCHECK if used for in-browser printing.
virtual void SetClientId(PrintBackendServiceManager::ClientId client_id);
#endif
int cookie() const;
mojom::ResultCode last_status() const { return last_status_; }
// Returns true if a PrintingContext is still associated to this instance.
bool is_valid() const;
// Provide an override for generating worker threads in tests.
static void SetCreatePrinterQueryCallbackForTest(
CreatePrinterQueryCallback* callback);
protected:
explicit PrinterQuery(content::GlobalRenderFrameHostId rfh_id);
// Returns the WebContents this work corresponds to.
content::WebContents* GetWebContents();
// Reports settings back to `callback`.
void InvokeSettingsCallback(SettingsCallback callback,
mojom::ResultCode result);
// Virtual so that tests can override.
virtual void GetSettingsDone(base::OnceClosure callback,
std::optional<bool> maybe_is_modifiable,
std::unique_ptr<PrintSettings> new_settings,
mojom::ResultCode result);
void PostSettingsDone(base::OnceClosure callback,
std::optional<bool> maybe_is_modifiable,
std::unique_ptr<PrintSettings> new_settings,
mojom::ResultCode result);
void SetSettingsForTest(std::unique_ptr<PrintSettings> settings);
// Asks the user for print settings.
// Required on Mac and Linux. Windows can display UI from non-main threads,
// but sticks with this for consistency.
virtual void GetSettingsWithUI(uint32_t document_page_count,
bool has_selection,
bool is_scripted,
SettingsCallback callback);
// Initializes the print settings for PDF.
std::unique_ptr<PrintSettings> GetPdfSettings();
// Use the default settings. When using GTK+ or Mac, this can still end up
// displaying a dialog. So this needs to happen from the UI thread on these
// systems.
virtual void UseDefaultSettings(SettingsCallback callback);
// Called to update the print settings.
virtual void UpdatePrintSettings(base::Value::Dict new_settings,
SettingsCallback callback);
#if BUILDFLAG(IS_CHROMEOS)
// Called to update the print settings.
void UpdatePrintSettingsFromPOD(
std::unique_ptr<printing::PrintSettings> new_settings,
SettingsCallback callback);
#endif
// Used by `TransferContextToNewWorker()`. Virtual to support testing.
virtual std::unique_ptr<PrintJobWorker> CreatePrintJobWorker(
PrintJob* print_job);
PrintingContext* printing_context() { return printing_context_.get(); }
// Printing context delegate.
std::unique_ptr<PrintingContext::Delegate> printing_context_delegate_;
// Information about the printer setting.
std::unique_ptr<PrintingContext> printing_context_;
const content::GlobalRenderFrameHostId rfh_id_;
// Is the Print... dialog box currently shown.
bool is_print_dialog_box_shown_ = false;
private:
// Cache of the print context settings.
std::unique_ptr<PrintSettings> settings_;
// Cookie that make this instance unique.
int cookie_;
// Results from the last GetSettingsDone() callback.
mojom::ResultCode last_status_ = mojom::ResultCode::kFailed;
base::WeakPtrFactory<PrinterQuery> weak_factory_{this};
};
} // namespace printing
#endif // CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
|