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 "JsonReader.h" 24 GetInstance()25SystemCapability& SystemCapability::GetInstance() 26 { 27 static SystemCapability instance; 28 return instance; 29 } 30 HasSystemCapability(const char * sysCapName)31bool SystemCapability::HasSystemCapability(const char* sysCapName) 32 { 33 std::string capName = sysCapName; 34 if (capabilities.find(sysCapName) == capabilities.end()) { 35 return false; 36 } 37 return capabilities[sysCapName]; 38 } 39 SystemCapability()40SystemCapability::SystemCapability() 41 { 42 ReadCapability(); 43 } 44 ReadCapability()45void SystemCapability::ReadCapability() 46 { 47 std::string separator = FileSystem::GetSeparator(); 48 std::string filePath = FileSystem::GetApplicationPath() + separator + ".." + separator + "config" + separator + 49 "system_capability.json"; 50 std::ifstream inFile(filePath); 51 if (!inFile.is_open()) { 52 ELOG("Open capability file failed."); 53 } 54 std::string jsonStr((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>()); 55 inFile.close(); 56 57 Json2::Value val = JsonReader::ParseJsonData2(jsonStr); 58 if (val.IsNull()) { 59 std::string message = JsonReader::GetErrorPtr(); 60 ELOG("Failed to parse the capability, errors: %s", message.c_str()); 61 } 62 if (val["systemCapability"].IsNull() || !val["systemCapability"].IsArray()) { 63 ELOG("Empty systemCapability json object: %s", val["systemCapability"].ToStyledString().c_str()); 64 return; 65 } 66 Json2::Value val2 = val["systemCapability"]; 67 for (int i = 0; i < val2.GetArraySize(); i++) { 68 Json2::Value cap = val2.GetArrayItem(i); 69 if (!cap.IsMember("name") || !cap.IsMember("register-on-startup")) { 70 ELOG("Invalid systemCapability json object"); 71 } 72 if (!cap["register-on-startup"].IsBool()) { 73 ELOG("Invalid systemCapability json object"); 74 } 75 capabilities[cap["name"].AsString()] = cap["register"].AsBool(); 76 } 77 } 78