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