blob: eca1179ec182abe4521b1e9a9080b9c8ed85c154 [file] [log] [blame]
Lei Zhang91af91d2012-08-03 11:02:20 -07001// 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 Chan81757a02018-01-23 14:06:08 -08005#ifndef MTPD_MTPD_SERVER_IMPL_H_
6#define MTPD_MTPD_SERVER_IMPL_H_
Lei Zhang91af91d2012-08-03 11:02:20 -07007
Ben Chan81757a02018-01-23 14:06:08 -08008#include <map>
Lei Zhang91af91d2012-08-03 11:02:20 -07009#include <string>
Ben Chan81757a02018-01-23 14:06:08 -080010#include <utility>
Lei Zhang91af91d2012-08-03 11:02:20 -070011#include <vector>
12
Lei Zhang91af91d2012-08-03 11:02:20 -070013#include <base/compiler_specific.h>
Alex Vakulenkof695ea02016-01-22 19:20:14 -080014#include <base/macros.h>
Eric Caruso2b923ad2017-08-22 17:18:40 -070015#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 Zhang91af91d2012-08-03 11:02:20 -070019
Eric Caruso0076fa72017-08-21 15:31:19 -070020#include "mtpd/dbus_adaptors/org.chromium.Mtpd.h"
Eric Caruso721d82e2017-08-21 15:57:30 -070021#include "mtpd/device_event_delegate.h"
22#include "mtpd/device_manager.h"
23#include "mtpd/file_entry.h"
Lei Zhang91af91d2012-08-03 11:02:20 -070024
25namespace mtpd {
26
27class DeviceManager;
28
29// The D-bus server for the mtpd daemon.
Eric Caruso2b923ad2017-08-22 17:18:40 -070030class MtpdServer : public org::chromium::MtpdInterface,
31 public org::chromium::MtpdAdaptor,
Lei Zhang63e89bd2012-08-06 17:05:33 -070032 public DeviceEventDelegate {
Lei Zhang91af91d2012-08-03 11:02:20 -070033 public:
Eric Caruso2b923ad2017-08-22 17:18:40 -070034 explicit MtpdServer(scoped_refptr<dbus::Bus> bus);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090035 MtpdServer(const MtpdServer&) = delete;
36 MtpdServer& operator=(const MtpdServer&) = delete;
37
Lei Zhang91af91d2012-08-03 11:02:20 -070038 virtual ~MtpdServer();
39
Eric Caruso2b923ad2017-08-22 17:18:40 -070040 // org::chromium::MtpdAdaptor implementation.
41 std::vector<std::string> EnumerateStorages() override;
42 std::vector<uint8_t> GetStorageInfo(const std::string& storage_name) override;
43 std::vector<uint8_t> GetStorageInfoFromDevice(
44 const std::string& storage_name) override;
45 bool OpenStorage(brillo::ErrorPtr* error,
46 const std::string& storage_name,
47 const std::string& mode,
48 std::string* id) override;
49 bool CloseStorage(brillo::ErrorPtr* error,
50 const std::string& handle) override;
51 bool ReadDirectoryEntryIds(brillo::ErrorPtr* error,
52 const std::string& handle,
53 uint32_t file_id,
54 std::vector<uint32_t>* directory_listing) override;
55 bool GetFileInfo(brillo::ErrorPtr* error,
56 const std::string& handle,
57 const std::vector<uint32_t>& file_ids,
58 std::vector<uint8_t>* serialized_file_entries) override;
59 bool ReadFileChunk(brillo::ErrorPtr* error,
60 const std::string& handle,
61 uint32_t file_id,
62 uint32_t offset,
63 uint32_t count,
64 std::vector<uint8_t>* file_contents) override;
65 bool CopyFileFromLocal(brillo::ErrorPtr* error,
66 const std::string& handle,
Eric Carusoabf933e2018-03-22 16:09:37 -070067 const base::ScopedFD& file_descriptor,
Eric Caruso2b923ad2017-08-22 17:18:40 -070068 uint32_t parent_id,
69 const std::string& file_name) override;
70 bool DeleteObject(brillo::ErrorPtr* error,
71 const std::string& handle,
72 uint32_t object_id) override;
73 bool RenameObject(brillo::ErrorPtr* error,
74 const std::string& handle,
75 uint32_t object_id,
76 const std::string& new_name) override;
77 bool CreateDirectory(brillo::ErrorPtr* error,
78 const std::string& handle,
79 uint32_t parent_id,
80 const std::string& directory_name) override;
81 bool IsAlive() override;
Lei Zhang91af91d2012-08-03 11:02:20 -070082
Lei Zhang63e89bd2012-08-06 17:05:33 -070083 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080084 void StorageAttached(const std::string& storage_name) override;
85 void StorageDetached(const std::string& storage_name) override;
Lei Zhang63e89bd2012-08-06 17:05:33 -070086
Lei Zhang91af91d2012-08-03 11:02:20 -070087 // Returns a file descriptor for monitoring device events.
88 int GetDeviceEventDescriptor() const;
89
90 // Processes the available device events.
91 void ProcessDeviceEvents();
92
Eric Caruso2b923ad2017-08-22 17:18:40 -070093 // Register D-Bus object.
94 void RegisterAsync(
95 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
96
Lei Zhang91af91d2012-08-03 11:02:20 -070097 private:
Yuki Awano3a9c4192015-02-24 13:37:40 +090098 // StorageHandleInfo is a pair of StorageName and Mode.
Lei Zhanga50737f2017-04-27 13:50:30 -070099 using StorageHandleInfo = std::pair<std::string, std::string>;
Yuki Awano3a9c4192015-02-24 13:37:40 +0900100
101 // Handle to StorageHandleInfo map.
Lei Zhanga50737f2017-04-27 13:50:30 -0700102 using HandleMap = std::map<std::string, StorageHandleInfo>;
Lei Zhang0d66f922012-08-07 15:22:05 -0700103
Lei Zhanga3264132012-08-16 22:52:49 -0700104 // Returns the StorageName for a handle, or an empty string on failure.
105 std::string LookupHandle(const std::string& handle);
106
Yuki Awano3a9c4192015-02-24 13:37:40 +0900107 // Returns true if the storage is opened with write access.
108 bool IsOpenedWithWrite(const std::string& handle);
109
Lei Zhang0d66f922012-08-07 15:22:05 -0700110 HandleMap handle_map_;
111
Eric Caruso2b923ad2017-08-22 17:18:40 -0700112 // Exported D-Bus object.
113 brillo::dbus_utils::DBusObject dbus_object_;
114
Lei Zhang0d66f922012-08-07 15:22:05 -0700115 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang63e89bd2012-08-06 17:05:33 -0700116 DeviceManager device_manager_;
Lei Zhang91af91d2012-08-03 11:02:20 -0700117};
118
119} // namespace mtpd
120
Ben Chan81757a02018-01-23 14:06:08 -0800121#endif // MTPD_MTPD_SERVER_IMPL_H_