blob: 4c7cf796ac9d83056f57bdb6671c1903f53666cc [file] [log] [blame]
Szymon Sidor2733b512011-06-30 18:00:51 -07001// 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 Tankiange1a317c2019-06-12 18:02:20 +100010#include <vector>
Szymon Sidor2733b512011-06-30 18:00:51 -070011
Ben Chan445852f2017-10-02 23:00:16 -070012#include <base/memory/weak_ptr.h>
Simon Glass2b1da092020-05-21 12:24:16 -060013#include <brillo/process/process_reaper.h>
Ben Chanc1e766c2011-11-21 12:56:59 -080014#include <chromeos/dbus/service_constants.h>
15#include <gtest/gtest_prod.h>
Szymon Sidor2733b512011-06-30 18:00:51 -070016
Ben Chan85272ef2017-11-17 18:35:22 -080017#include "cros-disks/sandboxed_process.h"
Szymon Sidor2733b512011-06-30 18:00:51 -070018
19namespace cros_disks {
20
Ben Chanc1e766c2011-11-21 12:56:59 -080021class FormatManagerObserverInterface;
Szymon Sidor2733b512011-06-30 18:00:51 -070022
23class FormatManager {
24 public:
Ben Chan445852f2017-10-02 23:00:16 -070025 explicit FormatManager(brillo::ProcessReaper* process_reaper);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090026 FormatManager(const FormatManager&) = delete;
27 FormatManager& operator=(const FormatManager&) = delete;
28
Ben Chanc1e766c2011-11-21 12:56:59 -080029 ~FormatManager();
Szymon Sidor2733b512011-06-30 18:00:51 -070030
31 // Starts a formatting process of a given device.
Ben Chanc1e766c2011-11-21 12:56:59 -080032 FormatErrorType StartFormatting(const std::string& device_path,
Toni Barzic6b5ac3a2014-05-05 11:03:06 -070033 const std::string& device_file,
Austin Tankiange1a317c2019-06-12 18:02:20 +100034 const std::string& filesystem,
35 const std::vector<std::string>& options);
Szymon Sidor2733b512011-06-30 18:00:51 -070036
Ben Chanc1e766c2011-11-21 12:56:59 -080037 void set_observer(FormatManagerObserverInterface* observer) {
38 observer_ = observer;
39 }
Szymon Sidor2733b512011-06-30 18:00:51 -070040
41 private:
Ben Chan445852f2017-10-02 23:00:16 -070042 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 Sidor2733b512011-06-30 18:00:51 -070048 // 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 Chan445852f2017-10-02 23:00:16 -070056 brillo::ProcessReaper* process_reaper_;
57
Szymon Sidor2733b512011-06-30 18:00:51 -070058 // A list of outstanding formatting processes indexed by device path.
Ben Chan85272ef2017-11-17 18:35:22 -080059 std::map<std::string, SandboxedProcess> format_process_;
Szymon Sidor2733b512011-06-30 18:00:51 -070060
Ben Chanc1e766c2011-11-21 12:56:59 -080061 FormatManagerObserverInterface* observer_;
62
Ben Chan445852f2017-10-02 23:00:16 -070063 base::WeakPtrFactory<FormatManager> weak_ptr_factory_;
Szymon Sidor2733b512011-06-30 18:00:51 -070064};
65
66} // namespace cros_disks
67
68#endif // CROS_DISKS_FORMAT_MANAGER_H_