blob: daec2a7e2168dde0448be72eb4e4fe705eee58a9 [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>
Eric Caruso2b923ad2017-08-22 17:18:40 -070013#include <brillo/dbus/async_event_sequencer.h>
14#include <brillo/dbus/dbus_object.h>
15#include <brillo/errors/error.h>
16#include <dbus/bus.h>
Lei Zhang91af91d2012-08-03 11:02:20 -070017
Eric Caruso0076fa72017-08-21 15:31:19 -070018#include "mtpd/dbus_adaptors/org.chromium.Mtpd.h"
Eric Caruso721d82e2017-08-21 15:57:30 -070019#include "mtpd/device_event_delegate.h"
20#include "mtpd/device_manager.h"
21#include "mtpd/file_entry.h"
Lei Zhang91af91d2012-08-03 11:02:20 -070022
23namespace mtpd {
24
25class DeviceManager;
26
27// The D-bus server for the mtpd daemon.
Eric Caruso2b923ad2017-08-22 17:18:40 -070028class MtpdServer : public org::chromium::MtpdInterface,
29 public org::chromium::MtpdAdaptor,
Lei Zhang63e89bd2012-08-06 17:05:33 -070030 public DeviceEventDelegate {
Lei Zhang91af91d2012-08-03 11:02:20 -070031 public:
Eric Caruso2b923ad2017-08-22 17:18:40 -070032 explicit MtpdServer(scoped_refptr<dbus::Bus> bus);
Lei Zhang91af91d2012-08-03 11:02:20 -070033 virtual ~MtpdServer();
34
Eric Caruso2b923ad2017-08-22 17:18:40 -070035 // org::chromium::MtpdAdaptor implementation.
36 std::vector<std::string> EnumerateStorages() override;
37 std::vector<uint8_t> GetStorageInfo(const std::string& storage_name) override;
38 std::vector<uint8_t> GetStorageInfoFromDevice(
39 const std::string& storage_name) override;
40 bool OpenStorage(brillo::ErrorPtr* error,
41 const std::string& storage_name,
42 const std::string& mode,
43 std::string* id) override;
44 bool CloseStorage(brillo::ErrorPtr* error,
45 const std::string& handle) override;
46 bool ReadDirectoryEntryIds(brillo::ErrorPtr* error,
47 const std::string& handle,
48 uint32_t file_id,
49 std::vector<uint32_t>* directory_listing) override;
50 bool GetFileInfo(brillo::ErrorPtr* error,
51 const std::string& handle,
52 const std::vector<uint32_t>& file_ids,
53 std::vector<uint8_t>* serialized_file_entries) override;
54 bool ReadFileChunk(brillo::ErrorPtr* error,
55 const std::string& handle,
56 uint32_t file_id,
57 uint32_t offset,
58 uint32_t count,
59 std::vector<uint8_t>* file_contents) override;
60 bool CopyFileFromLocal(brillo::ErrorPtr* error,
61 const std::string& handle,
62 const dbus::FileDescriptor& file_descriptor,
63 uint32_t parent_id,
64 const std::string& file_name) override;
65 bool DeleteObject(brillo::ErrorPtr* error,
66 const std::string& handle,
67 uint32_t object_id) override;
68 bool RenameObject(brillo::ErrorPtr* error,
69 const std::string& handle,
70 uint32_t object_id,
71 const std::string& new_name) override;
72 bool CreateDirectory(brillo::ErrorPtr* error,
73 const std::string& handle,
74 uint32_t parent_id,
75 const std::string& directory_name) override;
76 bool IsAlive() override;
Lei Zhang91af91d2012-08-03 11:02:20 -070077
Lei Zhang63e89bd2012-08-06 17:05:33 -070078 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080079 void StorageAttached(const std::string& storage_name) override;
80 void StorageDetached(const std::string& storage_name) override;
Lei Zhang63e89bd2012-08-06 17:05:33 -070081
Lei Zhang91af91d2012-08-03 11:02:20 -070082 // Returns a file descriptor for monitoring device events.
83 int GetDeviceEventDescriptor() const;
84
85 // Processes the available device events.
86 void ProcessDeviceEvents();
87
Eric Caruso2b923ad2017-08-22 17:18:40 -070088 // Register D-Bus object.
89 void RegisterAsync(
90 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
91
Lei Zhang91af91d2012-08-03 11:02:20 -070092 private:
Yuki Awano3a9c4192015-02-24 13:37:40 +090093 // StorageHandleInfo is a pair of StorageName and Mode.
Lei Zhanga50737f2017-04-27 13:50:30 -070094 using StorageHandleInfo = std::pair<std::string, std::string>;
Yuki Awano3a9c4192015-02-24 13:37:40 +090095
96 // Handle to StorageHandleInfo map.
Lei Zhanga50737f2017-04-27 13:50:30 -070097 using HandleMap = std::map<std::string, StorageHandleInfo>;
Lei Zhang0d66f922012-08-07 15:22:05 -070098
Lei Zhanga3264132012-08-16 22:52:49 -070099 // Returns the StorageName for a handle, or an empty string on failure.
100 std::string LookupHandle(const std::string& handle);
101
Yuki Awano3a9c4192015-02-24 13:37:40 +0900102 // Returns true if the storage is opened with write access.
103 bool IsOpenedWithWrite(const std::string& handle);
104
Lei Zhang0d66f922012-08-07 15:22:05 -0700105 HandleMap handle_map_;
106
Eric Caruso2b923ad2017-08-22 17:18:40 -0700107 // Exported D-Bus object.
108 brillo::dbus_utils::DBusObject dbus_object_;
109
Lei Zhang0d66f922012-08-07 15:22:05 -0700110 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang63e89bd2012-08-06 17:05:33 -0700111 DeviceManager device_manager_;
112
Lei Zhang91af91d2012-08-03 11:02:20 -0700113 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
114};
115
116} // namespace mtpd
117
118#endif // MTPD_SERVER_IMPL_H_