blob: 3590ca67df442eeb73271d09938d4c35fbe87f72 [file] [log] [blame]
[email protected]2321d282012-01-31 23:06:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4ae73292011-11-15 05:20:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]64e199252012-04-06 01:54:365#ifndef CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
6#define CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
[email protected]4ae73292011-11-15 05:20:187
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
[email protected]64e199252012-04-06 01:54:3613#include "chromeos/chromeos_export.h"
14#include "chromeos/dbus/dbus_client_implementation_type.h"
[email protected]4ae73292011-11-15 05:20:1815
16namespace dbus {
17class Bus;
18class Response;
19}
20
21namespace chromeos {
22
23// Enum describing types of mount used by cros-disks.
24enum MountType {
25 MOUNT_TYPE_INVALID,
26 MOUNT_TYPE_DEVICE,
27 MOUNT_TYPE_ARCHIVE,
[email protected]9bb24222012-02-09 02:00:4328 MOUNT_TYPE_GDATA,
[email protected]4ae73292011-11-15 05:20:1829 MOUNT_TYPE_NETWORK_STORAGE,
30};
31
32// Type of device.
33enum DeviceType {
[email protected]2321d282012-01-31 23:06:5934 DEVICE_TYPE_UNKNOWN,
35 DEVICE_TYPE_USB, // USB stick.
36 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0037 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
38 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
39 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1840};
41
42// Mount error code used by cros-disks.
43enum MountError {
44 MOUNT_ERROR_NONE = 0,
45 MOUNT_ERROR_UNKNOWN = 1,
46 MOUNT_ERROR_INTERNAL = 2,
47 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3348 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1849 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4350 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1851 MOUNT_ERROR_PATH_UNMOUNTED = 901,
52 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
53 // consider doing explicit translation from cros-disks error_types.
54};
55
56// Event type each corresponding to a signal sent from cros-disks.
57enum MountEventType {
58 DISK_ADDED,
59 DISK_REMOVED,
60 DISK_CHANGED,
61 DEVICE_ADDED,
62 DEVICE_REMOVED,
63 DEVICE_SCANNED,
64 FORMATTING_FINISHED,
65};
66
67// A class to represent information about a disk sent from cros-disks.
68class DiskInfo {
69 public:
70 DiskInfo(const std::string& device_path, dbus::Response* response);
71 ~DiskInfo();
72
73 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:2774 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:1875
76 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:2777 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:1878
79 // Disk system path given by udev.
80 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:2781 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:1882
83 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
84 bool is_drive() const { return is_drive_; }
85
86 // Does the disk have media content.
87 bool has_media() const { return has_media_; }
88
89 // Is the disk on deveice we booted the machine from.
90 bool on_boot_device() const { return on_boot_device_; }
91
92 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:2793 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:1894
95 // Disk label.
[email protected]85b95a2012012-08-07 18:57:2796 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:1897
[email protected]202e9fee2012-09-13 20:21:2998 // Vendor ID of the device (e.g. "18d1").
99 const std::string& vendor_id() const { return vendor_id_; }
100
101 // Vendor name of the device (e.g. "Google Inc.").
102 const std::string& vendor_name() const { return vendor_name_; }
103
104 // Product ID of the device (e.g. "4e11").
105 const std::string& product_id() const { return product_id_; }
106
107 // Product name of the device (e.g. "Nexus One").
108 const std::string& product_name() const { return product_name_; }
109
[email protected]4ae73292011-11-15 05:20:18110 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27111 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18112
113 // Device type. Not working well, yet.
114 DeviceType device_type() const { return device_type_; }
115
116 // Total size of the disk in bytes.
117 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
118
119 // Is the device read-only.
120 bool is_read_only() const { return is_read_only_; }
121
122 // Returns true if the device should be hidden from the file browser.
123 bool is_hidden() const { return is_hidden_; }
124
[email protected]9c5620d32012-07-31 01:00:38125 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27126 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38127
[email protected]4ae73292011-11-15 05:20:18128 private:
129 void InitializeFromResponse(dbus::Response* response);
130
131 std::string device_path_;
132 std::string mount_path_;
133 std::string system_path_;
134 bool is_drive_;
135 bool has_media_;
136 bool on_boot_device_;
137
138 std::string file_path_;
139 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29140 std::string vendor_id_;
141 std::string vendor_name_;
142 std::string product_id_;
143 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18144 std::string drive_model_;
145 DeviceType device_type_;
146 uint64 total_size_in_bytes_;
147 bool is_read_only_;
148 bool is_hidden_;
[email protected]9c5620d32012-07-31 01:00:38149 std::string uuid_;
[email protected]4ae73292011-11-15 05:20:18150};
151
152// A class to make the actual DBus calls for cros-disks service.
153// This class only makes calls, result/error handling should be done
154// by callbacks.
[email protected]64e199252012-04-06 01:54:36155class CHROMEOS_EXPORT CrosDisksClient {
[email protected]4ae73292011-11-15 05:20:18156 public:
157 // A callback to be called when DBus method call fails.
158 typedef base::Callback<void()> ErrorCallback;
159
160 // A callback to handle the result of Mount.
161 typedef base::Callback<void()> MountCallback;
162
163 // A callback to handle the result of Unmount.
164 // The argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27165 typedef base::Callback<void(const std::string& device_path)> UnmountCallback;
[email protected]4ae73292011-11-15 05:20:18166
167 // A callback to handle the result of EnumerateAutoMountableDevices.
168 // The argument is the enumerated device paths.
[email protected]85b95a2012012-08-07 18:57:27169 typedef base::Callback<void(const std::vector<std::string>& device_paths)
[email protected]4ae73292011-11-15 05:20:18170 > EnumerateAutoMountableDevicesCallback;
171
172 // A callback to handle the result of FormatDevice.
173 // The first argument is the device path.
174 // The second argument is true when formatting succeeded, false otherwise.
[email protected]85b95a2012012-08-07 18:57:27175 typedef base::Callback<void(const std::string& device_path,
176 bool format_succeeded)> FormatDeviceCallback;
[email protected]4ae73292011-11-15 05:20:18177
178 // A callback to handle the result of GetDeviceProperties.
179 // The argument is the information about the specified device.
[email protected]85b95a2012012-08-07 18:57:27180 typedef base::Callback<void(const DiskInfo& disk_info)
181 > GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18182
183 // A callback to handle MountCompleted signal.
184 // The first argument is the error code.
185 // The second argument is the source path.
186 // The third argument is the mount type.
187 // The fourth argument is the mount path.
[email protected]85b95a2012012-08-07 18:57:27188 typedef base::Callback<void(MountError error_code,
189 const std::string& source_path,
190 MountType mount_type,
191 const std::string& mount_path)
192 > MountCompletedHandler;
[email protected]4ae73292011-11-15 05:20:18193
194 // A callback to handle mount events.
195 // The first argument is the event type.
196 // The second argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27197 typedef base::Callback<void(MountEventType event_type,
198 const std::string& device_path)
[email protected]4ae73292011-11-15 05:20:18199 > MountEventHandler;
200
201 virtual ~CrosDisksClient();
202
203 // Calls Mount method. |callback| is called after the method call succeeds,
204 // otherwise, |error_callback| is called.
[email protected]dcad8fc2012-04-30 23:31:33205 // When mounting an archive, caller may set two optional arguments:
206 // - The |source_format| argument passes the file extension (with the leading
207 // dot, for example ".zip"). If |source_format| is empty then the source
208 // format is auto-detected.
209 // - The |mount_label| argument passes an optional mount label to be used as
210 // the directory name of the mount point. If |mount_label| is empty, the
211 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18212 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48213 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33214 const std::string& mount_label,
[email protected]4ae73292011-11-15 05:20:18215 MountType type,
[email protected]4a404e52012-04-11 02:25:35216 const MountCallback& callback,
217 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18218
219 // Calls Unmount method. |callback| is called after the method call succeeds,
220 // otherwise, |error_callback| is called.
221 virtual void Unmount(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35222 const UnmountCallback& callback,
223 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18224
225 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
226 // method call succeeds, otherwise, |error_callback| is called.
227 virtual void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35228 const EnumerateAutoMountableDevicesCallback& callback,
229 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18230
231 // Calls FormatDevice method. |callback| is called after the method call
232 // succeeds, otherwise, |error_callback| is called.
233 virtual void FormatDevice(const std::string& device_path,
234 const std::string& filesystem,
[email protected]4a404e52012-04-11 02:25:35235 const FormatDeviceCallback& callback,
236 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18237
238 // Calls GetDeviceProperties method. |callback| is called after the method
239 // call succeeds, otherwise, |error_callback| is called.
240 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35241 const GetDevicePropertiesCallback& callback,
242 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18243
244 // Registers given callback for events.
245 // |mount_event_handler| is called when mount event signal is received.
246 // |mount_completed_handler| is called when MountCompleted signal is received.
247 virtual void SetUpConnections(
[email protected]4a404e52012-04-11 02:25:35248 const MountEventHandler& mount_event_handler,
249 const MountCompletedHandler& mount_completed_handler) = 0;
[email protected]4ae73292011-11-15 05:20:18250
251 // Factory function, creates a new instance and returns ownership.
252 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]e8db03d62012-03-31 04:08:38253 static CrosDisksClient* Create(DBusClientImplementationType type,
254 dbus::Bus* bus);
[email protected]4ae73292011-11-15 05:20:18255
256 protected:
257 // Create() should be used instead.
258 CrosDisksClient();
259
260 private:
261 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
262};
263
264} // namespace chromeos
265
[email protected]64e199252012-04-06 01:54:36266#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_