blob: 7a239734cb20fc943203870ec090581732b6456f [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);
Lei Zhang91af91d2012-08-03 11:02:20 -070035 virtual ~MtpdServer();
36
Eric Caruso2b923ad2017-08-22 17:18:40 -070037 // org::chromium::MtpdAdaptor implementation.
38 std::vector<std::string> EnumerateStorages() override;
39 std::vector<uint8_t> GetStorageInfo(const std::string& storage_name) override;
40 std::vector<uint8_t> GetStorageInfoFromDevice(
41 const std::string& storage_name) override;
42 bool OpenStorage(brillo::ErrorPtr* error,
43 const std::string& storage_name,
44 const std::string& mode,
45 std::string* id) override;
46 bool CloseStorage(brillo::ErrorPtr* error,
47 const std::string& handle) override;
48 bool ReadDirectoryEntryIds(brillo::ErrorPtr* error,
49 const std::string& handle,
50 uint32_t file_id,
51 std::vector<uint32_t>* directory_listing) override;
52 bool GetFileInfo(brillo::ErrorPtr* error,
53 const std::string& handle,
54 const std::vector<uint32_t>& file_ids,
55 std::vector<uint8_t>* serialized_file_entries) override;
56 bool ReadFileChunk(brillo::ErrorPtr* error,
57 const std::string& handle,
58 uint32_t file_id,
59 uint32_t offset,
60 uint32_t count,
61 std::vector<uint8_t>* file_contents) override;
62 bool CopyFileFromLocal(brillo::ErrorPtr* error,
63 const std::string& handle,
64 const dbus::FileDescriptor& file_descriptor,
65 uint32_t parent_id,
66 const std::string& file_name) override;
67 bool DeleteObject(brillo::ErrorPtr* error,
68 const std::string& handle,
69 uint32_t object_id) override;
70 bool RenameObject(brillo::ErrorPtr* error,
71 const std::string& handle,
72 uint32_t object_id,
73 const std::string& new_name) override;
74 bool CreateDirectory(brillo::ErrorPtr* error,
75 const std::string& handle,
76 uint32_t parent_id,
77 const std::string& directory_name) override;
78 bool IsAlive() override;
Lei Zhang91af91d2012-08-03 11:02:20 -070079
Lei Zhang63e89bd2012-08-06 17:05:33 -070080 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080081 void StorageAttached(const std::string& storage_name) override;
82 void StorageDetached(const std::string& storage_name) override;
Lei Zhang63e89bd2012-08-06 17:05:33 -070083
Lei Zhang91af91d2012-08-03 11:02:20 -070084 // Returns a file descriptor for monitoring device events.
85 int GetDeviceEventDescriptor() const;
86
87 // Processes the available device events.
88 void ProcessDeviceEvents();
89
Eric Caruso2b923ad2017-08-22 17:18:40 -070090 // Register D-Bus object.
91 void RegisterAsync(
92 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
93
Lei Zhang91af91d2012-08-03 11:02:20 -070094 private:
Yuki Awano3a9c4192015-02-24 13:37:40 +090095 // StorageHandleInfo is a pair of StorageName and Mode.
Lei Zhanga50737f2017-04-27 13:50:30 -070096 using StorageHandleInfo = std::pair<std::string, std::string>;
Yuki Awano3a9c4192015-02-24 13:37:40 +090097
98 // Handle to StorageHandleInfo map.
Lei Zhanga50737f2017-04-27 13:50:30 -070099 using HandleMap = std::map<std::string, StorageHandleInfo>;
Lei Zhang0d66f922012-08-07 15:22:05 -0700100
Lei Zhanga3264132012-08-16 22:52:49 -0700101 // Returns the StorageName for a handle, or an empty string on failure.
102 std::string LookupHandle(const std::string& handle);
103
Yuki Awano3a9c4192015-02-24 13:37:40 +0900104 // Returns true if the storage is opened with write access.
105 bool IsOpenedWithWrite(const std::string& handle);
106
Lei Zhang0d66f922012-08-07 15:22:05 -0700107 HandleMap handle_map_;
108
Eric Caruso2b923ad2017-08-22 17:18:40 -0700109 // Exported D-Bus object.
110 brillo::dbus_utils::DBusObject dbus_object_;
111
Lei Zhang0d66f922012-08-07 15:22:05 -0700112 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang63e89bd2012-08-06 17:05:33 -0700113 DeviceManager device_manager_;
114
Lei Zhang91af91d2012-08-03 11:02:20 -0700115 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
116};
117
118} // namespace mtpd
119
Ben Chan81757a02018-01-23 14:06:08 -0800120#endif // MTPD_MTPD_SERVER_IMPL_H_