blob: 730c7b58e30b85a3b641115590bdd7409ba2dbe7 [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
5#ifndef MTPD_SERVER_IMPL_H_
6#define MTPD_SERVER_IMPL_H_
7
8#include <string>
9#include <vector>
10
Lei Zhang91af91d2012-08-03 11:02:20 -070011#include <base/compiler_specific.h>
Alex Vakulenkof695ea02016-01-22 19:20:14 -080012#include <base/macros.h>
Lei Zhang91af91d2012-08-03 11:02:20 -070013
Lei Zhange7e0ea52012-10-03 19:27:44 -070014#include "device_event_delegate.h"
15#include "device_manager.h"
16#include "file_entry.h"
Lei Zhang91af91d2012-08-03 11:02:20 -070017#include "mtpd_server/mtpd_server.h"
Lei Zhang91af91d2012-08-03 11:02:20 -070018
19namespace mtpd {
20
21class DeviceManager;
22
23// The D-bus server for the mtpd daemon.
Lei Zhang91af91d2012-08-03 11:02:20 -070024class MtpdServer : public org::chromium::Mtpd_adaptor,
Lei Zhang63e89bd2012-08-06 17:05:33 -070025 public DBus::ObjectAdaptor,
26 public DeviceEventDelegate {
Lei Zhang91af91d2012-08-03 11:02:20 -070027 public:
28 explicit MtpdServer(DBus::Connection& connection);
29 virtual ~MtpdServer();
30
31 // org::chromium::Mtpd_adaptor implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080032 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 Awano82560282015-05-19 16:41:20 +090035 std::vector<uint8_t> GetStorageInfoFromDevice(const std::string& storageName,
36 DBus::Error& error) override;
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080037 std::string OpenStorage(const std::string& storageName,
38 const std::string& mode,
39 DBus::Error& error) override;
40 void CloseStorage(const std::string& handle,
41 DBus::Error& error) override;
42 std::vector<uint32_t> ReadDirectoryEntryIds(
Lei Zhang8c3a3f32014-07-22 16:33:25 -070043 const std::string& handle,
44 const uint32_t& fileId,
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080045 DBus::Error& error) override;
46 std::vector<uint8_t> GetFileInfo(const std::string& handle,
47 const std::vector<uint32_t>& fileIds,
48 DBus::Error& error) override;
49 std::vector<uint8_t> ReadFileChunk(const std::string& handle,
50 const uint32_t& fileId,
51 const uint32_t& offset,
52 const uint32_t& count,
53 DBus::Error& error) override;
Yuki Awanod814ec12015-02-13 13:17:18 +090054 void CopyFileFromLocal(const std::string& handle,
55 const DBus::FileDescriptor& fileDescriptor,
56 const uint32_t& parentId,
57 const std::string& fileName,
58 DBus::Error& error) override;
Yuki Awano3a9c4192015-02-24 13:37:40 +090059 void DeleteObject(const std::string& handle,
60 const uint32_t& objectId,
61 DBus::Error& error) override;
Yuki Awano595a8e22015-03-09 15:58:18 +090062 void RenameObject(const std::string& handle,
63 const uint32_t& objectId,
64 const std::string& newName,
65 DBus::Error& error) override;
Yuki Awanob8062c02015-03-12 20:09:25 +090066 void CreateDirectory(const std::string& handle,
67 const uint32_t& parentId,
68 const std::string& directoryName,
69 DBus::Error& error) override;
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080070 bool IsAlive(DBus::Error& error) override;
Lei Zhang91af91d2012-08-03 11:02:20 -070071
Lei Zhang63e89bd2012-08-06 17:05:33 -070072 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080073 void StorageAttached(const std::string& storage_name) override;
74 void StorageDetached(const std::string& storage_name) override;
Lei Zhang63e89bd2012-08-06 17:05:33 -070075
Lei Zhang91af91d2012-08-03 11:02:20 -070076 // Returns a file descriptor for monitoring device events.
77 int GetDeviceEventDescriptor() const;
78
79 // Processes the available device events.
80 void ProcessDeviceEvents();
81
82 private:
Yuki Awano3a9c4192015-02-24 13:37:40 +090083 // StorageHandleInfo is a pair of StorageName and Mode.
84 typedef std::pair<std::string, std::string> StorageHandleInfo;
85
86 // Handle to StorageHandleInfo map.
87 typedef std::map<std::string, StorageHandleInfo> HandleMap;
Lei Zhang0d66f922012-08-07 15:22:05 -070088
Lei Zhanga3264132012-08-16 22:52:49 -070089 // Returns the StorageName for a handle, or an empty string on failure.
90 std::string LookupHandle(const std::string& handle);
91
Yuki Awano3a9c4192015-02-24 13:37:40 +090092 // Returns true if the storage is opened with write access.
93 bool IsOpenedWithWrite(const std::string& handle);
94
Lei Zhang0d66f922012-08-07 15:22:05 -070095 HandleMap handle_map_;
96
97 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang63e89bd2012-08-06 17:05:33 -070098 DeviceManager device_manager_;
99
Lei Zhang91af91d2012-08-03 11:02:20 -0700100 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
101};
102
103} // namespace mtpd
104
105#endif // MTPD_SERVER_IMPL_H_