1 /*
2 * Copyright (c) 2023 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 "security_mode_parser.h"
17
18 #include "ime_info_inquirer.h"
19 #include "input_method_utils.h"
20 #include "iservice_registry.h"
21 #include "nlohmann/json.hpp"
22 #include "settings_data_utils.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace MiscServices {
27 using json = nlohmann::json;
28 std::mutex SecurityModeParser::instanceMutex_;
29 sptr<SecurityModeParser> SecurityModeParser::instance_ = nullptr;
30
~SecurityModeParser()31 SecurityModeParser::~SecurityModeParser()
32 {
33 }
34
GetInstance()35 sptr<SecurityModeParser> SecurityModeParser::GetInstance()
36 {
37 if (instance_ == nullptr) {
38 std::lock_guard<std::mutex> autoLock(instanceMutex_);
39 if (instance_ == nullptr) {
40 IMSA_HILOGI("GetInstance need new SecurityModeParser");
41 instance_ = new (std::nothrow) SecurityModeParser();
42 if (instance_ == nullptr) {
43 IMSA_HILOGI("instance is nullptr.");
44 return instance_;
45 }
46 }
47 }
48 return instance_;
49 }
50
Initialize(const int32_t userId)51 int32_t SecurityModeParser::Initialize(const int32_t userId)
52 {
53 return GetFullModeList(userId);
54 }
55
GetFullModeList(const int32_t userId)56 int32_t SecurityModeParser::GetFullModeList(const int32_t userId)
57 {
58 IMSA_HILOGD("key: %{public}s.", SECURITY_MODE);
59 std::string valueStr;
60 int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SECURITY_MODE, valueStr);
61 if (ret != ErrorCode::NO_ERROR || valueStr.empty()) {
62 IMSA_HILOGW("Get value failed, or valueStr is empty");
63 return ErrorCode::ERROR_ENABLE_SECURITY_MODE;
64 }
65
66 if (!ParseJsonData(valueStr, userId)) {
67 IMSA_HILOGE("valueStr is empty");
68 return ErrorCode::ERROR_ENABLE_SECURITY_MODE;
69 }
70 return ErrorCode::NO_ERROR;
71 }
72
IsSecurityChange(const std::string bundleName,const int32_t userId)73 bool SecurityModeParser::IsSecurityChange(const std::string bundleName, const int32_t userId)
74 {
75 bool oldExit = IsFullMode(bundleName);
76 GetFullModeList(userId);
77 bool onewExit = IsFullMode(bundleName);
78 return oldExit!= onewExit;
79 }
80
ParseJsonData(const std::string & valueStr,const int32_t userId)81 bool SecurityModeParser::ParseJsonData(const std::string& valueStr, const int32_t userId)
82 {
83 IMSA_HILOGD("valueStr: %{public}s.", valueStr.c_str());
84 json jsonData = json::parse(valueStr.c_str());
85 if (jsonData.is_null() || jsonData.is_discarded()) {
86 IMSA_HILOGE("json parse failed.");
87 return false;
88 }
89 if (!jsonData.contains(SECURITY_KEY) || !jsonData[SECURITY_KEY].is_object()) {
90 IMSA_HILOGE("listName not find or abnormal");
91 return false;
92 }
93 std::string id = std::to_string(userId);
94 if (!jsonData[SECURITY_KEY].contains(id) || !jsonData[SECURITY_KEY][id].is_array()) {
95 IMSA_HILOGE("user id not find or abnormal");
96 return false;
97 }
98 std::vector<std::string> vecTemp;
99 for (const auto& bundleName : jsonData[SECURITY_KEY][id]) {
100 IMSA_HILOGD(" full mode app is : %{public}s.", std::string(bundleName).c_str());
101 vecTemp.push_back(bundleName);
102 }
103 std::lock_guard<std::mutex> autoLock(listMutex_);
104 fullModeList_.assign(vecTemp.begin(), vecTemp.end());
105 return true;
106 }
107
GetSecurityMode(const std::string bundleName,int32_t & security,const int32_t userId)108 int32_t SecurityModeParser::GetSecurityMode(const std::string bundleName, int32_t &security, const int32_t userId)
109 {
110 GetFullModeList(userId);
111 if (IsFullMode(bundleName)) {
112 security = static_cast<int32_t>(SecurityMode::FULL);
113 } else {
114 security = static_cast<int32_t>(SecurityMode::BASIC);
115 }
116 return ErrorCode::NO_ERROR;
117 }
118
IsFullMode(std::string bundleName)119 bool SecurityModeParser::IsFullMode(std::string bundleName)
120 {
121 std::lock_guard<std::mutex> autoLock(listMutex_);
122 auto it = std::find_if(fullModeList_.begin(), fullModeList_.end(), [&bundleName](const std::string& bundle) {
123 return bundle == bundleName;
124 });
125 return it != fullModeList_.end();
126 }
127 } // namespace MiscServices
128 } // namespace OHOS