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"
13 SuspendUnmountManager::SuspendUnmountManager(
14 DiskMountManager
* disk_mount_manager
,
15 PowerManagerClient
* power_manager_client
)
16 : disk_mount_manager_(disk_mount_manager
),
17 power_manager_client_(power_manager_client
),
18 weak_ptr_factory_(this) {
19 power_manager_client_
->AddObserver(this);
22 SuspendUnmountManager::~SuspendUnmountManager() {
23 power_manager_client_
->RemoveObserver(this);
24 if (!suspend_readiness_callback_
.is_null())
25 suspend_readiness_callback_
.Run();
28 void SuspendUnmountManager::SuspendImminent() {
29 DCHECK(unmounting_paths_
.empty());
30 if (!unmounting_paths_
.empty())
32 std::set
<std::string
> mount_paths
;
33 for (const auto& pair
: disk_mount_manager_
->disks()) {
34 if ((pair
.second
->device_type() == DEVICE_TYPE_USB
||
35 pair
.second
->device_type() == DEVICE_TYPE_SD
) &&
36 !pair
.second
->mount_path().empty()) {
37 mount_paths
.insert(pair
.second
->mount_path());
40 for (const auto& mount_path
: mount_paths
) {
41 if (suspend_readiness_callback_
.is_null()) {
42 suspend_readiness_callback_
=
43 power_manager_client_
->GetSuspendReadinessCallback();
45 disk_mount_manager_
->UnmountPath(
46 mount_path
, UNMOUNT_OPTIONS_NONE
,
47 base::Bind(&SuspendUnmountManager::OnUnmountComplete
,
48 weak_ptr_factory_
.GetWeakPtr(), mount_path
));
49 unmounting_paths_
.insert(mount_path
);
53 void SuspendUnmountManager::SuspendDone(const base::TimeDelta
& sleep_duration
) {
54 // SuspendDone can be called before OnUnmountComplete when suspend is
55 // cancelled, or it takes long time to unmount volumes.
56 unmounting_paths_
.clear();
57 suspend_readiness_callback_
.Reset();
60 void SuspendUnmountManager::OnUnmountComplete(const std::string
& mount_path
,
61 chromeos::MountError error_code
) {
62 // This can happen when unmount completes after suspend done is called.
63 if (unmounting_paths_
.erase(mount_path
) != 1)
65 if (unmounting_paths_
.empty() && !suspend_readiness_callback_
.is_null()) {
66 suspend_readiness_callback_
.Run();
67 suspend_readiness_callback_
.Reset();
71 } // namespace chromeos