blob: 67a8a04875c0f0f2ce330001dd5954400354b200 [file] [log] [blame]
Lei Zhangf0ab3ae2012-08-03 18:02:201// 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 Zhangf0ab3ae2012-08-03 18:02:2011#include <base/compiler_specific.h>
Alex Vakulenkoef2d18d2016-01-23 03:20:1412#include <base/macros.h>
Lei Zhangf0ab3ae2012-08-03 18:02:2013
Eric Caruso34eeec52017-08-21 22:31:1914#include "mtpd/dbus_adaptors/org.chromium.Mtpd.h"
Eric Carusoe7cd2682017-08-21 22:57:3015#include "mtpd/device_event_delegate.h"
16#include "mtpd/device_manager.h"
17#include "mtpd/file_entry.h"
Lei Zhangf0ab3ae2012-08-03 18:02:2018
19namespace mtpd {
20
21class DeviceManager;
22
23// The D-bus server for the mtpd daemon.
Lei Zhangf0ab3ae2012-08-03 18:02:2024class MtpdServer : public org::chromium::Mtpd_adaptor,
Lei Zhang0e5015f2012-08-07 00:05:3325 public DBus::ObjectAdaptor,
26 public DeviceEventDelegate {
Lei Zhangf0ab3ae2012-08-03 18:02:2027 public:
28 explicit MtpdServer(DBus::Connection& connection);
29 virtual ~MtpdServer();
30
31 // org::chromium::Mtpd_adaptor implementation.
Alex Vakulenkocd0d8a42014-12-11 15:53:4432 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 Awano7aa01892015-05-19 07:41:2035 std::vector<uint8_t> GetStorageInfoFromDevice(const std::string& storageName,
36 DBus::Error& error) override;
Alex Vakulenkocd0d8a42014-12-11 15:53:4437 std::string OpenStorage(const std::string& storageName,
38 const std::string& mode,
39 DBus::Error& error) override;
Lei Zhangdced4eb2017-04-18 05:09:1240 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 Vakulenkocd0d8a42014-12-11 15:53:4444 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 Awanoe2461532015-02-13 04:17:1852 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 Awano282c81f2015-02-24 04:37:4057 void DeleteObject(const std::string& handle,
58 const uint32_t& objectId,
59 DBus::Error& error) override;
Yuki Awano55acdfa2015-03-09 06:58:1860 void RenameObject(const std::string& handle,
61 const uint32_t& objectId,
62 const std::string& newName,
63 DBus::Error& error) override;
Yuki Awanod3a744b2015-03-12 11:09:2564 void CreateDirectory(const std::string& handle,
65 const uint32_t& parentId,
66 const std::string& directoryName,
67 DBus::Error& error) override;
Alex Vakulenkocd0d8a42014-12-11 15:53:4468 bool IsAlive(DBus::Error& error) override;
Lei Zhangf0ab3ae2012-08-03 18:02:2069
Lei Zhang0e5015f2012-08-07 00:05:3370 // DeviceEventDelegate implementation.
Alex Vakulenkocd0d8a42014-12-11 15:53:4471 void StorageAttached(const std::string& storage_name) override;
72 void StorageDetached(const std::string& storage_name) override;
Lei Zhang0e5015f2012-08-07 00:05:3373
Lei Zhangf0ab3ae2012-08-03 18:02:2074 // 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 Awano282c81f2015-02-24 04:37:4081 // StorageHandleInfo is a pair of StorageName and Mode.
Lei Zhangcdf3a942017-04-27 20:50:3082 using StorageHandleInfo = std::pair<std::string, std::string>;
Yuki Awano282c81f2015-02-24 04:37:4083
84 // Handle to StorageHandleInfo map.
Lei Zhangcdf3a942017-04-27 20:50:3085 using HandleMap = std::map<std::string, StorageHandleInfo>;
Lei Zhang6eaedbf2012-08-07 22:22:0586
Lei Zhang442d0e22012-08-17 05:52:4987 // Returns the StorageName for a handle, or an empty string on failure.
88 std::string LookupHandle(const std::string& handle);
89
Yuki Awano282c81f2015-02-24 04:37:4090 // Returns true if the storage is opened with write access.
91 bool IsOpenedWithWrite(const std::string& handle);
92
Lei Zhang6eaedbf2012-08-07 22:22:0593 HandleMap handle_map_;
94
95 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang0e5015f2012-08-07 00:05:3396 DeviceManager device_manager_;
97
Lei Zhangf0ab3ae2012-08-03 18:02:2098 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
99};
100
101} // namespace mtpd
102
103#endif // MTPD_SERVER_IMPL_H_