Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | #ifndef MTPD_SERVER_IMPL_H_ |
| 6 | #define MTPD_SERVER_IMPL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 11 | #include <base/compiler_specific.h> |
Alex Vakulenko | ef2d18d | 2016-01-23 03:20:14 | [diff] [blame] | 12 | #include <base/macros.h> |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 13 | #include <brillo/dbus/async_event_sequencer.h> |
| 14 | #include <brillo/dbus/dbus_object.h> |
| 15 | #include <brillo/errors/error.h> |
| 16 | #include <dbus/bus.h> |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 17 | |
Eric Caruso | 34eeec5 | 2017-08-21 22:31:19 | [diff] [blame] | 18 | #include "mtpd/dbus_adaptors/org.chromium.Mtpd.h" |
Eric Caruso | e7cd268 | 2017-08-21 22:57:30 | [diff] [blame] | 19 | #include "mtpd/device_event_delegate.h" |
| 20 | #include "mtpd/device_manager.h" |
| 21 | #include "mtpd/file_entry.h" |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 22 | |
| 23 | namespace mtpd { |
| 24 | |
| 25 | class DeviceManager; |
| 26 | |
| 27 | // The D-bus server for the mtpd daemon. |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 28 | class MtpdServer : public org::chromium::MtpdInterface, |
| 29 | public org::chromium::MtpdAdaptor, |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 30 | public DeviceEventDelegate { |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 31 | public: |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 32 | explicit MtpdServer(scoped_refptr<dbus::Bus> bus); |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 33 | virtual ~MtpdServer(); |
| 34 | |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 35 | // org::chromium::MtpdAdaptor implementation. |
| 36 | std::vector<std::string> EnumerateStorages() override; |
| 37 | std::vector<uint8_t> GetStorageInfo(const std::string& storage_name) override; |
| 38 | std::vector<uint8_t> GetStorageInfoFromDevice( |
| 39 | const std::string& storage_name) override; |
| 40 | bool OpenStorage(brillo::ErrorPtr* error, |
| 41 | const std::string& storage_name, |
| 42 | const std::string& mode, |
| 43 | std::string* id) override; |
| 44 | bool CloseStorage(brillo::ErrorPtr* error, |
| 45 | const std::string& handle) override; |
| 46 | bool ReadDirectoryEntryIds(brillo::ErrorPtr* error, |
| 47 | const std::string& handle, |
| 48 | uint32_t file_id, |
| 49 | std::vector<uint32_t>* directory_listing) override; |
| 50 | bool GetFileInfo(brillo::ErrorPtr* error, |
| 51 | const std::string& handle, |
| 52 | const std::vector<uint32_t>& file_ids, |
| 53 | std::vector<uint8_t>* serialized_file_entries) override; |
| 54 | bool ReadFileChunk(brillo::ErrorPtr* error, |
| 55 | const std::string& handle, |
| 56 | uint32_t file_id, |
| 57 | uint32_t offset, |
| 58 | uint32_t count, |
| 59 | std::vector<uint8_t>* file_contents) override; |
| 60 | bool CopyFileFromLocal(brillo::ErrorPtr* error, |
| 61 | const std::string& handle, |
| 62 | const dbus::FileDescriptor& file_descriptor, |
| 63 | uint32_t parent_id, |
| 64 | const std::string& file_name) override; |
| 65 | bool DeleteObject(brillo::ErrorPtr* error, |
| 66 | const std::string& handle, |
| 67 | uint32_t object_id) override; |
| 68 | bool RenameObject(brillo::ErrorPtr* error, |
| 69 | const std::string& handle, |
| 70 | uint32_t object_id, |
| 71 | const std::string& new_name) override; |
| 72 | bool CreateDirectory(brillo::ErrorPtr* error, |
| 73 | const std::string& handle, |
| 74 | uint32_t parent_id, |
| 75 | const std::string& directory_name) override; |
| 76 | bool IsAlive() override; |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 77 | |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 78 | // DeviceEventDelegate implementation. |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 79 | void StorageAttached(const std::string& storage_name) override; |
| 80 | void StorageDetached(const std::string& storage_name) override; |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 81 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 82 | // Returns a file descriptor for monitoring device events. |
| 83 | int GetDeviceEventDescriptor() const; |
| 84 | |
| 85 | // Processes the available device events. |
| 86 | void ProcessDeviceEvents(); |
| 87 | |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 88 | // Register D-Bus object. |
| 89 | void RegisterAsync( |
| 90 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb); |
| 91 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 92 | private: |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 93 | // StorageHandleInfo is a pair of StorageName and Mode. |
Lei Zhang | cdf3a94 | 2017-04-27 20:50:30 | [diff] [blame] | 94 | using StorageHandleInfo = std::pair<std::string, std::string>; |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 95 | |
| 96 | // Handle to StorageHandleInfo map. |
Lei Zhang | cdf3a94 | 2017-04-27 20:50:30 | [diff] [blame] | 97 | using HandleMap = std::map<std::string, StorageHandleInfo>; |
Lei Zhang | 6eaedbf | 2012-08-07 22:22:05 | [diff] [blame] | 98 | |
Lei Zhang | 442d0e2 | 2012-08-17 05:52:49 | [diff] [blame] | 99 | // Returns the StorageName for a handle, or an empty string on failure. |
| 100 | std::string LookupHandle(const std::string& handle); |
| 101 | |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 102 | // Returns true if the storage is opened with write access. |
| 103 | bool IsOpenedWithWrite(const std::string& handle); |
| 104 | |
Lei Zhang | 6eaedbf | 2012-08-07 22:22:05 | [diff] [blame] | 105 | HandleMap handle_map_; |
| 106 | |
Eric Caruso | 2e3c3ca | 2017-08-23 00:18:40 | [diff] [blame] | 107 | // Exported D-Bus object. |
| 108 | brillo::dbus_utils::DBusObject dbus_object_; |
| 109 | |
Lei Zhang | 6eaedbf | 2012-08-07 22:22:05 | [diff] [blame] | 110 | // Device manager needs to be last, so it is the first to be destroyed. |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 111 | DeviceManager device_manager_; |
| 112 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(MtpdServer); |
| 114 | }; |
| 115 | |
| 116 | } // namespace mtpd |
| 117 | |
| 118 | #endif // MTPD_SERVER_IMPL_H_ |