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

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

Issue 10825170: chromeos: Add dbus MTPDClient. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « chromeos/dbus/mtpd_client.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/mtpd_client.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/stl_util.h"
11 #include "base/stringprintf.h"
12 #include "dbus/bus.h"
13 #include "dbus/message.h"
14 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
17
18 namespace chromeos {
19
20 namespace {
21
22 // Pops a string value when |reader| is not NULL.
23 // Returns true when a value is popped, false otherwise.
24 bool MaybePopString(dbus::MessageReader* reader, std::string* value) {
25 if (!reader)
26 return false;
27 return reader->PopString(value);
28 }
29
30 // Pops a uint16 value when |reader| is not NULL.
31 // Returns true when a value is popped, false otherwise.
32 bool MaybePopUint16(dbus::MessageReader* reader, uint16* value) {
33 if (!reader)
34 return false;
35 return reader->PopUint16(value);
36 }
37
38 // Pops a uint32 value when |reader| is not NULL.
39 // Returns true when a value is popped, false otherwise.
40 bool MaybePopUint32(dbus::MessageReader* reader, uint32* value) {
41 if (!reader)
42 return false;
43 return reader->PopUint32(value);
44 }
45
46 // Pops a uint64 value when |reader| is not NULL.
47 // Returns true when a value is popped, false otherwise.
48 bool MaybePopUint64(dbus::MessageReader* reader, uint64* value) {
49 if (!reader)
50 return false;
51 return reader->PopUint64(value);
52 }
53
54 // Pops a int64 value when |reader| is not NULL.
55 // Returns true when a value is popped, false otherwise.
56 bool MaybePopInt64(dbus::MessageReader* reader, int64* value) {
57 if (!reader)
58 return false;
59 return reader->PopInt64(value);
60 }
61
62 // The MTPDClient implementation.
63 class MTPDClientImpl : public MTPDClient {
64 public:
65 // TODO(thestig) Use mtpd::kMTPDServiceName and mtpd::kMTPDServicePath below.
66 explicit MTPDClientImpl(dbus::Bus* bus)
67 : proxy_(bus->GetObjectProxy(
68 "org.chromium.MTPD",
69 dbus::ObjectPath("/org/chromium/MTPD"))),
70 weak_ptr_factory_(this) {
71 }
72
73 // MTPDClient override.
74 virtual void EnumerateStorage(const EnumerateStorageCallback& callback,
75 const ErrorCallback& error_callback) OVERRIDE {
76 // TODO(thestig) Use constants here.
77 dbus::MethodCall method_call("org.chromium.MTPD", "EnumerateStorage");
78 proxy_->CallMethod(
79 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
80 base::Bind(&MTPDClientImpl::OnEnumerateStorage,
81 weak_ptr_factory_.GetWeakPtr(),
82 callback,
83 error_callback));
84 }
85
86 // MTPDClient override.
87 virtual void GetStorageInfo(const std::string& storage_name,
88 const GetStorageInfoCallback& callback,
89 const ErrorCallback& error_callback) OVERRIDE {
90 // TODO(thestig) Use constants here.
91 dbus::MethodCall method_call("org.chromium.MTPD", "GetStorageInfo");
92 dbus::MessageWriter writer(&method_call);
93 writer.AppendString(storage_name);
94 proxy_->CallMethod(&method_call,
95 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
96 base::Bind(&MTPDClientImpl::OnGetStorageInfo,
97 weak_ptr_factory_.GetWeakPtr(),
98 storage_name,
99 callback,
100 error_callback));
101 }
102
103 // MTPDClient override.
104 virtual void OpenStorage(const std::string& storage_name,
105 OpenStorageMode mode,
106 const OpenStorageCallback& callback,
107 const ErrorCallback& error_callback) OVERRIDE {
108 // TODO(thestig) Use constants here.
109 dbus::MethodCall method_call("org.chromium.MTPD", "OpenStorage");
110 dbus::MessageWriter writer(&method_call);
111 writer.AppendString(storage_name);
112 DCHECK_EQ(OPEN_STORAGE_MODE_READ_ONLY, mode);
113 // TODO(thestig) Use constants here.
114 writer.AppendString("readonly");
115 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
116 base::Bind(&MTPDClientImpl::OnOpenStorage,
117 weak_ptr_factory_.GetWeakPtr(),
118 callback,
119 error_callback));
120 }
121
122 // MTPDClient override.
123 virtual void CloseStorage(const std::string& handle,
124 const CloseStorageCallback& callback,
125 const ErrorCallback& error_callback) OVERRIDE {
126 // TODO(thestig) Use constants here.
127 dbus::MethodCall method_call("org.chromium.MTPD", "CloseStorage");
128 dbus::MessageWriter writer(&method_call);
129 writer.AppendString(handle);
130 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
131 base::Bind(&MTPDClientImpl::OnCloseStorage,
132 weak_ptr_factory_.GetWeakPtr(),
133 callback,
134 error_callback));
135 }
136
137 virtual void ReadDirectoryByPath(
138 const std::string& handle,
139 const std::string& path,
140 const ReadDirectoryCallback& callback,
141 const ErrorCallback& error_callback) OVERRIDE {
142 // TODO(thestig) Use constants here.
143 dbus::MethodCall method_call("org.chromium.MTPD", "ReadDirectoryByPath");
144 dbus::MessageWriter writer(&method_call);
145 writer.AppendString(handle);
146 writer.AppendString(path);
147 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
148 base::Bind(&MTPDClientImpl::OnReadDirectory,
149 weak_ptr_factory_.GetWeakPtr(),
150 callback,
151 error_callback));
152 }
153
154 virtual void ReadDirectoryById(
155 const std::string& handle,
156 uint32 file_id,
157 const ReadDirectoryCallback& callback,
158 const ErrorCallback& error_callback) OVERRIDE {
159 // TODO(thestig) Use constants here.
160 dbus::MethodCall method_call("org.chromium.MTPD", "ReadDirectoryById");
161 dbus::MessageWriter writer(&method_call);
162 writer.AppendString(handle);
163 writer.AppendUint32(file_id);
164 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
165 base::Bind(&MTPDClientImpl::OnReadDirectory,
166 weak_ptr_factory_.GetWeakPtr(),
167 callback,
168 error_callback));
169 }
170
171 // MTPDClient override.
172 virtual void ReadFileByPath(const std::string& handle,
173 const std::string& path,
174 const ReadFileCallback& callback,
175 const ErrorCallback& error_callback) OVERRIDE {
176 // TODO(thestig) Use constants here.
177 dbus::MethodCall method_call("org.chromium.MTPD", "ReadFileByPath");
178 dbus::MessageWriter writer(&method_call);
179 writer.AppendString(handle);
180 writer.AppendString(path);
181 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
182 base::Bind(&MTPDClientImpl::OnReadFile,
183 weak_ptr_factory_.GetWeakPtr(),
184 callback,
185 error_callback));
186 }
187
188 // MTPDClient override.
189 virtual void ReadFileById(const std::string& handle,
190 uint32 file_id,
191 const ReadFileCallback& callback,
192 const ErrorCallback& error_callback) OVERRIDE {
193 // TODO(thestig) Use constants here.
194 dbus::MethodCall method_call("org.chromium.MTPD", "ReadFileById");
195 dbus::MessageWriter writer(&method_call);
196 writer.AppendString(handle);
197 writer.AppendUint32(file_id);
198 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
199 base::Bind(&MTPDClientImpl::OnReadFile,
200 weak_ptr_factory_.GetWeakPtr(),
201 callback,
202 error_callback));
203 }
204
205 // MTPDClient override.
206 virtual void SetUpConnections(
207 const MTPStorageEventHandler& handler) OVERRIDE {
208 static const SignalEventTuple kSignalEventTuples[] = {
209 // TODO(thestig) Use constants.
210 { "MTPStorageAttached", true },
211 { "MTPStorageDetached", false },
212 };
213 const size_t kNumSignalEventTuples = arraysize(kSignalEventTuples);
214
215 for (size_t i = 0; i < kNumSignalEventTuples; ++i) {
216 // TODO(thestig) Use mtpd::kMTPDInterface here.
217 proxy_->ConnectToSignal(
218 "org.chromium.MTPD",
219 kSignalEventTuples[i].signal_name,
220 base::Bind(&MTPDClientImpl::OnMTPStorageSignal,
221 weak_ptr_factory_.GetWeakPtr(),
222 handler,
223 kSignalEventTuples[i].is_attach),
224 base::Bind(&MTPDClientImpl::OnSignalConnected,
225 weak_ptr_factory_.GetWeakPtr()));
226 }
227 }
228
229 private:
230 // A struct to contain a pair of signal name and attachment event type.
231 // Used by SetUpConnections.
232 struct SignalEventTuple {
233 const char *signal_name;
234 bool is_attach;
235 };
236
237 // Handles the result of EnumerateStorage and calls |callback| or
238 // |error_callback|.
239 void OnEnumerateStorage(const EnumerateStorageCallback& callback,
240 const ErrorCallback& error_callback,
241 dbus::Response* response) {
242 if (!response) {
243 error_callback.Run();
244 return;
245 }
246 dbus::MessageReader reader(response);
247 std::vector<std::string> storage_names;
248 if (!reader.PopArrayOfStrings(&storage_names)) {
249 LOG(ERROR) << "Invalid response: " << response->ToString();
250 error_callback.Run();
251 return;
252 }
253 callback.Run(storage_names);
254 }
255
256 // Handles the result of GetStorageInfo and calls |callback| or
257 // |error_callback|.
258 void OnGetStorageInfo(const std::string& storage_name,
259 const GetStorageInfoCallback& callback,
260 const ErrorCallback& error_callback,
261 dbus::Response* response) {
262 if (!response) {
263 error_callback.Run();
264 return;
265 }
266 StorageInfo storage_info(storage_name, response);
267 callback.Run(storage_info);
268 }
269
270 // Handles the result of OpenStorage and calls |callback| or |error_callback|.
271 void OnOpenStorage(const OpenStorageCallback& callback,
272 const ErrorCallback& error_callback,
273 dbus::Response* response) {
274 if (!response) {
275 error_callback.Run();
276 return;
277 }
278 dbus::MessageReader reader(response);
279 std::string handle;
280 if (!reader.PopString(&handle)) {
281 LOG(ERROR) << "Invalid response: " << response->ToString();
282 error_callback.Run();
283 return;
284 }
285 callback.Run(handle);
286 }
287
288 // Handles the result of CloseStorage and calls |callback| or
289 // |error_callback|.
290 void OnCloseStorage(const CloseStorageCallback& callback,
291 const ErrorCallback& error_callback,
292 dbus::Response* response) {
293 if (!response) {
294 error_callback.Run();
295 return;
296 }
297 callback.Run();
298 }
299
300 // Handles the result of ReadDirectoryByPath/Id and calls |callback| or
301 // |error_callback|.
302 void OnReadDirectory(const ReadDirectoryCallback& callback,
303 const ErrorCallback& error_callback,
304 dbus::Response* response) {
305 if (!response) {
306 error_callback.Run();
307 return;
308 }
309
310 std::vector<FileEntry> file_entries;
Lei Zhang 2012/08/03 03:20:44 note: I'm not sure if I did this right. Please loo
311 dbus::MessageReader response_reader(response);
312 dbus::MessageReader array_reader(response);
313 if (!response_reader.PopArray(&array_reader)) {
314 LOG(ERROR) << "Invalid response: " << response->ToString();
hashimoto 2012/08/03 04:48:42 Run error_callback
Lei Zhang 2012/08/03 04:53:50 Done.
315 return;
316 }
317 while (array_reader.HasMoreData()) {
318 FileEntry entry(response);
319 file_entries.push_back(entry);
320 }
321 callback.Run(file_entries);
322 }
323
324 // Handles the result of ReadFileByPath/Id and calls |callback| or
325 // |error_callback|.
326 void OnReadFile(const ReadFileCallback& callback,
327 const ErrorCallback& error_callback,
328 dbus::Response* response) {
329 if (!response) {
330 error_callback.Run();
331 return;
332 }
333
334 uint8* data_bytes = NULL;
335 size_t data_length = 0;
336 dbus::MessageReader reader(response);
337 if (!reader.PopArrayOfBytes(&data_bytes, &data_length)) {
338 error_callback.Run();
339 return;
340 }
341 std::string data(reinterpret_cast<const char*>(data_bytes), data_length);
342 callback.Run(data);
343 }
344
345 // Handles MTPStorageAttached/Dettached signals and calls |handler|.
346 void OnMTPStorageSignal(MTPStorageEventHandler handler,
347 bool is_attach,
348 dbus::Signal* signal) {
349 dbus::MessageReader reader(signal);
350 std::string storage_name;
351 if (!reader.PopString(&storage_name)) {
352 LOG(ERROR) << "Invalid signal: " << signal->ToString();
353 return;
354 }
355 DCHECK(!storage_name.empty());
356 handler.Run(is_attach, storage_name);
357 }
358
359
360 // Handles the result of signal connection setup.
361 void OnSignalConnected(const std::string& interface,
362 const std::string& signal,
363 bool successed) {
364 LOG_IF(ERROR, !successed) << "Connect to " << interface << " "
365 << signal << " failed.";
366 }
367
368 dbus::ObjectProxy* proxy_;
369 base::WeakPtrFactory<MTPDClientImpl> weak_ptr_factory_;
370
371 DISALLOW_COPY_AND_ASSIGN(MTPDClientImpl);
372 };
373
374 // A stub implementaion of MTPDClient.
375 class MTPDClientStubImpl : public MTPDClient {
376 public:
377 MTPDClientStubImpl() {}
378 virtual ~MTPDClientStubImpl() {}
379
380 virtual void EnumerateStorage(
381 const EnumerateStorageCallback& callback,
382 const ErrorCallback& error_callback) OVERRIDE {}
383 virtual void GetStorageInfo(
384 const std::string& storage_name,
385 const GetStorageInfoCallback& callback,
386 const ErrorCallback& error_callback) OVERRIDE {}
387 virtual void OpenStorage(const std::string& storage_name,
388 OpenStorageMode mode,
389 const OpenStorageCallback& callback,
390 const ErrorCallback& error_callback) OVERRIDE {}
391 virtual void CloseStorage(const std::string& handle,
392 const CloseStorageCallback& callback,
393 const ErrorCallback& error_callback) OVERRIDE {}
394 virtual void ReadDirectoryByPath(
395 const std::string& handle,
396 const std::string& path,
397 const ReadDirectoryCallback& callback,
398 const ErrorCallback& error_callback) OVERRIDE {}
399 virtual void ReadDirectoryById(
400 const std::string& handle,
401 uint32 file_id,
402 const ReadDirectoryCallback& callback,
403 const ErrorCallback& error_callback) OVERRIDE {}
404 virtual void ReadFileByPath(const std::string& handle,
405 const std::string& path,
406 const ReadFileCallback& callback,
407 const ErrorCallback& error_callback) OVERRIDE {}
408 virtual void ReadFileById(const std::string& handle,
409 uint32 file_id,
410 const ReadFileCallback& callback,
411 const ErrorCallback& error_callback) OVERRIDE {}
412 virtual void SetUpConnections(
413 const MTPStorageEventHandler& handler) OVERRIDE {}
414
415 private:
416 DISALLOW_COPY_AND_ASSIGN(MTPDClientStubImpl);
417 };
418
419 } // namespace
420
421 ////////////////////////////////////////////////////////////////////////////////
422 // StorageInfo
423
424 StorageInfo::StorageInfo(const std::string& storage_name,
425 dbus::Response* response)
426 : vendor_id_(0),
427 product_id_(0),
428 device_flags_(0),
429 storage_name_(storage_name),
430 storage_type_(0),
431 filesystem_type_(0),
432 access_capability_(0),
433 max_capacity_(0),
434 free_space_in_bytes_(0),
435 free_space_in_objects_(0) {
436 InitializeFromResponse(response);
437 }
438
439 StorageInfo::~StorageInfo() {
440 }
441
442 // Initialize |this| from |response| given by the mtpd service.
443 void StorageInfo::InitializeFromResponse(dbus::Response* response) {
444 dbus::MessageReader response_reader(response);
445 dbus::MessageReader array_reader(response);
446 if (!response_reader.PopArray(&array_reader)) {
447 LOG(ERROR) << "Invalid response: " << response->ToString();
448 return;
449 }
450 // TODO(satorux): Rework this code using Protocol Buffers. crosbug.com/22626
451 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap;
452 PropertiesMap properties;
453 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties);
454 while (array_reader.HasMoreData()) {
455 dbus::MessageReader* value_reader = new dbus::MessageReader(response);
456 dbus::MessageReader dict_entry_reader(response);
457 std::string key;
458 if (!array_reader.PopDictEntry(&dict_entry_reader) ||
459 !dict_entry_reader.PopString(&key) ||
460 !dict_entry_reader.PopVariant(value_reader)) {
461 LOG(ERROR) << "Invalid response: " << response->ToString();
462 return;
463 }
464 properties[key] = value_reader;
465 }
466 // TODO(thestig) Add enums for fields below as appropriate.
467 MaybePopString(properties["Vendor"], &vendor_);
468 MaybePopString(properties["Product"], &product_);
469 MaybePopString(properties["StorageDescription"], &storage_description_);
470 MaybePopString(properties["VolumeIdentifier"], &volume_identifier_);
471 MaybePopUint16(properties["VendorId"], &vendor_id_);
472 MaybePopUint16(properties["ProductId"], &product_id_);
473 MaybePopUint16(properties["StorageType"], &storage_type_);
474 MaybePopUint16(properties["FilesystemType"], &filesystem_type_);
475 MaybePopUint16(properties["AccessCapability"], &access_capability_);
476 MaybePopUint32(properties["DeviceFlags"], &device_flags_);
477 MaybePopUint64(properties["MaxCapacity"], &max_capacity_);
478 MaybePopUint64(properties["FreeSpaceInBytes"], &free_space_in_bytes_);
479 MaybePopUint64(properties["FreeSpaceInObjects"], &free_space_in_objects_);
480 }
481
482 ////////////////////////////////////////////////////////////////////////////////
483 // FileEntry
484
485 FileEntry::FileEntry(dbus::Response* response)
486 : item_id_(0),
487 parent_id_(0),
488 filesize_(0),
489 file_type_(FILE_TYPE_UNKNOWN) {
490 InitializeFromResponse(response);
491 }
492
493 FileEntry::~FileEntry() {
494 }
495
496 // Initialize |this| from |response| given by the mtpd service.
497 void FileEntry::InitializeFromResponse(dbus::Response* response) {
498 dbus::MessageReader response_reader(response);
499 dbus::MessageReader array_reader(response);
500 if (!response_reader.PopArray(&array_reader)) {
501 LOG(ERROR) << "Invalid response: " << response->ToString();
502 return;
503 }
504 // TODO(satorux): Rework this code using Protocol Buffers. crosbug.com/22626
505 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap;
506 PropertiesMap properties;
507 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties);
508 while (array_reader.HasMoreData()) {
509 dbus::MessageReader* value_reader = new dbus::MessageReader(response);
510 dbus::MessageReader dict_entry_reader(response);
511 std::string key;
512 if (!array_reader.PopDictEntry(&dict_entry_reader) ||
513 !dict_entry_reader.PopString(&key) ||
514 !dict_entry_reader.PopVariant(value_reader)) {
515 LOG(ERROR) << "Invalid response: " << response->ToString();
516 return;
517 }
518 properties[key] = value_reader;
519 }
520
521 MaybePopString(properties["FileName"], &filename_);
522 MaybePopUint32(properties["ItemId"], &item_id_);
523 MaybePopUint32(properties["ParentId"], &parent_id_);
524 MaybePopUint64(properties["FileSize"], &filesize_);
525
526 int64 modification_date = -1;
527 if (MaybePopInt64(properties["ModificationDate"], &modification_date))
528 modification_date_ = base::Time::FromTimeT(modification_date);
529
530 uint16 file_type = FILE_TYPE_OTHER;
531 if (MaybePopUint16(properties["FileType"], &file_type)) {
532 switch (file_type) {
533 case FILE_TYPE_FOLDER:
534 file_type_ = FILE_TYPE_FOLDER;
535 case FILE_TYPE_JPEG:
536 file_type_ = FILE_TYPE_JPEG;
537 case FILE_TYPE_JFIF:
538 file_type_ = FILE_TYPE_JFIF;
539 case FILE_TYPE_TIFF:
540 file_type_ = FILE_TYPE_TIFF;
541 case FILE_TYPE_BMP:
542 file_type_ = FILE_TYPE_BMP;
543 case FILE_TYPE_GIF:
544 file_type_ = FILE_TYPE_GIF;
545 case FILE_TYPE_PICT:
546 file_type_ = FILE_TYPE_PICT;
547 case FILE_TYPE_PNG:
548 file_type_ = FILE_TYPE_PNG;
549 case FILE_TYPE_WINDOWSIMAGEFORMAT:
550 file_type_ = FILE_TYPE_WINDOWSIMAGEFORMAT;
551 case FILE_TYPE_JP2:
552 file_type_ = FILE_TYPE_JP2;
553 case FILE_TYPE_JPX:
554 file_type_ = FILE_TYPE_JPX;
555 case FILE_TYPE_UNKNOWN:
556 file_type_ = FILE_TYPE_UNKNOWN;
557 default:
558 file_type_ = FILE_TYPE_OTHER;
559 break;
560 }
561 }
562 }
563
564 ////////////////////////////////////////////////////////////////////////////////
565 // MTPDClient
566
567 MTPDClient::MTPDClient() {}
568
569 MTPDClient::~MTPDClient() {}
570
571 // static
572 MTPDClient* MTPDClient::Create(DBusClientImplementationType type,
573 dbus::Bus* bus) {
574 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
575 return new MTPDClientImpl(bus);
576 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
577 return new MTPDClientStubImpl();
578 }
579
580 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/mtpd_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698