File: cups_printers_handler.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 (317 lines) | stat: -rw-r--r-- 13,156 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2016 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_ASH_SETTINGS_PAGES_PRINTING_CUPS_PRINTERS_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_ASH_SETTINGS_PAGES_PRINTING_CUPS_PRINTERS_HANDLER_H_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ash/printing/cups_printers_manager.h"
#include "chrome/browser/ash/printing/printer_event_tracker.h"
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
#include "chromeos/ash/components/dbus/printscanmgr/printscanmgr_client.h"
#include "chromeos/printing/cups_printer_status.h"
#include "chromeos/printing/ppd_provider.h"
#include "chromeos/printing/printer_configuration.h"
#include "printing/printer_query_result.h"
#include "ui/shell_dialogs/select_file_dialog.h"

namespace base {
class FilePath;
}  // namespace base

namespace chromeos {
struct PrinterAuthenticationInfo;
}

namespace local_discovery {
class EndpointResolver;
}  // namespace local_discovery

namespace printing {
struct PrinterStatus;
}  // namespace printing

class GURL;
class Profile;

namespace ash {

class ServerPrintersFetcher;

namespace settings {

// Chrome OS CUPS printing settings page UI handler.
class CupsPrintersHandler : public ::settings::SettingsPageUIHandler,
                            public ui::SelectFileDialog::Listener,
                            public CupsPrintersManager::Observer,
                            public CupsPrintersManager::LocalPrintersObserver {
 public:
  static std::unique_ptr<CupsPrintersHandler> CreateForTesting(
      Profile* profile,
      scoped_refptr<chromeos::PpdProvider> ppd_provider,
      CupsPrintersManager* printers_manager);

  CupsPrintersHandler(Profile* profile, CupsPrintersManager* printers_manager);

  CupsPrintersHandler(const CupsPrintersHandler&) = delete;
  CupsPrintersHandler& operator=(const CupsPrintersHandler&) = delete;

  ~CupsPrintersHandler() override;

  // SettingsPageUIHandler overrides:
  void RegisterMessages() override;
  void OnJavascriptAllowed() override;
  void OnJavascriptDisallowed() override;

  void SetWebUIForTest(content::WebUI* web_ui);

 private:
  CupsPrintersHandler(Profile* profile,
                      scoped_refptr<chromeos::PpdProvider> ppd_provider,
                      CupsPrintersManager* printers_manager);

  // Gets all CUPS printers and return it to WebUI.
  void HandleGetCupsSavedPrintersList(const base::Value::List& args);
  void HandleGetCupsEnterprisePrintersList(const base::Value::List& args);
  void HandleUpdateCupsPrinter(const base::Value::List& args);
  void HandleRemoveCupsPrinter(const base::Value::List& args);
  void HandleRetrieveCupsPrinterPpd(const base::Value::List& args);

  void OnSetUpPrinter(const std::string& printer_id,
                      const std::string& printer_name,
                      const std::string& eula,
                      PrinterSetupResult result);

  // For a CupsPrinterInfo in |args|, retrieves the relevant PrinterInfo object
  // using an IPP call to the printer.
  void HandleGetPrinterInfo(const base::Value::List& args);

  // Handles the callback for HandleGetPrinterInfo. |callback_id| is the
  // identifier to resolve the correct Promise. |result| indicates if the query
  // was successful. |printer_status| contains the current status of the
  // printer. |make_and_model| is the unparsed printer-make-and-model string.
  // |ipp_everywhere| indicates if configuration using the CUPS IPP Everywhere
  // driver should be attempted. If |result| is not SUCCESS, the values of
  // |printer_status|, |make_and_model|, |document_formats|, |ipp_everywhere|
  // and |auth_info| are not specified.
  void OnAutoconfQueried(const std::string& callback_id,
                         printing::PrinterQueryResult result,
                         const printing::PrinterStatus& printer_status,
                         const std::string& make_and_model,
                         const std::vector<std::string>& document_formats,
                         bool ipp_everywhere,
                         const chromeos::PrinterAuthenticationInfo& auth_info,
                         const chromeos::IppPrinterInfo& ipp_printer_info);

  // Handles the callback for HandleGetPrinterInfo for a discovered printer.
  void OnAutoconfQueriedDiscovered(
      const std::string& callback_id,
      chromeos::Printer printer,
      printing::PrinterQueryResult result,
      const printing::PrinterStatus& printer_status,
      const std::string& make_and_model,
      const std::vector<std::string>& document_formats,
      bool ipp_everywhere,
      const chromeos::PrinterAuthenticationInfo& auth_info,
      const chromeos::IppPrinterInfo& ipp_printer_info);

  // Callback for PPD matching attempts;
  void OnPpdResolved(const std::string& callback_id,
                     base::Value::Dict info,
                     chromeos::PpdProvider::CallbackResultCode res,
                     const chromeos::Printer::PpdReference& ppd_ref,
                     const std::string& usb_manufacturer);

  void HandleAddCupsPrinter(const base::Value::List& args);

  void HandleReconfigureCupsPrinter(const base::Value::List& args);

  void AddOrReconfigurePrinter(const base::Value::List& args,
                               bool is_printer_edit);

  // Handles the result of adding a printer which the user specified the
  // location of (i.e. a printer that was not 'discovered' automatically).
  void OnAddedOrEditedSpecifiedPrinter(const std::string& callback_id,
                                       const chromeos::Printer& printer,
                                       bool is_printer_edit,
                                       PrinterSetupResult result);

  // Handles the result of failure to add a printer. |result_code| is used to
  // determine the reason for the failure.
  void OnAddOrEditPrinterError(const std::string& callback_id,
                               PrinterSetupResult result_code);

  // Get a list of all manufacturers for which we have at least one model of
  // printer supported.  Takes one argument, the callback id for the result.
  // The callback will be invoked with {success: <boolean>, models:
  // <Array<string>>}.
  void HandleGetCupsPrinterManufacturers(const base::Value::List& args);

  // Given a manufacturer, get a list of all models of printers for which we can
  // get drivers.  Takes two arguments - the callback id and the manufacturer
  // name for which we want to list models.  The callback will be called with
  // {success: <boolean>, models: Array<string>}.
  void HandleGetCupsPrinterModels(const base::Value::List& args);

  void HandleSelectPPDFile(const base::Value::List& args);

  // chromeos::PpdProvider callback handlers.
  void ResolveManufacturersDone(
      const std::string& callback_id,
      chromeos::PpdProvider::CallbackResultCode result_code,
      const std::vector<std::string>& available);
  void ResolvePrintersDone(
      const std::string& manufacturer,
      const std::string& callback_id,
      chromeos::PpdProvider::CallbackResultCode result_code,
      const chromeos::PpdProvider::ResolvedPrintersList& printers);

  void HandleStartDiscovery(const base::Value::List& args);
  void HandleStopDiscovery(const base::Value::List& args);

  // Logs printer set ups that are abandoned.
  void HandleSetUpCancel(const base::Value::List& args);

  // Given a printer id, find the corresponding ppdManufacturer and ppdModel.
  void HandleGetPrinterPpdManufacturerAndModel(const base::Value::List& args);
  void OnGetPrinterPpdManufacturerAndModel(
      const std::string& callback_id,
      chromeos::PpdProvider::CallbackResultCode result_code,
      const std::string& manufacturer,
      const std::string& model);

  // Emits the updated discovered printer list after new printers are received.
  void UpdateDiscoveredPrinters();

  // Attempt to add a discovered printer.
  void HandleAddDiscoveredPrinter(const base::Value::List& args);

  // Called when we get a response from
  // PrintscanmgrClient::CupsRetrievePrinterPpd.
  void OnRetrieveCupsPrinterPpd(
      const std::string& printer_id,
      const std::string& printer_name,
      const std::string& eula,
      std::optional<printscanmgr::CupsRetrievePpdResponse> response);

  void OnRetrievePpdError(const std::string& printer_name);
  void WriteAndDisplayPpdFile(const std::string& printer_name,
                              const std::string& ppd);
  void DisplayPpdFile(const base::FilePath& ppd_file_path);

  // Post printer setup callback.
  void OnAddedDiscoveredPrinter(const std::string& callback_id,
                                const chromeos::Printer& printer,
                                PrinterSetupResult result_code);

  // Code common between the discovered and manual add printer code paths.
  void OnAddedOrEditedPrinterCommon(const chromeos::Printer& printer,
                                    PrinterSetupResult result_code);

  // CupsPrintersManager::Observer override:
  void OnPrintersChanged(
      chromeos::PrinterClass printer_class,
      const std::vector<chromeos::Printer>& printers) override;

  // CupsPrintersManager::LocalPrintersObserver:
  void OnLocalPrintersUpdated() override;

  // Handles getting the EULA URL if available.
  void HandleGetEulaUrl(const base::Value::List& args);

  // Post EULA URL callback.
  void OnGetEulaUrl(const std::string& callback_id,
                    chromeos::PpdProvider::CallbackResultCode result,
                    const std::string& eula_url);

  // ui::SelectFileDialog::Listener override:
  void FileSelected(const ui::SelectedFileInfo& file, int index) override;
  void FileSelectionCanceled() override;

  // Used by FileSelected() in order to verify whether the beginning contents of
  // the selected file contain the magic number present in all PPD files. |path|
  // is used for display in the UI as this function calls back into javascript
  // with |path| as the result.
  void VerifyPpdContents(const base::FilePath& path,
                         const std::string& contents);

  // Fires the on-manually-add-discovered-printer event with the appropriate
  // parameters.  See https://crbug.com/835476
  void FireManuallyAddDiscoveredPrinter(const chromeos::Printer& printer);

  void OnIpResolved(const std::string& callback_id,
                    const chromeos::Printer& printer,
                    const net::IPEndPoint& endpoint);

  void HandleQueryPrintServer(const base::Value::List& args);

  void QueryPrintServer(const std::string& callback_id,
                        const GURL& server_url,
                        bool should_fallback);

  void OnQueryPrintServerCompleted(
      const std::string& callback_id,
      bool should_fallback,
      const ServerPrintersFetcher* sender,
      const GURL& server_url,
      std::vector<PrinterDetector::DetectedPrinter>&& returned_printers);

  void HandleOpenPrintManagementApp(const base::Value::List& args);

  void HandleOpenScanningApp(const base::Value::List& args);

  void HandleRequestPrinterStatus(const base::Value::List& args);

  void OnPrinterStatusReceived(
      const std::string& callback_id,
      const chromeos::CupsPrinterStatus& printer_status);

  raw_ptr<Profile, DanglingUntriaged> profile_;

  // Discovery support.  discovery_active_ tracks whether or not the UI
  // currently wants updates about printer availability.  The two vectors track
  // the most recent list of printers in each class.
  bool discovery_active_ = false;
  std::vector<chromeos::Printer> discovered_printers_;
  std::vector<chromeos::Printer> automatic_printers_;

  // These must be initialized before printers_manager_, as they are
  // used by callbacks that may be issued immediately by printers_manager_.
  //
  // TODO(crbug/757887) - Remove this subtle initialization constraint.
  scoped_refptr<chromeos::PpdProvider> ppd_provider_;

  // Cached list of {printer name, PpdReference} pairs for each manufacturer
  // that has been resolved in the lifetime of this object.
  std::map<std::string, chromeos::PpdProvider::ResolvedPrintersList>
      resolved_printers_;

  scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
  std::string webui_callback_id_;
  raw_ptr<CupsPrintersManager> printers_manager_;
  std::unique_ptr<local_discovery::EndpointResolver> endpoint_resolver_;

  std::unique_ptr<ServerPrintersFetcher> server_printers_fetcher_;

  base::ScopedObservation<CupsPrintersManager, CupsPrintersManager::Observer>
      printers_manager_observation_{this};

  base::ScopedObservation<CupsPrintersManager,
                          CupsPrintersManager::LocalPrintersObserver>
      local_printers_observation_{this};

  base::WeakPtrFactory<CupsPrintersHandler> weak_factory_{this};
};

}  // namespace settings
}  // namespace ash

#endif  // CHROME_BROWSER_UI_WEBUI_ASH_SETTINGS_PAGES_PRINTING_CUPS_PRINTERS_HANDLER_H_