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 "SystemCapability.h"
17
18 #include <fstream>
19
20 #include "CommandParser.h"
21 #include "FileSystem.h"
22 #include "PreviewerEngineLog.h"
23 #include "json.h"
24
25 using namespace std;
GetInstance()26 SystemCapability& SystemCapability::GetInstance()
27 {
28 static SystemCapability instance;
29 return instance;
30 }
31
HasSystemCapability(const char * sysCapName)32 bool SystemCapability::HasSystemCapability(const char* sysCapName)
33 {
34 string capName = sysCapName;
35 if (capabilities.find(sysCapName) == capabilities.end()) {
36 return false;
37 }
38 return capabilities[sysCapName];
39 }
40
SystemCapability()41 SystemCapability::SystemCapability()
42 {
43 ReadCapability();
44 }
45
ReadCapability()46 void SystemCapability::ReadCapability()
47 {
48 std::string separator = FileSystem::GetSeparator();
49 std::string filePath = FileSystem::GetApplicationPath() + separator + ".." + separator + "config" + separator +
50 "system_capability.json";
51 ifstream inFile(filePath);
52 if (!inFile.is_open()) {
53 ELOG("Open capability file failed.");
54 }
55 string jsonStr((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>());
56 inFile.close();
57
58 Json::Value val;
59 Json::CharReaderBuilder builder;
60 Json::CharReader* charReader = builder.newCharReader();
61 if (charReader == nullptr) {
62 ELOG("main: CharReader memory allocation failed.");
63 }
64 std::unique_ptr<Json::CharReader> reader(charReader);
65 if (reader.get() == nullptr) {
66 ELOG("main: CharReader memory allocation failed.");
67 }
68 string message;
69 if (!reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &val, &message)) {
70 ELOG("Failed to parse the capability, errors: %s", message.c_str());
71 }
72 if (val["systemCapability"].empty() || !val["systemCapability"].isArray()) {
73 ELOG("Empty systemCapability json object: %s", val["systemCapability"].toStyledString().c_str());
74 return;
75 }
76 val = val["systemCapability"];
77 for (Json::ValueIterator iter = val.begin(); iter != val.end(); iter++) {
78 Json::Value cap = *iter;
79 if (!cap.isMember("name") || !cap.isMember("register-on-startup")) {
80 ELOG("Invalid systemCapability json object");
81 }
82 if (!cap["register-on-startup"].isBool()) {
83 ELOG("Invalid systemCapability json object");
84 }
85 capabilities[cap["name"].asString()] = cap["register"].asBool();
86 }
87 }
88