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 OHOS_ABILITY_RUNTIME_SHELL_COMMAND_EXECUTOR_H
17 #define OHOS_ABILITY_RUNTIME_SHELL_COMMAND_EXECUTOR_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include "event_handler.h"
24 #include "shell_command_result.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 class ShellCommandExecutor : public std::enable_shared_from_this<ShellCommandExecutor> {
29 public:
30     /**
31      * A constructor used to create a ShellCommandExecutor instance with the input parameter passed.
32      *
33      * @param cmd, Indicates the specified shell command.
34      * @param timeoutSec, Indicates the specified time out time, in seconds.
35      */
36     ShellCommandExecutor(const std::string& cmd, const int64_t timeoutSec);
37 
38     /**
39      * Deconstructor used to deconstruct.
40      */
41     ~ShellCommandExecutor() = default;
42 
43     /**
44      * Waits for the result of the shell command.
45      *
46      * @return the result of the specified shell command.
47      */
48     ShellCommandResult WaitWorkDone();
49 
50 private:
51     bool DoWork();
52     bool CheckCommand();
53 private:
54     std::string cmd_;
55     int64_t timeoutSec_ = 0;
56     ShellCommandResult cmdResult_;
57     bool isDone_ = false;
58 
59     std::shared_ptr<AppExecFwk::EventHandler> handler_;
60     std::condition_variable cvWork_;
61     std::mutex mtxWork_;
62     std::mutex mtxCopy_;
63 };
64 }  // namespace AAFwk
65 }  // namespace OHOS
66 
67 #endif  // OHOS_ABILITY_RUNTIME_SHELL_COMMAND_EXECUTOR_H
68