• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "shell_command_config_loader.h"
17 #include <fstream>
18 #include <sstream>
19 #include <nlohmann/json.hpp>
20 #include "hilog_wrapper.h"
21 
22 using json = nlohmann::json;
23 namespace OHOS {
24 namespace AAFwk {
25 namespace {
26     constexpr const char* AA_TOOL_COMMAND_LIST = "command_list";
27     constexpr static int  COMMANDS_MAX_SIZE = 100;
28 }
29 
30 bool ShellCommandConfigLoder::configState_ = false;
31 std::set<std::string> ShellCommandConfigLoder::commands_ = {};
32 
ReadConfig(const std::string & filePath)33 bool ShellCommandConfigLoder::ReadConfig(const std::string &filePath)
34 {
35     HILOG_INFO("%{public}s", __func__);
36     if (configState_) {
37         HILOG_INFO("config has been read");
38         return true;
39     }
40 
41     std::ifstream inFile;
42     inFile.open(filePath, std::ios::in);
43     if (!inFile.is_open()) {
44         HILOG_INFO("read aa config error");
45         return false;
46     }
47 
48     json aaJson;
49     inFile >> aaJson;
50     inFile.close();
51     if (aaJson.is_discarded()) {
52         HILOG_INFO("json discarded error");
53         return false;
54     }
55 
56     if (aaJson.is_null() || aaJson.empty()) {
57         HILOG_INFO("invalid jsonObj");
58         return false;
59     }
60 
61     if (!aaJson.contains(AA_TOOL_COMMAND_LIST)) {
62         HILOG_INFO("json config not contains the key");
63         return false;
64     }
65 
66     if (aaJson[AA_TOOL_COMMAND_LIST].is_null() || !aaJson[AA_TOOL_COMMAND_LIST].is_array() ||
67         aaJson[AA_TOOL_COMMAND_LIST].empty()) {
68         HILOG_INFO("invalid command obj size");
69         return false;
70     }
71 
72     if (aaJson[AA_TOOL_COMMAND_LIST].size() > COMMANDS_MAX_SIZE) {
73         HILOG_INFO("command obj size overflow");
74         return false;
75     }
76 
77     std::lock_guard<std::mutex> lock(mtxRead_);
78     for (size_t i = 0; i < aaJson[AA_TOOL_COMMAND_LIST].size(); i++) {
79         if (aaJson[AA_TOOL_COMMAND_LIST][i].is_null() || !aaJson[AA_TOOL_COMMAND_LIST][i].is_string()) {
80             continue;
81         }
82         std::string cmd = aaJson[AA_TOOL_COMMAND_LIST][i].get<std::string>();
83         HILOG_DEBUG("add cmd: %{public}s", cmd.c_str());
84         commands_.emplace(cmd);
85     }
86 
87     aaJson.clear();
88     HILOG_INFO("read config success");
89     configState_ = true;
90     return true;
91 }
92 
93 }  // namespace AAFwk
94 }  // namespace OHOS