forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_print_backend.cc
188 lines (149 loc) · 5.87 KB
/
test_print_backend.cc
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
// Copyright 2016 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 "printing/backend/test_print_backend.h"
#include <memory>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/location.h"
#include "base/logging.h"
#include "printing/backend/print_backend.h"
#include "printing/mojom/print.mojom.h"
namespace printing {
namespace {
mojom::ResultCode ReportErrorAccessDenied(const base::Location& from_here) {
DLOG(ERROR) << from_here.ToString() << " failed, access denied";
return mojom::ResultCode::kAccessDenied;
}
mojom::ResultCode ReportErrorNoData(const base::Location& from_here) {
DLOG(ERROR) << from_here.ToString() << " failed, no data";
return mojom::ResultCode::kFailed;
}
mojom::ResultCode ReportErrorNoDevice(const base::Location& from_here) {
DLOG(ERROR) << from_here.ToString() << " failed, no such device";
return mojom::ResultCode::kFailed;
}
mojom::ResultCode ReportErrorNotImplemented(const base::Location& from_here) {
DLOG(ERROR) << from_here.ToString() << " failed, method not implemented";
return mojom::ResultCode::kFailed;
}
} // namespace
TestPrintBackend::TestPrintBackend() : PrintBackend(/*locale=*/std::string()) {}
TestPrintBackend::~TestPrintBackend() = default;
mojom::ResultCode TestPrintBackend::EnumeratePrinters(
PrinterList* printer_list) {
DCHECK(printer_list->empty());
if (printer_map_.empty())
return mojom::ResultCode::kSuccess;
for (const auto& entry : printer_map_) {
const std::unique_ptr<PrinterData>& data = entry.second;
// Can only return basic info for printers which have registered info.
if (data->info)
printer_list->emplace_back(*data->info);
}
return mojom::ResultCode::kSuccess;
}
std::string TestPrintBackend::GetDefaultPrinterName() {
return default_printer_name_;
}
mojom::ResultCode TestPrintBackend::GetPrinterBasicInfo(
const std::string& printer_name,
PrinterBasicInfo* printer_info) {
auto found = printer_map_.find(printer_name);
if (found == printer_map_.end()) {
// Matching entry not found.
return ReportErrorNoDevice(FROM_HERE);
}
const std::unique_ptr<PrinterData>& data = found->second;
if (data->blocked_by_permissions)
return ReportErrorAccessDenied(FROM_HERE);
// Basic info might not have been provided.
if (!data->info)
return ReportErrorNoData(FROM_HERE);
*printer_info = *data->info;
return mojom::ResultCode::kSuccess;
}
mojom::ResultCode TestPrintBackend::GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_caps) {
auto found = printer_map_.find(printer_name);
if (found == printer_map_.end())
return ReportErrorNoDevice(FROM_HERE);
const std::unique_ptr<PrinterData>& data = found->second;
if (data->blocked_by_permissions)
return ReportErrorAccessDenied(FROM_HERE);
// Capabilities might not have been provided.
if (!data->caps)
return ReportErrorNoData(FROM_HERE);
*printer_caps = *data->caps;
return mojom::ResultCode::kSuccess;
}
mojom::ResultCode TestPrintBackend::GetPrinterCapsAndDefaults(
const std::string& printer_name,
PrinterCapsAndDefaults* printer_caps) {
return ReportErrorNotImplemented(FROM_HERE);
}
std::string TestPrintBackend::GetPrinterDriverInfo(
const std::string& printer_name) {
// not implemented
return "";
}
bool TestPrintBackend::IsValidPrinter(const std::string& printer_name) {
return base::Contains(printer_map_, printer_name);
}
void TestPrintBackend::SetDefaultPrinterName(const std::string& printer_name) {
if (default_printer_name_ == printer_name)
return;
auto found = printer_map_.find(printer_name);
if (found == printer_map_.end()) {
DLOG(ERROR) << "Unable to set an unknown printer as the default. Unknown "
<< "printer name: " << printer_name;
return;
}
// Previous default printer is no longer the default.
const std::unique_ptr<PrinterData>& new_default_data = found->second;
if (!default_printer_name_.empty())
printer_map_[default_printer_name_]->info->is_default = false;
// Now update new printer as default.
default_printer_name_ = printer_name;
if (!default_printer_name_.empty())
new_default_data->info->is_default = true;
}
void TestPrintBackend::AddValidPrinter(
const std::string& printer_name,
std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
std::unique_ptr<PrinterBasicInfo> info) {
AddPrinter(printer_name, std::move(caps), std::move(info),
/*blocked_by_permissions=*/false);
}
void TestPrintBackend::AddAccessDeniedPrinter(const std::string& printer_name) {
AddPrinter(printer_name, /*caps=*/nullptr, /*info=*/nullptr,
/*blocked_by_permissions=*/true);
}
void TestPrintBackend::AddPrinter(
const std::string& printer_name,
std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
std::unique_ptr<PrinterBasicInfo> info,
bool blocked_by_permissions) {
DCHECK(!printer_name.empty());
const bool is_default = info && info->is_default;
printer_map_[printer_name] = std::make_unique<PrinterData>(
std::move(caps), std::move(info), blocked_by_permissions);
// Ensure that default settings are honored if more than one is attempted to
// be marked as default or if this prior default should no longer be so.
if (is_default)
SetDefaultPrinterName(printer_name);
else if (default_printer_name_ == printer_name)
default_printer_name_.clear();
}
TestPrintBackend::PrinterData::PrinterData(
std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
std::unique_ptr<PrinterBasicInfo> info,
bool blocked_by_permissions)
: caps(std::move(caps)),
info(std::move(info)),
blocked_by_permissions(blocked_by_permissions) {}
TestPrintBackend::PrinterData::~PrinterData() = default;
} // namespace printing