Chromium Code Reviews| Index: chromeos/dbus/cups_client.cc |
| diff --git a/chromeos/dbus/cups_client.cc b/chromeos/dbus/cups_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d9bbb398355af61c2859d1452bc870483dd54866 |
| --- /dev/null |
| +++ b/chromeos/dbus/cups_client.cc |
| @@ -0,0 +1,155 @@ |
| +// 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 "chromeos/dbus/cups_client.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/callback_forward.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "dbus/bus.h" |
| +#include "dbus/message.h" |
| +#include "dbus/object_proxy.h" |
| +#include "third_party/cros_system_api/dbus/debugd/dbus-constants.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const int kTimeoutMs = 1000; |
|
hashimoto
2016/09/07 05:53:17
Is there any reason to avoid using ObjectProxy::TI
skau
2016/09/09 15:25:32
I wasn't sure that was the right thing. Fixed now
|
| + |
| +class CupsClientImpl : public CupsClient { |
| + public: |
| + CupsClientImpl() : proxy_(nullptr), weak_ptr_factory_(this) {} |
| + |
| + void AddPrinter(const std::string& name, |
| + const std::string& uri, |
| + const std::string& ppd_path, |
| + bool ipp_everywhere, |
| + const AddPrinterCallback& callback, |
| + const base::Closure& error_callback) override; |
| + |
| + void RemovePrinter(const std::string& name, |
| + const RemovePrinterCallback& callback, |
| + const base::Closure& error_callback) override; |
| + |
| + protected: |
| + void Init(dbus::Bus* bus) override; |
| + |
| + private: |
| + void OnPrinterAdded(const AddPrinterCallback& callback, |
| + const base::Closure& error_callback, |
| + dbus::Response* response); |
| + |
| + void OnPrinterRemoved(const RemovePrinterCallback& callback, |
| + const base::Closure& error_callback, |
| + dbus::Response* response); |
| + |
| + void OnError(const base::Closure& error_callback, |
| + dbus::ErrorResponse* response); |
| + |
| + dbus::ObjectProxy* proxy_; |
| + |
| + base::WeakPtrFactory<CupsClientImpl> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CupsClientImpl); |
| +}; |
| + |
| +void CupsClientImpl::Init(dbus::Bus* bus) { |
| + proxy_ = bus->GetObjectProxy(debugd::kDebugdServiceName, |
| + dbus::ObjectPath(debugd::kDebugdServicePath)); |
| +} |
| + |
| +void CupsClientImpl::AddPrinter(const std::string& name, |
| + const std::string& uri, |
| + const std::string& ppd_path, |
| + bool ipp_everywhere, |
| + const AddPrinterCallback& callback, |
| + const base::Closure& error_callback) { |
| + dbus::MethodCall method_call(debugd::kDebugdInterface, |
| + debugd::kCupsAddPrinter); |
| + dbus::MessageWriter writer(&method_call); |
| + writer.AppendString(name); |
| + writer.AppendString(uri); |
| + writer.AppendString(ppd_path); |
| + writer.AppendBool(ipp_everywhere); |
| + |
| + DCHECK(proxy_); |
| + |
| + proxy_->CallMethodWithErrorCallback( |
| + &method_call, kTimeoutMs, |
| + base::Bind(&CupsClientImpl::OnPrinterAdded, |
| + weak_ptr_factory_.GetWeakPtr(), callback, error_callback), |
| + base::Bind(&CupsClientImpl::OnError, weak_ptr_factory_.GetWeakPtr(), |
| + error_callback)); |
| +} |
| + |
| +void CupsClientImpl::RemovePrinter(const std::string& name, |
| + const RemovePrinterCallback& callback, |
| + const base::Closure& error_callback) { |
| + dbus::MethodCall method_call(debugd::kDebugdInterface, |
| + debugd::kCupsRemovePrinter); |
| + dbus::MessageWriter writer(&method_call); |
| + writer.AppendString(name); |
| + |
| + DCHECK(proxy_); |
| + |
| + proxy_->CallMethodWithErrorCallback( |
| + &method_call, kTimeoutMs, |
| + base::Bind(&CupsClientImpl::OnPrinterRemoved, |
| + weak_ptr_factory_.GetWeakPtr(), callback, error_callback), |
| + base::Bind(&CupsClientImpl::OnError, weak_ptr_factory_.GetWeakPtr(), |
| + error_callback)); |
| +} |
| + |
| +void CupsClientImpl::OnPrinterAdded(const AddPrinterCallback& callback, |
| + const base::Closure& error_callback, |
| + dbus::Response* response) { |
| + bool result; |
| + dbus::MessageReader reader(response); |
| + if (!reader.PopBool(&result)) { |
| + LOG(WARNING) << "Missing bool in response"; |
| + error_callback.Run(); |
| + return; |
| + } |
| + |
| + callback.Run(result); |
| +} |
| + |
| +void CupsClientImpl::OnPrinterRemoved(const RemovePrinterCallback& callback, |
| + const base::Closure& error_callback, |
| + dbus::Response* response) { |
| + bool result; |
| + dbus::MessageReader reader(response); |
| + if (!reader.PopBool(&result)) { |
| + LOG(WARNING) << "Missing bool in response"; |
| + error_callback.Run(); |
| + return; |
| + } |
| + |
| + callback.Run(result); |
| +} |
| + |
| +void CupsClientImpl::OnError(const base::Closure& error_callback, |
| + dbus::ErrorResponse* response) { |
| + LOG(WARNING) << "Error with CUPS client" << response->GetErrorName(); |
| + error_callback.Run(); |
| +} |
| + |
| +} // namespace |
| + |
| +///////////////////////////////////////////////////////////////////// |
| +// CupsClient |
| + |
| +CupsClient::CupsClient() {} |
| + |
| +CupsClient::~CupsClient() {} |
| + |
| +// static |
| +CupsClient* CupsClient::Create() { |
| + return new CupsClientImpl(); |
| +} |
| + |
| +} // namespace chromeos |