Omid Tourzan | 6485632 | 2020-08-28 13:51:03 +1000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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. |
| 4 | |
| 5 | #ifndef CROS_DISKS_PARTITION_MANAGER_H_ |
| 6 | #define CROS_DISKS_PARTITION_MANAGER_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
| 12 | #include <base/bind.h> |
Omid Tourzan | 6485632 | 2020-08-28 13:51:03 +1000 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
| 14 | #include <brillo/process/process_reaper.h> |
| 15 | #include <chromeos/dbus/service_constants.h> |
| 16 | #include <gtest/gtest_prod.h> |
| 17 | |
| 18 | #include "cros-disks/disk_monitor.h" |
| 19 | #include "cros-disks/sandboxed_process.h" |
| 20 | |
| 21 | namespace cros_disks { |
| 22 | |
| 23 | using PartitionCompleteCallback = |
| 24 | base::OnceCallback<void(const base::FilePath&, PartitionErrorType)>; |
| 25 | |
| 26 | class PartitionManager { |
| 27 | public: |
| 28 | PartitionManager(brillo::ProcessReaper* process_reaper, |
| 29 | DiskMonitor* disk_monitor) |
| 30 | : process_reaper_(process_reaper), disk_monitor_(disk_monitor) {} |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 31 | PartitionManager(const PartitionManager&) = delete; |
| 32 | PartitionManager& operator=(const PartitionManager&) = delete; |
Omid Tourzan | 6485632 | 2020-08-28 13:51:03 +1000 | [diff] [blame] | 33 | |
| 34 | virtual ~PartitionManager() = default; |
| 35 | |
| 36 | // Starts a partition process of a given device to partition it into one |
| 37 | // partition. |
| 38 | void StartSinglePartitionFormat(const base::FilePath& device_path, |
| 39 | PartitionCompleteCallback callback); |
| 40 | |
| 41 | protected: |
| 42 | // virtual to be used for testing purpose. |
| 43 | virtual std::unique_ptr<SandboxedProcess> CreateSandboxedProcess() const; |
| 44 | |
| 45 | private: |
| 46 | void OnPartitionProcessTerminated(const base::FilePath& device_path, |
| 47 | PartitionCompleteCallback callback, |
| 48 | const siginfo_t& info); |
| 49 | |
| 50 | brillo::ProcessReaper* process_reaper_; |
| 51 | |
| 52 | // A list of outstanding partitioning processes indexed by device path. |
| 53 | std::set<base::FilePath> partition_process_; |
| 54 | |
| 55 | DiskMonitor* disk_monitor_; |
| 56 | |
| 57 | base::WeakPtrFactory<PartitionManager> weak_ptr_factory_{this}; |
Omid Tourzan | 6485632 | 2020-08-28 13:51:03 +1000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // namespace cros_disks |
| 61 | |
| 62 | #endif // CROS_DISKS_PARTITION_MANAGER_H_ |