• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fstream>
17 #include <sstream>
18 #include <unistd.h>
19 
20 #include "extension_config.h"
21 #include "hilog_wrapper.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
25 namespace {
26 constexpr const char* AMS_EXTENSION_CONFIG = "/system/etc/ams_extension_config.json";
27 
28 const std::string EXTENSION_AUTO_DISCONNECT_TIME_NAME = "ams_extension_config";
29 const std::string EXTENSION_TYPE_NAME = "extension_type_name";
30 const std::string EXTENSION_AUTO_DISCONNECT_TIME = "auto_disconnect_time";
31 
32 const int32_t DEFAULT_EXTENSION_AUTO_DISCONNECT_TIME = -1;
33 }
34 
LoadExtensionConfiguration()35 void ExtensionConfig::LoadExtensionConfiguration()
36 {
37     HILOG_DEBUG("call");
38     nlohmann::json jsonBuf;
39     if (!ReadFileInfoJson(AMS_EXTENSION_CONFIG, jsonBuf)) {
40         HILOG_ERROR("Parse file failed.");
41         return;
42     }
43 
44     LoadExtensionAutoDisconnectTime(jsonBuf);
45 }
46 
GetExtensionAutoDisconnectTime(std::string extensionTypeName)47 int32_t ExtensionConfig::GetExtensionAutoDisconnectTime(std::string extensionTypeName)
48 {
49     if (extensionAutoDisconnectTimeMap_.find(extensionTypeName) != extensionAutoDisconnectTimeMap_.end()) {
50         return extensionAutoDisconnectTimeMap_[extensionTypeName];
51     }
52     return DEFAULT_EXTENSION_AUTO_DISCONNECT_TIME;
53 }
54 
LoadExtensionAutoDisconnectTime(const nlohmann::json & object)55 void ExtensionConfig::LoadExtensionAutoDisconnectTime(const nlohmann::json &object)
56 {
57     if (!object.contains(EXTENSION_AUTO_DISCONNECT_TIME_NAME)) {
58         HILOG_ERROR("Disconnect time config not existed.");
59         return;
60     }
61 
62     for (auto &item : object.at(EXTENSION_AUTO_DISCONNECT_TIME_NAME).items()) {
63         const nlohmann::json& jsonObject = item.value();
64         if (!jsonObject.contains(EXTENSION_TYPE_NAME) || !jsonObject.at(EXTENSION_TYPE_NAME).is_string()) {
65             continue;
66         }
67         if (!jsonObject.contains(EXTENSION_AUTO_DISCONNECT_TIME) ||
68             !jsonObject.at(EXTENSION_AUTO_DISCONNECT_TIME).is_number()) {
69             continue;
70         }
71         std::string extensionTypeName = jsonObject.at(EXTENSION_TYPE_NAME).get<std::string>();
72         int32_t extensionAutoDisconnectTime = jsonObject.at(EXTENSION_AUTO_DISCONNECT_TIME).get<int32_t>();
73         extensionAutoDisconnectTimeMap_[extensionTypeName] = extensionAutoDisconnectTime;
74     }
75 }
76 
ReadFileInfoJson(const std::string & filePath,nlohmann::json & jsonBuf)77 bool ExtensionConfig::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
78 {
79     if (access(filePath.c_str(), F_OK) != 0) {
80         HILOG_DEBUG("%{public}s, not existed", filePath.c_str());
81         return false;
82     }
83 
84     std::fstream in;
85     char errBuf[256];
86     errBuf[0] = '\0';
87     in.open(filePath, std::ios_base::in);
88     if (!in.is_open()) {
89         strerror_r(errno, errBuf, sizeof(errBuf));
90         HILOG_ERROR("the file cannot be open due to  %{public}s", errBuf);
91         return false;
92     }
93 
94     in.seekg(0, std::ios::end);
95     int64_t size = in.tellg();
96     if (size <= 0) {
97         HILOG_ERROR("the file is an empty file");
98         in.close();
99         return false;
100     }
101 
102     in.seekg(0, std::ios::beg);
103     jsonBuf = nlohmann::json::parse(in, nullptr, false);
104     in.close();
105     if (jsonBuf.is_discarded()) {
106         HILOG_ERROR("bad profile file");
107         return false;
108     }
109 
110     return true;
111 }
112 }
113 }