[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 5 | #include "chromeos/dbus/cros_disks_client.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 6 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 10 | #include <map> |
Hidehiko Abe | 5a138a2 | 2017-10-05 16:22:21 | [diff] [blame] | 11 | #include <memory> |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 12 | #include <utility> |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 13 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
thestig | b44bd35 | 2014-09-10 01:47:06 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
[email protected] | 593cf3b | 2013-03-05 04:16:52 | [diff] [blame] | 17 | #include "base/location.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 18 | #include "base/macros.h" |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 19 | #include "base/observer_list.h" |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 20 | #include "base/stl_util.h" |
[email protected] | afa339d7 | 2013-06-11 06:32:51 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 22 | #include "base/sys_info.h" |
[email protected] | 593cf3b | 2013-03-05 04:16:52 | [diff] [blame] | 23 | #include "base/task_runner_util.h" |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 24 | #include "base/values.h" |
satorux | 8fd29380 | 2014-10-30 08:23:12 | [diff] [blame] | 25 | #include "chromeos/dbus/fake_cros_disks_client.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 26 | #include "dbus/bus.h" |
| 27 | #include "dbus/message.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 28 | #include "dbus/object_path.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 29 | #include "dbus/object_proxy.h" |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 30 | #include "dbus/values_util.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 31 | #include "third_party/cros_system_api/dbus/service_constants.h" |
| 32 | |
| 33 | namespace chromeos { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const char* kDefaultMountOptions[] = { |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 38 | "nodev", "noexec", "nosuid", |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 39 | }; |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 40 | const char kReadOnlyOption[] = "ro"; |
| 41 | const char kReadWriteOption[] = "rw"; |
yamaguchi | fa8efc7 | 2016-10-21 15:05:51 | [diff] [blame] | 42 | const char kRemountOption[] = "remount"; |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 43 | const char kMountLabelOption[] = "mountlabel"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 44 | |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 45 | const char kLazyUnmountOption[] = "lazy"; |
| 46 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 47 | // Checks if retrieved media type is in boundaries of DeviceMediaType. |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 48 | bool IsValidMediaType(uint32_t type) { |
| 49 | return type < static_cast<uint32_t>(cros_disks::DEVICE_MEDIA_NUM_VALUES); |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 52 | // Translates enum used in cros-disks to enum used in Chrome. |
| 53 | // Note that we could just do static_cast, but this is less sensitive to |
| 54 | // changes in cros-disks. |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 55 | DeviceType DeviceMediaTypeToDeviceType(uint32_t media_type_uint32) { |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 56 | if (!IsValidMediaType(media_type_uint32)) |
| 57 | return DEVICE_TYPE_UNKNOWN; |
| 58 | |
| 59 | cros_disks::DeviceMediaType media_type = |
| 60 | cros_disks::DeviceMediaType(media_type_uint32); |
| 61 | |
| 62 | switch (media_type) { |
| 63 | case(cros_disks::DEVICE_MEDIA_UNKNOWN): |
| 64 | return DEVICE_TYPE_UNKNOWN; |
| 65 | case(cros_disks::DEVICE_MEDIA_USB): |
| 66 | return DEVICE_TYPE_USB; |
| 67 | case(cros_disks::DEVICE_MEDIA_SD): |
| 68 | return DEVICE_TYPE_SD; |
| 69 | case(cros_disks::DEVICE_MEDIA_OPTICAL_DISC): |
| 70 | return DEVICE_TYPE_OPTICAL_DISC; |
| 71 | case(cros_disks::DEVICE_MEDIA_MOBILE): |
| 72 | return DEVICE_TYPE_MOBILE; |
[email protected] | f4ae40ac | 2012-05-04 21:57:00 | [diff] [blame] | 73 | case(cros_disks::DEVICE_MEDIA_DVD): |
| 74 | return DEVICE_TYPE_DVD; |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 75 | default: |
| 76 | return DEVICE_TYPE_UNKNOWN; |
| 77 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 80 | bool ReadMountEntryFromDbus(dbus::MessageReader* reader, MountEntry* entry) { |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 81 | uint32_t error_code = 0; |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 82 | std::string source_path; |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 83 | uint32_t mount_type = 0; |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 84 | std::string mount_path; |
| 85 | if (!reader->PopUint32(&error_code) || |
| 86 | !reader->PopString(&source_path) || |
| 87 | !reader->PopUint32(&mount_type) || |
| 88 | !reader->PopString(&mount_path)) { |
| 89 | return false; |
| 90 | } |
| 91 | *entry = MountEntry(static_cast<MountError>(error_code), source_path, |
| 92 | static_cast<MountType>(mount_type), mount_path); |
| 93 | return true; |
| 94 | } |
| 95 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 96 | // The CrosDisksClient implementation. |
| 97 | class CrosDisksClientImpl : public CrosDisksClient { |
| 98 | public: |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 99 | CrosDisksClientImpl() : proxy_(nullptr), weak_ptr_factory_(this) {} |
| 100 | |
| 101 | // CrosDisksClient override. |
| 102 | void AddObserver(Observer* observer) override { |
| 103 | observer_list_.AddObserver(observer); |
| 104 | } |
| 105 | |
| 106 | // CrosDisksClient override. |
| 107 | void RemoveObserver(Observer* observer) override { |
| 108 | observer_list_.RemoveObserver(observer); |
| 109 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 110 | |
| 111 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 112 | void Mount(const std::string& source_path, |
| 113 | const std::string& source_format, |
| 114 | const std::string& mount_label, |
Sergei Datsenko | d1924818 | 2018-05-11 01:52:56 | [diff] [blame^] | 115 | const std::vector<std::string>& mount_options, |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 116 | MountAccessMode access_mode, |
yamaguchi | fa8efc7 | 2016-10-21 15:05:51 | [diff] [blame] | 117 | RemountOption remount, |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 118 | VoidDBusMethodCallback callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 119 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 120 | cros_disks::kMount); |
| 121 | dbus::MessageWriter writer(&method_call); |
| 122 | writer.AppendString(source_path); |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 123 | writer.AppendString(source_format); |
Sergei Datsenko | d1924818 | 2018-05-11 01:52:56 | [diff] [blame^] | 124 | std::vector<std::string> options = |
| 125 | ComposeMountOptions(mount_options, mount_label, access_mode, remount); |
| 126 | writer.AppendArrayOfStrings(options); |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 127 | proxy_->CallMethod( |
| 128 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 129 | base::BindOnce(&CrosDisksClientImpl::OnVoidMethod, |
| 130 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 134 | void Unmount(const std::string& device_path, |
| 135 | UnmountOptions options, |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 136 | VoidDBusMethodCallback callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 137 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 138 | cros_disks::kUnmount); |
| 139 | dbus::MessageWriter writer(&method_call); |
| 140 | writer.AppendString(device_path); |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 141 | |
benchan | 8bcaea1 | 2017-03-11 00:26:50 | [diff] [blame] | 142 | std::vector<std::string> unmount_options; |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 143 | if (options == UNMOUNT_OPTIONS_LAZY) |
| 144 | unmount_options.push_back(kLazyUnmountOption); |
| 145 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 146 | writer.AppendArrayOfStrings(unmount_options); |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 147 | proxy_->CallMethod( |
| 148 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 149 | base::BindOnce(&CrosDisksClientImpl::OnUnmount, |
| 150 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 154 | void EnumerateAutoMountableDevices( |
Aga Wronska | ddc1a75 | 2017-12-01 19:44:02 | [diff] [blame] | 155 | const EnumerateDevicesCallback& callback, |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 156 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 157 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 158 | cros_disks::kEnumerateAutoMountableDevices); |
Aga Wronska | ddc1a75 | 2017-12-01 19:44:02 | [diff] [blame] | 159 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 160 | base::BindOnce(&CrosDisksClientImpl::OnEnumerateDevices, |
| 161 | weak_ptr_factory_.GetWeakPtr(), callback, |
| 162 | error_callback)); |
| 163 | } |
| 164 | |
| 165 | void EnumerateDevices(const EnumerateDevicesCallback& callback, |
| 166 | const base::Closure& error_callback) override { |
| 167 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 168 | cros_disks::kEnumerateDevices); |
| 169 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 170 | base::BindOnce(&CrosDisksClientImpl::OnEnumerateDevices, |
| 171 | weak_ptr_factory_.GetWeakPtr(), callback, |
| 172 | error_callback)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 176 | void EnumerateMountEntries(const EnumerateMountEntriesCallback& callback, |
| 177 | const base::Closure& error_callback) override { |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 178 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 179 | cros_disks::kEnumerateMountEntries); |
| 180 | proxy_->CallMethod( |
| 181 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
Hidehiko Abe | d62ed3e | 2017-09-05 05:16:56 | [diff] [blame] | 182 | base::BindOnce(&CrosDisksClientImpl::OnEnumerateMountEntries, |
| 183 | weak_ptr_factory_.GetWeakPtr(), callback, |
| 184 | error_callback)); |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 188 | void Format(const std::string& device_path, |
| 189 | const std::string& filesystem, |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 190 | VoidDBusMethodCallback callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 191 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 192 | cros_disks::kFormat); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 193 | dbus::MessageWriter writer(&method_call); |
| 194 | writer.AppendString(device_path); |
| 195 | writer.AppendString(filesystem); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 196 | // No format option is currently specified, but we can later use this |
| 197 | // argument to specify options for the format operation. |
| 198 | std::vector<std::string> format_options; |
| 199 | writer.AppendArrayOfStrings(format_options); |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 200 | proxy_->CallMethod( |
| 201 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 202 | base::BindOnce(&CrosDisksClientImpl::OnVoidMethod, |
| 203 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 204 | } |
| 205 | |
Klemen Kozjek | bf5610f | 2017-08-25 20:20:09 | [diff] [blame] | 206 | void Rename(const std::string& device_path, |
| 207 | const std::string& volume_name, |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 208 | VoidDBusMethodCallback callback) override { |
Klemen Kozjek | bf5610f | 2017-08-25 20:20:09 | [diff] [blame] | 209 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 210 | cros_disks::kRename); |
| 211 | dbus::MessageWriter writer(&method_call); |
| 212 | writer.AppendString(device_path); |
| 213 | writer.AppendString(volume_name); |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 214 | proxy_->CallMethod( |
| 215 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 216 | base::BindOnce(&CrosDisksClientImpl::OnVoidMethod, |
| 217 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
Klemen Kozjek | bf5610f | 2017-08-25 20:20:09 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 220 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 221 | void GetDeviceProperties(const std::string& device_path, |
| 222 | const GetDevicePropertiesCallback& callback, |
| 223 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 224 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 225 | cros_disks::kGetDeviceProperties); |
| 226 | dbus::MessageWriter writer(&method_call); |
| 227 | writer.AppendString(device_path); |
Hidehiko Abe | d62ed3e | 2017-09-05 05:16:56 | [diff] [blame] | 228 | proxy_->CallMethod( |
| 229 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 230 | base::BindOnce(&CrosDisksClientImpl::OnGetDeviceProperties, |
| 231 | weak_ptr_factory_.GetWeakPtr(), device_path, callback, |
| 232 | error_callback)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 235 | protected: |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 236 | void Init(dbus::Bus* bus) override { |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 237 | proxy_ = bus->GetObjectProxy( |
| 238 | cros_disks::kCrosDisksServiceName, |
| 239 | dbus::ObjectPath(cros_disks::kCrosDisksServicePath)); |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 240 | |
| 241 | // Register handlers for D-Bus signals. |
| 242 | constexpr SignalEventTuple kSignalEventTuples[] = { |
| 243 | {cros_disks::kDeviceAdded, CROS_DISKS_DEVICE_ADDED}, |
| 244 | {cros_disks::kDeviceScanned, CROS_DISKS_DEVICE_SCANNED}, |
| 245 | {cros_disks::kDeviceRemoved, CROS_DISKS_DEVICE_REMOVED}, |
| 246 | {cros_disks::kDiskAdded, CROS_DISKS_DISK_ADDED}, |
| 247 | {cros_disks::kDiskChanged, CROS_DISKS_DISK_CHANGED}, |
| 248 | {cros_disks::kDiskRemoved, CROS_DISKS_DISK_REMOVED}, |
| 249 | }; |
| 250 | for (const auto& entry : kSignalEventTuples) { |
| 251 | proxy_->ConnectToSignal( |
| 252 | cros_disks::kCrosDisksInterface, entry.signal_name, |
| 253 | base::BindRepeating(&CrosDisksClientImpl::OnMountEvent, |
| 254 | weak_ptr_factory_.GetWeakPtr(), entry.event_type), |
| 255 | base::BindOnce(&CrosDisksClientImpl::OnSignalConnected, |
| 256 | weak_ptr_factory_.GetWeakPtr())); |
| 257 | } |
| 258 | |
| 259 | proxy_->ConnectToSignal( |
| 260 | cros_disks::kCrosDisksInterface, cros_disks::kMountCompleted, |
| 261 | base::BindRepeating(&CrosDisksClientImpl::OnMountCompleted, |
| 262 | weak_ptr_factory_.GetWeakPtr()), |
| 263 | base::BindOnce(&CrosDisksClientImpl::OnSignalConnected, |
| 264 | weak_ptr_factory_.GetWeakPtr())); |
| 265 | |
| 266 | proxy_->ConnectToSignal( |
| 267 | cros_disks::kCrosDisksInterface, cros_disks::kFormatCompleted, |
| 268 | base::BindRepeating(&CrosDisksClientImpl::OnFormatCompleted, |
| 269 | weak_ptr_factory_.GetWeakPtr()), |
| 270 | base::BindOnce(&CrosDisksClientImpl::OnSignalConnected, |
| 271 | weak_ptr_factory_.GetWeakPtr())); |
| 272 | |
| 273 | proxy_->ConnectToSignal( |
| 274 | cros_disks::kCrosDisksInterface, cros_disks::kRenameCompleted, |
| 275 | base::BindRepeating(&CrosDisksClientImpl::OnRenameCompleted, |
| 276 | weak_ptr_factory_.GetWeakPtr()), |
| 277 | base::BindOnce(&CrosDisksClientImpl::OnSignalConnected, |
| 278 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 281 | private: |
| 282 | // A struct to contain a pair of signal name and mount event type. |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 283 | // Used by SetMountEventHandler. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 284 | struct SignalEventTuple { |
| 285 | const char *signal_name; |
| 286 | MountEventType event_type; |
| 287 | }; |
| 288 | |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 289 | // Handles the result of D-Bus method call with no return value. |
| 290 | void OnVoidMethod(VoidDBusMethodCallback callback, dbus::Response* response) { |
| 291 | std::move(callback).Run(response); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 294 | // Handles the result of Unmount and calls |callback| or |error_callback|. |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 295 | void OnUnmount(VoidDBusMethodCallback callback, dbus::Response* response) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 296 | if (!response) { |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 297 | std::move(callback).Run(false); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 298 | return; |
| 299 | } |
[email protected] | ddcb18e | 2013-09-19 07:17:28 | [diff] [blame] | 300 | |
| 301 | // Temporarly allow Unmount method to report failure both by setting dbus |
| 302 | // error (in which case response is not set) and by returning mount error |
| 303 | // different from MOUNT_ERROR_NONE. This is done so we can change Unmount |
| 304 | // method to return mount error (http://crbug.com/288974) without breaking |
| 305 | // Chrome. |
| 306 | // TODO(tbarzic): When Unmount implementation is changed on cros disks side, |
| 307 | // make this fail if reader is not able to read the error code value from |
| 308 | // the response. |
| 309 | dbus::MessageReader reader(response); |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 310 | uint32_t error_code = 0; |
[email protected] | ddcb18e | 2013-09-19 07:17:28 | [diff] [blame] | 311 | if (reader.PopUint32(&error_code) && |
| 312 | static_cast<MountError>(error_code) != MOUNT_ERROR_NONE) { |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 313 | std::move(callback).Run(false); |
[email protected] | ddcb18e | 2013-09-19 07:17:28 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | |
Hidehiko Abe | c293cdcf | 2018-02-08 02:10:45 | [diff] [blame] | 317 | std::move(callback).Run(true); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 318 | } |
| 319 | |
Aga Wronska | ddc1a75 | 2017-12-01 19:44:02 | [diff] [blame] | 320 | // Handles the result of EnumerateDevices and EnumarateAutoMountableDevices. |
| 321 | // Calls |callback| or |error_callback|. |
| 322 | void OnEnumerateDevices(const EnumerateDevicesCallback& callback, |
| 323 | const base::Closure& error_callback, |
| 324 | dbus::Response* response) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 325 | if (!response) { |
| 326 | error_callback.Run(); |
| 327 | return; |
| 328 | } |
| 329 | dbus::MessageReader reader(response); |
| 330 | std::vector<std::string> device_paths; |
| 331 | if (!reader.PopArrayOfStrings(&device_paths)) { |
| 332 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 333 | error_callback.Run(); |
| 334 | return; |
| 335 | } |
| 336 | callback.Run(device_paths); |
| 337 | } |
| 338 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 339 | // Handles the result of EnumerateMountEntries and calls |callback| or |
| 340 | // |error_callback|. |
| 341 | void OnEnumerateMountEntries( |
| 342 | const EnumerateMountEntriesCallback& callback, |
| 343 | const base::Closure& error_callback, |
| 344 | dbus::Response* response) { |
| 345 | if (!response) { |
| 346 | error_callback.Run(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | dbus::MessageReader reader(response); |
| 351 | dbus::MessageReader array_reader(NULL); |
| 352 | if (!reader.PopArray(&array_reader)) { |
| 353 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 354 | error_callback.Run(); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | std::vector<MountEntry> entries; |
| 359 | while (array_reader.HasMoreData()) { |
| 360 | MountEntry entry; |
| 361 | dbus::MessageReader sub_reader(NULL); |
| 362 | if (!array_reader.PopStruct(&sub_reader) || |
| 363 | !ReadMountEntryFromDbus(&sub_reader, &entry)) { |
| 364 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 365 | error_callback.Run(); |
| 366 | return; |
| 367 | } |
| 368 | entries.push_back(entry); |
| 369 | } |
| 370 | callback.Run(entries); |
| 371 | } |
| 372 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 373 | // Handles the result of GetDeviceProperties and calls |callback| or |
| 374 | // |error_callback|. |
| 375 | void OnGetDeviceProperties(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 376 | const GetDevicePropertiesCallback& callback, |
[email protected] | 5624a25 | 2013-07-04 03:17:53 | [diff] [blame] | 377 | const base::Closure& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 378 | dbus::Response* response) { |
| 379 | if (!response) { |
| 380 | error_callback.Run(); |
| 381 | return; |
| 382 | } |
| 383 | DiskInfo disk(device_path, response); |
| 384 | callback.Run(disk); |
| 385 | } |
| 386 | |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 387 | // Handles mount event signals and notifies observers. |
| 388 | void OnMountEvent(MountEventType event_type, dbus::Signal* signal) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 389 | dbus::MessageReader reader(signal); |
| 390 | std::string device; |
| 391 | if (!reader.PopString(&device)) { |
| 392 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 393 | return; |
| 394 | } |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 395 | |
| 396 | for (auto& observer : observer_list_) |
| 397 | observer.OnMountEvent(event_type, device); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 398 | } |
| 399 | |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 400 | // Handles MountCompleted signal and notifies observers. |
| 401 | void OnMountCompleted(dbus::Signal* signal) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 402 | dbus::MessageReader reader(signal); |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 403 | MountEntry entry; |
| 404 | if (!ReadMountEntryFromDbus(&reader, &entry)) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 405 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 406 | return; |
| 407 | } |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 408 | |
| 409 | for (auto& observer : observer_list_) |
| 410 | observer.OnMountCompleted(entry); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 411 | } |
| 412 | |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 413 | // Handles FormatCompleted signal and notifies observers. |
| 414 | void OnFormatCompleted(dbus::Signal* signal) { |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 415 | dbus::MessageReader reader(signal); |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 416 | uint32_t error_code = 0; |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 417 | std::string device_path; |
| 418 | if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) { |
| 419 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 420 | return; |
| 421 | } |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 422 | |
| 423 | for (auto& observer : observer_list_) { |
| 424 | observer.OnFormatCompleted(static_cast<FormatError>(error_code), |
| 425 | device_path); |
| 426 | } |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 427 | } |
| 428 | |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 429 | // Handles RenameCompleted signal and notifies observers. |
| 430 | void OnRenameCompleted(dbus::Signal* signal) { |
Klemen Kozjek | bf5610f | 2017-08-25 20:20:09 | [diff] [blame] | 431 | dbus::MessageReader reader(signal); |
| 432 | uint32_t error_code = 0; |
| 433 | std::string device_path; |
| 434 | if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) { |
| 435 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 436 | return; |
| 437 | } |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 438 | |
| 439 | for (auto& observer : observer_list_) { |
| 440 | observer.OnRenameCompleted(static_cast<RenameError>(error_code), |
| 441 | device_path); |
| 442 | } |
Klemen Kozjek | bf5610f | 2017-08-25 20:20:09 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 445 | // Handles the result of signal connection setup. |
| 446 | void OnSignalConnected(const std::string& interface, |
| 447 | const std::string& signal, |
[email protected] | d6311dcb | 2012-10-22 03:40:43 | [diff] [blame] | 448 | bool succeeded) { |
| 449 | LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 450 | signal << " failed."; |
| 451 | } |
| 452 | |
| 453 | dbus::ObjectProxy* proxy_; |
[email protected] | 926957b | 2012-09-07 05:34:16 | [diff] [blame] | 454 | |
Hidehiko Abe | 06ce6dc | 2017-12-08 19:32:03 | [diff] [blame] | 455 | base::ObserverList<Observer> observer_list_; |
| 456 | |
[email protected] | 926957b | 2012-09-07 05:34:16 | [diff] [blame] | 457 | // Note: This should remain the last member so it'll be destroyed and |
| 458 | // invalidate its weak pointers before any other members are destroyed. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 459 | base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_; |
| 460 | |
| 461 | DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl); |
| 462 | }; |
| 463 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 464 | } // namespace |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 465 | |
| 466 | //////////////////////////////////////////////////////////////////////////////// |
| 467 | // DiskInfo |
| 468 | |
| 469 | DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response) |
| 470 | : device_path_(device_path), |
| 471 | is_drive_(false), |
| 472 | has_media_(false), |
| 473 | on_boot_device_(false), |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 474 | on_removable_device_(false), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 475 | is_read_only_(false), |
Aga Wronska | ddc1a75 | 2017-12-01 19:44:02 | [diff] [blame] | 476 | is_hidden_(true), |
| 477 | is_virtual_(false), |
| 478 | device_type_(DEVICE_TYPE_UNKNOWN), |
| 479 | total_size_in_bytes_(0) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 480 | InitializeFromResponse(response); |
| 481 | } |
| 482 | |
Chris Watkins | 2c529d6 | 2017-11-29 02:14:41 | [diff] [blame] | 483 | DiskInfo::~DiskInfo() = default; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 484 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 485 | // Initializes |this| from |response| given by the cros-disks service. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 486 | // Below is an example of |response|'s raw message (long string is ellipsized). |
| 487 | // |
| 488 | // |
| 489 | // message_type: MESSAGE_METHOD_RETURN |
| 490 | // destination: :1.8 |
| 491 | // sender: :1.16 |
| 492 | // signature: a{sv} |
| 493 | // serial: 96 |
| 494 | // reply_serial: 267 |
| 495 | // |
| 496 | // array [ |
| 497 | // dict entry { |
| 498 | // string "DeviceFile" |
| 499 | // variant string "/dev/sdb" |
| 500 | // } |
| 501 | // dict entry { |
| 502 | // string "DeviceIsDrive" |
| 503 | // variant bool true |
| 504 | // } |
| 505 | // dict entry { |
| 506 | // string "DeviceIsMediaAvailable" |
| 507 | // variant bool true |
| 508 | // } |
| 509 | // dict entry { |
| 510 | // string "DeviceIsMounted" |
| 511 | // variant bool false |
| 512 | // } |
| 513 | // dict entry { |
| 514 | // string "DeviceIsOnBootDevice" |
| 515 | // variant bool false |
| 516 | // } |
| 517 | // dict entry { |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 518 | // string "DeviceIsOnRemovableDevice" |
| 519 | // variant bool true |
| 520 | // } |
| 521 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 522 | // string "DeviceIsReadOnly" |
| 523 | // variant bool false |
| 524 | // } |
| 525 | // dict entry { |
| 526 | // string "DeviceIsVirtual" |
| 527 | // variant bool false |
| 528 | // } |
| 529 | // dict entry { |
| 530 | // string "DeviceMediaType" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 531 | // variant uint32_t 1 |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 532 | // } |
| 533 | // dict entry { |
| 534 | // string "DeviceMountPaths" |
| 535 | // variant array [ |
| 536 | // ] |
| 537 | // } |
| 538 | // dict entry { |
| 539 | // string "DevicePresentationHide" |
| 540 | // variant bool true |
| 541 | // } |
| 542 | // dict entry { |
| 543 | // string "DeviceSize" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 544 | // variant uint64_t 7998537728 |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 545 | // } |
| 546 | // dict entry { |
| 547 | // string "DriveIsRotational" |
| 548 | // variant bool false |
| 549 | // } |
| 550 | // dict entry { |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 551 | // string "VendorId" |
| 552 | // variant string "18d1" |
| 553 | // } |
| 554 | // dict entry { |
| 555 | // string "VendorName" |
| 556 | // variant string "Google Inc." |
| 557 | // } |
| 558 | // dict entry { |
| 559 | // string "ProductId" |
| 560 | // variant string "4e11" |
| 561 | // } |
| 562 | // dict entry { |
| 563 | // string "ProductName" |
| 564 | // variant string "Nexus One" |
| 565 | // } |
| 566 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 567 | // string "DriveModel" |
| 568 | // variant string "TransMemory" |
| 569 | // } |
| 570 | // dict entry { |
| 571 | // string "IdLabel" |
| 572 | // variant string "" |
| 573 | // } |
| 574 | // dict entry { |
| 575 | // string "IdUuid" |
| 576 | // variant string "" |
| 577 | // } |
| 578 | // dict entry { |
| 579 | // string "NativePath" |
| 580 | // variant string "/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-4/... |
| 581 | // } |
Klemen Kozjek | 46f1c619 | 2017-08-25 19:20:54 | [diff] [blame] | 582 | // dict entry { |
| 583 | // string "FileSystemType" |
| 584 | // variant string "vfat" |
| 585 | // } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 586 | // ] |
| 587 | void DiskInfo::InitializeFromResponse(dbus::Response* response) { |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 588 | dbus::MessageReader reader(response); |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 589 | std::unique_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 590 | base::DictionaryValue* properties = NULL; |
| 591 | if (!value || !value->GetAsDictionary(&properties)) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 592 | return; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 593 | |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 594 | properties->GetBooleanWithoutPathExpansion( |
| 595 | cros_disks::kDeviceIsDrive, &is_drive_); |
| 596 | properties->GetBooleanWithoutPathExpansion( |
| 597 | cros_disks::kDeviceIsReadOnly, &is_read_only_); |
| 598 | properties->GetBooleanWithoutPathExpansion( |
| 599 | cros_disks::kDevicePresentationHide, &is_hidden_); |
| 600 | properties->GetBooleanWithoutPathExpansion( |
| 601 | cros_disks::kDeviceIsMediaAvailable, &has_media_); |
| 602 | properties->GetBooleanWithoutPathExpansion( |
| 603 | cros_disks::kDeviceIsOnBootDevice, &on_boot_device_); |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 604 | properties->GetBooleanWithoutPathExpansion( |
| 605 | cros_disks::kDeviceIsOnRemovableDevice, &on_removable_device_); |
Aga Wronska | ddc1a75 | 2017-12-01 19:44:02 | [diff] [blame] | 606 | properties->GetBooleanWithoutPathExpansion(cros_disks::kDeviceIsVirtual, |
| 607 | &is_virtual_); |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 608 | properties->GetStringWithoutPathExpansion( |
| 609 | cros_disks::kNativePath, &system_path_); |
| 610 | properties->GetStringWithoutPathExpansion( |
| 611 | cros_disks::kDeviceFile, &file_path_); |
| 612 | properties->GetStringWithoutPathExpansion(cros_disks::kVendorId, &vendor_id_); |
| 613 | properties->GetStringWithoutPathExpansion( |
| 614 | cros_disks::kVendorName, &vendor_name_); |
| 615 | properties->GetStringWithoutPathExpansion( |
| 616 | cros_disks::kProductId, &product_id_); |
| 617 | properties->GetStringWithoutPathExpansion( |
| 618 | cros_disks::kProductName, &product_name_); |
| 619 | properties->GetStringWithoutPathExpansion( |
| 620 | cros_disks::kDriveModel, &drive_model_); |
| 621 | properties->GetStringWithoutPathExpansion(cros_disks::kIdLabel, &label_); |
| 622 | properties->GetStringWithoutPathExpansion(cros_disks::kIdUuid, &uuid_); |
Klemen Kozjek | 46f1c619 | 2017-08-25 19:20:54 | [diff] [blame] | 623 | properties->GetStringWithoutPathExpansion(cros_disks::kFileSystemType, |
| 624 | &file_system_type_); |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 625 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 626 | // dbus::PopDataAsValue() pops uint64_t as double. |
| 627 | // The top 11 bits of uint64_t are dropped by the use of double. But, this |
| 628 | // works |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 629 | // unless the size exceeds 8 PB. |
| 630 | double device_size_double = 0; |
| 631 | if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceSize, |
| 632 | &device_size_double)) |
| 633 | total_size_in_bytes_ = device_size_double; |
| 634 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 635 | // dbus::PopDataAsValue() pops uint32_t as double. |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 636 | double media_type_double = 0; |
| 637 | if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceMediaType, |
| 638 | &media_type_double)) |
| 639 | device_type_ = DeviceMediaTypeToDeviceType(media_type_double); |
| 640 | |
| 641 | base::ListValue* mount_paths = NULL; |
| 642 | if (properties->GetListWithoutPathExpansion(cros_disks::kDeviceMountPaths, |
| 643 | &mount_paths)) |
| 644 | mount_paths->GetString(0, &mount_path_); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | //////////////////////////////////////////////////////////////////////////////// |
| 648 | // CrosDisksClient |
| 649 | |
Chris Watkins | 2c529d6 | 2017-11-29 02:14:41 | [diff] [blame] | 650 | CrosDisksClient::CrosDisksClient() = default; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 651 | |
Chris Watkins | 2c529d6 | 2017-11-29 02:14:41 | [diff] [blame] | 652 | CrosDisksClient::~CrosDisksClient() = default; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 653 | |
| 654 | // static |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 655 | CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) { |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 656 | if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 657 | return new CrosDisksClientImpl(); |
jamescook | 2b59099 | 2016-09-14 15:28:45 | [diff] [blame] | 658 | DCHECK_EQ(FAKE_DBUS_CLIENT_IMPLEMENTATION, type); |
satorux | 8fd29380 | 2014-10-30 08:23:12 | [diff] [blame] | 659 | return new FakeCrosDisksClient(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 660 | } |
| 661 | |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 662 | // static |
| 663 | base::FilePath CrosDisksClient::GetArchiveMountPoint() { |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 664 | return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 665 | FILE_PATH_LITERAL("/media/archive") : |
| 666 | FILE_PATH_LITERAL("/tmp/chromeos/media/archive")); |
| 667 | } |
| 668 | |
| 669 | // static |
| 670 | base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 671 | return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 672 | FILE_PATH_LITERAL("/media/removable") : |
| 673 | FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); |
| 674 | } |
| 675 | |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 676 | // static |
| 677 | std::vector<std::string> CrosDisksClient::ComposeMountOptions( |
Sergei Datsenko | d1924818 | 2018-05-11 01:52:56 | [diff] [blame^] | 678 | const std::vector<std::string>& options, |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 679 | const std::string& mount_label, |
yamaguchi | fa8efc7 | 2016-10-21 15:05:51 | [diff] [blame] | 680 | MountAccessMode access_mode, |
| 681 | RemountOption remount) { |
Sergei Datsenko | d1924818 | 2018-05-11 01:52:56 | [diff] [blame^] | 682 | std::vector<std::string> mount_options = options; |
| 683 | mount_options.insert(mount_options.end(), kDefaultMountOptions, |
| 684 | kDefaultMountOptions + base::size(kDefaultMountOptions)); |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 685 | switch (access_mode) { |
| 686 | case MOUNT_ACCESS_MODE_READ_ONLY: |
| 687 | mount_options.push_back(kReadOnlyOption); |
| 688 | break; |
| 689 | case MOUNT_ACCESS_MODE_READ_WRITE: |
| 690 | mount_options.push_back(kReadWriteOption); |
| 691 | break; |
| 692 | } |
yamaguchi | fa8efc7 | 2016-10-21 15:05:51 | [diff] [blame] | 693 | if (remount == REMOUNT_OPTION_REMOUNT_EXISTING_DEVICE) { |
| 694 | mount_options.push_back(kRemountOption); |
| 695 | } |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 696 | |
| 697 | if (!mount_label.empty()) { |
| 698 | std::string mount_label_option = |
| 699 | base::StringPrintf("%s=%s", kMountLabelOption, mount_label.c_str()); |
| 700 | mount_options.push_back(mount_label_option); |
| 701 | } |
Sergei Datsenko | d1924818 | 2018-05-11 01:52:56 | [diff] [blame^] | 702 | |
yamaguchi | 585d540 | 2016-08-02 09:27:36 | [diff] [blame] | 703 | return mount_options; |
| 704 | } |
| 705 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 706 | } // namespace chromeos |