Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [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 | |
| 11 | #include <base/basictypes.h> |
| 12 | #include <base/compiler_specific.h> |
| 13 | |
Lei Zhang | e7e0ea5 | 2012-10-03 19:27:44 -0700 | [diff] [blame] | 14 | #include "device_event_delegate.h" |
| 15 | #include "device_manager.h" |
| 16 | #include "file_entry.h" |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 17 | #include "mtpd_server/mtpd_server.h" |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 18 | |
| 19 | namespace mtpd { |
| 20 | |
| 21 | class DeviceManager; |
| 22 | |
| 23 | // The D-bus server for the mtpd daemon. |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 24 | class MtpdServer : public org::chromium::Mtpd_adaptor, |
Lei Zhang | 63e89bd | 2012-08-06 17:05:33 -0700 | [diff] [blame] | 25 | public DBus::ObjectAdaptor, |
| 26 | public DeviceEventDelegate { |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 27 | public: |
| 28 | explicit MtpdServer(DBus::Connection& connection); |
| 29 | virtual ~MtpdServer(); |
| 30 | |
| 31 | // org::chromium::Mtpd_adaptor implementation. |
Alex Vakulenko | 3a09a2b | 2014-12-11 07:53:44 -0800 | [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; |
| 35 | std::string OpenStorage(const std::string& storageName, |
| 36 | const std::string& mode, |
| 37 | DBus::Error& error) override; |
| 38 | void CloseStorage(const std::string& handle, |
| 39 | DBus::Error& error) override; |
| 40 | std::vector<uint32_t> ReadDirectoryEntryIds( |
Lei Zhang | 8c3a3f3 | 2014-07-22 16:33:25 -0700 | [diff] [blame] | 41 | const std::string& handle, |
| 42 | const uint32_t& fileId, |
Alex Vakulenko | 3a09a2b | 2014-12-11 07:53:44 -0800 | [diff] [blame] | 43 | DBus::Error& error) override; |
| 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 | d814ec1 | 2015-02-13 13:17:18 +0900 | [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 | 3a9c419 | 2015-02-24 13:37:40 +0900 | [diff] [blame] | 57 | void DeleteObject(const std::string& handle, |
| 58 | const uint32_t& objectId, |
| 59 | DBus::Error& error) override; |
Yuki Awano | 595a8e2 | 2015-03-09 15:58:18 +0900 | [diff] [blame^] | 60 | void RenameObject(const std::string& handle, |
| 61 | const uint32_t& objectId, |
| 62 | const std::string& newName, |
| 63 | DBus::Error& error) override; |
Alex Vakulenko | 3a09a2b | 2014-12-11 07:53:44 -0800 | [diff] [blame] | 64 | bool IsAlive(DBus::Error& error) override; |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 65 | |
Lei Zhang | 63e89bd | 2012-08-06 17:05:33 -0700 | [diff] [blame] | 66 | // DeviceEventDelegate implementation. |
Alex Vakulenko | 3a09a2b | 2014-12-11 07:53:44 -0800 | [diff] [blame] | 67 | void StorageAttached(const std::string& storage_name) override; |
| 68 | void StorageDetached(const std::string& storage_name) override; |
Lei Zhang | 63e89bd | 2012-08-06 17:05:33 -0700 | [diff] [blame] | 69 | |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 70 | // Returns a file descriptor for monitoring device events. |
| 71 | int GetDeviceEventDescriptor() const; |
| 72 | |
| 73 | // Processes the available device events. |
| 74 | void ProcessDeviceEvents(); |
| 75 | |
| 76 | private: |
Yuki Awano | 3a9c419 | 2015-02-24 13:37:40 +0900 | [diff] [blame] | 77 | // StorageHandleInfo is a pair of StorageName and Mode. |
| 78 | typedef std::pair<std::string, std::string> StorageHandleInfo; |
| 79 | |
| 80 | // Handle to StorageHandleInfo map. |
| 81 | typedef std::map<std::string, StorageHandleInfo> HandleMap; |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 82 | |
Lei Zhang | a326413 | 2012-08-16 22:52:49 -0700 | [diff] [blame] | 83 | // Returns the StorageName for a handle, or an empty string on failure. |
| 84 | std::string LookupHandle(const std::string& handle); |
| 85 | |
Yuki Awano | 3a9c419 | 2015-02-24 13:37:40 +0900 | [diff] [blame] | 86 | // Returns true if the storage is opened with write access. |
| 87 | bool IsOpenedWithWrite(const std::string& handle); |
| 88 | |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 89 | HandleMap handle_map_; |
| 90 | |
| 91 | // Device manager needs to be last, so it is the first to be destroyed. |
Lei Zhang | 63e89bd | 2012-08-06 17:05:33 -0700 | [diff] [blame] | 92 | DeviceManager device_manager_; |
| 93 | |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(MtpdServer); |
| 95 | }; |
| 96 | |
| 97 | } // namespace mtpd |
| 98 | |
| 99 | #endif // MTPD_SERVER_IMPL_H_ |