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 25 using namespace std; GetInstance()26SystemCapability& SystemCapability::GetInstance() 27 { 28 static SystemCapability instance; 29 return instance; 30 } 31 HasSystemCapability(const char * sysCapName)32bool 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()41SystemCapability::SystemCapability() 42 { 43 ReadCapability(); 44 } 45 ReadCapability()46void 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 Json2::Value val = JsonReader::ParseJsonData2(jsonStr); 59 if (val.IsNull()) { 60 string message = JsonReader::GetErrorPtr(); 61 ELOG("Failed to parse the capability, errors: %s", message.c_str()); 62 } 63 if (val["systemCapability"].IsNull() || !val["systemCapability"].IsArray()) { 64 ELOG("Empty systemCapability json object: %s", val["systemCapability"].ToStyledString().c_str()); 65 return; 66 } 67 Json2::Value val2 = val["systemCapability"]; 68 for (int i = 0; i < val2.GetArraySize(); i++) { 69 Json2::Value cap = val2.GetArrayItem(i); 70 if (!cap.IsMember("name") || !cap.IsMember("register-on-startup")) { 71 ELOG("Invalid systemCapability json object"); 72 } 73 if (!cap["register-on-startup"].IsBool()) { 74 ELOG("Invalid systemCapability json object"); 75 } 76 capabilities[cap["name"].AsString()] = cap["register"].AsBool(); 77 } 78 } 79