File: local_printer_handler_chromeos.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 6,433 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 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_UI_WEBUI_PRINT_PREVIEW_LOCAL_PRINTER_HANDLER_CHROMEOS_H_
#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_LOCAL_PRINTER_HANDLER_CHROMEOS_H_

#include <memory>
#include <optional>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chromeos/crosapi/mojom/local_printer.mojom.h"

namespace base {
class Value;
}

namespace content {
class WebContents;
}

namespace printing {

// Managed print options constants.
inline constexpr char kManagedPrintOptions[] = "managedPrintOptions";

inline constexpr char kManagedPrintOptions_DefaultValue[] = "defaultValue";
inline constexpr char kManagedPrintOptions_AllowedValues[] = "allowedValues";

inline constexpr char kManagedPrintOptions_SizeWidth[] = "width";
inline constexpr char kManagedPrintOptions_SizeHeight[] = "height";

inline constexpr char kManagedPrintOptions_DpiHorizontal[] = "horizontal";
inline constexpr char kManagedPrintOptions_DpiVertical[] = "vertical";

inline constexpr char kManagedPrintOptions_MediaSize[] = "mediaSize";
inline constexpr char kManagedPrintOptions_MediaType[] = "mediaType";
inline constexpr char kManagedPrintOptions_Duplex[] = "duplex";
inline constexpr char kManagedPrintOptions_Color[] = "color";
inline constexpr char kManagedPrintOptions_Dpi[] = "dpi";
inline constexpr char kManagedPrintOptions_Quality[] = "quality";
inline constexpr char kManagedPrintOptions_PrintAsImage[] = "printAsImage";

// This class must be created and used on the UI thread.
class LocalPrinterHandlerChromeos : public PrinterHandler {
 public:
  using AshJobSettingsCallback =
      base::OnceCallback<void(base::Value::Dict settings)>;

  static std::unique_ptr<LocalPrinterHandlerChromeos> Create(
      content::WebContents* preview_web_contents);

  // Creates an instance suitable for testing with the given mojo connection to
  // Ash Chrome and with `preview_web_contents_` set to nullptr. PrinterHandler
  // methods run input callbacks with reasonable defaults when the mojo
  // connection is unavailable.
  static std::unique_ptr<LocalPrinterHandlerChromeos> CreateForTesting(
      crosapi::mojom::LocalPrinter* local_printer);

  // Prefer using Create() above.
  explicit LocalPrinterHandlerChromeos(
      content::WebContents* preview_web_contents);
  LocalPrinterHandlerChromeos(const LocalPrinterHandlerChromeos&) = delete;
  LocalPrinterHandlerChromeos& operator=(const LocalPrinterHandlerChromeos&) =
      delete;
  ~LocalPrinterHandlerChromeos() override;

  // Returns a LocalDestinationInfo object (defined in
  // chrome/browser/resources/print_preview/data/local_parsers.js).
  static base::Value::Dict PrinterToValue(
      const crosapi::mojom::LocalDestinationInfo& printer);

  // Returns a CapabilitiesResponse object (defined in
  // chrome/browser/resources/print_preview/native_layer.js).
  static base::Value::Dict CapabilityToValue(
      crosapi::mojom::CapabilitiesResponsePtr caps);

  // Returns a PrinterStatus object (defined in
  // chrome/browser/resources/print_preview/data/printer_status_cros.ts).
  static base::Value::Dict StatusToValue(
      const crosapi::mojom::PrinterStatus& status);

  // Return a ManagedPrintOptions object (defined in
  // chrome/browser/resources/print_preview/data/managed_print_options_cros.ts).
  static base::Value::Dict ManagedPrintOptionsToValue(
      const crosapi::mojom::ManagedPrintOptions& managed_print_options);

  // PrinterHandler implementation.
  void Reset() override;
  void GetDefaultPrinter(DefaultPrinterCallback callback) override;
  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::Dict settings,
                  scoped_refptr<base::RefCountedMemory> print_data,
                  PrintCallback callback) override;
  void StartGetEulaUrl(const std::string& destination_id,
                       GetEulaUrlCallback callback) override;
  void StartPrinterStatusRequest(
      const std::string& printer_id,
      PrinterStatusRequestCallback callback) override;

  // Public wrapper for `GetAshJobSettings` to use in tests.
  void GetAshJobSettingsForTesting(std::string printer_id,
                                   AshJobSettingsCallback callback,
                                   base::Value::Dict settings);

 private:
  // Get ash-specific job settings for the specified printer, merge them with
  // `settings`, and run `callback` with the result.
  void GetAshJobSettings(std::string printer_id,
                         AshJobSettingsCallback callback,
                         base::Value::Dict settings);

  // These functions call the corresponding `LocalPrinter` function, convert the
  // result to a job setting, add it to `settings`, and call `callback` with the
  // result.
  void GetUsernamePerPolicy(AshJobSettingsCallback callback,
                            base::Value::Dict settings) const;
  void GetOAuthToken(const std::string& printer_id,
                     AshJobSettingsCallback callback,
                     base::Value::Dict settings) const;
  void GetIppClientInfo(const std::string& printer_id,
                        AshJobSettingsCallback callback,
                        base::Value::Dict settings) const;

  // Wrapper for `printing::StartLocalPrint()` to use as a callback bound to the
  // lifetime of `this`.
  void CallStartLocalPrint(scoped_refptr<base::RefCountedMemory> print_data,
                           PrinterHandler::PrintCallback callback,
                           base::Value::Dict settings);

  const raw_ptr<content::WebContents> preview_web_contents_;
  raw_ptr<crosapi::mojom::LocalPrinter> local_printer_ = nullptr;
  base::WeakPtrFactory<LocalPrinterHandlerChromeos> weak_ptr_factory_{this};
};

}  // namespace printing

#endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_LOCAL_PRINTER_HANDLER_CHROMEOS_H_