OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromeos/dbus/cups_client.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback_forward.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "dbus/bus.h" | |
13 #include "dbus/message.h" | |
14 #include "dbus/object_proxy.h" | |
15 #include "third_party/cros_system_api/dbus/debugd/dbus-constants.h" | |
16 | |
17 namespace chromeos { | |
18 | |
19 namespace { | |
20 | |
21 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
| |
22 | |
23 class CupsClientImpl : public CupsClient { | |
24 public: | |
25 CupsClientImpl() : proxy_(nullptr), weak_ptr_factory_(this) {} | |
26 | |
27 void AddPrinter(const std::string& name, | |
28 const std::string& uri, | |
29 const std::string& ppd_path, | |
30 bool ipp_everywhere, | |
31 const AddPrinterCallback& callback, | |
32 const base::Closure& error_callback) override; | |
33 | |
34 void RemovePrinter(const std::string& name, | |
35 const RemovePrinterCallback& callback, | |
36 const base::Closure& error_callback) override; | |
37 | |
38 protected: | |
39 void Init(dbus::Bus* bus) override; | |
40 | |
41 private: | |
42 void OnPrinterAdded(const AddPrinterCallback& callback, | |
43 const base::Closure& error_callback, | |
44 dbus::Response* response); | |
45 | |
46 void OnPrinterRemoved(const RemovePrinterCallback& callback, | |
47 const base::Closure& error_callback, | |
48 dbus::Response* response); | |
49 | |
50 void OnError(const base::Closure& error_callback, | |
51 dbus::ErrorResponse* response); | |
52 | |
53 dbus::ObjectProxy* proxy_; | |
54 | |
55 base::WeakPtrFactory<CupsClientImpl> weak_ptr_factory_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(CupsClientImpl); | |
58 }; | |
59 | |
60 void CupsClientImpl::Init(dbus::Bus* bus) { | |
61 proxy_ = bus->GetObjectProxy(debugd::kDebugdServiceName, | |
62 dbus::ObjectPath(debugd::kDebugdServicePath)); | |
63 } | |
64 | |
65 void CupsClientImpl::AddPrinter(const std::string& name, | |
66 const std::string& uri, | |
67 const std::string& ppd_path, | |
68 bool ipp_everywhere, | |
69 const AddPrinterCallback& callback, | |
70 const base::Closure& error_callback) { | |
71 dbus::MethodCall method_call(debugd::kDebugdInterface, | |
72 debugd::kCupsAddPrinter); | |
73 dbus::MessageWriter writer(&method_call); | |
74 writer.AppendString(name); | |
75 writer.AppendString(uri); | |
76 writer.AppendString(ppd_path); | |
77 writer.AppendBool(ipp_everywhere); | |
78 | |
79 DCHECK(proxy_); | |
80 | |
81 proxy_->CallMethodWithErrorCallback( | |
82 &method_call, kTimeoutMs, | |
83 base::Bind(&CupsClientImpl::OnPrinterAdded, | |
84 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
85 base::Bind(&CupsClientImpl::OnError, weak_ptr_factory_.GetWeakPtr(), | |
86 error_callback)); | |
87 } | |
88 | |
89 void CupsClientImpl::RemovePrinter(const std::string& name, | |
90 const RemovePrinterCallback& callback, | |
91 const base::Closure& error_callback) { | |
92 dbus::MethodCall method_call(debugd::kDebugdInterface, | |
93 debugd::kCupsRemovePrinter); | |
94 dbus::MessageWriter writer(&method_call); | |
95 writer.AppendString(name); | |
96 | |
97 DCHECK(proxy_); | |
98 | |
99 proxy_->CallMethodWithErrorCallback( | |
100 &method_call, kTimeoutMs, | |
101 base::Bind(&CupsClientImpl::OnPrinterRemoved, | |
102 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
103 base::Bind(&CupsClientImpl::OnError, weak_ptr_factory_.GetWeakPtr(), | |
104 error_callback)); | |
105 } | |
106 | |
107 void CupsClientImpl::OnPrinterAdded(const AddPrinterCallback& callback, | |
108 const base::Closure& error_callback, | |
109 dbus::Response* response) { | |
110 bool result; | |
111 dbus::MessageReader reader(response); | |
112 if (!reader.PopBool(&result)) { | |
113 LOG(WARNING) << "Missing bool in response"; | |
114 error_callback.Run(); | |
115 return; | |
116 } | |
117 | |
118 callback.Run(result); | |
119 } | |
120 | |
121 void CupsClientImpl::OnPrinterRemoved(const RemovePrinterCallback& callback, | |
122 const base::Closure& error_callback, | |
123 dbus::Response* response) { | |
124 bool result; | |
125 dbus::MessageReader reader(response); | |
126 if (!reader.PopBool(&result)) { | |
127 LOG(WARNING) << "Missing bool in response"; | |
128 error_callback.Run(); | |
129 return; | |
130 } | |
131 | |
132 callback.Run(result); | |
133 } | |
134 | |
135 void CupsClientImpl::OnError(const base::Closure& error_callback, | |
136 dbus::ErrorResponse* response) { | |
137 LOG(WARNING) << "Error with CUPS client" << response->GetErrorName(); | |
138 error_callback.Run(); | |
139 } | |
140 | |
141 } // namespace | |
142 | |
143 ///////////////////////////////////////////////////////////////////// | |
144 // CupsClient | |
145 | |
146 CupsClient::CupsClient() {} | |
147 | |
148 CupsClient::~CupsClient() {} | |
149 | |
150 // static | |
151 CupsClient* CupsClient::Create() { | |
152 return new CupsClientImpl(); | |
153 } | |
154 | |
155 } // namespace chromeos | |
OLD | NEW |