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