Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: chromeos/dbus/lorgnette_manager_client.cc

Issue 2299143002: chromeos: Remove dbus::FileDescriptor from LorgnetteManagerClient (Closed)
Patch Set: Rename callback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/dbus/lorgnette_manager_client.h ('k') | chromeos/dbus/mock_lorgnette_manager_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/lorgnette_manager_client.h" 5 #include "chromeos/dbus/lorgnette_manager_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/files/scoped_file.h"
12 #include "base/location.h" 13 #include "base/location.h"
13 #include "base/macros.h" 14 #include "base/macros.h"
14 #include "base/task_runner_util.h" 15 #include "base/task_runner_util.h"
15 #include "base/threading/worker_pool.h" 16 #include "base/threading/worker_pool.h"
16 #include "chromeos/dbus/pipe_reader.h" 17 #include "chromeos/dbus/pipe_reader.h"
17 #include "dbus/bus.h" 18 #include "dbus/bus.h"
18 #include "dbus/message.h" 19 #include "dbus/message.h"
19 #include "dbus/object_path.h" 20 #include "dbus/object_path.h"
20 #include "dbus/object_proxy.h" 21 #include "dbus/object_proxy.h"
21 #include "net/base/file_stream.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h" 22 #include "third_party/cros_system_api/dbus/service_constants.h"
23 23
24 namespace chromeos { 24 namespace chromeos {
25 25
26 // The LorgnetteManagerClient implementation used in production. 26 // The LorgnetteManagerClient implementation used in production.
27 class LorgnetteManagerClientImpl : public LorgnetteManagerClient { 27 class LorgnetteManagerClientImpl : public LorgnetteManagerClient {
28 public: 28 public:
29 LorgnetteManagerClientImpl() : 29 LorgnetteManagerClientImpl() :
30 lorgnette_daemon_proxy_(NULL), weak_ptr_factory_(this) {} 30 lorgnette_daemon_proxy_(NULL), weak_ptr_factory_(this) {}
31 31
32 ~LorgnetteManagerClientImpl() override {} 32 ~LorgnetteManagerClientImpl() override {}
33 33
34 void ListScanners(const ListScannersCallback& callback) override { 34 void ListScanners(const ListScannersCallback& callback) override {
35 dbus::MethodCall method_call(lorgnette::kManagerServiceInterface, 35 dbus::MethodCall method_call(lorgnette::kManagerServiceInterface,
36 lorgnette::kListScannersMethod); 36 lorgnette::kListScannersMethod);
37 lorgnette_daemon_proxy_->CallMethod( 37 lorgnette_daemon_proxy_->CallMethod(
38 &method_call, 38 &method_call,
39 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 39 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
40 base::Bind(&LorgnetteManagerClientImpl::OnListScanners, 40 base::Bind(&LorgnetteManagerClientImpl::OnListScanners,
41 weak_ptr_factory_.GetWeakPtr(), 41 weak_ptr_factory_.GetWeakPtr(),
42 callback)); 42 callback));
43 } 43 }
44 44
45 // LorgnetteManagerClient override. 45 // LorgnetteManagerClient override.
46 void ScanImageToFile(
47 std::string device_name,
48 const ScanProperties& properties,
49 const ScanImageToFileCallback& callback,
50 base::File* file) override {
51 dbus::FileDescriptor* file_descriptor = new dbus::FileDescriptor();
52 file_descriptor->PutValue(file->TakePlatformFile());
53 // Punt descriptor validity check to a worker thread; on return we'll
54 // issue the D-Bus request to stop tracing and collect results.
55 base::WorkerPool::PostTaskAndReply(
56 FROM_HERE,
57 base::Bind(&LorgnetteManagerClientImpl::CheckValidity,
58 file_descriptor),
59 base::Bind(&LorgnetteManagerClientImpl::OnCheckValidityScanImage,
60 weak_ptr_factory_.GetWeakPtr(),
61 base::Owned(file_descriptor),
62 device_name,
63 properties,
64 callback),
65 false);
66 }
67
68 void ScanImageToString( 46 void ScanImageToString(
69 std::string device_name, 47 std::string device_name,
70 const ScanProperties& properties, 48 const ScanProperties& properties,
71 const ScanImageToStringCallback& callback) override { 49 const ScanImageToStringCallback& callback) override {
72 // Owned by the callback created in scan_to_string_completion->Start(). 50 // Owned by the callback created in scan_to_string_completion->Start().
73 ScanToStringCompletion* scan_to_string_completion = 51 ScanToStringCompletion* scan_to_string_completion =
74 new ScanToStringCompletion(); 52 new ScanToStringCompletion();
75 base::ScopedFD fd; 53 base::ScopedFD fd;
76 ScanImageToFileCallback file_callback = 54 ScanToStringCompletion::CompletionCallback file_callback =
77 scan_to_string_completion->Start(callback, &fd); 55 scan_to_string_completion->Start(callback, &fd);
78 base::File file(fd.release()); 56
79 ScanImageToFile(device_name, properties, file_callback, &file); 57 // Issue the dbus request to scan an image.
58 dbus::MethodCall method_call(lorgnette::kManagerServiceInterface,
59 lorgnette::kScanImageMethod);
60 dbus::MessageWriter writer(&method_call);
61 writer.AppendString(device_name);
62 writer.AppendFileDescriptor(fd.get());
63
64 dbus::MessageWriter option_writer(NULL);
65 dbus::MessageWriter element_writer(NULL);
66 writer.OpenArray("{sv}", &option_writer);
67 if (!properties.mode.empty()) {
68 option_writer.OpenDictEntry(&element_writer);
69 element_writer.AppendString(lorgnette::kScanPropertyMode);
70 element_writer.AppendVariantOfString(properties.mode);
71 option_writer.CloseContainer(&element_writer);
72 }
73 if (properties.resolution_dpi) {
74 option_writer.OpenDictEntry(&element_writer);
75 element_writer.AppendString(lorgnette::kScanPropertyResolution);
76 element_writer.AppendVariantOfUint32(properties.resolution_dpi);
77 option_writer.CloseContainer(&element_writer);
78 }
79 writer.CloseContainer(&option_writer);
80
81 lorgnette_daemon_proxy_->CallMethod(
82 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
83 base::Bind(&LorgnetteManagerClientImpl::OnScanImageComplete,
84 weak_ptr_factory_.GetWeakPtr(), file_callback));
80 } 85 }
81 86
82 protected: 87 protected:
83 void Init(dbus::Bus* bus) override { 88 void Init(dbus::Bus* bus) override {
84 lorgnette_daemon_proxy_ = 89 lorgnette_daemon_proxy_ =
85 bus->GetObjectProxy(lorgnette::kManagerServiceName, 90 bus->GetObjectProxy(lorgnette::kManagerServiceName,
86 dbus::ObjectPath(lorgnette::kManagerServicePath)); 91 dbus::ObjectPath(lorgnette::kManagerServicePath));
87 } 92 }
88 93
89 private: 94 private:
90 class ScanToStringCompletion { 95 class ScanToStringCompletion {
91 public: 96 public:
97 typedef base::Callback<void(bool succeeded)> CompletionCallback;
98
92 ScanToStringCompletion() {} 99 ScanToStringCompletion() {}
93 virtual ~ScanToStringCompletion() {} 100 virtual ~ScanToStringCompletion() {}
94 101
95 // Creates a file stream in |file| that will stream image data to 102 // Creates a file stream in |file| that will stream image data to
96 // a string that will be supplied to |callback|. Passes ownership 103 // a string that will be supplied to |callback|. Passes ownership
97 // of |this| to a returned callback that can be handed to a 104 // of |this| to a returned callback that can be handed to a
98 // ScanImageToFile invocation. 105 // ScanImageToFile invocation.
satorux1 2016/09/02 05:22:46 ScanImageToFile no longer exists? please update th
hashimoto 2016/09/02 05:25:51 Removed that part from the comment.
99 ScanImageToFileCallback Start(const ScanImageToStringCallback& callback, 106 CompletionCallback Start(const ScanImageToStringCallback& callback,
100 base::ScopedFD* fd) { 107 base::ScopedFD* fd) {
101 CHECK(!pipe_reader_.get()); 108 CHECK(!pipe_reader_.get());
102 const bool kTasksAreSlow = true; 109 const bool kTasksAreSlow = true;
103 scoped_refptr<base::TaskRunner> task_runner = 110 scoped_refptr<base::TaskRunner> task_runner =
104 base::WorkerPool::GetTaskRunner(kTasksAreSlow); 111 base::WorkerPool::GetTaskRunner(kTasksAreSlow);
105 pipe_reader_.reset( 112 pipe_reader_.reset(
106 new chromeos::PipeReaderForString( 113 new chromeos::PipeReaderForString(
107 task_runner, 114 task_runner,
108 base::Bind(&ScanToStringCompletion::OnScanToStringDataCompleted, 115 base::Bind(&ScanToStringCompletion::OnScanToStringDataCompleted,
109 base::Unretained(this)))); 116 base::Unretained(this))));
110 *fd = pipe_reader_->StartIO(); 117 *fd = pipe_reader_->StartIO();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 186 }
180 187
181 if (decode_failure) { 188 if (decode_failure) {
182 LOG(ERROR) << "Failed to decode response from ListScanners"; 189 LOG(ERROR) << "Failed to decode response from ListScanners";
183 callback.Run(false, scanners); 190 callback.Run(false, scanners);
184 } else { 191 } else {
185 callback.Run(true, scanners); 192 callback.Run(true, scanners);
186 } 193 }
187 } 194 }
188 195
189 // Called to check descriptor validity on a thread where i/o is permitted.
190 static void CheckValidity(dbus::FileDescriptor* file_descriptor) {
191 file_descriptor->CheckValidity();
192 }
193
194 // Called when a CheckValidity response is received.
195 void OnCheckValidityScanImage(
196 dbus::FileDescriptor* file_descriptor,
197 std::string device_name,
198 const ScanProperties& properties,
199 const ScanImageToFileCallback& callback) {
200 if (!file_descriptor->is_valid()) {
201 LOG(ERROR) << "Failed to scan image: file descriptor is invalid";
202 callback.Run(false);
203 return;
204 }
205 // Issue the dbus request to scan an image.
206 dbus::MethodCall method_call(
207 lorgnette::kManagerServiceInterface,
208 lorgnette::kScanImageMethod);
209 dbus::MessageWriter writer(&method_call);
210 writer.AppendString(device_name);
211 writer.AppendFileDescriptor(*file_descriptor);
212
213 dbus::MessageWriter option_writer(NULL);
214 dbus::MessageWriter element_writer(NULL);
215 writer.OpenArray("{sv}", &option_writer);
216 if (!properties.mode.empty()) {
217 option_writer.OpenDictEntry(&element_writer);
218 element_writer.AppendString(lorgnette::kScanPropertyMode);
219 element_writer.AppendVariantOfString(properties.mode);
220 option_writer.CloseContainer(&element_writer);
221 }
222 if (properties.resolution_dpi) {
223 option_writer.OpenDictEntry(&element_writer);
224 element_writer.AppendString(lorgnette::kScanPropertyResolution);
225 element_writer.AppendVariantOfUint32(properties.resolution_dpi);
226 option_writer.CloseContainer(&element_writer);
227 }
228 writer.CloseContainer(&option_writer);
229
230 lorgnette_daemon_proxy_->CallMethod(
231 &method_call,
232 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
233 base::Bind(&LorgnetteManagerClientImpl::OnScanImageComplete,
234 weak_ptr_factory_.GetWeakPtr(),
235 callback));
236 }
237
238 // Called when a response for ScanImage() is received. 196 // Called when a response for ScanImage() is received.
239 void OnScanImageComplete(const ScanImageToFileCallback& callback, 197 void OnScanImageComplete(
240 dbus::Response* response) { 198 const ScanToStringCompletion::CompletionCallback& callback,
199 dbus::Response* response) {
241 if (!response) { 200 if (!response) {
242 LOG(ERROR) << "Failed to scan image"; 201 LOG(ERROR) << "Failed to scan image";
243 callback.Run(false); 202 callback.Run(false);
244 return; 203 return;
245 } 204 }
246 callback.Run(true); 205 callback.Run(true);
247 } 206 }
248 207
249 dbus::ObjectProxy* lorgnette_daemon_proxy_; 208 dbus::ObjectProxy* lorgnette_daemon_proxy_;
250 base::WeakPtrFactory<LorgnetteManagerClientImpl> weak_ptr_factory_; 209 base::WeakPtrFactory<LorgnetteManagerClientImpl> weak_ptr_factory_;
251 210
252 DISALLOW_COPY_AND_ASSIGN(LorgnetteManagerClientImpl); 211 DISALLOW_COPY_AND_ASSIGN(LorgnetteManagerClientImpl);
253 }; 212 };
254 213
255 LorgnetteManagerClient::LorgnetteManagerClient() { 214 LorgnetteManagerClient::LorgnetteManagerClient() {
256 } 215 }
257 216
258 LorgnetteManagerClient::~LorgnetteManagerClient() { 217 LorgnetteManagerClient::~LorgnetteManagerClient() {
259 } 218 }
260 219
261 // static 220 // static
262 LorgnetteManagerClient* LorgnetteManagerClient::Create() { 221 LorgnetteManagerClient* LorgnetteManagerClient::Create() {
263 return new LorgnetteManagerClientImpl(); 222 return new LorgnetteManagerClientImpl();
264 } 223 }
265 224
266 } // namespace chromeos 225 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/lorgnette_manager_client.h ('k') | chromeos/dbus/mock_lorgnette_manager_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698