Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / chromeos / disks / suspend_unmount_manager.cc
blobdfed1ed0e44d4a6e7638a5d5f6f020b68358e24a
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"
7 #include "base/bind.h"
8 #include "chromeos/disks/disk_mount_manager.h"
10 namespace chromeos {
11 namespace disks {
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())
31 return;
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->mount_path().empty()) {
36 mount_paths.insert(pair.second->mount_path());
39 for (const auto& mount_path : mount_paths) {
40 if (suspend_readiness_callback_.is_null()) {
41 suspend_readiness_callback_ =
42 power_manager_client_->GetSuspendReadinessCallback();
44 disk_mount_manager_->UnmountPath(
45 mount_path, UNMOUNT_OPTIONS_NONE,
46 base::Bind(&SuspendUnmountManager::OnUnmountComplete,
47 weak_ptr_factory_.GetWeakPtr(), mount_path));
48 unmounting_paths_.insert(mount_path);
52 void SuspendUnmountManager::SuspendDone(const base::TimeDelta& sleep_duration) {
53 // SuspendDone can be called before OnUnmountComplete when suspend is
54 // cancelled, or it takes long time to unmount volumes.
55 unmounting_paths_.clear();
56 suspend_readiness_callback_.Reset();
59 void SuspendUnmountManager::OnUnmountComplete(const std::string& mount_path,
60 chromeos::MountError error_code) {
61 // This can happen when unmount completes after suspend done is called.
62 if (unmounting_paths_.erase(mount_path) != 1)
63 return;
64 if (unmounting_paths_.empty() && !suspend_readiness_callback_.is_null()) {
65 suspend_readiness_callback_.Run();
66 suspend_readiness_callback_.Reset();
70 } // namespace chromeos
71 } // namespace disks