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> |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 13 | |
Eric Caruso | 34eeec5 | 2017-08-21 22:31:19 | [diff] [blame] | 14 | #include "mtpd/dbus_adaptors/org.chromium.Mtpd.h" |
Eric Caruso | e7cd268 | 2017-08-21 22:57:30 | [diff] [blame^] | 15 | #include "mtpd/device_event_delegate.h" |
| 16 | #include "mtpd/device_manager.h" |
| 17 | #include "mtpd/file_entry.h" |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 18 | |
| 19 | namespace mtpd { |
| 20 | |
| 21 | class DeviceManager; |
| 22 | |
| 23 | // The D-bus server for the mtpd daemon. |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 24 | class MtpdServer : public org::chromium::Mtpd_adaptor, |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 25 | public DBus::ObjectAdaptor, |
| 26 | public DeviceEventDelegate { |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 27 | public: |
| 28 | explicit MtpdServer(DBus::Connection& connection); |
| 29 | virtual ~MtpdServer(); |
| 30 | |
| 31 | // org::chromium::Mtpd_adaptor implementation. |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 32 | std::vector<std::string> EnumerateStorages(DBus::Error& error) override; |
| 33 | std::vector<uint8_t> GetStorageInfo(const std::string& storageName, |
| 34 | DBus::Error& error) override; |
Yuki Awano | 7aa0189 | 2015-05-19 07:41:20 | [diff] [blame] | 35 | std::vector<uint8_t> GetStorageInfoFromDevice(const std::string& storageName, |
| 36 | DBus::Error& error) override; |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 37 | std::string OpenStorage(const std::string& storageName, |
| 38 | const std::string& mode, |
| 39 | DBus::Error& error) override; |
Lei Zhang | dced4eb | 2017-04-18 05:09:12 | [diff] [blame] | 40 | void CloseStorage(const std::string& handle, DBus::Error& error) override; |
| 41 | std::vector<uint32_t> ReadDirectoryEntryIds(const std::string& handle, |
| 42 | const uint32_t& fileId, |
| 43 | DBus::Error& error) override; |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 44 | std::vector<uint8_t> GetFileInfo(const std::string& handle, |
| 45 | const std::vector<uint32_t>& fileIds, |
| 46 | DBus::Error& error) override; |
| 47 | std::vector<uint8_t> ReadFileChunk(const std::string& handle, |
| 48 | const uint32_t& fileId, |
| 49 | const uint32_t& offset, |
| 50 | const uint32_t& count, |
| 51 | DBus::Error& error) override; |
Yuki Awano | e246153 | 2015-02-13 04:17:18 | [diff] [blame] | 52 | void CopyFileFromLocal(const std::string& handle, |
| 53 | const DBus::FileDescriptor& fileDescriptor, |
| 54 | const uint32_t& parentId, |
| 55 | const std::string& fileName, |
| 56 | DBus::Error& error) override; |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 57 | void DeleteObject(const std::string& handle, |
| 58 | const uint32_t& objectId, |
| 59 | DBus::Error& error) override; |
Yuki Awano | 55acdfa | 2015-03-09 06:58:18 | [diff] [blame] | 60 | void RenameObject(const std::string& handle, |
| 61 | const uint32_t& objectId, |
| 62 | const std::string& newName, |
| 63 | DBus::Error& error) override; |
Yuki Awano | d3a744b | 2015-03-12 11:09:25 | [diff] [blame] | 64 | void CreateDirectory(const std::string& handle, |
| 65 | const uint32_t& parentId, |
| 66 | const std::string& directoryName, |
| 67 | DBus::Error& error) override; |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 68 | bool IsAlive(DBus::Error& error) override; |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 69 | |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 70 | // DeviceEventDelegate implementation. |
Alex Vakulenko | cd0d8a4 | 2014-12-11 15:53:44 | [diff] [blame] | 71 | void StorageAttached(const std::string& storage_name) override; |
| 72 | void StorageDetached(const std::string& storage_name) override; |
Lei Zhang | 0e5015f | 2012-08-07 00:05:33 | [diff] [blame] | 73 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 74 | // Returns a file descriptor for monitoring device events. |
| 75 | int GetDeviceEventDescriptor() const; |
| 76 | |
| 77 | // Processes the available device events. |
| 78 | void ProcessDeviceEvents(); |
| 79 | |
| 80 | private: |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 81 | // StorageHandleInfo is a pair of StorageName and Mode. |
Lei Zhang | cdf3a94 | 2017-04-27 20:50:30 | [diff] [blame] | 82 | using StorageHandleInfo = std::pair<std::string, std::string>; |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 83 | |
| 84 | // Handle to StorageHandleInfo map. |
Lei Zhang | cdf3a94 | 2017-04-27 20:50:30 | [diff] [blame] | 85 | using HandleMap = std::map<std::string, StorageHandleInfo>; |
Lei Zhang | 6eaedbf | 2012-08-07 22:22:05 | [diff] [blame] | 86 | |
Lei Zhang | 442d0e2 | 2012-08-17 05:52:49 | [diff] [blame] | 87 | // Returns the StorageName for a handle, or an empty string on failure. |
| 88 | std::string LookupHandle(const std::string& handle); |
| 89 | |
Yuki Awano | 282c81f | 2015-02-24 04:37:40 | [diff] [blame] | 90 | // Returns true if the storage is opened with write access. |
| 91 | bool IsOpenedWithWrite(const std::string& handle); |
| 92 | |
Lei Zhang | 6eaedbf | 2012-08-07 22:22:05 | [diff] [blame] | 93 | HandleMap handle_map_; |
| 94 | |
| 95 | // 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] | 96 | DeviceManager device_manager_; |
| 97 | |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(MtpdServer); |
| 99 | }; |
| 100 | |
| 101 | } // namespace mtpd |
| 102 | |
| 103 | #endif // MTPD_SERVER_IMPL_H_ |