summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/printing/printer_query.h
blob: a0549367bb196534deab4758f6e02d794dd1c11d (plain)
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
// 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 "base/callback.h"
#include "base/values.h"
#include "build/build_config.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"
#include "printing/printing_context.h"

namespace base {
class Location;
}

namespace content {
struct GlobalRenderFrameHostId;
}

namespace printing {

class PrintJobWorker;

using CreatePrintJobWorkerCallback =
    base::RepeatingCallback<std::unique_ptr<PrintJobWorker>(
        content::GlobalRenderFrameHostId rfh_id)>;

// Query the printer for settings.  All code in this class runs in the UI
// thread.
class PrinterQuery {
 public:
  explicit PrinterQuery(content::GlobalRenderFrameHostId rfh_id);

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

  virtual ~PrinterQuery();

  // Detach the PrintJobWorker associated to this object. Virtual so that tests
  // can override.
  virtual std::unique_ptr<PrintJobWorker> DetachWorker();

  const PrintSettings& settings() const;

  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);
  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

  // Stops the worker thread since the client is done with this object.
  virtual void StopWorker();

  int cookie() const;
  mojom::ResultCode last_status() const { return last_status_; }

  // Returns if a worker thread is still associated to this instance.
  bool is_valid() const;

  // Posts the given task to be run.
  bool PostTask(const base::Location& from_here, base::OnceClosure task);

  // Provide an override for generating worker threads in tests.
  static void SetCreatePrintJobWorkerCallbackForTest(
      CreatePrintJobWorkerCallback* callback);

 protected:
  // Virtual so that tests can override.
  virtual void GetSettingsDone(base::OnceClosure callback,
                               absl::optional<bool> maybe_is_modifiable,
                               std::unique_ptr<PrintSettings> new_settings,
                               mojom::ResultCode result);

  void PostSettingsDone(base::OnceClosure callback,
                        absl::optional<bool> maybe_is_modifiable,
                        std::unique_ptr<PrintSettings> new_settings,
                        mojom::ResultCode result);

  void SetSettingsForTest(std::unique_ptr<PrintSettings> settings);

 private:
  // Lazy create the worker thread. There is one worker thread per print job.
  void StartWorker();

  // Cache of the print context settings for access in the UI thread.
  std::unique_ptr<PrintSettings> settings_;

  // Is the Print... dialog box currently shown.
  bool is_print_dialog_box_shown_ = false;

  // Cookie that make this instance unique.
  int cookie_;

  // Results from the last GetSettingsDone() callback.
  mojom::ResultCode last_status_ = mojom::ResultCode::kFailed;

  // All the UI is done in a worker thread because many Win32 print functions
  // are blocking and enters a message loop without your consent. There is one
  // worker thread per print job.
  std::unique_ptr<PrintJobWorker> worker_;
};

}  // namespace printing

#endif  // CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_