Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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_FORMAT_MANAGER_H_ |
| 6 | #define CROS_DISKS_FORMAT_MANAGER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
Austin Tankiang | e1a317c | 2019-06-12 18:02:20 +1000 | [diff] [blame] | 10 | #include <vector> |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 11 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 12 | #include <base/memory/weak_ptr.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 13 | #include <brillo/process/process_reaper.h> |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
| 15 | #include <gtest/gtest_prod.h> |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 16 | |
Ben Chan | 85272ef | 2017-11-17 18:35:22 -0800 | [diff] [blame] | 17 | #include "cros-disks/sandboxed_process.h" |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 18 | |
| 19 | namespace cros_disks { |
| 20 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 21 | class FormatManagerObserverInterface; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 22 | |
| 23 | class FormatManager { |
| 24 | public: |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 25 | explicit FormatManager(brillo::ProcessReaper* process_reaper); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 26 | FormatManager(const FormatManager&) = delete; |
| 27 | FormatManager& operator=(const FormatManager&) = delete; |
| 28 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 29 | ~FormatManager(); |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 30 | |
| 31 | // Starts a formatting process of a given device. |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 32 | FormatErrorType StartFormatting(const std::string& device_path, |
Toni Barzic | 6b5ac3a | 2014-05-05 11:03:06 -0700 | [diff] [blame] | 33 | const std::string& device_file, |
Austin Tankiang | e1a317c | 2019-06-12 18:02:20 +1000 | [diff] [blame] | 34 | const std::string& filesystem, |
| 35 | const std::vector<std::string>& options); |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 36 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 37 | void set_observer(FormatManagerObserverInterface* observer) { |
| 38 | observer_ = observer; |
| 39 | } |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 40 | |
| 41 | private: |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 42 | FRIEND_TEST(FormatManagerTest, GetFormatProgramPath); |
| 43 | FRIEND_TEST(FormatManagerTest, IsFilesystemSupported); |
| 44 | |
| 45 | void OnFormatProcessTerminated(const std::string& device_path, |
| 46 | const siginfo_t& info); |
| 47 | |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 48 | // Returns the full path of an external formatting program if it is |
| 49 | // found in some predefined locations. Otherwise, an empty string is |
| 50 | // returned. |
| 51 | std::string GetFormatProgramPath(const std::string& filesystem) const; |
| 52 | |
| 53 | // Returns true if formatting a given file system is supported. |
| 54 | bool IsFilesystemSupported(const std::string& filesystem) const; |
| 55 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 56 | brillo::ProcessReaper* process_reaper_; |
| 57 | |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 58 | // A list of outstanding formatting processes indexed by device path. |
Ben Chan | 85272ef | 2017-11-17 18:35:22 -0800 | [diff] [blame] | 59 | std::map<std::string, SandboxedProcess> format_process_; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 60 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 61 | FormatManagerObserverInterface* observer_; |
| 62 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 63 | base::WeakPtrFactory<FormatManager> weak_ptr_factory_; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace cros_disks |
| 67 | |
| 68 | #endif // CROS_DISKS_FORMAT_MANAGER_H_ |