summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/printing/printer_query.cc
blob: c9b011eedbe402ce9aa0e36262f32456e8fd32db (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
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/printing/printer_query.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/task/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/printing/print_job_worker.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "printing/print_settings.h"

namespace printing {

PrinterQuery::PrinterQuery(int render_process_id, int render_frame_id)
    : cookie_(PrintSettings::NewCookie()),
      worker_(std::make_unique<PrintJobWorker>(render_process_id,
                                               render_frame_id)) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}

PrinterQuery::~PrinterQuery() {
  // The job should be finished (or at least canceled) when it is destroyed.
  DCHECK(!is_print_dialog_box_shown_);
  // If this fires, it is that this pending printer context has leaked.
  DCHECK(!worker_);
}

void PrinterQuery::GetSettingsDone(base::OnceClosure callback,
                                   std::unique_ptr<PrintSettings> new_settings,
                                   PrintingContext::Result result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  is_print_dialog_box_shown_ = false;
  last_status_ = result;
  if (result != PrintingContext::FAILED) {
    settings_ = std::move(new_settings);
    cookie_ = PrintSettings::NewCookie();
  } else {
    // Failure.
    cookie_ = 0;
  }

  std::move(callback).Run();
}

void PrinterQuery::PostSettingsDoneToIO(
    base::OnceClosure callback,
    std::unique_ptr<PrintSettings> new_settings,
    PrintingContext::Result result) {
  // |this| is owned by |callback|, so |base::Unretained()| is safe.
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(&PrinterQuery::GetSettingsDone, base::Unretained(this),
                     std::move(callback), std::move(new_settings), result));
}

std::unique_ptr<PrintJobWorker> PrinterQuery::DetachWorker() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(worker_);

  return std::move(worker_);
}

const PrintSettings& PrinterQuery::settings() const {
  return *settings_;
}

std::unique_ptr<PrintSettings> PrinterQuery::ExtractSettings() {
  return std::move(settings_);
}

void PrinterQuery::SetSettingsForTest(std::unique_ptr<PrintSettings> settings) {
  settings_ = std::move(settings);
}

int PrinterQuery::cookie() const {
  return cookie_;
}

void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
                               uint32_t expected_page_count,
                               bool has_selection,
                               mojom::MarginType margin_type,
                               bool is_scripted,
                               bool is_modifiable,
                               base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  DCHECK(!is_print_dialog_box_shown_ || !is_scripted);

  StartWorker();

  // Real work is done in PrintJobWorker::GetSettings().
  is_print_dialog_box_shown_ =
      ask_user_for_settings == GetSettingsAskParam::ASK_USER;
  // |this| is owned by |callback|, so |base::Unretained()| is safe.
  worker_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
          is_print_dialog_box_shown_, expected_page_count, has_selection,
          margin_type, is_scripted, is_modifiable,
          base::BindOnce(&PrinterQuery::PostSettingsDoneToIO,
                         base::Unretained(this), std::move(callback))));
}

void PrinterQuery::SetSettings(base::Value new_settings,
                               base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  StartWorker();
  // |this| is owned by |callback|, so |base::Unretained()| is safe.
  worker_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &PrintJobWorker::SetSettings, base::Unretained(worker_.get()),
          std::move(new_settings),
          base::BindOnce(&PrinterQuery::PostSettingsDoneToIO,
                         base::Unretained(this), std::move(callback))));
}

#if defined(OS_CHROMEOS)
void PrinterQuery::SetSettingsFromPOD(
    std::unique_ptr<printing::PrintSettings> new_settings,
    base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  StartWorker();
  // |this| is owned by |callback|, so |base::Unretained()| is safe.
  worker_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &PrintJobWorker::SetSettingsFromPOD, base::Unretained(worker_.get()),
          std::move(new_settings),
          base::BindOnce(&PrinterQuery::PostSettingsDoneToIO,
                         base::Unretained(this), std::move(callback))));
}
#endif

void PrinterQuery::StartWorker() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  DCHECK(worker_);

  // Lazily create the worker thread. There is one worker thread per print job.
  if (!worker_->IsRunning())
    worker_->Start();
}

void PrinterQuery::StopWorker() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  if (worker_) {
    // http://crbug.com/66082: We're blocking on the PrinterQuery's worker
    // thread.  It's not clear to me if this may result in blocking the current
    // thread for an unacceptable time.  We should probably fix it.
    base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_thread_join;
    worker_->Stop();
    worker_.reset();
  }
}

bool PrinterQuery::PostTask(const base::Location& from_here,
                            base::OnceClosure task) {
  return base::PostTask(from_here, {content::BrowserThread::IO},
                        std::move(task));
}

bool PrinterQuery::is_valid() const {
  return !!worker_;
}

}  // namespace printing