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