1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_AAFWK_STANDARD_TOOLS_AA_INCLUDE_SHELL_COMMAND_EXECUTOR_H 17 #define FOUNDATION_AAFWK_STANDARD_TOOLS_AA_INCLUDE_SHELL_COMMAND_EXECUTOR_H 18 19 #include <condition_variable> 20 #include <memory> 21 #include <mutex> 22 #include "event_handler.h" 23 #include "shell_command_result.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 class ShellCommandExecutor : public std::enable_shared_from_this<ShellCommandExecutor> { 28 public: 29 /** 30 * A constructor used to create a ShellCommandExecutor instance with the input parameter passed. 31 * 32 * @param cmd, Indicates the specified shell command. 33 * @param timeoutSec, Indicates the specified time out time, in seconds. 34 */ 35 ShellCommandExecutor(const std::string &cmd, const int64_t timeoutSec); 36 37 /** 38 * Deconstructor used to deconstruct. 39 */ 40 ~ShellCommandExecutor() = default; 41 42 /** 43 * Waits for the result of the shell command. 44 * 45 * @return the result of the specified shell command. 46 */ 47 ShellCommandResult WaitWorkDone(); 48 49 private: 50 bool DoWork(); 51 52 private: 53 std::string cmd_; 54 int64_t timeoutSec_ {0}; 55 ShellCommandResult cmdResult_; 56 57 std::shared_ptr<AppExecFwk::EventHandler> handler_; 58 std::condition_variable cvWork_; 59 std::mutex mtxWork_; 60 std::mutex mtxSyncWork_; 61 std::mutex mtxCopy_; 62 }; 63 } // namespace AAFwk 64 } // namespace OHOS 65 66 #endif // FOUNDATION_AAFWK_STANDARD_TOOLS_AA_INCLUDE_SHELL_COMMAND_EXECUTOR_H