blob: f75e1ac44cda5428cb630ef2a3da1339b68a3dd6 [file] [log] [blame]
[email protected]2321d282012-01-31 23:06:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4ae73292011-11-15 05:20:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4cbead42012-08-26 12:02:395#include "chromeos/disks/disk_mount_manager.h"
[email protected]a66a23cb2012-06-19 23:15:336
avi6e1a22d2015-12-21 03:43:207#include <stddef.h>
8#include <stdint.h>
9
[email protected]4ae73292011-11-15 05:20:1810#include <set>
11
[email protected]4ae73292011-11-15 05:20:1812#include "base/bind.h"
avi6e1a22d2015-12-21 03:43:2013#include "base/macros.h"
[email protected]c6944272012-01-06 22:12:2814#include "base/memory/weak_ptr.h"
[email protected]3fc40c142011-12-01 13:09:0415#include "base/observer_list.h"
[email protected]2bc706d2012-11-21 15:55:4716#include "base/stl_util.h"
[email protected]afa339d72013-06-11 06:32:5117#include "base/strings/string_util.h"
[email protected]64e199252012-04-06 01:54:3618#include "chromeos/dbus/dbus_thread_manager.h"
hirono9f5eae542015-06-22 04:28:4119#include "chromeos/disks/suspend_unmount_manager.h"
[email protected]4ae73292011-11-15 05:20:1820
21namespace chromeos {
22namespace disks {
23
24namespace {
25
26const char kDeviceNotFound[] = "Device could not be found";
27
28DiskMountManager* g_disk_mount_manager = NULL;
29
30// The DiskMountManager implementation.
31class DiskMountManagerImpl : public DiskMountManager {
32 public:
[email protected]a2e4ee22014-07-11 05:16:3533 DiskMountManagerImpl() :
34 already_refreshed_(false),
35 weak_ptr_factory_(this) {
[email protected]4ae73292011-11-15 05:20:1836 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
[email protected]4ae73292011-11-15 05:20:1837 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient();
hirono9f5eae542015-06-22 04:28:4138 PowerManagerClient* power_manager_client =
39 dbus_thread_manager->GetPowerManagerClient();
40 suspend_unmount_manager_.reset(
41 new SuspendUnmountManager(this, power_manager_client));
[email protected]a0278d52014-05-06 03:36:1542 cros_disks_client_->SetMountEventHandler(
[email protected]4ae73292011-11-15 05:20:1843 base::Bind(&DiskMountManagerImpl::OnMountEvent,
[email protected]a0278d52014-05-06 03:36:1544 weak_ptr_factory_.GetWeakPtr()));
45 cros_disks_client_->SetMountCompletedHandler(
[email protected]4ae73292011-11-15 05:20:1846 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
47 weak_ptr_factory_.GetWeakPtr()));
[email protected]a0278d52014-05-06 03:36:1548 cros_disks_client_->SetFormatCompletedHandler(
49 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
50 weak_ptr_factory_.GetWeakPtr()));
[email protected]4ae73292011-11-15 05:20:1851 }
52
dchengae98daa2015-01-21 20:30:4953 ~DiskMountManagerImpl() override {
[email protected]2bc706d2012-11-21 15:55:4754 STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
[email protected]4ae73292011-11-15 05:20:1855 }
56
57 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4958 void AddObserver(Observer* observer) override {
[email protected]4ae73292011-11-15 05:20:1859 observers_.AddObserver(observer);
60 }
61
62 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4963 void RemoveObserver(Observer* observer) override {
[email protected]4ae73292011-11-15 05:20:1864 observers_.RemoveObserver(observer);
65 }
66
67 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4968 void MountPath(const std::string& source_path,
69 const std::string& source_format,
70 const std::string& mount_label,
yamaguchicc8186b2016-08-08 06:38:5471 MountType type,
72 MountAccessMode access_mode) override {
[email protected]4ae73292011-11-15 05:20:1873 // 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]d7760592014-05-16 07:57:5277 OnMountCompleted(MountEntry(MOUNT_ERROR_INTERNAL, source_path, type,
78 ""));
[email protected]4ae73292011-11-15 05:20:1879 return;
80 }
81 }
[email protected]4ae73292011-11-15 05:20:1882 cros_disks_client_->Mount(
yamaguchicc8186b2016-08-08 06:38:5483 source_path, source_format, mount_label, access_mode,
[email protected]4ae73292011-11-15 05:20:1884 // When succeeds, OnMountCompleted will be called by
85 // "MountCompleted" signal instead.
[email protected]c6944272012-01-06 22:12:2886 base::Bind(&base::DoNothing),
[email protected]4ae73292011-11-15 05:20:1887 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
88 weak_ptr_factory_.GetWeakPtr(),
[email protected]d7760592014-05-16 07:57:5289 MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, "")));
[email protected]4ae73292011-11-15 05:20:1890 }
91
92 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4993 void UnmountPath(const std::string& mount_path,
94 UnmountOptions options,
95 const UnmountPathCallback& callback) override {
[email protected]3af8e1b2012-09-15 20:46:0596 UnmountChildMounts(mount_path);
[email protected]10795ae2012-10-10 07:33:4997 cros_disks_client_->Unmount(mount_path, options,
[email protected]4ae73292011-11-15 05:20:1898 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
[email protected]10795ae2012-10-10 07:33:4999 weak_ptr_factory_.GetWeakPtr(),
[email protected]8f919ee2013-03-14 19:53:29100 callback,
[email protected]ffdcc7a9c2013-07-02 06:59:39101 true,
102 mount_path),
[email protected]10795ae2012-10-10 07:33:49103 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
104 weak_ptr_factory_.GetWeakPtr(),
[email protected]8f919ee2013-03-14 19:53:29105 callback,
[email protected]ffdcc7a9c2013-07-02 06:59:39106 false,
107 mount_path));
[email protected]4ae73292011-11-15 05:20:18108 }
109
110 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49111 void FormatMountedDevice(const std::string& mount_path) override {
[email protected]e3c1fc92012-11-15 00:56:46112 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]f026c0f2014-05-06 21:52:35115 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, mount_path);
[email protected]4ae73292011-11-15 05:20:18116 return;
117 }
[email protected]e3c1fc92012-11-15 00:56:46118
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]f026c0f2014-05-06 21:52:35123 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
[email protected]e3c1fc92012-11-15 00:56:46124 return;
125 }
126
[email protected]8f919ee2013-03-14 19:53:29127 UnmountPath(disk->second->mount_path(),
128 UNMOUNT_OPTIONS_NONE,
129 base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat,
130 weak_ptr_factory_.GetWeakPtr(),
131 device_path));
[email protected]4ae73292011-11-15 05:20:18132 }
133
134 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49135 void UnmountDeviceRecursively(
[email protected]4ae73292011-11-15 05:20:18136 const std::string& device_path,
mostynb4f4cf142014-10-06 13:57:52137 const UnmountDeviceRecursivelyCallbackType& callback) override {
[email protected]4ae73292011-11-15 05:20:18138 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]e008d4fa2013-02-21 06:38:01149
[email protected]4ae73292011-11-15 05:20:18150 // We should detect at least original device.
151 if (devices_to_unmount.empty()) {
152 if (disks_.find(device_path) == disks_.end()) {
[email protected]e008d4fa2013-02-21 06:38:01153 LOG(WARNING) << "Unmount recursive request failed for device "
154 << device_path << ", with error: " << kDeviceNotFound;
155 callback.Run(false);
[email protected]4ae73292011-11-15 05:20:18156 return;
157 }
[email protected]e008d4fa2013-02-21 06:38:01158
159 // Nothing to unmount.
160 callback.Run(true);
161 return;
[email protected]4ae73292011-11-15 05:20:18162 }
[email protected]e008d4fa2013-02-21 06:38:01163
164 // We will send the same callback data object to all Unmount calls and use
[email protected]a2e4ee22014-07-11 05:16:35165 // it to synchronize callbacks.
[email protected]e008d4fa2013-02-21 06:38:01166 // 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]ffdcc7a9c2013-07-02 06:59:39186 weak_ptr_factory_.GetWeakPtr(),
187 cb_data,
188 true,
189 devices_to_unmount[i]),
[email protected]e008d4fa2013-02-21 06:38:01190 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
[email protected]ffdcc7a9c2013-07-02 06:59:39191 weak_ptr_factory_.GetWeakPtr(),
192 cb_data,
193 false,
194 devices_to_unmount[i]));
[email protected]4ae73292011-11-15 05:20:18195 }
196 }
197
198 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49199 void EnsureMountInfoRefreshed(
hironoa4b675d2015-07-29 01:13:37200 const EnsureMountInfoRefreshedCallback& callback,
201 bool force) override {
202 if (!force && already_refreshed_) {
[email protected]a2e4ee22014-07-11 05:16:35203 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]4ae73292011-11-15 05:20:18216 }
217
218 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49219 const DiskMap& disks() const override { return disks_; }
[email protected]4ae73292011-11-15 05:20:18220
[email protected]bcfa0072012-08-07 01:08:57221 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49222 const Disk* FindDiskBySourcePath(
223 const std::string& source_path) const override {
[email protected]bcfa0072012-08-07 01:08:57224 DiskMap::const_iterator disk_it = disks_.find(source_path);
225 return disk_it == disks_.end() ? NULL : disk_it->second;
226 }
[email protected]4ae73292011-11-15 05:20:18227
228 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49229 const MountPointMap& mount_points() const override { return mount_points_; }
[email protected]4ae73292011-11-15 05:20:18230
[email protected]e3c1fc92012-11-15 00:56:46231 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49232 bool AddDiskForTest(Disk* disk) override {
[email protected]e3c1fc92012-11-15 00:56:46233 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.
dchengae98daa2015-01-21 20:30:49244 bool AddMountPointForTest(const MountPointInfo& mount_point) override {
[email protected]e3c1fc92012-11-15 00:56:46245 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]4ae73292011-11-15 05:20:18259 private:
[email protected]e008d4fa2013-02-21 06:38:01260 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]4ae73292011-11-15 05:20:18266 }
[email protected]e008d4fa2013-02-21 06:38:01267
268 const UnmountDeviceRecursivelyCallbackType callback;
269 size_t num_pending_callbacks;
[email protected]4ae73292011-11-15 05:20:18270 };
271
[email protected]3af8e1b2012-09-15 20:46:05272 // 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.
pkasting9022cb42016-02-05 00:08:56277 if (mount_path.back() != '/')
[email protected]3af8e1b2012-09-15 20:46:05278 mount_path += '/';
279
280 for (MountPointMap::iterator it = mount_points_.begin();
281 it != mount_points_.end();
282 ++it) {
brettw95509312015-07-16 23:57:33283 if (base::StartsWith(it->second.source_path, mount_path,
284 base::CompareCase::SENSITIVE)) {
[email protected]8f919ee2013-03-14 19:53:29285 // TODO(tbarzic): Handle the case where this fails.
286 UnmountPath(it->second.mount_path,
287 UNMOUNT_OPTIONS_NONE,
288 UnmountPathCallback());
[email protected]3af8e1b2012-09-15 20:46:05289 }
290 }
291 }
292
[email protected]e008d4fa2013-02-21 06:38:01293 // Callback for UnmountDeviceRecursively.
294 void OnUnmountDeviceRecursively(
295 UnmountDeviceRecursivelyCallbackData* cb_data,
296 bool success,
297 const std::string& mount_path) {
[email protected]4ae73292011-11-15 05:20:18298 if (success) {
299 // Do standard processing for Unmount event.
[email protected]8f919ee2013-03-14 19:53:29300 OnUnmountPath(UnmountPathCallback(), true, mount_path);
[email protected]a0278d52014-05-06 03:36:15301 VLOG(1) << mount_path << " unmounted.";
[email protected]4ae73292011-11-15 05:20:18302 }
303 // This is safe as long as all callbacks are called on the same thread as
[email protected]e008d4fa2013-02-21 06:38:01304 // UnmountDeviceRecursively.
305 cb_data->num_pending_callbacks--;
[email protected]4ae73292011-11-15 05:20:18306
[email protected]e008d4fa2013-02-21 06:38:01307 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]4ae73292011-11-15 05:20:18312 delete cb_data;
313 }
314 }
315
316 // Callback to handle MountCompleted signal and Mount method call failure.
[email protected]d7760592014-05-16 07:57:52317 void OnMountCompleted(const MountEntry& entry) {
[email protected]4ae73292011-11-15 05:20:18318 MountCondition mount_condition = MOUNT_CONDITION_NONE;
[email protected]d7760592014-05-16 07:57:52319 if (entry.mount_type() == MOUNT_TYPE_DEVICE) {
320 if (entry.error_code() == MOUNT_ERROR_UNKNOWN_FILESYSTEM) {
[email protected]4ae73292011-11-15 05:20:18321 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM;
[email protected]a66a23cb2012-06-19 23:15:33322 }
[email protected]d7760592014-05-16 07:57:52323 if (entry.error_code() == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) {
[email protected]4ae73292011-11-15 05:20:18324 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
[email protected]a66a23cb2012-06-19 23:15:33325 }
[email protected]4ae73292011-11-15 05:20:18326 }
[email protected]d7760592014-05-16 07:57:52327 const MountPointInfo mount_info(entry.source_path(),
328 entry.mount_path(),
329 entry.mount_type(),
[email protected]4ae73292011-11-15 05:20:18330 mount_condition);
331
[email protected]d7760592014-05-16 07:57:52332 NotifyMountStatusUpdate(MOUNTING, entry.error_code(), mount_info);
[email protected]4ae73292011-11-15 05:20:18333
334 // If the device is corrupted but it's still possible to format it, it will
335 // be fake mounted.
[email protected]d7760592014-05-16 07:57:52336 if ((entry.error_code() == MOUNT_ERROR_NONE ||
337 mount_info.mount_condition) &&
[email protected]4ae73292011-11-15 05:20:18338 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]d7760592014-05-16 07:57:52342 if ((entry.error_code() == MOUNT_ERROR_NONE ||
343 mount_info.mount_condition) &&
[email protected]4ae73292011-11-15 05:20:18344 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]4ae73292011-11-15 05:20:18355 }
356 }
357
358 // Callback for UnmountPath.
[email protected]8f919ee2013-03-14 19:53:29359 void OnUnmountPath(const UnmountPathCallback& callback,
360 bool success,
361 const std::string& mount_path) {
[email protected]4ae73292011-11-15 05:20:18362 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
[email protected]8f919ee2013-03-14 19:53:29363 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]4ae73292011-11-15 05:20:18368 return;
[email protected]8f919ee2013-03-14 19:53:29369 }
[email protected]e3c1fc92012-11-15 00:56:46370
371 NotifyMountStatusUpdate(
[email protected]10795ae2012-10-10 07:33:49372 UNMOUNTING,
373 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
[email protected]a66a23cb2012-06-19 23:15:33374 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]10795ae2012-10-10 07:33:49378
[email protected]4ae73292011-11-15 05:20:18379 std::string path(mount_points_it->second.source_path);
[email protected]10795ae2012-10-10 07:33:49380 if (success)
381 mount_points_.erase(mount_points_it);
382
[email protected]e3c1fc92012-11-15 00:56:46383 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]4ae73292011-11-15 05:20:18388 }
[email protected]10795ae2012-10-10 07:33:49389
[email protected]8f919ee2013-03-14 19:53:29390 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]f026c0f2014-05-06 21:52:35400 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
[email protected]4ae73292011-11-15 05:20:18401 }
402 }
403
[email protected]e3c1fc92012-11-15 00:56:46404 // 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]f026c0f2014-05-06 21:52:35410 cros_disks_client_->Format(
[email protected]e3c1fc92012-11-15 00:56:46411 device_path,
412 kFormatVFAT,
[email protected]f026c0f2014-05-06 21:52:35413 base::Bind(&DiskMountManagerImpl::OnFormatStarted,
[email protected]15a2c282013-07-03 08:39:49414 weak_ptr_factory_.GetWeakPtr(),
415 device_path),
[email protected]f026c0f2014-05-06 21:52:35416 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
[email protected]e3c1fc92012-11-15 00:56:46417 weak_ptr_factory_.GetWeakPtr(),
[email protected]f026c0f2014-05-06 21:52:35418 FORMAT_ERROR_UNKNOWN,
419 device_path));
[email protected]e3c1fc92012-11-15 00:56:46420 }
421
[email protected]f026c0f2014-05-06 21:52:35422 // Callback for Format.
423 void OnFormatStarted(const std::string& device_path) {
424 NotifyFormatStatusUpdate(FORMAT_STARTED, FORMAT_ERROR_NONE, device_path);
[email protected]4ae73292011-11-15 05:20:18425 }
426
[email protected]a0278d52014-05-06 03:36:15427 // Callback to handle FormatCompleted signal and Format method call failure.
428 void OnFormatCompleted(FormatError error_code,
[email protected]f026c0f2014-05-06 21:52:35429 const std::string& device_path) {
430 NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, device_path);
[email protected]a0278d52014-05-06 03:36:15431 }
432
[email protected]a2e4ee22014-07-11 05:16:35433 // Callback for GetDeviceProperties.
[email protected]4ae73292011-11-15 05:20:18434 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]202e9fee2012-09-13 20:21:29456 disk_info.vendor_id(),
457 disk_info.vendor_name(),
458 disk_info.product_id(),
459 disk_info.product_name(),
[email protected]9c5620d32012-07-31 01:00:38460 disk_info.uuid(),
[email protected]4ae73292011-11-15 05:20:18461 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]79ed457b2014-07-22 04:07:26468 disk_info.on_removable_device(),
[email protected]4ae73292011-11-15 05:20:18469 disk_info.is_hidden());
470 disks_.insert(std::make_pair(disk_info.device_path(), disk));
[email protected]e3c1fc92012-11-15 00:56:46471 NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk);
[email protected]4ae73292011-11-15 05:20:18472 }
473
[email protected]a2e4ee22014-07-11 05:16:35474 // 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]4ae73292011-11-15 05:20:18478 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) {
479 if (current_device_set.find(iter->first) == current_device_set.end()) {
[email protected]4ae73292011-11-15 05:20:18480 delete iter->second;
481 disks_.erase(iter++);
482 } else {
483 ++iter;
484 }
485 }
[email protected]a2e4ee22014-07-11 05:16:35486 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),
hirono8067bf02015-08-10 03:12:25506 base::Bind(&DiskMountManagerImpl::RefreshDeviceAtIndex,
507 weak_ptr_factory_.GetWeakPtr(), devices, index + 1));
[email protected]a2e4ee22014-07-11 05:16:35508 }
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]4ae73292011-11-15 05:20:18532 }
533
534 // Callback to handle mount event signals.
[email protected]e24f8762011-12-20 00:10:04535 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]4ae73292011-11-15 05:20:18538 switch (event) {
[email protected]e3c1fc92012-11-15 00:56:46539 case CROS_DISKS_DISK_ADDED: {
[email protected]4ae73292011-11-15 05:20:18540 cros_disks_client_->GetDeviceProperties(
541 device_path,
542 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
543 weak_ptr_factory_.GetWeakPtr()),
[email protected]c6944272012-01-06 22:12:28544 base::Bind(&base::DoNothing));
[email protected]e3c1fc92012-11-15 00:56:46545 break;
[email protected]4ae73292011-11-15 05:20:18546 }
[email protected]e3c1fc92012-11-15 00:56:46547 case CROS_DISKS_DISK_REMOVED: {
[email protected]4ae73292011-11-15 05:20:18548 // 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]e3c1fc92012-11-15 00:56:46552 NotifyDiskStatusUpdate(DISK_REMOVED, disk);
[email protected]4ae73292011-11-15 05:20:18553 delete iter->second;
554 disks_.erase(iter);
555 }
[email protected]e3c1fc92012-11-15 00:56:46556 break;
[email protected]4ae73292011-11-15 05:20:18557 }
[email protected]e3c1fc92012-11-15 00:56:46558 case CROS_DISKS_DEVICE_ADDED: {
[email protected]4ae73292011-11-15 05:20:18559 system_path_prefixes_.insert(device_path);
[email protected]e3c1fc92012-11-15 00:56:46560 NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path);
[email protected]4ae73292011-11-15 05:20:18561 break;
562 }
[email protected]e3c1fc92012-11-15 00:56:46563 case CROS_DISKS_DEVICE_REMOVED: {
[email protected]4ae73292011-11-15 05:20:18564 system_path_prefixes_.erase(device_path);
[email protected]e3c1fc92012-11-15 00:56:46565 NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path);
[email protected]4ae73292011-11-15 05:20:18566 break;
567 }
[email protected]e3c1fc92012-11-15 00:56:46568 case CROS_DISKS_DEVICE_SCANNED: {
569 NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path);
[email protected]4ae73292011-11-15 05:20:18570 break;
571 }
[email protected]4ae73292011-11-15 05:20:18572 default: {
573 LOG(ERROR) << "Unknown event: " << event;
[email protected]4ae73292011-11-15 05:20:18574 }
575 }
[email protected]4ae73292011-11-15 05:20:18576 }
577
578 // Notifies all observers about disk status update.
[email protected]e3c1fc92012-11-15 00:56:46579 void NotifyDiskStatusUpdate(DiskEvent event,
[email protected]4ae73292011-11-15 05:20:18580 const Disk* disk) {
[email protected]e3c1fc92012-11-15 00:56:46581 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
[email protected]4ae73292011-11-15 05:20:18582 }
583
584 // Notifies all observers about device status update.
[email protected]e3c1fc92012-11-15 00:56:46585 void NotifyDeviceStatusUpdate(DeviceEvent event,
[email protected]4ae73292011-11-15 05:20:18586 const std::string& device_path) {
[email protected]e3c1fc92012-11-15 00:56:46587 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path));
[email protected]4ae73292011-11-15 05:20:18588 }
589
590 // Notifies all observers about mount completion.
[email protected]e3c1fc92012-11-15 00:56:46591 void NotifyMountStatusUpdate(MountEvent event,
592 MountError error_code,
593 const MountPointInfo& mount_info) {
[email protected]4ae73292011-11-15 05:20:18594 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]e3c1fc92012-11-15 00:56:46595 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]4ae73292011-11-15 05:20:18603 }
604
[email protected]4ae73292011-11-15 05:20:18605 // Finds system path prefix from |system_path|.
606 const std::string& FindSystemPathPrefix(const std::string& system_path) {
607 if (system_path.empty())
[email protected]8790210c2013-12-02 05:29:53608 return base::EmptyString();
[email protected]4ae73292011-11-15 05:20:18609 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin();
610 it != system_path_prefixes_.end();
611 ++it) {
612 const std::string& prefix = *it;
brettw95509312015-07-16 23:57:33613 if (base::StartsWith(system_path, prefix, base::CompareCase::SENSITIVE))
[email protected]4ae73292011-11-15 05:20:18614 return prefix;
615 }
[email protected]8790210c2013-12-02 05:29:53616 return base::EmptyString();
[email protected]4ae73292011-11-15 05:20:18617 }
618
[email protected]4ae73292011-11-15 05:20:18619 // Mount event change observers.
brettw236d3172015-06-03 16:31:43620 base::ObserverList<Observer> observers_;
[email protected]4ae73292011-11-15 05:20:18621
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]a2e4ee22014-07-11 05:16:35632 bool already_refreshed_;
633 std::vector<EnsureMountInfoRefreshedCallback> refresh_callbacks_;
634
dcheng0a6e80c2016-04-08 18:37:38635 std::unique_ptr<SuspendUnmountManager> suspend_unmount_manager_;
hirono9f5eae542015-06-22 04:28:41636
[email protected]4ae73292011-11-15 05:20:18637 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_;
638
639 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl);
640};
641
[email protected]a66a23cb2012-06-19 23:15:33642} // namespace
[email protected]4ae73292011-11-15 05:20:18643
644DiskMountManager::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]202e9fee2012-09-13 20:21:29650 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]9c5620d32012-07-31 01:00:38654 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:18655 const std::string& system_path_prefix,
656 DeviceType device_type,
avi6e1a22d2015-12-21 03:43:20657 uint64_t total_size_in_bytes,
[email protected]4ae73292011-11-15 05:20:18658 bool is_parent,
659 bool is_read_only,
660 bool has_media,
661 bool on_boot_device,
[email protected]79ed457b2014-07-22 04:07:26662 bool on_removable_device,
[email protected]4ae73292011-11-15 05:20:18663 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]202e9fee2012-09-13 20:21:29670 vendor_id_(vendor_id),
671 vendor_name_(vendor_name),
672 product_id_(product_id),
673 product_name_(product_name),
[email protected]9c5620d32012-07-31 01:00:38674 fs_uuid_(fs_uuid),
[email protected]4ae73292011-11-15 05:20:18675 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]79ed457b2014-07-22 04:07:26682 on_removable_device_(on_removable_device),
avi6e1a22d2015-12-21 03:43:20683 is_hidden_(is_hidden) {}
[email protected]4ae73292011-11-15 05:20:18684
vmpstr2a0c10402016-04-08 21:28:07685DiskMountManager::Disk::Disk(const Disk& other) = default;
686
[email protected]4ae73292011-11-15 05:20:18687DiskMountManager::Disk::~Disk() {}
688
[email protected]e3c1fc92012-11-15 00:56:46689bool DiskMountManager::AddDiskForTest(Disk* disk) {
690 return false;
691}
692
693bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) {
694 return false;
695}
696
[email protected]4ae73292011-11-15 05:20:18697// static
[email protected]4ae73292011-11-15 05:20:18698std::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]2321d282012-01-31 23:06:59713std::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]4ae73292011-11-15 05:20:18729void DiskMountManager::Initialize() {
[email protected]b307bceb2011-11-17 07:49:55730 if (g_disk_mount_manager) {
731 LOG(WARNING) << "DiskMountManager was already initialized";
732 return;
733 }
[email protected]4ae73292011-11-15 05:20:18734 g_disk_mount_manager = new DiskMountManagerImpl();
[email protected]b307bceb2011-11-17 07:49:55735 VLOG(1) << "DiskMountManager initialized";
736}
737
738// static
739void 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]4ae73292011-11-15 05:20:18747}
748
749// static
750void DiskMountManager::Shutdown() {
[email protected]b307bceb2011-11-17 07:49:55751 if (!g_disk_mount_manager) {
752 LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager";
753 return;
[email protected]4ae73292011-11-15 05:20:18754 }
[email protected]b307bceb2011-11-17 07:49:55755 delete g_disk_mount_manager;
756 g_disk_mount_manager = NULL;
757 VLOG(1) << "DiskMountManager Shutdown completed";
[email protected]4ae73292011-11-15 05:20:18758}
759
760// static
761DiskMountManager* DiskMountManager::GetInstance() {
762 return g_disk_mount_manager;
763}
764
765} // namespace disks
766} // namespace chromeos