blob: a7687b947ba71e830cfda64c407803bfba84c4e1 [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
11#include <base/basictypes.h>
12#include <base/compiler_specific.h>
13
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;
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 Zhang8c3a3f32014-07-22 16:33:25 -070041 const std::string& handle,
42 const uint32_t& fileId,
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080043 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 Awanod814ec12015-02-13 13:17:18 +090052 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 Awano3a9c4192015-02-24 13:37:40 +090057 void DeleteObject(const std::string& handle,
58 const uint32_t& objectId,
59 DBus::Error& error) override;
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080060 bool IsAlive(DBus::Error& error) override;
Lei Zhang91af91d2012-08-03 11:02:20 -070061
Lei Zhang63e89bd2012-08-06 17:05:33 -070062 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080063 void StorageAttached(const std::string& storage_name) override;
64 void StorageDetached(const std::string& storage_name) override;
Lei Zhang63e89bd2012-08-06 17:05:33 -070065
Lei Zhang91af91d2012-08-03 11:02:20 -070066 // Returns a file descriptor for monitoring device events.
67 int GetDeviceEventDescriptor() const;
68
69 // Processes the available device events.
70 void ProcessDeviceEvents();
71
72 private:
Yuki Awano3a9c4192015-02-24 13:37:40 +090073 // StorageHandleInfo is a pair of StorageName and Mode.
74 typedef std::pair<std::string, std::string> StorageHandleInfo;
75
76 // Handle to StorageHandleInfo map.
77 typedef std::map<std::string, StorageHandleInfo> HandleMap;
Lei Zhang0d66f922012-08-07 15:22:05 -070078
Lei Zhanga3264132012-08-16 22:52:49 -070079 // Returns the StorageName for a handle, or an empty string on failure.
80 std::string LookupHandle(const std::string& handle);
81
Yuki Awano3a9c4192015-02-24 13:37:40 +090082 // Returns true if the storage is opened with write access.
83 bool IsOpenedWithWrite(const std::string& handle);
84
Lei Zhang0d66f922012-08-07 15:22:05 -070085 HandleMap handle_map_;
86
87 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang63e89bd2012-08-06 17:05:33 -070088 DeviceManager device_manager_;
89
Lei Zhang91af91d2012-08-03 11:02:20 -070090 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
91};
92
93} // namespace mtpd
94
95#endif // MTPD_SERVER_IMPL_H_