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