Joel Fernandes | 8632adc | 2021-02-16 19:42:58 -0500 | [diff] [blame] | 1 | // Copyright 2021 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 DEBUGD_SRC_KERNEL_FEATURE_TOOL_H_ |
| 6 | #define DEBUGD_SRC_KERNEL_FEATURE_TOOL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/macros.h> |
| 11 | #include <base/values.h> |
| 12 | #include <brillo/errors/error.h> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace debugd { |
| 16 | class FeatureCommand { |
| 17 | public: |
| 18 | FeatureCommand(const std::string& name) : name_(name) {} |
| 19 | FeatureCommand(FeatureCommand&& other) = default; |
| 20 | // virtual destructor is required because we create a unique pointer |
| 21 | // of an abstract class. See KernelFeature class definition. |
| 22 | virtual ~FeatureCommand() = default; |
| 23 | |
| 24 | std::string name() { return name_; } |
| 25 | virtual bool Execute() = 0; |
| 26 | |
| 27 | private: |
| 28 | std::string name_; |
| 29 | }; |
| 30 | |
| 31 | class WriteFileCommand : public FeatureCommand { |
| 32 | public: |
| 33 | WriteFileCommand(const std::string& file_name, const std::string& value); |
| 34 | WriteFileCommand(WriteFileCommand&& other) = default; |
| 35 | bool Execute() override; |
| 36 | |
| 37 | private: |
| 38 | std::string file_name_; |
| 39 | std::string value_; |
| 40 | }; |
| 41 | |
| 42 | class FileExistsCommand : public FeatureCommand { |
| 43 | public: |
| 44 | FileExistsCommand(const std::string& file_name); |
| 45 | FileExistsCommand(FileExistsCommand&& other) = default; |
| 46 | bool Execute() override; |
| 47 | |
| 48 | private: |
| 49 | std::string file_name_; |
| 50 | }; |
| 51 | |
| 52 | class AlwaysSupportedCommand : public FeatureCommand { |
| 53 | public: |
| 54 | AlwaysSupportedCommand() : FeatureCommand("AlwaysSupported") {} |
| 55 | AlwaysSupportedCommand(AlwaysSupportedCommand&& other) = default; |
| 56 | bool Execute() override { return true; } |
| 57 | }; |
| 58 | |
| 59 | class KernelFeature { |
| 60 | public: |
| 61 | KernelFeature() = default; |
| 62 | KernelFeature(KernelFeature&& other) = default; |
| 63 | KernelFeature(const KernelFeature& other) = delete; |
| 64 | KernelFeature& operator=(const KernelFeature& other) = delete; |
| 65 | |
| 66 | std::string name() { return name_; } |
| 67 | void SetName(std::string name) { name_ = name; } |
| 68 | |
| 69 | // Check if feature is supported on the device |
| 70 | bool IsSupported() const; |
| 71 | |
| 72 | // Execute a sequence of commands to enable a feature |
| 73 | bool Execute() const; |
| 74 | |
| 75 | // Used by the parser to add commands to a feature |
| 76 | void AddCmd(std::unique_ptr<FeatureCommand> cmd); |
| 77 | void AddQueryCmd(std::unique_ptr<FeatureCommand> cmd); |
| 78 | |
| 79 | private: |
| 80 | std::vector<std::unique_ptr<FeatureCommand>> exec_cmds_; |
| 81 | std::vector<std::unique_ptr<FeatureCommand>> support_check_cmds_; |
| 82 | std::string name_; |
| 83 | }; |
| 84 | |
| 85 | class FeatureParserBase { |
| 86 | using FeatureMap = std::unordered_map<std::string, KernelFeature>; |
| 87 | |
| 88 | public: |
| 89 | virtual bool ParseFile(const base::FilePath& path, std::string* err_str) = 0; |
| 90 | virtual ~FeatureParserBase() = default; |
| 91 | const FeatureMap* GetFeatureMap() { return &feature_map_; } |
| 92 | |
| 93 | protected: |
| 94 | std::unordered_map<std::string, KernelFeature> feature_map_; |
| 95 | // Parse features only once per object |
| 96 | bool features_parsed_ = false; |
| 97 | }; |
| 98 | |
| 99 | class JsonFeatureParser : public FeatureParserBase { |
| 100 | public: |
| 101 | bool ParseFile(const base::FilePath& path, std::string* err_str) override; |
| 102 | |
| 103 | private: |
| 104 | bool MakeFeatureObject(base::Value* feature_obj, |
| 105 | std::string* err_str, |
| 106 | KernelFeature& kf); |
| 107 | }; |
| 108 | |
| 109 | class KernelFeatureTool { |
| 110 | public: |
| 111 | KernelFeatureTool(); |
| 112 | ~KernelFeatureTool(); |
| 113 | |
| 114 | // Enables a kernel feature |
| 115 | bool KernelFeatureEnable(brillo::ErrorPtr* error, |
| 116 | const std::string& name, |
| 117 | bool* result, |
| 118 | std::string* err_str); |
| 119 | |
| 120 | // Provide a kernel feature list |
| 121 | bool KernelFeatureList(brillo::ErrorPtr* error, |
| 122 | bool* result, |
| 123 | std::string* out_str); |
| 124 | |
| 125 | private: |
| 126 | bool ParseFeatureList(std::string* err_str); |
| 127 | bool GetFeatureList(std::string* csv_list, std::string* err_str); |
| 128 | std::unique_ptr<FeatureParserBase> parser_; |
| 129 | }; |
| 130 | } // namespace debugd |
| 131 | |
| 132 | #endif // DEBUGD_SRC_KERNEL_FEATURE_TOOL_H_ |