1 /*
2 * Copyright (c) 2024 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 "net_access_policy_config.h"
17
18 #include <fstream>
19 #include <sstream>
20 #include <sys/stat.h>
21 #include <vector>
22
23 #include "config_policy_utils.h"
24 #include "net_mgr_log_wrapper.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 NetAccessPolicyConfigUtils NetAccessPolicyConfigUtils::instance_;
29 namespace {
30 constexpr const char *PATH = "etc/netmanager/net_access_policy_config.json";
31 constexpr const char *ARRAY_NAME = "configs";
32 constexpr const char *ITEM_BUNDLE_NAME = "bundleName";
33 constexpr const char *ITEM_DISABLE_WLAN_SWITCH = "disableWlanSwitch";
34 constexpr const char *ITEM_DISABLE_CELLULAR_SWITCH = "disableCellularSwitch";
35 } // namespace
GetInstance()36 NetAccessPolicyConfigUtils &NetAccessPolicyConfigUtils::GetInstance()
37 {
38 return instance_;
39 }
GetNetAccessPolicyConfig()40 std::vector<NetAccessPolicyConfig> NetAccessPolicyConfigUtils::GetNetAccessPolicyConfig()
41 {
42 if (!isInit_) {
43 Init();
44 }
45 return netAccessPolicyConfigs_;
46 }
47
Init()48 void NetAccessPolicyConfigUtils::Init()
49 {
50 std::lock_guard<ffrt::mutex> lock(lock_);
51 if (isInit_) {
52 return;
53 }
54 netAccessPolicyConfigs_.clear();
55 ParseNetAccessPolicyConfigs();
56 isInit_ = true;
57 }
ParseNetAccessPolicyConfigs()58 void NetAccessPolicyConfigUtils::ParseNetAccessPolicyConfigs()
59 {
60 std::string content;
61 if (!ReadFile(content, PATH)) {
62 NETMGR_LOG_E("read json file failed.");
63 return;
64 }
65 if (content.empty()) {
66 NETMGR_LOG_E("read content is empty.");
67 return;
68 }
69 cJSON *root = cJSON_Parse(content.c_str());
70 if (root == nullptr) {
71 NETMGR_LOG_E("json root is nullptr.");
72 return;
73 }
74 cJSON *configs = cJSON_GetObjectItem(root, ARRAY_NAME);
75 if (configs == nullptr || !cJSON_IsArray(configs) || cJSON_GetArraySize(configs) == 0) {
76 cJSON_Delete(root);
77 configs = nullptr;
78 root = nullptr;
79 return;
80 }
81
82 cJSON *item = nullptr;
83 for (int i = 0; i < cJSON_GetArraySize(configs); i++) {
84 item = cJSON_GetArrayItem(configs, i);
85 if (item == nullptr) {
86 NETMGR_LOG_E("config item is nullptr.");
87 continue;
88 }
89 NetAccessPolicyConfig tmp;
90 tmp.bundleName = ParseString(cJSON_GetObjectItem(item, ITEM_BUNDLE_NAME));
91 tmp.disableWlanSwitch = ParseBoolean(cJSON_GetObjectItem(item, ITEM_DISABLE_WLAN_SWITCH));
92 tmp.disableCellularSwitch = ParseBoolean(cJSON_GetObjectItem(item, ITEM_DISABLE_CELLULAR_SWITCH));
93 netAccessPolicyConfigs_.push_back(tmp);
94 }
95
96 cJSON_Delete(root);
97 configs = nullptr;
98 root = nullptr;
99 }
100
ReadFile(std::string & content,const std::string & fileName)101 bool NetAccessPolicyConfigUtils::ReadFile(std::string &content, const std::string &fileName)
102 {
103 char buf[PATH_MAX];
104 char* cfgFilePath = GetOneCfgFile(fileName.c_str(), buf, PATH_MAX);
105 char realPath[PATH_MAX] = {0};
106 if (!cfgFilePath || strlen(cfgFilePath) == 0 || strlen(cfgFilePath) > PATH_MAX ||
107 !realpath(cfgFilePath, realPath)) {
108 NETMGR_LOG_E("file does not exist");
109 return false;
110 }
111
112 struct stat st;
113 if (stat(realPath, &st) != 0) {
114 NETMGR_LOG_E("stat file fail");
115 return false;
116 }
117
118 std::fstream file(realPath, std::fstream::in);
119 if (!file.is_open()) {
120 NETMGR_LOG_E("file open fail");
121 return false;
122 }
123
124 std::stringstream buffer;
125 buffer << file.rdbuf();
126 content = buffer.str();
127 file.close();
128 return true;
129 }
130
ParseString(cJSON * value)131 std::string NetAccessPolicyConfigUtils::ParseString(cJSON *value)
132 {
133 if (cJSON_IsString(value) && value->valuestring != nullptr) {
134 return value->valuestring;
135 }
136 return "";
137 }
138
ParseBoolean(cJSON * value)139 bool NetAccessPolicyConfigUtils::ParseBoolean(cJSON *value)
140 {
141 if (cJSON_IsBool(value)) {
142 return cJSON_IsTrue(value);
143 }
144 return false;
145 }
146
ParseInt32(cJSON * value)147 int32_t NetAccessPolicyConfigUtils::ParseInt32(cJSON *value)
148 {
149 if (cJSON_IsNumber(value)) {
150 return value->valueint;
151 }
152 return false;
153 }
154 } // namespace NetManagerStandard
155 } // namespace OHOS