1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/disks/suspend_unmount_manager.h"
8 #include "chromeos/disks/disk_mount_manager.h"
14 void OnRefreshCompleted(bool success
) {}
18 SuspendUnmountManager::SuspendUnmountManager(
19 DiskMountManager
* disk_mount_manager
,
20 PowerManagerClient
* power_manager_client
)
21 : disk_mount_manager_(disk_mount_manager
),
22 power_manager_client_(power_manager_client
),
23 weak_ptr_factory_(this) {
24 power_manager_client_
->AddObserver(this);
27 SuspendUnmountManager::~SuspendUnmountManager() {
28 power_manager_client_
->RemoveObserver(this);
29 if (!suspend_readiness_callback_
.is_null())
30 suspend_readiness_callback_
.Run();
33 void SuspendUnmountManager::SuspendImminent() {
34 DCHECK(unmounting_paths_
.empty());
35 if (!unmounting_paths_
.empty())
37 std::set
<std::string
> mount_paths
;
38 for (const auto& pair
: disk_mount_manager_
->disks()) {
39 if ((pair
.second
->device_type() == DEVICE_TYPE_USB
||
40 pair
.second
->device_type() == DEVICE_TYPE_SD
) &&
41 !pair
.second
->mount_path().empty()) {
42 mount_paths
.insert(pair
.second
->mount_path());
45 for (const auto& mount_path
: mount_paths
) {
46 if (suspend_readiness_callback_
.is_null()) {
47 suspend_readiness_callback_
=
48 power_manager_client_
->GetSuspendReadinessCallback();
50 disk_mount_manager_
->UnmountPath(
51 mount_path
, UNMOUNT_OPTIONS_NONE
,
52 base::Bind(&SuspendUnmountManager::OnUnmountComplete
,
53 weak_ptr_factory_
.GetWeakPtr(), mount_path
));
54 unmounting_paths_
.insert(mount_path
);
58 void SuspendUnmountManager::SuspendDone(const base::TimeDelta
& sleep_duration
) {
59 // SuspendDone can be called before OnUnmountComplete when suspend is
60 // cancelled, or it takes long time to unmount volumes.
61 unmounting_paths_
.clear();
62 disk_mount_manager_
->EnsureMountInfoRefreshed(base::Bind(&OnRefreshCompleted
),
64 suspend_readiness_callback_
.Reset();
67 void SuspendUnmountManager::OnUnmountComplete(const std::string
& mount_path
,
68 chromeos::MountError error_code
) {
69 // This can happen when unmount completes after suspend done is called.
70 if (unmounting_paths_
.erase(mount_path
) != 1)
72 if (unmounting_paths_
.empty() && !suspend_readiness_callback_
.is_null()) {
73 suspend_readiness_callback_
.Run();
74 suspend_readiness_callback_
.Reset();
79 } // namespace chromeos