[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] | 4cbead4 | 2012-08-26 12:02:39 | [diff] [blame] | 5 | #include "chromeos/disks/disk_mount_manager.h" |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 6 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 10 | #include <set> |
| 11 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 12 | #include "base/bind.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 13 | #include "base/macros.h" |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 14 | #include "base/memory/ptr_util.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | 3fc40c14 | 2011-12-01 13:09:04 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | afa339d7 | 2013-06-11 06:32:51 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 18 | #include "chromeos/dbus/dbus_thread_manager.h" |
hirono | 9f5eae54 | 2015-06-22 04:28:41 | [diff] [blame] | 19 | #include "chromeos/disks/suspend_unmount_manager.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 20 | |
| 21 | namespace chromeos { |
| 22 | namespace disks { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const char kDeviceNotFound[] = "Device could not be found"; |
| 27 | |
| 28 | DiskMountManager* g_disk_mount_manager = NULL; |
| 29 | |
| 30 | // The DiskMountManager implementation. |
| 31 | class DiskMountManagerImpl : public DiskMountManager { |
| 32 | public: |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 33 | DiskMountManagerImpl() : |
| 34 | already_refreshed_(false), |
| 35 | weak_ptr_factory_(this) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 36 | DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 37 | cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient(); |
hirono | 9f5eae54 | 2015-06-22 04:28:41 | [diff] [blame] | 38 | PowerManagerClient* power_manager_client = |
| 39 | dbus_thread_manager->GetPowerManagerClient(); |
| 40 | suspend_unmount_manager_.reset( |
| 41 | new SuspendUnmountManager(this, power_manager_client)); |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 42 | cros_disks_client_->SetMountEventHandler( |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 43 | base::Bind(&DiskMountManagerImpl::OnMountEvent, |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 44 | weak_ptr_factory_.GetWeakPtr())); |
| 45 | cros_disks_client_->SetMountCompletedHandler( |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 46 | base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 47 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 48 | cros_disks_client_->SetFormatCompletedHandler( |
| 49 | base::Bind(&DiskMountManagerImpl::OnFormatCompleted, |
| 50 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 51 | } |
| 52 | |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 53 | ~DiskMountManagerImpl() override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 57 | void AddObserver(Observer* observer) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 58 | observers_.AddObserver(observer); |
| 59 | } |
| 60 | |
| 61 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 62 | void RemoveObserver(Observer* observer) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 63 | observers_.RemoveObserver(observer); |
| 64 | } |
| 65 | |
| 66 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 67 | void MountPath(const std::string& source_path, |
| 68 | const std::string& source_format, |
| 69 | const std::string& mount_label, |
yamaguchi | cc8186b | 2016-08-08 06:38:54 | [diff] [blame] | 70 | MountType type, |
| 71 | MountAccessMode access_mode) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 72 | // Hidden and non-existent devices should not be mounted. |
| 73 | if (type == MOUNT_TYPE_DEVICE) { |
| 74 | DiskMap::const_iterator it = disks_.find(source_path); |
| 75 | if (it == disks_.end() || it->second->is_hidden()) { |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 76 | OnMountCompleted(MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, |
| 77 | "")); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 81 | cros_disks_client_->Mount( |
yamaguchi | cc8186b | 2016-08-08 06:38:54 | [diff] [blame] | 82 | source_path, source_format, mount_label, access_mode, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 83 | // When succeeds, OnMountCompleted will be called by |
| 84 | // "MountCompleted" signal instead. |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 85 | base::Bind(&base::DoNothing), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 86 | base::Bind(&DiskMountManagerImpl::OnMountCompleted, |
| 87 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 88 | MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, ""))); |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 89 | |
| 90 | // Record the access mode option passed to CrosDisks. |
| 91 | // This is needed because CrosDisks service methods doesn't return the info |
| 92 | // via DBus. |
| 93 | access_modes_.insert(std::make_pair(source_path, access_mode)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 97 | void UnmountPath(const std::string& mount_path, |
| 98 | UnmountOptions options, |
| 99 | const UnmountPathCallback& callback) override { |
[email protected] | 3af8e1b | 2012-09-15 20:46:05 | [diff] [blame] | 100 | UnmountChildMounts(mount_path); |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 101 | cros_disks_client_->Unmount(mount_path, options, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 102 | base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 103 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 104 | callback, |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 105 | true, |
| 106 | mount_path), |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 107 | base::Bind(&DiskMountManagerImpl::OnUnmountPath, |
| 108 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 109 | callback, |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 110 | false, |
| 111 | mount_path)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 115 | void FormatMountedDevice(const std::string& mount_path) override { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 116 | MountPointMap::const_iterator mount_point = mount_points_.find(mount_path); |
| 117 | if (mount_point == mount_points_.end()) { |
| 118 | LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found."; |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 119 | OnFormatCompleted(FORMAT_ERROR_UNKNOWN, mount_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 120 | return; |
| 121 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 122 | |
| 123 | std::string device_path = mount_point->second.source_path; |
| 124 | DiskMap::const_iterator disk = disks_.find(device_path); |
| 125 | if (disk == disks_.end()) { |
| 126 | LOG(ERROR) << "Device with path \"" << device_path << "\" not found."; |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 127 | OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 128 | return; |
| 129 | } |
yamaguchi | bffc32eb | 2016-08-17 06:35:39 | [diff] [blame] | 130 | if (disk->second->is_read_only()) { |
| 131 | LOG(ERROR) << "Mount point with path \"" << mount_path |
| 132 | << "\" is read-only."; |
| 133 | OnFormatCompleted(FORMAT_ERROR_DEVICE_NOT_ALLOWED, mount_path); |
| 134 | return; |
| 135 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 136 | |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 137 | UnmountPath(disk->second->mount_path(), |
| 138 | UNMOUNT_OPTIONS_NONE, |
| 139 | base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat, |
| 140 | weak_ptr_factory_.GetWeakPtr(), |
| 141 | device_path)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 145 | void UnmountDeviceRecursively( |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 146 | const std::string& device_path, |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 147 | const UnmountDeviceRecursivelyCallbackType& callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 148 | std::vector<std::string> devices_to_unmount; |
| 149 | |
| 150 | // Get list of all devices to unmount. |
| 151 | int device_path_len = device_path.length(); |
| 152 | for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) { |
| 153 | if (!it->second->mount_path().empty() && |
| 154 | strncmp(device_path.c_str(), it->second->device_path().c_str(), |
| 155 | device_path_len) == 0) { |
| 156 | devices_to_unmount.push_back(it->second->mount_path()); |
| 157 | } |
| 158 | } |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 159 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 160 | // We should detect at least original device. |
| 161 | if (devices_to_unmount.empty()) { |
| 162 | if (disks_.find(device_path) == disks_.end()) { |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 163 | LOG(WARNING) << "Unmount recursive request failed for device " |
| 164 | << device_path << ", with error: " << kDeviceNotFound; |
| 165 | callback.Run(false); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 166 | return; |
| 167 | } |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 168 | |
| 169 | // Nothing to unmount. |
| 170 | callback.Run(true); |
| 171 | return; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 172 | } |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 173 | |
| 174 | // We will send the same callback data object to all Unmount calls and use |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 175 | // it to synchronize callbacks. |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 176 | // Note: this implementation has a potential memory leak issue. For |
| 177 | // example if this instance is destructed before all the callbacks for |
| 178 | // Unmount are invoked, the memory pointed by |cb_data| will be leaked. |
| 179 | // It is because the UnmountDeviceRecursivelyCallbackData keeps how |
| 180 | // many times OnUnmountDeviceRecursively callback is called and when |
| 181 | // all the callbacks are called, |cb_data| will be deleted in the method. |
| 182 | // However destructing the instance before all callback invocations will |
| 183 | // cancel all pending callbacks, so that the |cb_data| would never be |
| 184 | // deleted. |
| 185 | // Fortunately, in the real scenario, the instance will be destructed |
| 186 | // only for ShutDown. So, probably the memory would rarely be leaked. |
| 187 | // TODO(hidehiko): Fix the issue. |
| 188 | UnmountDeviceRecursivelyCallbackData* cb_data = |
| 189 | new UnmountDeviceRecursivelyCallbackData( |
| 190 | callback, devices_to_unmount.size()); |
| 191 | for (size_t i = 0; i < devices_to_unmount.size(); ++i) { |
| 192 | cros_disks_client_->Unmount( |
| 193 | devices_to_unmount[i], |
| 194 | UNMOUNT_OPTIONS_NONE, |
| 195 | base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively, |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 196 | weak_ptr_factory_.GetWeakPtr(), |
| 197 | cb_data, |
| 198 | true, |
| 199 | devices_to_unmount[i]), |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 200 | base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively, |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 201 | weak_ptr_factory_.GetWeakPtr(), |
| 202 | cb_data, |
| 203 | false, |
| 204 | devices_to_unmount[i])); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 209 | void EnsureMountInfoRefreshed( |
hirono | a4b675d | 2015-07-29 01:13:37 | [diff] [blame] | 210 | const EnsureMountInfoRefreshedCallback& callback, |
| 211 | bool force) override { |
| 212 | if (!force && already_refreshed_) { |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 213 | callback.Run(true); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | refresh_callbacks_.push_back(callback); |
| 218 | if (refresh_callbacks_.size() == 1) { |
| 219 | // If there's no in-flight refreshing task, start it. |
| 220 | cros_disks_client_->EnumerateAutoMountableDevices( |
| 221 | base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateDevices, |
| 222 | weak_ptr_factory_.GetWeakPtr()), |
| 223 | base::Bind(&DiskMountManagerImpl::RefreshCompleted, |
| 224 | weak_ptr_factory_.GetWeakPtr(), false)); |
| 225 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 229 | const DiskMap& disks() const override { return disks_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 230 | |
[email protected] | bcfa007 | 2012-08-07 01:08:57 | [diff] [blame] | 231 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 232 | const Disk* FindDiskBySourcePath( |
| 233 | const std::string& source_path) const override { |
[email protected] | bcfa007 | 2012-08-07 01:08:57 | [diff] [blame] | 234 | DiskMap::const_iterator disk_it = disks_.find(source_path); |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 235 | return disk_it == disks_.end() ? NULL : disk_it->second.get(); |
[email protected] | bcfa007 | 2012-08-07 01:08:57 | [diff] [blame] | 236 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 237 | |
| 238 | // DiskMountManager override. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 239 | const MountPointMap& mount_points() const override { return mount_points_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 240 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 241 | // DiskMountManager override. |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 242 | bool AddDiskForTest(std::unique_ptr<Disk> disk) override { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 243 | if (disks_.find(disk->device_path()) != disks_.end()) { |
| 244 | LOG(ERROR) << "Attempt to add a duplicate disk"; |
| 245 | return false; |
| 246 | } |
| 247 | |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 248 | disks_.insert(std::make_pair(disk->device_path(), std::move(disk))); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | // DiskMountManager override. |
| 253 | // Corresponding disk should be added to the manager before this is called. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 254 | bool AddMountPointForTest(const MountPointInfo& mount_point) override { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 255 | if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) { |
| 256 | LOG(ERROR) << "Attempt to add a duplicate mount point"; |
| 257 | return false; |
| 258 | } |
| 259 | if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE && |
| 260 | disks_.find(mount_point.source_path) == disks_.end()) { |
| 261 | LOG(ERROR) << "Device mount points must have a disk entry."; |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point)); |
| 266 | return true; |
| 267 | } |
| 268 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 269 | private: |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 270 | struct UnmountDeviceRecursivelyCallbackData { |
| 271 | UnmountDeviceRecursivelyCallbackData( |
| 272 | const UnmountDeviceRecursivelyCallbackType& in_callback, |
| 273 | int in_num_pending_callbacks) |
| 274 | : callback(in_callback), |
| 275 | num_pending_callbacks(in_num_pending_callbacks) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 276 | } |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 277 | |
| 278 | const UnmountDeviceRecursivelyCallbackType callback; |
| 279 | size_t num_pending_callbacks; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 280 | }; |
| 281 | |
[email protected] | 3af8e1b | 2012-09-15 20:46:05 | [diff] [blame] | 282 | // Unmounts all mount points whose source path is transitively parented by |
| 283 | // |mount_path|. |
| 284 | void UnmountChildMounts(const std::string& mount_path_in) { |
| 285 | std::string mount_path = mount_path_in; |
| 286 | // Let's make sure mount path has trailing slash. |
pkasting | 9022cb4 | 2016-02-05 00:08:56 | [diff] [blame] | 287 | if (mount_path.back() != '/') |
[email protected] | 3af8e1b | 2012-09-15 20:46:05 | [diff] [blame] | 288 | mount_path += '/'; |
| 289 | |
| 290 | for (MountPointMap::iterator it = mount_points_.begin(); |
| 291 | it != mount_points_.end(); |
| 292 | ++it) { |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 293 | if (base::StartsWith(it->second.source_path, mount_path, |
| 294 | base::CompareCase::SENSITIVE)) { |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 295 | // TODO(tbarzic): Handle the case where this fails. |
| 296 | UnmountPath(it->second.mount_path, |
| 297 | UNMOUNT_OPTIONS_NONE, |
| 298 | UnmountPathCallback()); |
[email protected] | 3af8e1b | 2012-09-15 20:46:05 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 303 | // Callback for UnmountDeviceRecursively. |
| 304 | void OnUnmountDeviceRecursively( |
| 305 | UnmountDeviceRecursivelyCallbackData* cb_data, |
| 306 | bool success, |
| 307 | const std::string& mount_path) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 308 | if (success) { |
| 309 | // Do standard processing for Unmount event. |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 310 | OnUnmountPath(UnmountPathCallback(), true, mount_path); |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 311 | VLOG(1) << mount_path << " unmounted."; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 312 | } |
| 313 | // This is safe as long as all callbacks are called on the same thread as |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 314 | // UnmountDeviceRecursively. |
| 315 | cb_data->num_pending_callbacks--; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 316 | |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 317 | if (cb_data->num_pending_callbacks == 0) { |
| 318 | // This code has a problem that the |success| status used here is for the |
| 319 | // last "unmount" callback, but not whether all unmounting is succeeded. |
| 320 | // TODO(hidehiko): Fix the issue. |
| 321 | cb_data->callback.Run(success); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 322 | delete cb_data; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Callback to handle MountCompleted signal and Mount method call failure. |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 327 | void OnMountCompleted(const MountEntry& entry) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 328 | MountCondition mount_condition = MOUNT_CONDITION_NONE; |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 329 | if (entry.mount_type() == MOUNT_TYPE_DEVICE) { |
| 330 | if (entry.error_code() == MOUNT_ERROR_UNKNOWN_FILESYSTEM) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 331 | mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM; |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 332 | } |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 333 | if (entry.error_code() == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 334 | mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 335 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 336 | } |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 337 | const MountPointInfo mount_info(entry.source_path(), |
| 338 | entry.mount_path(), |
| 339 | entry.mount_type(), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 340 | mount_condition); |
| 341 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 342 | // If the device is corrupted but it's still possible to format it, it will |
| 343 | // be fake mounted. |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 344 | if ((entry.error_code() == MOUNT_ERROR_NONE || |
| 345 | mount_info.mount_condition) && |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 346 | mount_points_.find(mount_info.mount_path) == mount_points_.end()) { |
| 347 | mount_points_.insert(MountPointMap::value_type(mount_info.mount_path, |
| 348 | mount_info)); |
| 349 | } |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 350 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 351 | if ((entry.error_code() == MOUNT_ERROR_NONE || |
| 352 | mount_info.mount_condition) && |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 353 | mount_info.mount_type == MOUNT_TYPE_DEVICE && |
| 354 | !mount_info.source_path.empty() && |
| 355 | !mount_info.mount_path.empty()) { |
| 356 | DiskMap::iterator iter = disks_.find(mount_info.source_path); |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 357 | if (iter != disks_.end()) { // disk might have been removed by now? |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 358 | Disk* disk = iter->second.get(); |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 359 | DCHECK(disk); |
| 360 | // The is_read_only field in *disk may be incorrect when this is called |
| 361 | // from CrosDisksClientImpl::OnMountCompleted. |
yamaguchi | 21448d5b | 2016-09-06 02:04:56 | [diff] [blame] | 362 | // The disk should be treated as read-only when: |
| 363 | // - the read-only option was passed when issuing mount command |
| 364 | // - or the device hardware is read-only. |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 365 | // |source_path| should be same as |disk->device_path| because |
| 366 | // |VolumeManager::OnDiskEvent()| passes the latter to cros-disks as a |
| 367 | // source path when mounting a device. |
| 368 | AccessModeMap::iterator it = access_modes_.find(entry.source_path()); |
yamaguchi | 21448d5b | 2016-09-06 02:04:56 | [diff] [blame] | 369 | if (it != access_modes_.end() && |
| 370 | it->second == chromeos::MOUNT_ACCESS_MODE_READ_ONLY) { |
| 371 | disk->set_read_only(true); |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 372 | } |
| 373 | disk->set_mount_path(mount_info.mount_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 374 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 375 | } |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 376 | // Observers may read the values of disks_. So notify them after tweaking |
| 377 | // values of disks_. |
| 378 | NotifyMountStatusUpdate(MOUNTING, entry.error_code(), mount_info); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Callback for UnmountPath. |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 382 | void OnUnmountPath(const UnmountPathCallback& callback, |
| 383 | bool success, |
| 384 | const std::string& mount_path) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 385 | MountPointMap::iterator mount_points_it = mount_points_.find(mount_path); |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 386 | if (mount_points_it == mount_points_.end()) { |
| 387 | // The path was unmounted, but not as a result of this unmount request, |
| 388 | // so return error. |
| 389 | if (!callback.is_null()) |
| 390 | callback.Run(MOUNT_ERROR_INTERNAL); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 391 | return; |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 392 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 393 | |
| 394 | NotifyMountStatusUpdate( |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 395 | UNMOUNTING, |
| 396 | success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL, |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 397 | MountPointInfo(mount_points_it->second.source_path, |
| 398 | mount_points_it->second.mount_path, |
| 399 | mount_points_it->second.mount_type, |
| 400 | mount_points_it->second.mount_condition)); |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 401 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 402 | std::string path(mount_points_it->second.source_path); |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 403 | if (success) |
| 404 | mount_points_.erase(mount_points_it); |
| 405 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 406 | DiskMap::iterator disk_iter = disks_.find(path); |
| 407 | if (disk_iter != disks_.end()) { |
| 408 | DCHECK(disk_iter->second); |
| 409 | if (success) |
| 410 | disk_iter->second->clear_mount_path(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 411 | } |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 412 | |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 413 | if (!callback.is_null()) |
| 414 | callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL); |
| 415 | } |
| 416 | |
| 417 | void OnUnmountPathForFormat(const std::string& device_path, |
| 418 | MountError error_code) { |
| 419 | if (error_code == MOUNT_ERROR_NONE && |
| 420 | disks_.find(device_path) != disks_.end()) { |
| 421 | FormatUnmountedDevice(device_path); |
| 422 | } else { |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 423 | OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 427 | // Starts device formatting. |
| 428 | void FormatUnmountedDevice(const std::string& device_path) { |
| 429 | DiskMap::const_iterator disk = disks_.find(device_path); |
| 430 | DCHECK(disk != disks_.end() && disk->second->mount_path().empty()); |
| 431 | |
| 432 | const char kFormatVFAT[] = "vfat"; |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 433 | cros_disks_client_->Format( |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 434 | device_path, |
| 435 | kFormatVFAT, |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 436 | base::Bind(&DiskMountManagerImpl::OnFormatStarted, |
[email protected] | 15a2c28 | 2013-07-03 08:39:49 | [diff] [blame] | 437 | weak_ptr_factory_.GetWeakPtr(), |
| 438 | device_path), |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 439 | base::Bind(&DiskMountManagerImpl::OnFormatCompleted, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 440 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 441 | FORMAT_ERROR_UNKNOWN, |
| 442 | device_path)); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 445 | // Callback for Format. |
| 446 | void OnFormatStarted(const std::string& device_path) { |
| 447 | NotifyFormatStatusUpdate(FORMAT_STARTED, FORMAT_ERROR_NONE, device_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 450 | // Callback to handle FormatCompleted signal and Format method call failure. |
| 451 | void OnFormatCompleted(FormatError error_code, |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 452 | const std::string& device_path) { |
| 453 | NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, device_path); |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 456 | // Callback for GetDeviceProperties. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 457 | void OnGetDeviceProperties(const DiskInfo& disk_info) { |
| 458 | // TODO(zelidrag): Find a better way to filter these out before we |
| 459 | // fetch the properties: |
| 460 | // Ignore disks coming from the device we booted the system from. |
| 461 | if (disk_info.on_boot_device()) |
| 462 | return; |
| 463 | |
| 464 | LOG(WARNING) << "Found disk " << disk_info.device_path(); |
| 465 | // Delete previous disk info for this path: |
| 466 | bool is_new = true; |
| 467 | DiskMap::iterator iter = disks_.find(disk_info.device_path()); |
| 468 | if (iter != disks_.end()) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 469 | disks_.erase(iter); |
| 470 | is_new = false; |
| 471 | } |
| 472 | Disk* disk = new Disk(disk_info.device_path(), |
| 473 | disk_info.mount_path(), |
| 474 | disk_info.system_path(), |
| 475 | disk_info.file_path(), |
| 476 | disk_info.label(), |
| 477 | disk_info.drive_label(), |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 478 | disk_info.vendor_id(), |
| 479 | disk_info.vendor_name(), |
| 480 | disk_info.product_id(), |
| 481 | disk_info.product_name(), |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 482 | disk_info.uuid(), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 483 | FindSystemPathPrefix(disk_info.system_path()), |
| 484 | disk_info.device_type(), |
| 485 | disk_info.total_size_in_bytes(), |
| 486 | disk_info.is_drive(), |
| 487 | disk_info.is_read_only(), |
| 488 | disk_info.has_media(), |
| 489 | disk_info.on_boot_device(), |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 490 | disk_info.on_removable_device(), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 491 | disk_info.is_hidden()); |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 492 | // If the device was mounted by the instance, apply recorded parameter. |
| 493 | // Lookup by |device_path| which we pass to cros-disks when mounting a |
| 494 | // device in |VolumeManager::OnDiskEvent()|. |
| 495 | AccessModeMap::iterator access_mode = |
| 496 | access_modes_.find(disk->device_path()); |
| 497 | if (access_mode != access_modes_.end()) { |
| 498 | disk->set_read_only(access_mode->second == |
| 499 | chromeos::MOUNT_ACCESS_MODE_READ_ONLY); |
| 500 | } |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 501 | disks_.insert( |
| 502 | std::make_pair(disk_info.device_path(), base::WrapUnique(disk))); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 503 | NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 506 | // Part of EnsureMountInfoRefreshed(). Called after the list of devices are |
| 507 | // enumerated. |
| 508 | void RefreshAfterEnumerateDevices(const std::vector<std::string>& devices) { |
| 509 | std::set<std::string> current_device_set(devices.begin(), devices.end()); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 510 | for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) { |
| 511 | if (current_device_set.find(iter->first) == current_device_set.end()) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 512 | disks_.erase(iter++); |
| 513 | } else { |
| 514 | ++iter; |
| 515 | } |
| 516 | } |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 517 | RefreshDeviceAtIndex(devices, 0); |
| 518 | } |
| 519 | |
| 520 | // Part of EnsureMountInfoRefreshed(). Called for each device to refresh info. |
| 521 | void RefreshDeviceAtIndex(const std::vector<std::string>& devices, |
| 522 | size_t index) { |
| 523 | if (index == devices.size()) { |
| 524 | // All devices info retrieved. Proceed to enumerate mount point info. |
| 525 | cros_disks_client_->EnumerateMountEntries( |
| 526 | base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateMountEntries, |
| 527 | weak_ptr_factory_.GetWeakPtr()), |
| 528 | base::Bind(&DiskMountManagerImpl::RefreshCompleted, |
| 529 | weak_ptr_factory_.GetWeakPtr(), false)); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | cros_disks_client_->GetDeviceProperties( |
| 534 | devices[index], |
| 535 | base::Bind(&DiskMountManagerImpl::RefreshAfterGetDeviceProperties, |
| 536 | weak_ptr_factory_.GetWeakPtr(), devices, index + 1), |
hirono | 8067bf0 | 2015-08-10 03:12:25 | [diff] [blame] | 537 | base::Bind(&DiskMountManagerImpl::RefreshDeviceAtIndex, |
| 538 | weak_ptr_factory_.GetWeakPtr(), devices, index + 1)); |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // Part of EnsureMountInfoRefreshed(). |
| 542 | void RefreshAfterGetDeviceProperties(const std::vector<std::string>& devices, |
| 543 | size_t next_index, |
| 544 | const DiskInfo& disk_info) { |
| 545 | OnGetDeviceProperties(disk_info); |
| 546 | RefreshDeviceAtIndex(devices, next_index); |
| 547 | } |
| 548 | |
| 549 | // Part of EnsureMountInfoRefreshed(). Called after mount entries are listed. |
| 550 | void RefreshAfterEnumerateMountEntries( |
| 551 | const std::vector<MountEntry>& entries) { |
| 552 | for (size_t i = 0; i < entries.size(); ++i) |
| 553 | OnMountCompleted(entries[i]); |
| 554 | RefreshCompleted(true); |
| 555 | } |
| 556 | |
| 557 | // Part of EnsureMountInfoRefreshed(). Called when the refreshing is done. |
| 558 | void RefreshCompleted(bool success) { |
| 559 | already_refreshed_ = true; |
| 560 | for (size_t i = 0; i < refresh_callbacks_.size(); ++i) |
| 561 | refresh_callbacks_[i].Run(success); |
| 562 | refresh_callbacks_.clear(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Callback to handle mount event signals. |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 566 | void OnMountEvent(MountEventType event, const std::string& device_path_arg) { |
| 567 | // Take a copy of the argument so we can modify it below. |
| 568 | std::string device_path = device_path_arg; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 569 | switch (event) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 570 | case CROS_DISKS_DISK_ADDED: { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 571 | cros_disks_client_->GetDeviceProperties( |
| 572 | device_path, |
| 573 | base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties, |
| 574 | weak_ptr_factory_.GetWeakPtr()), |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 575 | base::Bind(&base::DoNothing)); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 576 | break; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 577 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 578 | case CROS_DISKS_DISK_REMOVED: { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 579 | // Search and remove disks that are no longer present. |
| 580 | DiskMountManager::DiskMap::iterator iter = disks_.find(device_path); |
| 581 | if (iter != disks_.end()) { |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 582 | Disk* disk = iter->second.get(); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 583 | NotifyDiskStatusUpdate(DISK_REMOVED, disk); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 584 | disks_.erase(iter); |
| 585 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 586 | break; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 587 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 588 | case CROS_DISKS_DEVICE_ADDED: { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 589 | system_path_prefixes_.insert(device_path); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 590 | NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 591 | break; |
| 592 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 593 | case CROS_DISKS_DEVICE_REMOVED: { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 594 | system_path_prefixes_.erase(device_path); |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 595 | NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 596 | break; |
| 597 | } |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 598 | case CROS_DISKS_DEVICE_SCANNED: { |
| 599 | NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 600 | break; |
| 601 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 602 | default: { |
| 603 | LOG(ERROR) << "Unknown event: " << event; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 604 | } |
| 605 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Notifies all observers about disk status update. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 609 | void NotifyDiskStatusUpdate(DiskEvent event, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 610 | const Disk* disk) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 611 | FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // Notifies all observers about device status update. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 615 | void NotifyDeviceStatusUpdate(DeviceEvent event, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 616 | const std::string& device_path) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 617 | FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Notifies all observers about mount completion. |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 621 | void NotifyMountStatusUpdate(MountEvent event, |
| 622 | MountError error_code, |
| 623 | const MountPointInfo& mount_info) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 624 | FOR_EACH_OBSERVER(Observer, observers_, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 625 | OnMountEvent(event, error_code, mount_info)); |
| 626 | } |
| 627 | |
| 628 | void NotifyFormatStatusUpdate(FormatEvent event, |
| 629 | FormatError error_code, |
| 630 | const std::string& device_path) { |
| 631 | FOR_EACH_OBSERVER(Observer, observers_, |
| 632 | OnFormatEvent(event, error_code, device_path)); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 633 | } |
| 634 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 635 | // Finds system path prefix from |system_path|. |
| 636 | const std::string& FindSystemPathPrefix(const std::string& system_path) { |
| 637 | if (system_path.empty()) |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 638 | return base::EmptyString(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 639 | for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin(); |
| 640 | it != system_path_prefixes_.end(); |
| 641 | ++it) { |
| 642 | const std::string& prefix = *it; |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 643 | if (base::StartsWith(system_path, prefix, base::CompareCase::SENSITIVE)) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 644 | return prefix; |
| 645 | } |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 646 | return base::EmptyString(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 649 | // Mount event change observers. |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 650 | base::ObserverList<Observer> observers_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 651 | |
| 652 | CrosDisksClient* cros_disks_client_; |
| 653 | |
| 654 | // The list of disks found. |
| 655 | DiskMountManager::DiskMap disks_; |
| 656 | |
| 657 | DiskMountManager::MountPointMap mount_points_; |
| 658 | |
| 659 | typedef std::set<std::string> SystemPathPrefixSet; |
| 660 | SystemPathPrefixSet system_path_prefixes_; |
| 661 | |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 662 | bool already_refreshed_; |
| 663 | std::vector<EnsureMountInfoRefreshedCallback> refresh_callbacks_; |
| 664 | |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 665 | std::unique_ptr<SuspendUnmountManager> suspend_unmount_manager_; |
hirono | 9f5eae54 | 2015-06-22 04:28:41 | [diff] [blame] | 666 | |
yamaguchi | 6594bf7e1 | 2016-08-24 22:16:11 | [diff] [blame] | 667 | // Whether the instance attempted to mount a device in read-only mode for |
| 668 | // each source path. |
| 669 | typedef std::map<std::string, chromeos::MountAccessMode> AccessModeMap; |
| 670 | AccessModeMap access_modes_; |
| 671 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 672 | base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_; |
| 673 | |
| 674 | DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl); |
| 675 | }; |
| 676 | |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 677 | } // namespace |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 678 | |
| 679 | DiskMountManager::Disk::Disk(const std::string& device_path, |
| 680 | const std::string& mount_path, |
| 681 | const std::string& system_path, |
| 682 | const std::string& file_path, |
| 683 | const std::string& device_label, |
| 684 | const std::string& drive_label, |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 685 | const std::string& vendor_id, |
| 686 | const std::string& vendor_name, |
| 687 | const std::string& product_id, |
| 688 | const std::string& product_name, |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 689 | const std::string& fs_uuid, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 690 | const std::string& system_path_prefix, |
| 691 | DeviceType device_type, |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 692 | uint64_t total_size_in_bytes, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 693 | bool is_parent, |
| 694 | bool is_read_only, |
| 695 | bool has_media, |
| 696 | bool on_boot_device, |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 697 | bool on_removable_device, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 698 | bool is_hidden) |
| 699 | : device_path_(device_path), |
| 700 | mount_path_(mount_path), |
| 701 | system_path_(system_path), |
| 702 | file_path_(file_path), |
| 703 | device_label_(device_label), |
| 704 | drive_label_(drive_label), |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 705 | vendor_id_(vendor_id), |
| 706 | vendor_name_(vendor_name), |
| 707 | product_id_(product_id), |
| 708 | product_name_(product_name), |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 709 | fs_uuid_(fs_uuid), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 710 | system_path_prefix_(system_path_prefix), |
| 711 | device_type_(device_type), |
| 712 | total_size_in_bytes_(total_size_in_bytes), |
| 713 | is_parent_(is_parent), |
| 714 | is_read_only_(is_read_only), |
| 715 | has_media_(has_media), |
| 716 | on_boot_device_(on_boot_device), |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 717 | on_removable_device_(on_removable_device), |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 718 | is_hidden_(is_hidden) {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 719 | |
vmpstr | 2a0c1040 | 2016-04-08 21:28:07 | [diff] [blame] | 720 | DiskMountManager::Disk::Disk(const Disk& other) = default; |
| 721 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 722 | DiskMountManager::Disk::~Disk() {} |
| 723 | |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame^] | 724 | bool DiskMountManager::AddDiskForTest(std::unique_ptr<Disk> disk) { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | |
| 728 | bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) { |
| 729 | return false; |
| 730 | } |
| 731 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 732 | // static |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 733 | std::string DiskMountManager::MountConditionToString(MountCondition condition) { |
| 734 | switch (condition) { |
| 735 | case MOUNT_CONDITION_NONE: |
| 736 | return ""; |
| 737 | case MOUNT_CONDITION_UNKNOWN_FILESYSTEM: |
| 738 | return "unknown_filesystem"; |
| 739 | case MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM: |
| 740 | return "unsupported_filesystem"; |
| 741 | default: |
| 742 | NOTREACHED(); |
| 743 | } |
| 744 | return ""; |
| 745 | } |
| 746 | |
| 747 | // static |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 748 | std::string DiskMountManager::DeviceTypeToString(DeviceType type) { |
| 749 | switch (type) { |
| 750 | case DEVICE_TYPE_USB: |
| 751 | return "usb"; |
| 752 | case DEVICE_TYPE_SD: |
| 753 | return "sd"; |
| 754 | case DEVICE_TYPE_OPTICAL_DISC: |
| 755 | return "optical"; |
| 756 | case DEVICE_TYPE_MOBILE: |
| 757 | return "mobile"; |
| 758 | default: |
| 759 | return "unknown"; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // static |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 764 | void DiskMountManager::Initialize() { |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 765 | if (g_disk_mount_manager) { |
| 766 | LOG(WARNING) << "DiskMountManager was already initialized"; |
| 767 | return; |
| 768 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 769 | g_disk_mount_manager = new DiskMountManagerImpl(); |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 770 | VLOG(1) << "DiskMountManager initialized"; |
| 771 | } |
| 772 | |
| 773 | // static |
| 774 | void DiskMountManager::InitializeForTesting( |
| 775 | DiskMountManager* disk_mount_manager) { |
| 776 | if (g_disk_mount_manager) { |
| 777 | LOG(WARNING) << "DiskMountManager was already initialized"; |
| 778 | return; |
| 779 | } |
| 780 | g_disk_mount_manager = disk_mount_manager; |
| 781 | VLOG(1) << "DiskMountManager initialized"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // static |
| 785 | void DiskMountManager::Shutdown() { |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 786 | if (!g_disk_mount_manager) { |
| 787 | LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager"; |
| 788 | return; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 789 | } |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 790 | delete g_disk_mount_manager; |
| 791 | g_disk_mount_manager = NULL; |
| 792 | VLOG(1) << "DiskMountManager Shutdown completed"; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | // static |
| 796 | DiskMountManager* DiskMountManager::GetInstance() { |
| 797 | return g_disk_mount_manager; |
| 798 | } |
| 799 | |
| 800 | } // namespace disks |
| 801 | } // namespace chromeos |