1 /*
2 * Copyright (c) 2021-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 "lock_screen_white_list.h"
17 #include <sys/stat.h>
18 #include <sys/types.h>
19
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 using json = nlohmann::json;
25
SetWhiteListInfo(const std::string & bundleName,bool isAllow)26 bool LockScreenWhiteList::SetWhiteListInfo(const std::string &bundleName, bool isAllow)
27 {
28 HILOG_INFO("SetWhiteListInfo bundleName %{public}s, isAllow %{public}d",
29 bundleName.c_str(),
30 static_cast<int>(isAllow));
31 nlohmann::json jsonFile;
32 std::ifstream inFile;
33 inFile.open(AmsWhiteList::AMS_WHITE_LIST_FILE_PATH, std::ios::in);
34 if (!inFile.is_open()) {
35 HILOG_INFO("no such file...");
36 return false;
37 } else {
38 inFile.seekg(0, std::ios::end);
39 int len = static_cast<int>(inFile.tellg());
40 if (len == 0) {
41 HILOG_INFO("file is null");
42 } else {
43 inFile.seekg(0, std::ios::beg);
44 HILOG_INFO("file is not null");
45 inFile >> jsonFile;
46 if (jsonFile.is_discarded()) {
47 HILOG_INFO("json discarded error ...");
48 inFile.close();
49 return false;
50 }
51 }
52 inFile.close();
53 }
54
55 std::ofstream outFile(AmsWhiteList::AMS_WHITE_LIST_FILE_PATH, std::ios::out);
56 bool isOpen = outFile.good();
57 if (isOpen) {
58 HILOG_INFO("open file succeed");
59 HILOG_INFO("bundleName = %{public}s is ", bundleName.c_str());
60 if (isAllow) {
61 HILOG_INFO("add json");
62 jsonFile[bundleName][AmsWhiteList::ISAWAKEN_SCREEN] = true;
63 } else {
64 HILOG_INFO("delete json");
65 jsonFile.erase(bundleName);
66 }
67 outFile << std::setw(AmsWhiteList::DUMP_INDENT) << jsonFile << std::endl;
68 } else {
69 HILOG_INFO("open file fail");
70 return false;
71 }
72 jsonFile.clear();
73 outFile.close();
74 return true;
75 }
76
GetWhiteListInfo(nlohmann::json & jsonFile)77 bool LockScreenWhiteList::GetWhiteListInfo(nlohmann::json &jsonFile)
78 {
79 HILOG_INFO("%{public}s", __func__);
80 std::ifstream inFile;
81 inFile.open(AmsWhiteList::AMS_WHITE_LIST_FILE_PATH, std::ios::in);
82 if (!inFile.is_open()) {
83 HILOG_INFO("read Permit list error ...");
84 return false;
85 }
86 inFile.seekg(0, std::ios::end);
87 int len = static_cast<int>(inFile.tellg());
88 if (len == 0) {
89 HILOG_INFO("file is null");
90 return false;
91 } else {
92 inFile.seekg(0, std::ios::beg);
93 HILOG_INFO("file is not null");
94 inFile >> jsonFile;
95 if (jsonFile.is_discarded()) {
96 HILOG_INFO("json discarded error ...");
97 inFile.close();
98 return false;
99 }
100 }
101 inFile.close();
102 return true;
103 }
104
FindBundleNameOnWhiteList(const std::string & bundleName,bool & isAwakenScreen)105 bool LockScreenWhiteList::FindBundleNameOnWhiteList(const std::string &bundleName, bool &isAwakenScreen)
106 {
107 nlohmann::json jsonFile;
108 if (!GetWhiteListInfo(jsonFile)) {
109 HILOG_INFO("GetWhiteListInfo fail...");
110 return false;
111 }
112 if (jsonFile.contains(bundleName)) {
113 HILOG_INFO("json info contains bundleName...");
114 isAwakenScreen = jsonFile.at(bundleName).at(AmsWhiteList::ISAWAKEN_SCREEN).get<bool>();
115 jsonFile.clear();
116 return true;
117 }
118 HILOG_INFO("json info not contains bundleName...");
119 jsonFile.clear();
120 return false;
121 }
122
IsExistFile(const std::string & path)123 bool LockScreenWhiteList::IsExistFile(const std::string &path)
124 {
125 HILOG_INFO("%{public}s", __func__);
126 if (path.empty()) {
127 return false;
128 }
129 struct stat buf = {};
130 if (stat(path.c_str(), &buf) != 0) {
131 return false;
132 }
133 HILOG_INFO("%{public}s :file exists", __func__);
134 return S_ISREG(buf.st_mode);
135 }
136 } // namespace AAFwk
137 } // namespace OHOS
138