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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/dbus/printscanmgr/printscanmgr_client.h"
#include <stdint.h>
#include <dbus/dbus-protocol.h>
#include <map>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "chromeos/ash/components/dbus/printscanmgr/fake_printscanmgr_client.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
PrintscanmgrClient* g_instance = nullptr;
constexpr base::TimeDelta kAutoconfiguredTimeout = base::Seconds(30);
printscanmgr::AddPrinterResult DBusErrorFromString(
const std::string& dbus_error_string) {
static const base::NoDestructor<
std::map<std::string, printscanmgr::AddPrinterResult>>
error_string_map({
{DBUS_ERROR_NO_REPLY,
printscanmgr::AddPrinterResult::ADD_PRINTER_RESULT_DBUS_NO_REPLY},
{DBUS_ERROR_TIMEOUT,
printscanmgr::AddPrinterResult::ADD_PRINTER_RESULT_DBUS_TIMEOUT},
});
auto it = error_string_map->find(dbus_error_string);
return it != error_string_map->end()
? it->second
: printscanmgr::AddPrinterResult::ADD_PRINTER_RESULT_DBUS_GENERIC;
}
// The PrintscanmgrClient implementation used in production.
class PrintscanmgrClientImpl : public PrintscanmgrClient {
public:
PrintscanmgrClientImpl() = default;
PrintscanmgrClientImpl(const PrintscanmgrClientImpl&) = delete;
PrintscanmgrClientImpl& operator=(const PrintscanmgrClientImpl&) = delete;
~PrintscanmgrClientImpl() override = default;
// DBusClient overrides:
void Init(dbus::Bus* bus) override {
printscanmgr_proxy_ = bus->GetObjectProxy(
printscanmgr::kPrintscanmgrServiceName,
dbus::ObjectPath(printscanmgr::kPrintscanmgrServicePath));
}
// PrintscanmgrClient overrides:
void CupsAddManuallyConfiguredPrinter(
const printscanmgr::CupsAddManuallyConfiguredPrinterRequest& request,
chromeos::DBusMethodCallback<
printscanmgr::CupsAddManuallyConfiguredPrinterResponse> callback)
override {
dbus::MethodCall method_call(
printscanmgr::kPrintscanmgrInterface,
printscanmgr::kCupsAddManuallyConfiguredPrinter);
dbus::MessageWriter writer(&method_call);
if (!writer.AppendProtoAsArrayOfBytes(request)) {
LOG(ERROR) << "Failed to encode CupsAddManuallyConfiguredPrinterRequest "
"protobuf.";
printscanmgr::CupsAddManuallyConfiguredPrinterResponse response;
response.set_result(printscanmgr::AddPrinterResult::
ADD_PRINTER_RESULT_DBUS_ENCODING_FAILURE);
std::move(callback).Run(std::move(response));
return;
}
printscanmgr_proxy_->CallMethodWithErrorResponse(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(
&PrintscanmgrClientImpl::OnPrinterAdded<
printscanmgr::CupsAddManuallyConfiguredPrinterResponse>,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void CupsAddAutoConfiguredPrinter(
const printscanmgr::CupsAddAutoConfiguredPrinterRequest& request,
chromeos::DBusMethodCallback<
printscanmgr::CupsAddAutoConfiguredPrinterResponse> callback)
override {
dbus::MethodCall method_call(printscanmgr::kPrintscanmgrInterface,
printscanmgr::kCupsAddAutoConfiguredPrinter);
dbus::MessageWriter writer(&method_call);
if (!writer.AppendProtoAsArrayOfBytes(request)) {
LOG(ERROR) << "Failed to encode CupsAddAutoConfiguredPrinterRequest "
"protobuf.";
printscanmgr::CupsAddAutoConfiguredPrinterResponse response;
response.set_result(printscanmgr::AddPrinterResult::
ADD_PRINTER_RESULT_DBUS_ENCODING_FAILURE);
std::move(callback).Run(std::move(response));
return;
}
printscanmgr_proxy_->CallMethodWithErrorResponse(
&method_call, kAutoconfiguredTimeout.InMilliseconds(),
base::BindOnce(&PrintscanmgrClientImpl::OnPrinterAdded<
printscanmgr::CupsAddAutoConfiguredPrinterResponse>,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void CupsRemovePrinter(
const printscanmgr::CupsRemovePrinterRequest& request,
chromeos::DBusMethodCallback<printscanmgr::CupsRemovePrinterResponse>
callback,
base::OnceClosure error_callback) override {
dbus::MethodCall method_call(printscanmgr::kPrintscanmgrInterface,
printscanmgr::kCupsRemovePrinter);
dbus::MessageWriter writer(&method_call);
if (!writer.AppendProtoAsArrayOfBytes(request)) {
LOG(ERROR) << "Failed to encode CupsRemovePrinterRequest protobuf.";
std::move(error_callback).Run();
return;
}
printscanmgr_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&PrintscanmgrClientImpl::OnPrinterRemoved,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)));
}
void CupsRetrievePrinterPpd(
const printscanmgr::CupsRetrievePpdRequest& request,
chromeos::DBusMethodCallback<printscanmgr::CupsRetrievePpdResponse>
callback,
base::OnceClosure error_callback) override {
dbus::MethodCall method_call(printscanmgr::kPrintscanmgrInterface,
printscanmgr::kCupsRetrievePpd);
dbus::MessageWriter writer(&method_call);
if (!writer.AppendProtoAsArrayOfBytes(request)) {
LOG(ERROR) << "Failed to encode CupsRetrievePpdRequest protobuf.";
std::move(error_callback).Run();
return;
}
printscanmgr_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&PrintscanmgrClientImpl::OnRetrievedPrinterPpd,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)));
}
private:
template <typename T>
void OnPrinterAdded(chromeos::DBusMethodCallback<T> callback,
dbus::Response* response,
dbus::ErrorResponse* err_response) {
if (!response) {
SendErrorResponse(std::move(callback), err_response);
return;
}
T result;
if (!dbus::MessageReader(response).PopArrayOfBytesAsProto(&result)) {
SendErrorResponse(std::move(callback), err_response);
return;
}
DCHECK_GE(result.result(), 0);
std::move(callback).Run(result);
}
void OnPrinterRemoved(chromeos::DBusMethodCallback<
printscanmgr::CupsRemovePrinterResponse> callback,
base::OnceClosure error_callback,
dbus::Response* response) {
if (!response) {
std::move(error_callback).Run();
return;
}
printscanmgr::CupsRemovePrinterResponse result;
if (!dbus::MessageReader(response).PopArrayOfBytesAsProto(&result)) {
std::move(error_callback).Run();
return;
}
std::move(callback).Run(result);
}
void OnRetrievedPrinterPpd(
chromeos::DBusMethodCallback<printscanmgr::CupsRetrievePpdResponse>
callback,
base::OnceClosure error_callback,
dbus::Response* response) {
printscanmgr::CupsRetrievePpdResponse result;
if (!(response &&
dbus::MessageReader(response).PopArrayOfBytesAsProto(&result))) {
LOG(ERROR) << "Failed to retrieve printer PPD";
std::move(error_callback).Run();
return;
}
std::move(callback).Run(result);
}
template <typename T>
void SendErrorResponse(chromeos::DBusMethodCallback<T> callback,
dbus::ErrorResponse* err_response) {
printscanmgr::AddPrinterResult dbus_error =
printscanmgr::AddPrinterResult::ADD_PRINTER_RESULT_DBUS_GENERIC;
if (err_response) {
dbus::MessageReader err_reader(err_response);
std::string err_str = err_response->GetErrorName();
dbus_error = DBusErrorFromString(err_str);
}
T response;
response.set_result(dbus_error);
std::move(callback).Run(response);
}
raw_ptr<dbus::ObjectProxy, LeakedDanglingUntriaged> printscanmgr_proxy_ =
nullptr;
base::WeakPtrFactory<PrintscanmgrClientImpl> weak_ptr_factory_{this};
};
} // namespace
// static
PrintscanmgrClient* PrintscanmgrClient::Get() {
return g_instance;
}
// static
void PrintscanmgrClient::Initialize(dbus::Bus* bus) {
CHECK(bus);
CHECK(!g_instance);
g_instance = new PrintscanmgrClientImpl();
g_instance->Init(bus);
}
// static
void PrintscanmgrClient::InitializeFake() {
CHECK(!g_instance);
g_instance = new FakePrintscanmgrClient();
g_instance->Init(nullptr);
}
// static
void PrintscanmgrClient::InitializeFakeForTest() {
g_instance = new FakePrintscanmgrClient();
g_instance->Init(nullptr);
}
// static
void PrintscanmgrClient::Shutdown() {
CHECK(g_instance);
delete g_instance;
g_instance = nullptr;
}
PrintscanmgrClient::PrintscanmgrClient() = default;
PrintscanmgrClient::~PrintscanmgrClient() = default;
} // namespace ash
|